2016-06-21 03:00:02 -04:00
|
|
|
try:
|
|
|
|
import btree
|
2016-07-30 19:29:05 -04:00
|
|
|
import uio
|
2016-09-29 11:12:31 -04:00
|
|
|
import uerrno
|
2016-06-21 03:00:02 -04:00
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
2017-06-10 13:14:16 -04:00
|
|
|
raise SystemExit
|
2016-06-21 03:00:02 -04:00
|
|
|
|
2016-07-30 19:29:05 -04:00
|
|
|
# f = open("_test.db", "w+b")
|
|
|
|
f = uio.BytesIO()
|
2016-10-29 12:53:31 -04:00
|
|
|
db = btree.open(f, pagesize=512)
|
2016-07-30 19:29:05 -04:00
|
|
|
|
2022-06-10 13:28:56 -04:00
|
|
|
mv = memoryview(b"bar1foo1")
|
|
|
|
|
2016-06-21 03:00:02 -04:00
|
|
|
db[b"foo3"] = b"bar3"
|
|
|
|
db[b"foo1"] = b"bar1"
|
2022-06-10 13:28:56 -04:00
|
|
|
# any type that implements buffer protocol works for key and value
|
|
|
|
db["foo2"] = "bar2"
|
|
|
|
db[mv[:4]] = mv[4:]
|
2016-06-21 03:00:02 -04:00
|
|
|
|
2016-09-29 11:12:31 -04:00
|
|
|
dbstr = str(db)
|
|
|
|
print(dbstr[:7], dbstr[-1:])
|
|
|
|
|
2016-06-21 03:00:02 -04:00
|
|
|
print(db[b"foo2"])
|
|
|
|
try:
|
|
|
|
print(db[b"foo"])
|
|
|
|
except KeyError:
|
|
|
|
print("KeyError")
|
|
|
|
print(db.get(b"foo"))
|
|
|
|
print(db.get(b"foo", b"dflt"))
|
|
|
|
|
|
|
|
del db[b"foo2"]
|
|
|
|
try:
|
|
|
|
del db[b"foo"]
|
|
|
|
except KeyError:
|
|
|
|
print("KeyError")
|
|
|
|
|
|
|
|
for k, v in db.items():
|
|
|
|
print((k, v))
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
for k, v in db.items(None, None):
|
|
|
|
print((k, v))
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
for k, v in db.items(b"f"):
|
|
|
|
print((k, v))
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
for k, v in db.items(b"f", b"foo3"):
|
|
|
|
print((k, v))
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
for k, v in db.items(None, b"foo3"):
|
|
|
|
print((k, v))
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
for k, v in db.items(b"f", b"foo3", btree.INCL):
|
|
|
|
print((k, v))
|
|
|
|
|
|
|
|
print("---")
|
|
|
|
for k, v in db.items(None, None, btree.DESC):
|
|
|
|
print((k, v))
|
|
|
|
|
2016-09-29 11:12:31 -04:00
|
|
|
print(db.seq(1, b"foo1"))
|
|
|
|
print(db.seq(1, b"qux"))
|
|
|
|
|
|
|
|
try:
|
|
|
|
db.seq(b"foo1")
|
|
|
|
except OSError as e:
|
2021-04-22 05:32:21 -04:00
|
|
|
print(e.errno == uerrno.EINVAL)
|
2016-09-29 11:12:31 -04:00
|
|
|
|
2016-06-21 03:00:02 -04:00
|
|
|
print(list(db.keys()))
|
|
|
|
print(list(db.values()))
|
2016-06-24 16:43:20 -04:00
|
|
|
|
|
|
|
for k in db:
|
|
|
|
print(k)
|
2016-07-23 17:30:32 -04:00
|
|
|
|
2016-09-29 11:12:31 -04:00
|
|
|
db.put(b"baz1", b"qux1")
|
|
|
|
|
2016-08-01 17:21:05 -04:00
|
|
|
print("foo1", "foo1" in db)
|
|
|
|
print("foo2", "foo2" in db)
|
2016-09-29 11:12:31 -04:00
|
|
|
print("baz1", "baz1" in db)
|
|
|
|
|
|
|
|
try:
|
|
|
|
print(db + db[b"foo1"])
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|
2016-08-01 17:21:05 -04:00
|
|
|
|
2016-12-04 17:55:05 -05:00
|
|
|
db.flush()
|
2016-07-23 17:30:32 -04:00
|
|
|
db.close()
|
2016-07-30 19:29:05 -04:00
|
|
|
f.close()
|