Add FoundationDB stuff
[slow/toolbox.git] / fdb-test.py
diff --git a/fdb-test.py b/fdb-test.py
new file mode 100755 (executable)
index 0000000..5d977a5
--- /dev/null
@@ -0,0 +1,58 @@
+#!/usr/bin/env python
+
+import fdb
+from   fdb.tuple import pack, unpack
+import time
+
+fdb.api_version(710)
+
+db = fdb.open()
+
+@fdb.transactional
+def set_fdb_txn(tr, _key, value):
+    key = pack((_key, ))
+    _t = tr.get(key)
+    if _t is not None:
+        _v, = unpack(_t)
+        print(_v)
+
+    time.sleep(3)
+
+    tr.set(key, pack((value,)))
+    value = tr.get(key)
+
+    _v, = unpack(value)
+    print(_v)
+
+def set_manual_txn(db, _key, value):
+    key = pack((_key, ))
+
+    tr = db.create_transaction()
+
+    while True:
+        try:
+            # Read two values from the transaction snapshot
+            a = tr.get(key)
+            if a is not None:
+                _v, = unpack(a)
+                print(_v)
+
+            time.sleep(2)
+
+            # Write two key-value pairs to the transaction snapshot
+            tr.set(key, pack((time.monotonic_ns(), )))
+
+            # Try to commit the transaction, and wait for the result
+            tr.commit().wait()
+
+            # Success!
+            return
+        except fdb.FDBError as error:
+            # The transaction conflicted or there was a transient failure.
+            # Ask the FDB API whether and when to retry the error
+            # (Also resets the tr object to be ready to use again)
+            print(error)
+            retry = tr.on_error(error).wait()
+            print(retry)
+
+set_manual_txn(db, "key", time.monotonic_ns())