ExamplesΒΆ

Embedded mode

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from CodernityDB.database import Database


def main():
    db = Database('/tmp/test_emb')
    db.create()

    for x in range(100):
        db.insert(dict(x=x))

    for curr in db.all('id'):
        print curr


if __name__ == '__main__':
    main()

CodernityDB-HTTP with CodernityDB-Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from CodernityDBPyClient import client
client.setup()

from CodernityDB.database import Database


def main():
    db = Database('/tmp/test_client')
    db.create()

    for x in range(100):
        db.insert(dict(x=x))

    for curr in db.all('id'):
        print curr


if __name__ == '__main__':
    main()

As you can see there is no difference in main code. Only one difference is in lines 4 and 5. That’s all. It will register import hooks.

Even this will work with no code changes

Embedded mode

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python
# -*- coding: utf-8 -*-


from CodernityDB.database import Database
from CodernityDB.tree_index import TreeBasedIndex


class SimpleTreeIndex(TreeBasedIndex):

    def __init__(self, *args, **kwargs):
        kwargs['node_capacity'] = 13
        kwargs['key_format'] = 'I'
        super(SimpleTreeIndex, self).__init__(*args, **kwargs)

    def make_key_value(self, data):
        a_val = data.get('a')
        if a_val is not None:
            return a_val, None
        return None

    def make_key(self, key):
        return key


def main():
    db = Database('/tmp/big_emb')
    db.create()
    db.add_index(SimpleTreeIndex(db.path, 'tree'))

    for a in xrange(100):
        db.insert(dict(a=a, demo=True))

    for c in db.get_many('tree', start=3, end=10, limit=-1, inclusive_end=True, inclusive_start=True):
        print c

    db.close()


if __name__ == '__main__':
    main()

CodernityDB-HTTP with CodernityDB-Client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from CodernityDBPyClient import client
client.setup()


from CodernityDB.database import Database
from CodernityDB.tree_index import TreeBasedIndex


class SimpleTreeIndex(TreeBasedIndex):

    def __init__(self, *args, **kwargs):
        kwargs['node_capacity'] = 13
        kwargs['key_format'] = 'I'
        super(SimpleTreeIndex, self).__init__(*args, **kwargs)

    def make_key_value(self, data):
        a_val = data.get('a')
        if a_val is not None:
            return a_val, None
        return None

    def make_key(self, key):
        return key


def main():
    db = Database('/tmp/big_client')
    db.create()
    db.add_index(SimpleTreeIndex(db.path, 'tree'))

    for a in xrange(100):
        db.insert(dict(a=a, demo=True))

    for c in db.get_many('tree', start=3, end=10, limit=-1, inclusive_end=True, inclusive_start=True):
        print c

    db.close()


if __name__ == '__main__':
    main()

As you can see it’s very easy to use CodernityDB-PyClient.