docs/btree: Add hints about opening db file and need to flush db.

pull/1728/merge
Paul Sokolovsky 2017-06-11 17:44:11 +03:00
rodzic 869cdcfdfc
commit 6ca086a89a
1 zmienionych plików z 12 dodań i 1 usunięć

Wyświetl plik

@ -23,7 +23,13 @@ Example::
# First, we need to open a stream which holds a database
# This is usually a file, but can be in-memory database
# using uio.BytesIO, a raw flash section, etc.
f = open("mydb", "w+b")
# Oftentimes, you want to create a database file if it doesn't
# exist and open if it exists. Idiom below takes care of this.
# DO NOT open database with "a+b" access mode.
try:
f = open("mydb", "r+b")
except OSError:
f = open("mydb", "w+b")
# Now open a database itself
db = btree.open(f)
@ -33,6 +39,11 @@ Example::
db[b"1"] = b"one"
db[b"2"] = b"two"
# Assume that any changes are cached in memory unless
# explicitly flushed (or database closed). Flush database
# at the end of each "transaction".
db.flush()
# Prints b'two'
print(db[b"2"])