Skip to content

Commit 01c9d46

Browse files
features
1 parent 9a9581a commit 01c9d46

4 files changed

Lines changed: 67 additions & 39 deletions

File tree

montycat/store_classes/inmemory.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class inmemory_kv:
99
@classmethod
1010
async def _run_query(cls, query: str):
1111
return await send_data(cls.host, cls.port, query)
12-
12+
1313
@classmethod
1414
async def do_snaphots_for_keyspace(cls):
1515
"""
@@ -22,15 +22,15 @@ async def do_snaphots_for_keyspace(cls):
2222

2323
query = orjson.dumps({
2424
"raw": [
25-
"do-snapshots-for-keyspace",
26-
"store", cls.store,
27-
"keyspace", cls.keyspace,
25+
"do-snapshots-for-keyspace",
26+
"store", cls.store,
27+
"keyspace", cls.keyspace,
2828
],
2929
"credentials": [cls.username, cls.password]
3030
})
3131

3232
return await cls._run_query(query)
33-
33+
3434
@classmethod
3535
async def clean_snapshots_for_keyspace(cls):
3636
"""
@@ -182,16 +182,20 @@ async def insert_bulk(cls, bulk_values: list, expire_sec: int = 0):
182182
return await cls._run_query(query)
183183

184184
@classmethod
185-
async def get_keys(cls):
185+
async def get_keys(cls, volumes: list = [], latest_volume: bool = False):
186186
"""
187187
Get all keys in the store.
188188
189189
Returns:
190190
A list of keys in the store. Class 'str' if the get operation failed.
191191
"""
192+
193+
if latest_volume and len(volumes) > 0:
194+
raise ValueError("Select either latest volume or volumes list, not both.")
195+
192196
cls.command = "get_keys"
193197

194-
query = convert_to_binary_query(cls)
198+
query = convert_to_binary_query(cls, volumes=volumes, latest_volume=latest_volume)
195199
return await cls._run_query(query)
196200

197201
@classmethod

montycat/store_classes/kv.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ async def remove_enforced_schema(cls, schema):
100100
return await cls._run_query(query)
101101

102102
@classmethod
103-
async def get_value(cls, key: Union[str, None] = None, custom_key: Union[str, None] = None, with_pointers: bool = False):
103+
async def get_value(cls, key: Union[str, None] = None, custom_key: Union[str, None] = None, with_pointers: bool = False, key_included: bool = False, pointers_metadata: bool = False):
104104

105105
"""
106106
Args:
@@ -110,6 +110,9 @@ async def get_value(cls, key: Union[str, None] = None, custom_key: Union[str, No
110110
Returns:
111111
The value associated with the key or custom key. Class 'str' if the get operation failed.
112112
"""
113+
if pointers_metadata and with_pointers:
114+
raise ValueError("You select both pointers value and pointers metadata. Choose one")
115+
113116
if custom_key and len(custom_key) > 0:
114117
key = convert_custom_key(custom_key)
115118

