Skip to content

Commit

Permalink
refactor(leveldb): fix to new basepbindexer
Browse files Browse the repository at this point in the history
  • Loading branch information
hanxiao committed Jun 11, 2020
1 parent 8970409 commit 86a625d
Showing 1 changed file with 8 additions and 23 deletions.
31 changes: 8 additions & 23 deletions jina/executors/indexers/keyvalue/leveldb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from typing import Union

from google.protobuf.json_format import Parse

from jina.executors.indexers.keyvalue.proto import BasePbIndexer
from jina.executors.indexers.keyvalue.proto import jina_pb2

Expand All @@ -14,28 +15,19 @@ class LeveldbIndexer(BasePbIndexer):
:class:`LeveldbIndexer` use `LevelDB` to save and query protobuf chunk/document.
"""

def post_init(self):
super().post_init()
self._db_handler = None

@property
def db_handler(self):
if self._db_handler is None:
import plyvel
self._db_handler = plyvel.DB(self.index_abspath, create_if_missing=True)
return self._db_handler

def get_add_handler(self):
"""Get the database handler
"""
return self.db_handler
import plyvel
return plyvel.DB(self.index_abspath, create_if_missing=True)

def get_create_handler(self):
"""Get the database handler
"""
return self.db_handler
import plyvel
return plyvel.DB(self.index_abspath, create_if_missing=True)

def add(self, objs):
"""Add a JSON-friendly object to the indexer
Expand All @@ -52,7 +44,8 @@ def get_query_handler(self):
"""Get the database handler
"""
return self.db_handler
import plyvel
return plyvel.DB(self.index_abspath, create_if_missing=True)

def query(self, key: str, *args, **kwargs) -> Union['jina_pb2.Chunk', 'jina_pb2.Document']:
"""Find the protobuf chunk/doc using id
Expand All @@ -63,17 +56,9 @@ def query(self, key: str, *args, **kwargs) -> Union['jina_pb2.Chunk', 'jina_pb2.
v = self.query_handler.get(key.encode('utf8'))
value = None
if v is not None:
_parser = jina_pb2.Chunk if key[0] == 'c' else jina_pb2.Document
value = Parse(json.loads(v.decode('utf8')), _parser())
value = Parse(json.loads(v.decode('utf8')), self._parser())
return value

def close(self):
"""Close the database handler
"""
super().close()
self.write_handler.close()


class ChunkLeveldbIndexer(LeveldbIndexer):
""""""
Expand Down

0 comments on commit 86a625d

Please sign in to comment.