aboutsummaryrefslogtreecommitdiffstats
path: root/py_bindings/example.py
diff options
context:
space:
mode:
Diffstat (limited to 'py_bindings/example.py')
-rw-r--r--py_bindings/example.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/py_bindings/example.py b/py_bindings/example.py
new file mode 100644
index 0000000..f175b8a
--- /dev/null
+++ b/py_bindings/example.py
@@ -0,0 +1,69 @@
+from pprint import pformat, pprint
+import log_db
+from log_db import Value, Type, Bound
+
+# TODO: This should be imported from the log_db module
+# but exporting type aliases does not work automatically
+Record = list[Value]
+
+class Inst:
+ def __init__(self, id: int, name: str):
+ self.id = id
+ self.name = name
+
+ def into_record(self) -> Record:
+ return [
+ Value.int(self.id),
+ Value.string(self.name),
+ ]
+
+ @staticmethod
+ def from_record(rec: Record):
+ return Inst(
+ rec[0].as_int(),
+ rec[1].as_string(),
+ )
+
+ def __repr__(self) -> str:
+ return pformat(self.__dict__)
+
+# Create a new database
+db = log_db.DB \
+ .configure() \
+ .data_dir("db") \
+ .schema([
+ ("id", Type.int()),
+ ("name", Type.string()),
+ ]) \
+ .primary_key("id") \
+ .secondary_keys(["name"]) \
+ .initialize()
+
+#db.upsert(Inst(1, "foo").into_record())
+
+#res = db.find_by("name", log_db.Value.string("foo"))
+#insts = [Inst.from_record(r) for r in res]
+res = db.range_by("name",
+ Bound.unbounded(),
+ Bound.unbounded(),
+)
+# insts = [Inst.from_record(r) for r in res]
+
+print("before delete:")
+res = db.find_by("name", Value.string("foo"))
+print(len(res))
+
+res = db.delete_by("name", Value.string("foo"))
+#res = db.delete_by("id", Value.int(1))
+
+print("deleted:")
+for r in res:
+ pprint(Inst.from_record(r))
+
+print("find name after delete:")
+res = db.find_by("name", Value.string("foo"))
+print(len(res))
+
+print("find id after delete:")
+res = db.find_by("id", Value.int(1))
+print(len(res))