leveldb

A LevelDB wrapper for Nim in a Nim friendly way.

LevelDB is a fast and simple key/value data storage library built by Google that provides an ordered mapping from string keys to string values.

Create a database:

import leveldb
import options

var db = leveldb.open("/tmp/mydata")

Read or modify the database content:

assert db.getOrDefault("nothing", "") == ""

db.put("hello", "world")
db.put("bin", "GIF89a\1\0")
echo db.get("hello")
assert db.get("hello").isSome()

var key, val = ""
for key, val in db.iter():
  echo key, ": ", repr(val)

db.delete("hello")
assert db.get("hello").isNone()

Batch writes:

let batch = newBatch()
for i in 1..10:
  batch.put("key" & $i, $i)
batch.delete("bin")
db.write(batch)

Iterate over subset of database content:

for key, val in db.iterPrefix(prefix = "key1"):
  echo key, ": ", val
for key, val in db.iter(seek = "key3", reverse = true):
  echo key, ": ", val

db.close()

Types

LevelDb = ref object
  path*: string
  db: ptr leveldb_t
  cache: ptr leveldb_cache_t
  readOptions: ptr leveldb_readoptions_t
  syncWriteOptions: ptr leveldb_writeoptions_t
  asyncWriteOptions: ptr leveldb_writeoptions_t
  Source Edit
LevelDbWriteBatch = ref object
  batch: ptr leveldb_writebatch_t
Write batches for bulk data modification.   Source Edit
CompressionType = enum
  ctNoCompression = 0, ctSnappyCompression = 1
No compression or using Snappy algorithm (default).   Source Edit
LevelDbException = object of CatchableError
  Source Edit

Consts

version = "0.4.1"
  Source Edit

Procs

proc getLibVersion(): (int, int) {...}{.raises: [], tags: [].}
Get the version of leveldb C library.   Source Edit
proc close(self: LevelDb) {...}{.raises: [], tags: [].}

Closes the database.

See also:

  Source Edit
proc open(path: string; create = true; reuse = true; paranoidChecks = true;
          compressionType = ctSnappyCompression; cacheCapacity = 0;
          blockSize = 4 * 1024; writeBufferSize = 4096 * 1024;
          maxOpenFiles = 1000; maxFileSize = 2048 * 1024;
          blockRestartInterval = 16): LevelDb {...}{.raises: [LevelDbException],
    tags: [].}

Opens a database.

Raises LevelDbException if corruption detected in the database.

See also:

  Source Edit
proc put(self: LevelDb; key: string; value: string; sync = false) {...}{.
    raises: [LevelDbException], tags: [].}

Set a value for the specified key.

By default, sync is turned off, each write to leveldb is asynchronous. Unless reboot, a crash of just the writing process will not cause any loss since even when sync is false.

See also:

Example:

let db = leveldb.open("/tmp/test")
db.put("hello", "world")
db.close()
  Source Edit
proc get(self: LevelDb; key: string): Option[string] {...}{.
    raises: [LevelDbException], tags: [].}

Get the value for the specified key.

See also:

Example:

let db = leveldb.open("/tmp/test")
db.put("hello", "world")
echo db.get("hello")
db.close()
  Source Edit
proc getOrDefault(self: LevelDb; key: string; default = ""): string {...}{.
    raises: [LevelDbException], tags: [].}

Get the value for the specified key, or default if no value was set.

See also:

Example:

let db = leveldb.open("/tmp/test")
doAssert db.getOrDefault("what?", "nothing") == "nothing"
db.close()
  Source Edit
proc delete(self: LevelDb; key: string; sync = false) {...}{.
    raises: [LevelDbException], tags: [].}

Delete the key/value pair for the specified key.

See also:

  Source Edit
proc destroy(self: LevelDbWriteBatch) {...}{.raises: [], tags: [].}

Destroys this batch.

See also:

  Source Edit
proc newBatch(): LevelDbWriteBatch {...}{.raises: [], tags: [].}

Creates a new database write batch.

See also:

Example:

let db = leveldb.open("/tmp/test")
let batch = newBatch()
for i in 1..10:
  batch.put("key" & $i, $i)
batch.delete("another")
db.write(batch)
db.close()
  Source Edit
proc put(self: LevelDbWriteBatch; key: string; value: string; sync = false) {...}{.
    raises: [], tags: [].}

Set a value for the specified key. Same as put but operates on the write batch instead.

See also:

  Source Edit
proc append(self, source: LevelDbWriteBatch) {...}{.raises: [], tags: [].}

Merges the source batch into this batch.

See also:

  Source Edit
proc delete(self: LevelDbWriteBatch; key: string) {...}{.raises: [], tags: [].}

Delete the key/value pair for the specified key. Same as delete but operates on the write batch instead.

See also:

  Source Edit
proc clear(self: LevelDbWriteBatch) {...}{.raises: [], tags: [].}

Clear all updates buffered in this batch.

See also:

  Source Edit
proc write(self: LevelDb; batch: LevelDbWriteBatch) {...}{.
    raises: [LevelDbException], tags: [].}

Write apply the given batch to the database.

See also:

  Source Edit
proc removeDb(name: string) {...}{.raises: [LevelDbException], tags: [].}
Remove the database name.   Source Edit
proc repairDb(name: string) {...}{.raises: [LevelDbException], tags: [].}
Repairs the corrupted database name.   Source Edit

Iterators

iterator iter(self: LevelDb; seek: string = ""; reverse: bool = false): (string,
    string) {...}{.raises: [LevelDbException], tags: [].}

Iterate all key/value pairs in the database from the first one or the specified key seek. By default, the ordering will be lexicographic byte-wise ordering with leveldb builtin comparator, unless reverse set to true.

See also:

  Source Edit
iterator iterPrefix(self: LevelDb; prefix: string): (string, string) {...}{.
    raises: [LevelDbException], tags: [].}

Iterate subset key/value pairs in the database with a particular prefix.

See also:

  Source Edit
iterator iterRange(self: LevelDb; start, limit: string): (string, string) {...}{.
    raises: [LevelDbException], tags: [].}

Yields all key/value pairs between the start and limit keys (inclusive) in the database.

See also:

  Source Edit