CodernityDB / CodernityDB / index.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011-2012 Codernity (http://codernity.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import os
import marshal

import io


class IndexException(Exception):
    pass


class IndexNotFoundException(IndexException):
    pass


class ReindexException(IndexException):
    pass


class TryReindexException(ReindexException):
    pass


class ElemNotFound(IndexException):
    pass


class DocIdNotFound(ElemNotFound):
    pass


class IndexConflict(IndexException):
    pass


class IndexPreconditionsException(IndexException):
    pass


class Index(object):

    __version__ = __version__

    custom_header = ""  # : use it for imports required by your index

    def __init__(self,
                 db_path,
                 name):
        self.name = name
        self._start_ind = 500
        self.db_path = db_path

    def open_index(self):
        if not os.path.isfile(os.path.join(self.db_path, self.name + '_buck')):
            raise IndexException("Doesn't exists")
        self.buckets = io.open(
            os.path.join(self.db_path, self.name + "_buck"), 'r+b', buffering=0)
        self._fix_params()
        self._open_storage()

    def _close(self):
        self.buckets.close()
        self.storage.close()

    def close_index(self):
        self.flush()
        self.fsync()
        self._close()

    def create_index(self):
        raise NotImplemented

    def _fix_params(self):
        self.buckets.seek(0)
        props = marshal.loads(self.buckets.read(self._start_ind))
        for k, v in props.iteritems():
            self.__dict__[k] = v
        self.buckets.seek(0, 2)

    def _save_params(self, in_params={}):
        self.buckets.seek(0)
        props = marshal.loads(self.buckets.read(self._start_ind))
        props.update(in_params)
        self.buckets.seek(0)
        data = marshal.dumps(props)
        if len(data) > self._start_ind:
            raise IndexException("To big props")
        self.buckets.write(data)
        self.flush()
        self.buckets.seek(0, 2)
        self.__dict__.update(props)

    def _open_storage(self, *args, **kwargs):
        pass

    def _create_storage(self, *args, **kwargs):
        pass

    def _destroy_storage(self, *args, **kwargs):
        self.storage.destroy()

    def _find_key(self, key):
        raise NotImplemented

    def update(self, key, start, size):
        raise NotImplemented

    def insert(self, key, start, size):
        raise NotImplemented

    def get(self, key):
        raise NotImplemented

    def get_many(self, key, start_from=None, limit=0):
        raise NotImplemented

    def all(self, start_pos):
        raise NotImplemented

    def delete(self, key, start, size):
        raise NotImplemented

    def make_key_value(self, data):
        raise NotImplemented

    def make_key(self, data):
        raise NotImplemented

    def compact(self, *args, **kwargs):
        raise NotImplemented

    def destroy(self, *args, **kwargs):
        self._close()
        bucket_file = os.path.join(self.db_path, self.name + '_buck')
        os.unlink(bucket_file)
        self._destroy_storage()
        self._find_key.clear()

    def flush(self):
        try:
            self.buckets.flush()
            self.storage.flush()
        except:
            pass

    def fsync(self):
        try:
            os.fsync(self.buckets.fileno())
            self.storage.fsync()
        except:
            pass
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.