1
1
from datetime import datetime
2
2
from typing import Union , Dict , List , Awaitable , Optional
3
+
3
4
from pysirix .types import Commit , Revision as RevisionType , SubtreeRevision
4
5
from json import dumps , loads
5
6
6
7
from abc import ABC
7
8
8
- from pysirix .constants import DBType , Revision
9
+ from pysirix .constants import DBType , Revision , TimeAxisShift
9
10
from pysirix .async_client import AsyncClient
10
11
from pysirix .auth import Auth
11
12
from pysirix .sync_client import SyncClient
@@ -39,7 +40,7 @@ def stringify(v: Union[None, int, str, Dict, List]):
39
40
query_function_include = (
40
41
"declare function local:q($i, $q) {"
41
42
"let $m := for $k in jn:keys($q) return if (not(empty($i=>$k))) then deep-equal($i=>$k, $q=>$k) else false()"
42
- "return empty(index-of($m, false()))"
43
+ " return empty(index-of($m, false()))"
43
44
"};"
44
45
)
45
46
@@ -178,8 +179,9 @@ def _prepare_find_all(
178
179
query_dict : Dict ,
179
180
projection : List [str ] = None ,
180
181
revision : Revision = None ,
181
- node_key = True ,
182
- hash = False ,
182
+ node_key : bool = True ,
183
+ hash : bool = False ,
184
+ time_axis_shift : TimeAxisShift = TimeAxisShift .none ,
183
185
start_result_index : Optional [int ] = None ,
184
186
end_result_index : Optional [int ] = None ,
185
187
):
@@ -200,6 +202,10 @@ def _prepare_find_all(
200
202
f"for $i in bit:array-values(jn:doc('{ self .db_name } ','{ self .name } ',{ revision } )){ self .root } " ,
201
203
]
202
204
query_list .append (f"where local:q($i, { stringify (query_dict )} )" )
205
+ if time_axis_shift == TimeAxisShift .oldest :
206
+ query_list .append ("let $i := jn:first-existing($i)" )
207
+ elif time_axis_shift == TimeAxisShift .latest :
208
+ query_list .append ("let $i := jn:last-existing($i)" )
203
209
return_obj = (
204
210
"return {$i,'nodeKey': sdb:nodekey($i),'hash': sdb:hash($i)}"
205
211
if node_key and hash
@@ -230,8 +236,9 @@ def find_all(
230
236
query_dict : Dict ,
231
237
projection : List [str ] = None ,
232
238
revision : Revision = None ,
233
- node_key = True ,
234
- hash = False ,
239
+ node_key : bool = True ,
240
+ hash : bool = False ,
241
+ time_axis_shift : TimeAxisShift = TimeAxisShift .none ,
235
242
start_result_index : Optional [int ] = None ,
236
243
end_result_index : Optional [int ] = None ,
237
244
) -> List [QueryResult ]:
@@ -251,6 +258,7 @@ def find_all(
251
258
the nodeKey of the record.
252
259
:param hash: a ``bool`` determining whether or not to return a ``hash`` field containing the hash of the record.
253
260
:param revision: the revision to search, defaults to latest. May be an integer or a ``datetime`` instance
261
+ :param time_axis_shift: specify fetching the most or least recent existing revision of the record
254
262
:param start_result_index: index of first result to return.
255
263
:param end_result_index: index of last result to return.
256
264
:return: a ``list`` of :py:class:`QueryResult` records matching the query.
@@ -366,8 +374,9 @@ def find_one(
366
374
query_dict : Dict ,
367
375
projection : List [str ] = None ,
368
376
revision : Revision = None ,
369
- node_key = True ,
370
- hash = False ,
377
+ node_key : bool = True ,
378
+ hash : bool = False ,
379
+ time_axis_shift : TimeAxisShift = TimeAxisShift .none ,
371
380
) -> List [QueryResult ]:
372
381
"""
373
382
This method is the same as :py:meth:`find_many`, except that this method will only return the first result,
@@ -378,6 +387,7 @@ def find_one(
378
387
:param revision:
379
388
:param node_key:
380
389
:param hash:
390
+ :param time_axis_shift:
381
391
:return:
382
392
"""
383
393
return self .find_all (
@@ -386,6 +396,7 @@ def find_one(
386
396
revision ,
387
397
node_key ,
388
398
hash ,
399
+ time_axis_shift ,
389
400
start_result_index = 0 ,
390
401
end_result_index = 0 ,
391
402
)
@@ -406,8 +417,9 @@ def find_all(
406
417
query_dict : Dict ,
407
418
projection : List [str ] = None ,
408
419
revision : Revision = None ,
409
- node_key = True ,
410
- hash = False ,
420
+ node_key : bool = True ,
421
+ hash : bool = False ,
422
+ time_axis_shift : TimeAxisShift = TimeAxisShift .none ,
411
423
start_result_index : Optional [int ] = None ,
412
424
end_result_index : Optional [int ] = None ,
413
425
) -> List [QueryResult ]:
@@ -417,6 +429,7 @@ def find_all(
417
429
revision ,
418
430
node_key ,
419
431
hash ,
432
+ time_axis_shift ,
420
433
start_result_index ,
421
434
end_result_index ,
422
435
)
@@ -459,8 +472,9 @@ async def find_all(
459
472
query_dict : Dict ,
460
473
projection : List [str ] = None ,
461
474
revision : Revision = None ,
462
- node_key = True ,
463
- hash = False ,
475
+ node_key : bool = True ,
476
+ hash : bool = False ,
477
+ time_axis_shift : TimeAxisShift = TimeAxisShift .none ,
464
478
start_result_index : Optional [int ] = None ,
465
479
end_result_index : Optional [int ] = None ,
466
480
) -> List [QueryResult ]:
@@ -470,6 +484,7 @@ async def find_all(
470
484
revision ,
471
485
node_key ,
472
486
hash ,
487
+ time_axis_shift ,
473
488
start_result_index ,
474
489
end_result_index ,
475
490
)
0 commit comments