Created
June 3, 2024 15:27
-
-
Save johnxx/3463fc8c975f160349408b270f6c0b88 to your computer and use it in GitHub Desktop.
Trying to figure out the right way to get the latest entry for a key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import iroh | |
import time | |
node = iroh.IrohNode("./iroh_data") | |
author1 = node.author_create() | |
author2 = node.author_create() | |
doc = node.doc_create() | |
key = b'our_key' | |
other_key = b'some_other_key' | |
ts1 = 'earliest'.encode('utf-8') | |
print(f"ts1: {ts1}") | |
doc.set_bytes(author1, key, ts1) | |
doc.set_bytes(author1, other_key, ts1) | |
time.sleep(1) | |
ts2 = 'latest'.encode('utf-8') | |
print(f"ts2: {ts2}") | |
doc.set_bytes(author2, key, ts2) | |
q1 = iroh.Query.key_exact(key, None).single_latest_per_key(None) | |
e = doc.get_one(q1) | |
print("q1: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q1aopts = iroh.QueryOptions(sort_by=iroh.SortBy.KEY_AUTHOR, direction=iroh.SortDirection.ASC, offset=0, limit=0) | |
q1a = iroh.Query.key_exact(key, None).single_latest_per_key(None) | |
e = doc.get_one(q1a) | |
print("q1a: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q1bopts = iroh.QueryOptions(sort_by=iroh.SortBy.KEY_AUTHOR, direction=iroh.SortDirection.ASC, offset=0, limit=0) | |
q1b = iroh.Query.key_exact(key, None).single_latest_per_key(None) | |
e = doc.get_one(q1b) | |
print("q1b: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q2 = iroh.Query.single_latest_per_key(None).key_exact(key, None) | |
e = doc.get_one(q2) | |
print("q2: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q2aopts = iroh.QueryOptions(sort_by=iroh.SortBy.AUTHOR_KEY, direction=iroh.SortDirection.ASC, offset=0, limit=0) | |
q2a = iroh.Query.single_latest_per_key(q2aopts).key_exact(key, None) | |
e = doc.get_one(q2a) | |
print("q2a: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q2bopts = iroh.QueryOptions(sort_by=iroh.SortBy.AUTHOR_KEY, direction=iroh.SortDirection.DESC, offset=0, limit=0) | |
q2b = iroh.Query.single_latest_per_key(q2bopts).key_exact(key, None) | |
e = doc.get_one(q2b) | |
print("q2b: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q3 = iroh.Query.single_latest_per_key(None).key_exact(key, None) | |
res = doc.get_many(q3) | |
for e in res: | |
print("q3: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) | |
q4 = iroh.Query.key_exact(key, None).single_latest_per_key(None) | |
res = doc.get_many(q4) | |
for e in res: | |
print("q4: %s: %s" % (e.key().decode('utf-8'), e.content_bytes(doc).decode('utf-8'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment