Ejemplo n.º 1
0
def create_SlicePredicate(columns, column_start, column_finish,
                          column_reversed, column_count):
    if columns is not None:
        return SlicePredicate(column_names=columns)
    sr = SliceRange(start=column_start,
                    finish=column_finish,
                    reversed=column_reversed,
                    count=column_count)
    return SlicePredicate(slice_range=sr)
Ejemplo n.º 2
0
    def _cols(self, start_col=None, end_col=None):
        """Yield columns in the view."""
        client = self._get_cas()
        assert isinstance(client, Client), \
            "Incorrect client instance: %s" % client.__class__
        last_col = start_col or self.start_col or ""
        end_col = end_col or ""
        chunk_size = self.chunk_size
        passes = 0
        while True:
            # When you give Cassandra a start key, it's included in the
            # results. We want it in the first pass, but subsequent iterations
            # need to the count adjusted and the first record dropped.
            fudge = 1 if self.exclusive else int(passes > 0)

            cols = client.get_slice(
                self.key.key, self.key,
                SlicePredicate(slice_range=SliceRange(
                        last_col, end_col, self.reversed, chunk_size + fudge)),
                self.consistency)

            if len(cols) == 0:
                raise StopIteration()

            for col in unpack(cols[fudge:]):
                yield col
                last_col = col.name

            passes += 1

            if len(cols) < self.chunk_size:
                raise StopIteration()
Ejemplo n.º 3
0
def sparse_get(key, columns):
    """Return an iterator over a specific set of columns."""

    client = get_pool(key.keyspace)
    res = client.get_slice(key.key, key, SlicePredicate(column_names=columns),
                           ConsistencyLevel.ONE)

    return unpack(res)
Ejemplo n.º 4
0
def slice_iterator(key, consistency, **predicate_args):
    """Return an iterator over a row."""

    predicate = SlicePredicate()
    if "columns" in predicate_args:
        predicate.column_names = predicate_args["columns"]
    else:
        args = {"start": "", "finish": "", "count": 100000, "reversed": False}
        args.update(predicate_args)
        predicate.slice_range = SliceRange(**args)

    consistency = consistency or ConsistencyLevel.ONE

    client = get_pool(key.keyspace)
    res = client.get_slice(key.key, key, predicate, consistency)

    if not res:
        raise exc.ErrorNoSuchRecord("No record matching key %s" % key)

    return unpack(res)
Ejemplo n.º 5
0
def slice_iterator(key, consistency, **predicate_args):
    """Return an iterator over a row."""

    predicate = SlicePredicate()
    if 'columns' in predicate_args:
        predicate.column_names = predicate_args['columns']
    else:
        args = {'start': "", 'finish': "", 'count': 100000, 'reversed': False}
        args.update(predicate_args)
        predicate.slice_range = SliceRange(**args)

    consistency = consistency or ConsistencyLevel.ONE

    client = get_pool(key.keyspace)
    res = client.get_slice(key.key, key, predicate, consistency)

    if not res:
        raise exc.ErrorNoSuchRecord("No record matching key %s" % key)

    return unpack(res)
Ejemplo n.º 6
0
def sparse_multiget(keys, columns):
    """Return an iterator over a specific set of columns."""

    first_key = iter(keys).next()
    client = get_pool(first_key.keyspace)
    row_keys = [key.key for key in keys]
    res = client.multiget_slice(row_keys, first_key,
                                SlicePredicate(column_names=columns),
                                ConsistencyLevel.ONE)

    out = {}
    for (row_key, cols) in res.iteritems():
        out[row_key] = [corsc.column or corsc.super_column for corsc in cols]
    return out
Ejemplo n.º 7
0
    def remove(self,
               key,
               columns=None,
               super_column=None,
               write_consistency_level=None):
        """
        Remove a specified key or columns

        Parameters
        ----------
        key : str
            The key to remove. If columns is not set, remove all columns
        columns : list
            Delete the columns or super_columns in this list
        super_column : str
            Delete the columns from this super_column
        write_consistency_level : ConsistencyLevel
            Affects the guaranteed replication factor before returning from
            any write operation

        Returns
        -------
        int timestamp
        """
        timestamp = self.timestamp()
        if columns is not None:
            # Deletion doesn't support SliceRange predicates as of Cassandra 0.6.0,
            # so we can't add column_start, column_finish, etc... yet
            sp = SlicePredicate(column_names=columns)
            deletion = Deletion(timestamp=timestamp,
                                super_column=super_column,
                                predicate=sp)
            mutation = Mutation(deletion=deletion)
            self.client.batch_mutate(self.keyspace,
                                     {key: {
                                         self.column_family: [mutation]
                                     }}, self._wcl(write_consistency_level))
        else:
            cp = ColumnPath(column_family=self.column_family,
                            super_column=super_column)
            self.client.remove(self.keyspace, key, cp, timestamp,
                               self._wcl(write_consistency_level))
        return timestamp
Ejemplo n.º 8
0
def multigetterator(keys, consistency, **range_args):
    """Return a dictionary of data from Cassandra.

    This fetches data with the minumum number of network requests. It
    DOES NOT preserve order.

    If you depend on ordering, use list_multigetterator. This may
    require more requests.
    """
    kwargs = {'start': "", 'finish': "", 'count': 100000, 'reversed': False}
    kwargs.update(range_args)
    predicate = SlicePredicate(slice_range=SliceRange(**kwargs))
    consistency = consistency or ConsistencyLevel.ONE

    out = {}
    for (keyspace, ks_keys) in groupsort(keys, GET_KEYSPACE):
        client = get_pool(keyspace)
        out[keyspace] = {}
        for (colfam, cf_keys) in groupsort(ks_keys, GET_COLFAM):

            if colfam not in out[keyspace]:
                out[keyspace][colfam] = defaultdict(dict)

            for (supercol, sc_keys) in groupsort(cf_keys, GET_SUPERCOL):
                records = client.multiget_slice(map(GET_KEY, sc_keys),
                                                ColumnParent(colfam, supercol),
                                                predicate, consistency)

                for (row_key, cols) in records.iteritems():
                    cols = unpack(cols)
                    if supercol is None:
                        out[keyspace][colfam][row_key] = cols
                    else:
                        out[keyspace][colfam][row_key][supercol] = cols

    return out
Ejemplo n.º 9
0
 def __len__(self):
     """Return the number of records in this view."""
     return self._get_cas().get_count(
         self.key.key, self.key,
         SlicePredicate(slice_range=SliceRange("", "", False, 2147483647)),
         self.consistency)