Skip to content

Commit

Permalink
Return value-for-return-value mongo compat
Browse files Browse the repository at this point in the history
  • Loading branch information
rescrv committed Jan 12, 2015
1 parent 428ca19 commit 3d04041
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 31 deletions.
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1439,3 +1439,4 @@ maintainer-generate:
python2 test/doc-extract.py python doc/atomic-ops.tex test/doc.atomic-ops.py
python2 test/doc-extract.py python doc/documents.tex test/doc.documents.py
python2 test/doc-extract.py python doc/authorization.tex test/doc.authorization.py
python2 test/doc-extract.py python doc/mongo.tex test/doc.mongo.py
67 changes: 36 additions & 31 deletions bindings/python/hyperdex/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,22 @@
import hyperdex.admin

class HyperIterator:
def __init__(self, innerIter_):
self.innerIter = innerIter_
def __init__(self, innerIter):
self.innerIter = innerIter

def hasNext(self):
return self.innerIter.hasNext()

def __iter__(self):
return self.innerIter
return self

def next(self):
if not self.hasNext():
raise RuntimeError('Cannot get next element. There is none!')

return self.innerIter.next()['v'].doc()
raise StopIteration()
o = self.innerIter.next()
x = o['v'].doc()
x['_id'] = o['k']
return x

class HyperSpace:
Document = hyperdex.client.Document
Expand Down Expand Up @@ -103,12 +105,10 @@ def find(self, conditions = {}):
self.init()
hyperconds = self.convert_conds(conditions)
result = self.client.search(self.name, hyperconds)

return HyperIterator(result)

def findOne(self, conditions = {}):
def find_one(self, conditions = {}):
it = self.find(conditions)

if not it.hasNext():
return None
else:
Expand Down Expand Up @@ -143,9 +143,12 @@ def insert(self, value):
if value is None or value['_id'] is None:
#TODO auto generate id
raise ValueError("Document is missing an id field")

self.init()
return self.client.put(self.name, value['_id'], {'v' : self.Document(value)})
copy = value.copy()
del copy['_id']
if self.client.put(self.name, value['_id'], {'v' :
self.Document(copy)}):
return value['_id']

def save(self, value):
return self.insert(value)
Expand All @@ -160,39 +163,41 @@ def list_keys(self):

def update(self, select, arg):
self.init()
count = None

for k,v in arg.items():
if k == "$inc":
self.group_atomic_add(select, v)
count = count or self.group_atomic_add(select, v)
elif k == '$bit':
if not isinstance(v, dict):
raise ValueError('$bit argument must a dict')

op, mask = v.iteritems().next()

if op == 'and':
self.group_atomic_and(select, mask)
elif op == 'or':
self.group_atomic_or(select, mask)
elif op == 'mod':
self.group_atomic_mod(select, mask)
elif op =='xor':
self.group_atomic_xor(select, mask)
else:
raise ValueError("Unknown bit-operation")

for field, ops in v.items():
for op, mask in ops.items():
mask = {field: mask}
if op == 'and':
count = count or self.group_atomic_and(select, mask)
elif op == 'or':
count = count or self.group_atomic_or(select, mask)
elif op == 'mod':
count = count or self.group_atomic_mod(select, mask)
elif op =='xor':
count = count or self.group_atomic_xor(select, mask)
else:
raise ValueError("Unknown bit-operation")
elif k == '$set':
self.group_set(select, v)
count = count or self.group_set(select, v)
elif k == '$mul':
self.group_atomic_mul(select, v)
count = count or self.group_atomic_mul(select, v)
elif k == '$div':
self.group_atomic_div(select, v)
count = count or self.group_atomic_div(select, v)
elif k == '$push':
self.group_list_rpush(select, v)
count = count or self.group_list_rpush(select, v)
elif k == '$rename':
self.group_rename(select, v)
count = count or self.group_rename(select, v)
else:
raise ValueError("Unknown command " + k)
return {'updatedExisting': bool(count), u'nModified': count, u'ok': 1,
u'n': 1 if count else 0}

def convert_docargs(self, args):
if (not isinstance(args, dict)) or (len(args) is 0):
Expand Down

0 comments on commit 3d04041

Please sign in to comment.