@@ -118,7 +121,7 @@ async def get_value(cls, key: Union[str, None] = None, custom_key: Union[str, No
118121

119122
cls.command = "get_value"
120123

121-
query = convert_to_binary_query(cls, key=key, with_pointers=with_pointers)
124+
query = convert_to_binary_query(cls, key=key, with_pointers=with_pointers, key_included=key_included, pointers_metadata=pointers_metadata)
122125
return await cls._run_query(query)
123126

124127
@classmethod
@@ -167,6 +170,7 @@ async def delete_bulk(cls, bulk_keys: list = [], bulk_custom_keys: list = []):
167170
168171
Raises:
169172
ValueError: If both `bulk_keys` and `bulk_custom_keys` are empty.
173+
ValueError: If both `pointers_metadata` and `with_pointers` are True.
170174
"""
171175
if len(bulk_custom_keys) > 0:
172176
bulk_custom_keys = convert_custom_keys(bulk_custom_keys)
@@ -180,7 +184,7 @@ async def delete_bulk(cls, bulk_keys: list = [], bulk_custom_keys: list = []):
180184
return await cls._run_query(query) # Execute the query and return the result
181185

182186
@classmethod
183-
async def get_bulk(cls, bulk_keys: list = [], bulk_custom_keys: list = [], limit: list = [], with_pointers: bool = False):
187+
async def get_bulk(cls, bulk_keys: list = [], bulk_custom_keys: list = [], limit: list = [], with_pointers: bool = False, key_included: bool = False, pointers_metadata: bool = False):
184188
"""
185189
Retrieve multiple keys in bulk. Custom keys can be converted and added to the bulk retrieval list.
186190
Additionally, a limit on the number of records to retrieve can be applied, and whether to include pointers
@@ -202,7 +206,12 @@ async def get_bulk(cls, bulk_keys: list = [], bulk_custom_keys: list = [], limit
202206
203207
Raises:
204208
ValueError: If both `bulk_keys` and `bulk_custom_keys` are empty.
209+
ValueError: If both `pointers_metadata` and `with_pointers` are True.
205210
"""
211+
212+
if pointers_metadata and with_pointers:
213+
raise ValueError("You select both pointers value and pointers metadata. Choose one")
214+
206215
if len(bulk_custom_keys) > 0:
207216
bulk_custom_keys = convert_custom_keys(bulk_custom_keys) # Convert custom keys if provided
208217
bulk_keys += bulk_custom_keys
@@ -212,7 +221,7 @@ async def get_bulk(cls, bulk_keys: list = [], bulk_custom_keys: list = [], limit
212221

213222
cls.command = "get_bulk"
214223
cls.limit_output = handle_limit(limit)
215-
query = convert_to_binary_query(cls, bulk_keys=bulk_keys, with_pointers=with_pointers)
224+
query = convert_to_binary_query(cls, bulk_keys=bulk_keys, with_pointers=with_pointers, key_included=key_included, pointers_metadata=pointers_metadata)
216225
return await cls._run_query(query)
217226

218227
@classmethod
@@ -276,7 +285,7 @@ async def lookup_keys_where(cls, limit: Union[int, list] = 0, schema: Union[str,
276285
return await cls._run_query(query)
277286

278287
@classmethod
279-
async def lookup_values_where(cls, limit: Union[int, list] = 0, with_pointers: bool = False, schema: Union[str, None] = None, **filters):
288+
async def lookup_values_where(cls, limit: Union[int, list] = 0, with_pointers: bool = False, key_included: bool = False, pointers_metadata: bool = False, schema: Union[str, None] = None, **filters):
280289
"""
281290
Perform a lookup for values matching the given filters, with options to apply a limit and include pointer information.
282291
@@ -294,14 +303,17 @@ async def lookup_values_where(cls, limit: Union[int, list] = 0, with_pointers: b
294303
# if not filters: # Ensure filters are provided for the lookup
295304
# raise ValueError("No criteria provided for the lookup.")
296305

306+
if pointers_metadata and with_pointers:
307+
raise ValueError("You select both pointers value and pointers metadata. Choose one")
308+
297309
if schema:
298310
cls.schema = str(schema)
299311
else:
300312
cls.schema = None
301313

302314
cls.command = "lookup_values"
303315
cls.limit_output = handle_limit(limit)
304-
query = convert_to_binary_query(cls, search_criteria=filters, with_pointers=with_pointers)
316+
query = convert_to_binary_query(cls, search_criteria=filters, with_pointers=with_pointers, key_included=key_included, pointers_metadata=pointers_metadata)
305317
return await cls._run_query(query)
306318

307319
@classmethod

montycat/store_classes/persistent.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,22 @@ async def insert_bulk(cls, bulk_values: list):
117117
return await cls._run_query(query)
118118

119119
@classmethod
120-
async def get_keys(cls, limit: Union[list, int] = []):
120+
async def get_keys(cls, limit: Union[list, int] = [], volumes: list = [], latest_volume: bool = False):
121121
"""
122122
Args:
123123
Limit: A list of two integers that determine the range of keys to retrieve.
124124
Example: limit = [10, 20] will retrieve keys 10 to 20.
125125
Returns:
126126
A list of keys in the store. Class 'str' if the get operation failed.
127127
"""
128+
129+
if latest_volume and len(volumes) > 0:
130+
raise ValueError("Select either latest volume or volumes list, not both.")
131+
128132
cls.limit_output = handle_limit(limit)
129133
cls.command = "get_keys"
130134

131-
query = convert_to_binary_query(cls)
135+
query = convert_to_binary_query(cls, volumes=volumes, latest_volume=latest_volume)
132136
return await cls._run_query(query)
133137

134138
@classmethod

montycat/store_functions/store_generic_functions.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66
def convert_custom_key(key: Union[int, str]) -> int:
77
"""
88
Converts a custom key (either an integer or string) into a hash value using xxHash.
9-
9+
1010
Args:
1111
key (int | str): The custom key to be hashed.
12-
12+
1313
Returns:
1414
int: The xxHash digest of the provided key as an integer.
15-
15+
1616
This function ensures that any input key, whether integer or string, is consistently
1717
hashed into a unique integer for use as a custom key in further queries.
1818
"""
@@ -21,13 +21,13 @@ def convert_custom_key(key: Union[int, str]) -> int:
2121
def convert_custom_keys(keys: list) -> list:
2222
"""
2323
Converts a list of custom keys into their hashed equivalents.
24-
24+
2525
Args:
2626
keys (list): A list of custom keys to be hashed.
27-
27+
2828
Returns:
2929
list: A list of hashed keys as integers.
30-
30+
3131
This function maps over the provided list of keys and applies `convert_custom_key`
3232
to each one to ensure they are converted to hashed integers.
3333
"""
@@ -43,14 +43,14 @@ def convert_custom_keys_values(keys_values: dict) -> dict:
4343
"""
4444
Converts a dictionary of custom keys and their corresponding values into a new
4545
dictionary with hashed keys.
46-
46+
4747
Args:
4848
keys_values (dict): A dictionary where the keys are custom keys (either
4949
integers or strings) and the values are associated values.
50-
50+
5151
Returns:
5252
dict: A dictionary where the custom keys have been hashed into integers.
53-
53+
5454
This function maps over the dictionary and applies `convert_custom_key` to each
5555
key while leaving the corresponding value unchanged.
5656
"""
@@ -59,13 +59,13 @@ def convert_custom_keys_values(keys_values: dict) -> dict:
5959
def modify_pointers(value: dict) -> dict:
6060
"""
6161
Modifies the pointers in the given value dictionary to ensure they are in the correct format.
62-
62+
6363
Args:
6464
value (dict): The dictionary containing pointers to be modified.
65-
65+
6666
Returns:
6767
dict: The modified dictionary with pointers in the correct format.
68-
68+
6969
Raises:
7070
ValueError: If there is an error processing the pointers.
7171
"""
@@ -88,7 +88,7 @@ def modify_pointers(value: dict) -> dict:
8888

8989
except Exception as e:
9090
raise ValueError(f"Error processing pointers: {e}")
91-
91+
9292
return value
9393

9494
def normalize_bools(s: str) -> str:
@@ -104,10 +104,14 @@ def convert_to_binary_query(
104104
bulk_keys: List[str] = None,
105105
bulk_keys_values: Dict[str, Any] = None,
106106
with_pointers: bool = False,
107+
volumes: List[int] = [],
108+
latest_volume: bool = False,
109+
key_included: bool = False,
110+
pointers_metadata: bool = False,
107111
) -> bytes:
108112
"""
109113
Converts parameters into a binary query format suitable for transmission.
110-
114+
111115
Args:
112116
cls: Query class containing connection details and command settings
113117
key: Single key for the query
@@ -118,10 +122,10 @@ def convert_to_binary_query(
118122
bulk_keys: List of keys for bulk operations
119123
bulk_keys_values: Dictionary of keys and values for bulk operations
120124
with_pointers: Flag to include pointers
121-
125+
122126
Returns:
123127
bytes: Binary-encoded query in appropriate format
124-
128+
125129
Raises:
126130
ValueError: If bulk values contain multiple schemas
127131
"""
@@ -131,7 +135,7 @@ def convert_to_binary_query(
131135
bulk_values = bulk_values or []
132136
bulk_keys = bulk_keys or []
133137
bulk_keys_values = bulk_keys_values or {}
134-
138+
135139
if value:
136140
value = modify_pointers(value)
137141

@@ -142,32 +146,32 @@ def convert_to_binary_query(
142146
schemas.extend([item['schema']])
143147
else:
144148
schemas.extend([None])
145-
149+
146150
unique_schemas = set(schemas)
147151
if len(unique_schemas) > 1:
148152
raise ValueError("Bulk values should fit only one schema")
149-
153+
150154
cls.schema = schemas[0] if schemas else None
151155
bulk_values = [
152156
str(modify_pointers({k: v for k, v in item.items() if k != 'schema'}))
153157
for item in bulk_values
154158
]
155-
159+
156160
if bulk_keys_values:
157161
bulk_keys_values = {
158-
k: str(modify_pointers(v))
162+
k: str(modify_pointers(v))
159163
for k, v in bulk_keys_values.items()
160164
}
161165

162166
if bulk_keys:
163167
bulk_keys = [str(k) for k in bulk_keys]
164-
168+
165169
if 'schema' in value:
166170
cls.schema = value.pop('schema')
167171

168172
search_criteria = handle_timestamps_and_pointers(search_criteria)
169173
value = handle_timestamps_and_pointers(value)
170-
174+
171175
query_dict = {
172176
"schema": cls.schema,
173177
"username": cls.username,
@@ -186,8 +190,12 @@ def convert_to_binary_query(
186190
"bulk_keys_values": {k: normalize_bools(str(v)) for k, v in bulk_keys_values.items()},
187191
"search_criteria": normalize_bools(str(search_criteria)),
188192
"with_pointers": with_pointers,
193+
"volumes": volumes,
194+
"latest_volume": latest_volume,
195+
"key_included": key_included,
196+
"pointers_metadata": pointers_metadata,
189197
}
190-
198+
191199
return orjson.dumps(query_dict)
192200

193201
def handle_timestamps_and_pointers(search_criteria: dict) -> dict:

0 commit comments

Comments
 (0)