2015-10-27 4 views
3

Я пытаюсь использовать CodernityDB в одном из моих проектов. Я получил пример minitwit, но попробовать какой-то пример кода в моем проекте, похоже, не работает. Чего я пропущу?Не может показаться, что индекс CodernityDB работает

from CodernityDB.database_thread_safe import ThreadSafeDatabase 
from CodernityDB.hash_index import HashIndex 

import uuid 

class PumpIndex(HashIndex): 

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

    def make_key_value(self, data): 
     if data['t'] == 'pump': 
      id = data['id'] 
      # if not isinstance(login, basestring): 
      #  login = str(login) 
      return id, {'name': data['name']} 

    def make_key(self, key): 
     return key 

    def run_all(self, db): 
     it = db.get_many(self.name, with_doc=True) 
     for curr in it: 
      curr['id'] = curr['doc']['id'] 
      curr['name'] = curr['doc']['name'] 
      curr['hardware_type'] = curr['doc']['hardware_type'] 
      curr['onboard_pump_id'] = curr['doc']['onboard_pump_id'] 
      curr['pressure_sensor_id'] = curr['doc']['pressure_sensor_id'] 
      curr['pressure_min'] = curr['doc']['pressure_min'] 
      curr['pressure_max'] = curr['doc']['pressure_max'] 
      curr['ignore_errors'] = curr['doc']['ignore_errors'] 
      curr['simultaneous_zones'] = curr['doc']['simultaneous_zones'] 
      del curr['doc'] 
      yield curr 

db = ThreadSafeDatabase('~/Desktop/test') 

if db.exists(): 
    db.open() 
    db.reindex() 
else: 

    db.create() 
    db.add_index(PumpIndex(db.path, 'pump')) 

for x in range(0,10): 
    id = uuid.uuid4().hex 
    name = 'Test %d' % x 
    db.insert(dict(
       t='pump', 
       id=id, 
       name=name, 
       hardware_type="onboard", 
       onboard_pump_id=None, 
       pressure_sensor_id=None, 
       pressure_min=None, 
       pressure_max=None, 
       ignore_errors=None, 
       simultaneous_zones=None 
      )) 

print int(db.count(db.get_many, 'pump')) 
print int(len(list(db.run('pump', 'all')))) 

Выход консоли я получаю:

{'status': 'o', 'start': 100, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 304, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 508, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 712, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 916, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 1120, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 1324, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 1528, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 1732, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
{'status': 'o', 'start': 1936, 'self': <CodernityDB.storage.IU_Storage object at 0x10bb1ed50>, 'size': 204} 
0 
0 

Другой вопрос, можно ли не получить выход статуса Codernity? Его полезно для развития, но не столько для производства.

+0

Вы узнали, как удалить консольную распечатку? – Glycerine

ответ

0

Кажется, что метод get_many on возвращает несколько данных для заданного ключа. Таким образом, он не будет возвращать все данные. Вместо этого используйте метод all.

db.count(db.all, 'pump') 
1

После небольшой охоты я обнаружил ложную печать.

В CodernityDB\storage.py::135 Метод get() печатает местных:

def get(self, start, size, status='c'): 
     if status == 'd': 
      return None 
     else: 
      print locals() 
      self._f.seek(start) 
      return self.data_from(self._f.read(size)) 

без редактирования сайта-пакет я обезьяна исправлен метод с моим собственным.

Смежные вопросы