Skip to content

Commit

Permalink
Added an (untested) additiveOnly command, that implicitly puts anythi…
Browse files Browse the repository at this point in the history
…ng with an _id and _rev into skip.
  • Loading branch information
Eli Stevens committed Dec 23, 2011
1 parent 60bcb31 commit 82a4c27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Expand Up @@ -25,16 +25,14 @@ junk
*.delme

# Temporary, externally sourced
couchable
solari*
#couchable
#solari*

# For testing solari
selenium*.jar
#selenium*.jar

# vim tempfiles
.*.swp

# test data
.coverage
mms/common/storage/data
gamma_data.csv
30 changes: 22 additions & 8 deletions couchable/core.py
Expand Up @@ -317,7 +317,7 @@ def __deepcopy__(self, memo):
# inst.__dict__.update({copy.deepcopy(k): copy.deepcopy(v) for k,v in self.__dict__.items if k not in ['_cdb']})


def store(self, what, skip=None):
def store(self, what, skip=None, additiveOnly=False):
"""
Stores the documents in the C{what} parameter in CouchDB. If a C{._id}
does not yet exist on the object, it will be added. If the C{._id} is
Expand Down Expand Up @@ -353,6 +353,8 @@ def store(self, what, skip=None):
else:
self._skip_list = [x for x in skip if hasattr(x, '_id') and hasattr(x, '_rev')]

self._additiveOnly = additiveOnly

if not isinstance(what, list):
store_list = [what]
else:
Expand All @@ -373,6 +375,7 @@ def store(self, what, skip=None):
mime_list = []
bulk_list = []
for (obj, doc, attachment_dict) in todo_list:
log_internal.info("TODO: {}".format(doc['_id']))
if obj not in self._skip_list:
if 'pickles' in attachment_dict:
content_tup = attachment_dict['pickles']
Expand Down Expand Up @@ -424,24 +427,30 @@ def store(self, what, skip=None):
params = {}
status, msg, data = self.db.resource.post(doc['_id'], body, http_headers, **params)

if status != 201:
log_internal.warn("Error updating multipart, status: {}, msg: {}".format(staus, msg))
raise Exception("Error updating multipart, status: {}, msg: {}".format(staus, msg))
#print 'status', status
#print 'msg', msg
#print 'data', str(data.getvalue())
#assert status == 201

data_dict = couchdb.json.decode(data.getvalue())

#print data_dict

obj._id = data_dict['id']
obj._rev = data_dict['rev']

#print 'status', status
#print 'msg', msg
#print 'data', str(data.getvalue())

#print 'hitting bulk docs:', [x for x in [str(bulk_tup[1].get('_id', None)) for bulk_tup in bulk_list] if 'CoordinateSystem' not in x]
ret_list = self.db.update([bulk_tup[1] for bulk_tup in bulk_list])

#print ret_list
for (success, _id, _rev), (obj, doc) in itertools.izip(ret_list, bulk_list):
if not success:
log_internal.warn("Error updating {}: {} @ {}".format(type(obj), _id, _rev)) # + repr(doc))
log_internal.warn("Error updating {}: {} @ {}".format(type(obj), _id, getattr(obj, '_rev', None)))
#log_internal.warn("Error updating {}: {} > {}".format(type(obj), _id, vars(_rev)))
raise _rev
else:
obj._rev = _rev
Expand All @@ -450,6 +459,7 @@ def store(self, what, skip=None):
del self._done_dict
del self._cycle_set
del self._skip_list
del self._additiveOnly

if not isinstance(what, list):
return what._id
Expand Down Expand Up @@ -607,7 +617,11 @@ def _pack_object(self, parent_doc, data, attachment_dict, name, isKey, topLevel=

# Means this needs to be a new top-level document.
if base_cls and not topLevel:
if data not in self._skip_list:
if self._additiveOnly and \
getattr(data, '_id', None) != None and \
getattr(data, '_rev', None) != None:
pass
elif data not in self._skip_list:
self._store(data)

return '{}{}:{}'.format(FIELD_NAME, 'id', data._id)
Expand Down Expand Up @@ -1145,7 +1159,7 @@ def load(self, what, loaded=None):

def _load(self, _id, loaded_dict):
if _id not in loaded_dict:
log_internal.info("Fetching object from DB: {}".format(_id))
log_internal.debug("Fetching object from DB: {}".format(_id))
try:
loaded_dict[_id] = self.db[_id]
except:
Expand All @@ -1158,7 +1172,7 @@ def _load(self, _id, loaded_dict):

obj = self._obj_by_id.get(_id, None)
if obj is None or getattr(obj, '_rev', None) != doc['_rev']:
log_internal.info("Unpacking object: {}".format(_id))
log_internal.debug("Unpacking object: {}".format(_id))
obj = self._unpack(doc, doc, loaded_dict, obj)

base_cls, func_tuple = findHandler(type(obj), _couchable_types)
Expand Down

0 comments on commit 82a4c27

Please sign in to comment.