#!/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.importosimportmarshalimportstructimportshutilfromCodernityDB.storageimportIU_Storage,DummyStoragetry:fromCodernityDBimport__version__exceptImportError:from__init__import__version__importioclassIndexException(Exception):passclassIndexNotFoundException(IndexException):passclassReindexException(IndexException):passclassTryReindexException(ReindexException):passclassElemNotFound(IndexException):passclassDocIdNotFound(ElemNotFound):passclassIndexConflict(IndexException):passclassIndexPreconditionsException(IndexException):passclassIndex(object):__version__=__version__custom_header=""# : use it for imports required by your indexdef__init__(self,db_path,name):self.name=nameself._start_ind=500self.db_path=db_pathdefopen_index(self):ifnotos.path.isfile(os.path.join(self.db_path,self.name+'_buck')):raiseIndexException("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()defclose_index(self):self.flush()self.fsync()self._close()defcreate_index(self):raiseNotImplementeddef_fix_params(self):self.buckets.seek(0)props=marshal.loads(self.buckets.read(self._start_ind))fork,vinprops.iteritems():self.__dict__[k]=vself.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)iflen(data)>self._start_ind:raiseIndexException("To big props")self.buckets.write(data)self.flush()self.buckets.seek(0,2)self.__dict__.update(props)def_open_storage(self,*args,**kwargs):passdef_create_storage(self,*args,**kwargs):passdef_destroy_storage(self,*args,**kwargs):self.storage.destroy()def_find_key(self,key):raiseNotImplementeddefupdate(self,key,start,size):raiseNotImplementeddefinsert(self,key,start,size):raiseNotImplementeddefget(self,key):raiseNotImplementeddefget_many(self,key,start_from=None,limit=0):raiseNotImplementeddefall(self,start_pos):raiseNotImplementeddefdelete(self,key,start,size):raiseNotImplementeddefmake_key_value(self,data):raiseNotImplementeddefmake_key(self,data):raiseNotImplementeddefcompact(self,*args,**kwargs):raiseNotImplementeddefdestroy(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()defflush(self):try:self.buckets.flush()self.storage.flush()except:passdeffsync(self):try:os.fsync(self.buckets.fileno())self.storage.fsync()except:pass