Example #1
0
    def get(self, name, detailed=False):
        fields = ['w', 'u', 'o']
        values = self._db.hmget(_key(name), fields)

        if not all(values):
            raise errors.ShardDoesNotExist(name)

        return _normalize(name, values[0], values[1], values[2], detailed)
Example #2
0
    def get(self, name, detailed=False):
        stmt = sa.sql.select([tables.Shards
                              ]).where(tables.Shards.c.name == name)

        shard = self._conn.execute(stmt).fetchone()
        if shard is None:
            raise errors.ShardDoesNotExist(name)

        return _normalize(shard, detailed)
Example #3
0
 def update(self, name, **kwargs):
     names = ('uri', 'weight', 'options')
     fields = common_utils.fields(kwargs,
                                  names,
                                  pred=lambda x: x is not None,
                                  key_transform=lambda x: x[0])
     assert fields, '`weight`, `uri`, or `options` not found in kwargs'
     res = self._col.update({'n': name}, {'$set': fields}, upsert=False)
     if not res['updatedExisting']:
         raise errors.ShardDoesNotExist(name)
Example #4
0
    def get(self, name, detailed=False):
        if not self.exists(name):
            raise errors.ShardDoesNotExist(name)

        shard_info = self._client.hgetall(name)
        shard_info['n'] = name

        if not detailed:
            del shard_info['o']
        return _normalize(shard_info, detailed)
Example #5
0
    def update(self, name, **kwargs):
        client = self._client
        if not self.exists(name):
            raise errors.ShardDoesNotExist(name)

        shard_info = client.hgetall(name)

        # Override the parameters from the client.
        shard_new_info = {
            'u': kwargs['uri'] or shard_info['u'],
            'w': kwargs['weight'] or shard_info['w'],
            'o': kwargs['options'] or shard_info['o']
        }

        client.hmset(name, shard_new_info)
Example #6
0
    def update(self, name, **kwargs):
        # NOTE(cpp-cabrera): by pruning None-valued kwargs, we avoid
        # overwriting the existing options field with None, since that
        # one can be null.
        names = ('uri', 'weight', 'options')
        fields = common_utils.fields(kwargs,
                                     names,
                                     pred=lambda x: x is not None)

        assert fields, '`weight`, `uri`, or `options` not found in kwargs'

        if 'options' in fields:
            fields['options'] = json.dumps(fields['options'])

        stmt = sa.sql.update(
            tables.Shards).where(tables.Shards.c.name == name).values(**fields)

        res = self._conn.execute(stmt)
        if res.rowcount == 0:
            raise errors.ShardDoesNotExist(name)
Example #7
0
    def get(self, name, detailed=False):
        res = self._col.find_one({'n': name}, _field_spec(detailed))
        if not res:
            raise errors.ShardDoesNotExist(name)

        return _normalize(res, detailed)