You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Manipulate DJB's Constant Database files. These are 2 level disk-based hash tables that efficiently handle thousands of keys, while remaining space-efficient.
1
+
## Introduction
2
2
3
-
http://cr.yp.to/cdb.html
3
+
`python-pure-cdb` (`pure-cdb` on [PyPI](https://pypi.org/project/pure-cdb/)) is a Python library for reading and writing djb's "constant database" files. These disk-based associative arrays that allow efficient storage and retrieval of thousands of keys. For more information on `cdb`, see [djb's page](https://cr.yp.to/cdb.html) and [Wikipedia](https://en.wikipedia.org/wiki/Cdb_(software)).
4
4
5
-
Note the Reader class reads the entire CDB into memory.
5
+
### Installation and usage
6
6
7
-
------
7
+
Install `pure-cdb` with `pip`:
8
+
9
+
```
10
+
pip install pure-cdb
11
+
```
12
+
13
+
Then import `cdblib` to access the `Reader` and `Writer` classes.
14
+
15
+
---
16
+
17
+
`Reader` provides an interface to a `cdb` file's contents - use the `.iterkeys()` method to iterate over keys, and the `.get()` method to retrieve a key's value.
18
+
19
+
```python
20
+
import cdblib
21
+
22
+
withopen('info.cdb', 'rb') as f:
23
+
data = f.read()
24
+
25
+
reader = cdblib.Reader(data)
26
+
27
+
reader.get(b'hello')
28
+
```
29
+
30
+
All keys are `bytes` objects (`str` in Python 2). By default all values are also retrieved as `bytes` objects, but the `.getstring()` and `.getint()` methods can be used to retrieve decoded strings and integers, respectively.
31
+
32
+
---
33
+
34
+
`Writer` allows for creating new `cdb` files. Use the `.put()` method to insert a key / value pair, and the `.finalize()` method to create the CDB structure.
35
+
36
+
```python
37
+
withopen('/home/bo/Desktop/new.cdb', 'wb') as f:
38
+
writer = cdblib.Writer(f)
39
+
writer.put(b'key', b'value')
40
+
writer.finalize()
41
+
```
42
+
43
+
As with the `Reader` class, keys and values are `bytes` objects. The `.putstrings()` and `.putint()` methods can be used to insert encoded text data and integers, respectively.
44
+
45
+
---
46
+
47
+
### Remarks on usage
8
48
9
49
Constant databases have the desirable property of requiring low overhead to open. This makes them ideal for use in environments where it's not always possible to have data hanging around in RAM awaiting use, for example in a CGI script or Google App Engine.
10
50
11
51
On App Engine, reading a 700kb database into memory from the filesystem costs around 90ms, after which the cost of individual lookups is negligible (one simple benchmark achieved 94k lookups/sec running on App Engine, and that was using `djb_hash()`). This makes CDBs ideal for storing many small keys that are infrequently accessed, for example in per-language corpora containing 10k+ internationalized strings (my original use case).
12
52
13
53
The format might also be useful as a composite file storage alternative to the `zipfile` module. Since the Reader interface only requires a file-like object, it is possible to wrap a string stored in a Datastore entity in a `cStringIO` object, allowing convenient access to it as a CDB.
14
54
15
-
When storing many thousands of keys in a Datastore entity, CDBs might enable you to bypass the "indexed fields" limit of the `Expando` class without resorting to `pickle`, as well as reduce deserialization overhead when only a few keys of such an entity are normally accessed and rarely updated.
55
+
When storing many thousands of keys in a Datastore entity, CDBs might enable you to bypass the "indexed fields" limit of the `Expando` class without resorting to `pickle`, as well as reduce deserialization overhead when only a few keys of such an entity are normally accessed and rarely updated.
0 commit comments