2015-10-27 107 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上返回給定密鑰的多個數據。因此它不會返回所有數據。改用all方法。

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

經過一點點狩獵後,我發現了虛假的印花。

CodernityDB\storage.py::135get()方法打印當地人

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)) 

,而無需編輯站點包我有猴子打補丁用我自己的方法。

相關問題