def get(self, key): '''Retrieves the current entity addressed by `key`''' if not isinstance(key, Key): raise ValueError('key must be of type %s' % Key) # lookup the key in the datastore data = self._store.get(key) if data is None: return data # handle the data. if any conversion fails, propagate the exception up. serialRep = SerialRepresentation(data) version = Version(serialRep) return Model.from_version(version)
def merge(self, newVersionOrEntity): '''Merges a new version of an instance with the current one in the store.''' # get the new version new_version = self._cleanVersion(newVersionOrEntity) # get the instance key = new_version.key curr_instance = self.get(key) #THINKME(jbenet): try contains first? # brand new version. just store it. if curr_instance is None: self.put(new_version) return Model.from_version(new_version) # NOTE: semantically, we must merge into the current instance in the repo # so that merge strategies favor the incumbent version. curr_instance.merge(new_version) # store it back self.put(curr_instance) return curr_instance
def next(self): '''Returns an instance of the version represented by the next object.''' if self.iter is None: raise StopIteration # if it returns none, return None as well. None is not necessarily the end. next = self.iter.next() if next is None: return None # if it is a dictionary, assume raw serial representation if isinstance(next, dict): next = serial.SerialRepresentation(next) # if it is a serialRepresentation, turn it into a Version if isinstance(next, serial.SerialRepresentation): next = Version(next) # if it is a Version, turn it into a Model if isinstance(next, Version): next = Model.from_version(next) # return whatever it is we have! return next