Skip to content

Commit

Permalink
Add search method in ResourceCache
Browse files Browse the repository at this point in the history
  • Loading branch information
eonpatapon committed Apr 20, 2016
1 parent 4ec0772 commit 16fba19
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
8 changes: 1 addition & 7 deletions contrail_api_cli/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,8 @@ def get_completions(self, document, complete_event):
# fq_name search
text_before_cursor
]
searches = [s for s in searches if s in self.cache.trie]

if not searches:
return

# limit list to 50 entries
resources = [self.cache.resources[p] for p in self.cache.trie[searches[0]]][:50]

resources = self.cache.search(searches, limit=50)
for res in resources:
rel_path = text_type(res.path.relative_to(ShellContext.current_path))
if rel_path in ('.', '/', ''):
Expand Down
17 changes: 17 additions & 0 deletions contrail_api_cli/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,23 @@ def __init__(self):
Collection.register('created', self._add_resource)
Collection.register('deleted', self._del_resource)

def search(self, paths, limit=None):
"""Search in cache
:param strings: list of paths to get from the cache
:type strings: str list
:param limit: limit search results
:type limit: int
:rtype: Resource list
"""
results = [p for p in paths if p in self.trie]
if not results:
return []
resources = [self.resources[p]
for p in self.trie[results[0]]][:limit]
return resources

def _action_in_trie(self, value, path, action):
v = ""
for c in value:
Expand Down

0 comments on commit 16fba19

Please sign in to comment.