Source

graphalchemy / graphalchemy / mixins / workers.py

Full commit
# -*- coding: utf-8 -*-
'''graph reader mixins'''

import nanotime

from graphalchemy.core import defer

__all__ = ('WriterMixin', 'ReaderMixin')


class ReaderMixin(object):

    '''graph reader base'''

    def id(self, element):
        '''
        graph element identifier

        @param element: a graph element
        '''
        return self.r.id(element)

    def properties(self, element):
        '''
        fetch a graph model's properties

        @param element: element with properties
        '''
        return self.r.properties(element)


class WriterMixin(object):

    '''graph writer base'''

    @defer
    def index(self, index, fts=False):
        '''
        create link index

        @param index: node index name
        @param fts: create a full text index (default: False)
        '''
        self.w.create_index(index, fts)

    @defer
    def index_many(self, index, this, indexed):
        '''
        index properties on a graph element

        @param index: graph index label
        @param this: graph object
        @param indexed: properties to index
        '''
        self.w.index_many(self.r.index(index), this, indexed)

    @defer
    def index_one(self, index, key, value, this):
        '''
        index one property of a graph element

        @param index: graph index label
        @param key: graph element property key
        @param value: graph element property value
        @param this: graph object
        '''
        self.w.index_one(self.r.index(index), key, value, this)

    @staticmethod
    def now():
        '''current time in microseconds'''
        return nanotime.now().microseconds()