-
Notifications
You must be signed in to change notification settings - Fork 151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added store_many_vectors on Mongo Storage #87
Open
etudor
wants to merge
8
commits into
pixelogik:master
Choose a base branch
from
etudor:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c93c3cd
added store_many_vectors on mongo storage
etudor 86821b0
added tests, fixed deprecation warnings
etudor 1579e24
added pymongo to test req
etudor 8ad62c4
reverted the changes
etudor 7ce25ec
updated pymongo insert dict to unicode format
etudor 4506f5b
add convert to unicode
etudor 25b4c48
fixed return
etudor 152c49a
fix encoding error
etudor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,13 +29,20 @@ | |
|
||
import numpy | ||
import scipy | ||
from nearpy.utils.utils import convert2unicode | ||
|
||
try: | ||
import cPickle as pickle | ||
except ImportError: | ||
import pickle | ||
|
||
from future.builtins import bytes | ||
try: | ||
from pymongo import InsertOne | ||
except ImportError: | ||
pass | ||
|
||
from nearpy.storage.storage import Storage | ||
from future.builtins import zip | ||
|
||
|
||
class MongoStorage(Storage): | ||
|
@@ -45,7 +52,22 @@ def __init__(self, mongo_object): | |
""" Uses specified pymongo object for storage. """ | ||
self.mongo_object = mongo_object | ||
|
||
def store_many_vectors(self, hash_name, bucket_keys, vs, data): | ||
requests = [] | ||
|
||
for v, d, bk in zip(vs, data, bucket_keys): | ||
vc = self._get_vector(hash_name, bk, v, d) | ||
|
||
requests.append(InsertOne(vc)) | ||
|
||
self.mongo_object.bulk_write(requests, ordered=False) | ||
|
||
def store_vector(self, hash_name, bucket_key, v, data): | ||
val_dict = self._get_vector(hash_name, bucket_key, v, data) | ||
|
||
self.mongo_object.insert_one(val_dict) | ||
|
||
def _get_vector(self, hash_name, bucket_key, v, data): | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This docstring belongs to |
||
Stores vector and JSON-serializable data in MongoDB with specified key. | ||
""" | ||
|
@@ -83,8 +105,9 @@ def store_vector(self, hash_name, bucket_key, v, data): | |
if data is not None: | ||
val_dict['data'] = data | ||
|
||
# Push JSON representation of dict to end of bucket list | ||
self.mongo_object.insert_one(val_dict) | ||
convert2unicode(val_dict) | ||
|
||
return val_dict | ||
|
||
def _format_mongo_key(self, hash_name, bucket_key): | ||
return '{}{}'.format(self._format_hash_prefix(hash_name), bucket_key) | ||
|
@@ -147,7 +170,7 @@ def get_bucket(self, hash_name, bucket_key): | |
shape=(val_dict['dim'], 1)) | ||
|
||
else: | ||
vector = numpy.fromstring(val_dict['vector'], | ||
vector = numpy.frombuffer(val_dict['vector'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I updated this because I got some deprecation warnings |
||
dtype=val_dict['dtype']) | ||
[val_dict.pop(k) for k in ['vector', 'dtype', '_id']] | ||
# Add data to result tuple, if present | ||
|
@@ -186,5 +209,6 @@ def load_hash_configuration(self, hash_name): | |
conf = self.mongo_object.find_one( | ||
{'hash_conf_name': hash_name + '_conf'} | ||
) | ||
|
||
return pickle.loads(conf['hash_configuration']) if conf is not None\ | ||
else None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,5 +28,6 @@ | |
"redis", | ||
"mockredispy", | ||
"mongomock", | ||
"pymongo" | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest using
from future.builtins import zip
because it is more efficient in python2.7.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the code, thanks