Skip to content

Commit 4d25dc9

Browse files
authored
refactor storage and retention variables to be enum based (#118)
1 parent de07a2f commit 4d25dc9

File tree

7 files changed

+29
-38
lines changed

7 files changed

+29
-38
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ $ pip3 install memphis-py
5555

5656
```python
5757
from memphis import Memphis, Headers
58-
from memphis import retention_types, storage_types
58+
from memphis.types import Retention, Storage
5959
```
6060

6161
### Connecting to Memphis
@@ -108,9 +108,9 @@ _If a station already exists nothing happens, the new configuration will not be
108108
station = memphis.station(
109109
name="<station-name>",
110110
schema_name="<schema-name>",
111-
retention_type=retention_types.MAX_MESSAGE_AGE_SECONDS, # MAX_MESSAGE_AGE_SECONDS/MESSAGES/BYTES. Defaults to MAX_MESSAGE_AGE_SECONDS
111+
retention_type=Retention.MAX_MESSAGE_AGE_SECONDS, # MAX_MESSAGE_AGE_SECONDS/MESSAGES/BYTES. Defaults to MAX_MESSAGE_AGE_SECONDS
112112
retention_value=604800, # defaults to 604800
113-
storage_type=storage_types.DISK, # storage_types.DISK/storage_types.MEMORY. Defaults to DISK
113+
storage_type=Storage.DISK, # Storage.DISK/Storage.MEMORY. Defaults to DISK
114114
replicas=1, # defaults to 1
115115
idempotency_window_ms=120000, # defaults to 2 minutes
116116
send_poison_msg_to_dls=True, # defaults to true
@@ -124,19 +124,19 @@ station = memphis.station(
124124
Memphis currently supports the following types of retention:
125125

126126
```python
127-
memphis.retention_types.MAX_MESSAGE_AGE_SECONDS
127+
memphis.types.Retention.MAX_MESSAGE_AGE_SECONDS
128128
```
129129

130130
Means that every message persists for the value set in retention value field (in seconds)
131131

132132
```python
133-
memphis.retention_types.MESSAGES
133+
memphis.types.Retention.MESSAGES
134134
```
135135

136136
Means that after max amount of saved messages (set in retention value), the oldest messages will be deleted
137137

138138
```python
139-
memphis.retention_types.BYTES
139+
memphis.types.Retention.BYTES
140140
```
141141

142142
Means that after max amount of saved bytes (set in retention value), the oldest messages will be deleted
@@ -146,13 +146,13 @@ Means that after max amount of saved bytes (set in retention value), the oldest
146146
Memphis currently supports the following types of messages storage:
147147

148148
```python
149-
memphis.storage_types.DISK
149+
memphis.types.Storage.DISK
150150
```
151151

152152
Means that messages persist on disk
153153

154154
```python
155-
memphis.storage_types.MEMORY
155+
memphis.types.Storage.MEMORY
156156
```
157157

158158
Means that messages persist on the main memory

examples/consumer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
23
import asyncio
34

45
from memphis import Memphis, MemphisConnectError, MemphisError, MemphisHeaderError

examples/producer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from __future__ import annotations
2+
23
import asyncio
34

45
from memphis import (

memphis/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import memphis.retention_types
16-
import memphis.storage_types
1715
from memphis.memphis import (
1816
Headers,
1917
Memphis,

memphis/memphis.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@
2222
from typing import Callable, Iterable, Union
2323

2424
import graphql
25-
import memphis.retention_types as retention_types
26-
import memphis.storage_types as storage_types
2725
import nats as broker
2826
from google.protobuf import descriptor_pb2, descriptor_pool
2927
from google.protobuf.message_factory import MessageFactory
3028
from graphql import build_schema as build_graphql_schema
3129
from graphql import parse as parse_graphql
3230
from graphql import validate as validate_graphql
3331
from jsonschema import validate
34-
32+
from memphis.types import Retention, Storage
3533

3634
schemaVFailAlertType = "schema_validation_fail_alert"
3735

@@ -199,22 +197,22 @@ async def send_notification(self, title, msg, failedMsg, type):
199197
async def station(
200198
self,
201199
name: str,
202-
retention_type: str = retention_types.MAX_MESSAGE_AGE_SECONDS,
200+
retention_type: Retention = Retention.MAX_MESSAGE_AGE_SECONDS,
203201
retention_value: int = 604800,
204-
storage_type: str = storage_types.DISK,
202+
storage_type: Storage = Storage.DISK,
205203
replicas: int = 1,
206204
idempotency_window_ms: int = 120000,
207205
schema_name: str = "",
208206
send_poison_msg_to_dls: bool = True,
209207
send_schema_failed_msg_to_dls: bool = True,
210-
tiered_storage_enabled: bool = False
208+
tiered_storage_enabled: bool = False,
211209
):
212210
"""Creates a station.
213211
Args:
214212
name (str): station name.
215-
retention_type (str, optional): retention type: message_age_sec/messages/bytes . Defaults to "message_age_sec".
213+
retention_type (Retention, optional): retention type: message_age_sec/messages/bytes . Defaults to "message_age_sec".
216214
retention_value (int, optional): number which represents the retention based on the retention_type. Defaults to 604800.
217-
storage_type (str, optional): persistance storage for messages of the station: disk/memory. Defaults to "disk".
215+
storage_type (Storage, optional): persistance storage for messages of the station: disk/memory. Defaults to "disk".
218216
replicas (int, optional):number of replicas for the messages of the data. Defaults to 1.
219217
idempotency_window_ms (int, optional): time frame in which idempotent messages will be tracked, happens based on message ID Defaults to 120000.
220218
schema_name (str): schema name.
@@ -238,7 +236,7 @@ async def station(
238236
"Schemaverse": send_schema_failed_msg_to_dls,
239237
},
240238
"username": self.username,
241-
"tiered_storage_enabled": tiered_storage_enabled
239+
"tiered_storage_enabled": tiered_storage_enabled,
242240
}
243241
create_station_req_bytes = json.dumps(createStationReq, indent=2).encode(
244242
"utf-8"

memphis/storage_types.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

memphis/retention_types.py renamed to memphis/types.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
MAX_MESSAGE_AGE_SECONDS = "message_age_sec"
16-
MESSAGES = "messages"
17-
BYTES = "bytes"
15+
from enum import Enum
16+
17+
18+
class Retention(Enum):
19+
MAX_MESSAGE_AGE_SECONDS = "message_age_sec"
20+
MESSAGES = "messages"
21+
BYTES = "bytes"
22+
23+
24+
class Storage(Enum):
25+
DISK = "file"
26+
MEMORY = "memory"

0 commit comments

Comments
 (0)