Exemplo n.º 1
0
    def reindex_one(self, obj, attributes=None):
        """
        Reindex one object which is already in the database

        :param obj: Object to add to the database or its uid
        :type obj: zerodb.models.Model, int
        :param attributes: Attributes of obj to be reindex
        :type attributes: tuple, list
        """

        if isinstance(obj, six.integer_types):
            uid = obj
            obj = self._objects[uid]
        elif isinstance(obj, self._model):
            if hasattr(obj, "_p_uid"):
                uid = obj._p_uid
            else:
                raise ModelException("Object %s is not indexed" % obj)
        else:
            raise TypeError("Wrong type of argument passed: obj must be integer or model instance")

        if attributes is None:
            self._catalog.reindex_doc(uid, obj)
        elif isinstance(attributes, (tuple, list)):
            for attr in attributes:
                if attr in self._catalog:
                    self._catalog[attr].reindex_doc(uid, obj)
        else:
            raise TypeError("Wrong type of argument passed: attributes must be tuple or list")
Exemplo n.º 2
0
    def __getitem__(self, model):
        """
        DbModels (which we query) are accessed by using db as a dictionary

        :param model: Subclass of zerodb.models.Model to return or create db entry for
        :rtype: zerodb.db.DbModel
        """
        # TODO implement list of keys, writing to arbitrary (new) dbmodel (which is not defined)
        if not issubclass(model, models.Model):
            raise ModelException("Class <%s> is not a Model" % model.__name__)
        if model not in self._models:
            self._models[model] = DbModel(self, model)
        return self._models[model]
Exemplo n.º 3
0
    def __getitem__(self, uids):
        """
        DbModels (which we query) are accessed by using db as a dictionary

        :param int uids: object's uid or list of them
        :return: Persistent object(s)
        """
        if isinstance(uids, (int, long)):
            return self._objects[uids]
        elif isinstance(uids, (tuple, list, set)):
            objects = [self._objects[uid] for uid in uids]
            self._db._storage.loadBulk([o._p_oid for o in objects])
            return objects
        else:
            raise ModelException("Integer or list of integers is expected")
Exemplo n.º 4
0
    def remove(self, obj):
        """
        Remove existing object from the database + unindex it

        :param zerodb.models.Model obj: Object to add to the database
        """
        if isinstance(obj, models.Model):
            self[obj.__class__].remove(obj)
            return 1
        elif hasattr(obj, "__iter__"):
            ctr = 0
            for o in obj:
                ctr += 1
                self[o.__class__].remove(o)
            return ctr
        else:
            raise ModelException("Class <%s> is not a Model or iterable" % obj.__class__.__name__)
Exemplo n.º 5
0
    def __getitem__(self, uids):
        """
        DbModels (which we query) are accessed by using db as a dictionary

        :param int uids: object's uid or list of them
        :return: Persistent object(s)
        """
        if isinstance(uids, six.integer_types):
            obj = self._objects[uids]
            if not hasattr(obj, "_p_uid"):
                obj._p_uid = uids
            return obj

        elif isinstance(uids, (tuple, list, set)):
            objects = [self._objects[uid] for uid in uids]
            self._db._storage.loadBulk([o._p_oid for o in objects])
            for o, uid in izip(objects, uids):
                if not hasattr(o, "_p_uid"):
                    o._p_uid = uid
            return objects

        else:
            raise ModelException("Integer or list of integers is expected")