Exemplo n.º 1
0
    def _incr(self, prop, amt = 1):
        if self._dirty:
            raise ValueError, "cannot incr dirty thing"

        #make sure we're incr'ing an _int_prop or _data_int_prop.
        if prop not in self._int_props:
            if (prop in self._data_int_props or
                self._int_prop_suffix and prop.endswith(self._int_prop_suffix)):
                #if we're incr'ing a data_prop, make sure we're loaded
                if not self._loaded:
                    self._load()
            else:
                msg = ("cannot incr non int prop %r on %r -- it's not in %r or %r" %
                       (prop, self, self._int_props, self._data_int_props))
                raise ValueError, msg

        with g.make_lock("thing_commit", 'commit_' + self._fullname):
            self._sync_latest()
            old_val = getattr(self, prop)
            if self._defaults.has_key(prop) and self._defaults[prop] == old_val:
                #potential race condition if the same property gets incr'd
                #from default at the same time
                setattr(self, prop, old_val + amt)
                self._commit(prop)
            else:
                self.__setattr__(prop, old_val + amt, False)
                #db
                if prop.startswith('_'):
                    tdb.incr_thing_prop(self._type_id, self._id, prop[1:], amt)
                else:
                    self._incr_data(self._type_id, self._id, prop, amt)

            self._cache_myself()
Exemplo n.º 2
0
    def _incr(self, prop, amt=1):
        if self._dirty:
            raise ValueError, "cannot incr dirty thing"

        #make sure we're incr'ing an _int_prop or _data_int_prop.
        if prop not in self._int_props:
            if (prop in self._data_int_props or self._int_prop_suffix
                    and prop.endswith(self._int_prop_suffix)):
                #if we're incr'ing a data_prop, make sure we're loaded
                if not self._loaded:
                    self._load()
            else:
                msg = (
                    "cannot incr non int prop %r on %r -- it's not in %r or %r"
                    % (prop, self, self._int_props, self._data_int_props))
                raise ValueError, msg

        with g.make_lock("thing_commit", 'commit_' + self._fullname):
            self._sync_latest()
            old_val = getattr(self, prop)
            if self._defaults.has_key(
                    prop) and self._defaults[prop] == old_val:
                #potential race condition if the same property gets incr'd
                #from default at the same time
                setattr(self, prop, old_val + amt)
                self._commit(prop)
            else:
                self.__setattr__(prop, old_val + amt, False)
                #db
                if prop.startswith('_'):
                    tdb.incr_thing_prop(self._type_id, self._id, prop[1:], amt)
                else:
                    self._incr_data(self._type_id, self._id, prop, amt)

            self._cache_myself()
Exemplo n.º 3
0
    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")
Exemplo n.º 4
0
    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")