Skip to content

Commit

Permalink
docs/btree: Add hints about opening db file and need to flush db.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfalcon committed Jun 11, 2017
1 parent 869cdcf commit 6ca086a
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion docs/library/btree.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"])

Expand Down

0 comments on commit 6ca086a

Please sign in to comment.