Example #1
0
    def __getitem__(self, key):
        if isinstance(key, slice):
            connection = get_pool(self.key.pool_name)
            # @TODO: slice.step is completly ignored.
            response = connection.lrange(str(self.key), key.start, key.stop)
            return [self.record_class().load(record_key)
                    for record_key in response]

        # Else return the one at the spot.
        record_key = get_pool(self.key.pool_name).lindex(str(self.key), key)
        return self.record_class().load(record_key)
Example #2
0
 def load_by_index(self, field, value):
     """Load the Record by the unqiue index. field must be contained in
     self._indices and is the value of the field. Constructs a key throught
     self.make_key()"""
     self._clean()
     key = self.make_index_key(field)
     return self.load(get_pool(key.pool_name).hget(str(key), value))
Example #3
0
    def __getitem__(self, key):
        if isinstance(key, slice):
            connection = get_pool(self.key.pool_name)
            response = connection.zrange(
                str(self.key),
                key.start,
                key.stop,
                self.reverse)
            return [self.record_class().load(record_key)
                    for record_key in response]

        record_key = get_pool(self.key.pool_name).zrange(
            str(self.key),
            key,
            key,
            self.reverse)
        return self.record_class().load(record_key[0])
Example #4
0
 def __iter__(self):
     connection_pool = get_pool(self.key.pool_name)
     for x in xrange(0, len(self)):
         record_key = connection_pool.zrange(
             str(self.key),
             x,
             x,
             self.reverse)
         yield self.record_class().load(record_key[0])
Example #5
0
    def load(self, key):
        """Load the Record on its key. key can be either an instance of Key or a
        string. In the latter case, it will be sent to Record.make_key"""
        if not isinstance(key, Key):
            key = self.make_key(key)

        self._clean()

        pool_name = key.pool_name or self._pool_name
        self._original = get_pool(pool_name).hgetall(str(key))

        self.revert()
        self.key = key

        return self
Example #6
0
    def _save_internal(self, key, changes):
        """Internal save method."""
        if not key:
            return

        pool_name = key.pool_name or self._pool_name
        connection_pool = get_pool(pool_name)

        # Delete items
        if changes['deleted']:
            deleted_fields = []
            for field, old_value in changes['deleted']:
                deleted_fields.append(field)
                # Delete the record from the unique indices.
                if field in self._indices:
                    unique_field_key = self.make_index_key(field, key)
                    connection_pool.hdel(
                        str(unique_field_key),
                        old_value)

            # Remove the deleted field from hash
            deleted_fields = [x[0] for x in changes['deleted']]
            connection_pool.hdel(str(key), *deleted_fields)

        self._deleted.clear()

        # Update items
        if changes['changed']:
            # @TODO: Use Pipeline to protect this.
            for field, value, original_value in changes['changed']:
                connection_pool.hset(str(key), field, value)

                # Update the unique indexes
                if field in self._indices:
                    unique_field_key = self.make_index_key(field, key)

                    # Delete the old index.
                    if original_value:
                        connection_pool.hdel(
                            str(unique_field_key),
                            original_value)

                    # Add the new index.
                    connection_pool.hset(
                        str(unique_field_key),
                        value,
                        key.key)
Example #7
0
 def remove(self):
     """Remove this record from Redis."""
     pool_name = self.key.pool_name
     try:
         try:
             # Save mirrors
             for mirror in self.get_mirrors():
                 mirror.key = mirror.mirror_key(self)
                 mirror.remove()
         finally:
             # Update viewes
             for view in self.get_views():
                 view.record_class = self.__class__
                 view.remove(self)
     finally:
         pool = get_pool(pool_name)
         pool.delete(str(self.key))
         for index in self._indices:
             unqiue_field_key = self.make_index_key(index, self.key)
             pool.hdel(unqiue_field_key, self[index])
         self._clean()
     return self
Example #8
0
 def remove(self, record):
     """Remove the record from the set"""
     get_pool(self.key.pool_name).zrem(str(self.key), record.key.key)
Example #9
0
 def append(self, record, new_record):
     """Add the Record to the View"""
     score = self.score(record)
     get_pool(self.key.pool_name).zadd(str(self.key), score, record.key.key)
Example #10
0
 def append(self, record, new_record):
     """Add the Record to the View"""
     # Save the records, non-prefix version.
     if new_record:
         get_pool(self.key.pool_name).rpush(str(self.key), record.key.key)
Example #11
0
 def append(self, record, new_record):
     if new_record:
         get_pool(self.key.pool_name).lpush(str(self.key), record.key.key)
Example #12
0
 def __len__(self):
     return get_pool(self.key.pool_name).llen(str(self.key))
Example #13
0
 def remove(self, record):
     """Remove the Record from the View"""
     get_pool(self.key.pool_name).lrem(str(self.key), 0, record.key.key)