Source

CodernityDB / docs / examples.rst

Examples

Here you will find some examples, from different projects / ideas. So they might miss some context information etc.

Example indexes

Custom Hash

It defines index that separates data in two groups >5 and <=5 based on test parameter.

class CustomHashIndex(HashIndex):

    def __init__(self, *args, **kwargs):
        kwargs['key_format'] = 'I'
        kwargs['hash_lim'] = 1
        super(CustomHashIndex, self).__init__(*args, **kwargs)

    def make_key_value(self, data):
        d = data.get('test')
        if d is None:
            return None
        if d > 5:
            k = 1
        else:
            k = 0
        return k, dict(test=d)

    def make_key(self, key):
        return key

MD5 Hash

Nothing more than index shown in :ref:`design`.

class Md5Index(HashIndex):

    def __init__(self, *args, **kwargs):
        kwargs['key_format'] = '32s'
        super(Md5Index, self).__init__(*args, **kwargs)

    def make_key_value(self, data):
        return md5(data['name']).hexdigest(), None

    def make_key(self, key):
        return md5(key).hexdigest()

With A

It allows to search in database for objects that contains a in their structure.

class WithAIndex(HashIndex):

    def __init__(self, *args, **kwargs):
        kwargs['key_format'] = '32s'
        super(WithAIndex, self).__init__(*args, **kwargs)

    def make_key_value(self, data):
        a_val = data.get("a")
        if a_val:
            if not isinstance(a_val, basestring):
                a_val = str(a_val)
            return md5(a_val).hexdigest(), None
        return None

    def make_key(self, key):
        if not isinstance(key, basestring):
            key = str(key)
        return md5(key).hexdigest()

Simple Tree

That index uses Tree index. It will allow you to search in order for elements that have t in their structure.

class Simple_TreeIndex(TreeBasedIndex):

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

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

    def make_key(self, key):
        return key

With Run

This index will allow you to run sum function on database context (you don't have to retrieve all the data first, and then process it). Strongly recommended on :ref:`server` usage.

class WithRun_Index(HashIndex):

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

    def run_sum(self, db_obj, key):
        gen = db_obj.get_many(index_name=self.name, key=key, limit=-1, with_storage=True)
        vals = []
        while True:
            try:
                d = gen.next()
            except StopIteration:
                break
            else:
                vals.append(d.get('x', 0))
        return sum(vals)

    def make_key_value(self, data):
        a_val = data.get("a")
        if a_val is not None:
            out = {'x': data.get('x')}
            return a_val, out
        return None

    def make_key(self, key):
        return key

Example storages

Secure storage

It allows you to crypt storage information with Salsa20 algorithm. To use it you need to have index that will open storage with encryption key.

Example database functions

Join like 1

It will join user with timeline entries. See |CodernityDB-Demos| minitwitt to see more things like this.

def run_timeline(self, db, user, limit):
    u = db.get('user', user)
    it = db.get_many(self.name, user, end=10 ** 11, limit=limit, with_doc=True)
    for curr in it:
        curr['username'] = user
        curr['email'] = u['email']
        curr['pub_date'] = curr['doc']['pub_date']
        curr['text'] = curr['doc']['text']
        del curr['doc']
        yield curr
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.