def _incr(self, prop, amt=1): """Increment self.prop.""" assert not self._dirty is_base_prop = prop in self._int_props is_data_prop = prop in self._data_int_props or self._int_prop_suffix and prop.endswith(self._int_prop_suffix) db_prop = prop[1:] if is_base_prop else prop assert is_base_prop or is_data_prop with self.get_read_modify_write_lock() as lock: self.update_from_cache(lock) old_val = getattr(self, prop) new_val = old_val + amt self.__setattr__(prop, new_val, make_dirty=False) with TdbTransactionContext(): if is_base_prop: # can just incr a base prop because it must have been set # when the object was created tdb.incr_thing_prop(type_id=self.__class__._type_id, thing_id=self._id, prop=db_prop, amount=amt) elif prop in self.__class__._defaults and self.__class__._defaults[prop] == old_val: # when updating a data prop from the default value assume # the value was never actually set so it's not safe to incr tdb.set_thing_data( type_id=self.__class__._type_id, thing_id=self._id, brand_new_thing=False, **{db_prop: new_val} ) else: tdb.incr_thing_data(type_id=self.__class__._type_id, thing_id=self._id, prop=db_prop, amount=amt) # write to cache within the transaction context so an exception # will cause a transaction rollback self.write_thing_to_cache(lock) self.record_cache_write(event="incr")
def write_props_to_db(self, props, data_props, brand_new_thing): """Write the props to db.""" if data_props: tdb.set_thing_data( type_id=self.__class__._type_id, thing_id=self._id, brand_new_thing=brand_new_thing, **data_props ) if props and not brand_new_thing: # if the thing is brand new its props have just been written by # write_new_thing_to_db tdb.set_thing_props(type_id=self.__class__._type_id, thing_id=self._id, **props)
def _incr(self, prop, amt=1): """Increment self.prop.""" assert not self._dirty is_base_prop = prop in self._int_props is_data_prop = (prop in self._data_int_props or self._int_prop_suffix and prop.endswith(self._int_prop_suffix)) db_prop = prop[1:] if is_base_prop else prop assert is_base_prop or is_data_prop with self.get_read_modify_write_lock() as lock: self.update_from_cache(lock) old_val = getattr(self, prop) new_val = old_val + amt self.__setattr__(prop, new_val, make_dirty=False) if is_base_prop: # can just incr a base prop because it must have been set when # the object was created tdb.incr_thing_prop( type_id=self.__class__._type_id, thing_id=self._id, prop=db_prop, amount=amt, ) elif (prop in self.__class__._defaults and self.__class__._defaults[prop] == old_val): # when updating a data prop from the default value assume the # value was never actually set so it's not safe to incr tdb.set_thing_data( type_id=self.__class__._type_id, thing_id=self._id, brand_new_thing=False, **{db_prop: new_val} ) else: tdb.incr_thing_data( type_id=self.__class__._type_id, thing_id=self._id, prop=db_prop, amount=amt, ) self.write_thing_to_cache(lock) self.record_cache_write(event="incr")
def write_props_to_db(self, props, data_props, brand_new_thing): """Write the props to db.""" if data_props: tdb.set_thing_data( type_id=self.__class__._type_id, thing_id=self._id, brand_new_thing=brand_new_thing, **data_props ) if props and not brand_new_thing: # if the thing is brand new its props have just been written by # write_new_thing_to_db tdb.set_thing_props( type_id=self.__class__._type_id, thing_id=self._id, **props )