This repository has been archived by the owner on Aug 4, 2020. It is now read-only.
/
columnfamilymap.py
330 lines (288 loc) · 12.8 KB
/
columnfamilymap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from pycassa.types import Column
from cassandra.ttypes import IndexExpression
__all__ = ['ColumnFamilyMap']
def create_instance(cls, **kwargs):
instance = cls()
instance.__dict__.update(kwargs)
return instance
class ColumnFamilyMap(object):
def __init__(self, cls, column_family, columns=None, raw_columns=False):
"""
Construct a ObjectFamily
Parameters
----------
cls : class
Instances of cls are generated on get*() requests
column_family: ColumnFamily
The ColumnFamily to tie with cls
raw_columns: boolean
Whether all columns should be fetched into the raw_columns field in
requests
"""
self.cls = cls
self.column_family = column_family
self.raw_columns = raw_columns
self.dict_class = self.column_family.dict_class
self.columns = self.dict_class()
for name, column in self.cls.__dict__.iteritems():
if not isinstance(column, Column):
continue
self.columns[name] = column
def combine_columns(self, columns):
combined_columns = self.dict_class()
if self.raw_columns:
combined_columns['raw_columns'] = self.dict_class()
for column, type in self.columns.iteritems():
combined_columns[column] = type.default
for column, value in columns.iteritems():
col_cls = self.columns.get(column, None)
if col_cls is not None:
combined_columns[column] = col_cls.unpack(value)
if self.raw_columns:
combined_columns['raw_columns'][column] = value
return combined_columns
def get(self, key, *args, **kwargs):
"""
Fetch a key from a Cassandra server
Parameters
----------
key : str
The key to fetch
columns : [str]
Limit the columns or super_columns fetched to the specified list
column_start : str
Only fetch when a column or super_column is >= column_start
column_finish : str
Only fetch when a column or super_column is <= column_finish
column_reversed : bool
Fetch the columns or super_columns in reverse order. This will do
nothing unless you passed a dict_class to the constructor.
column_count : int
Limit the number of columns or super_columns fetched per key
super_column : str
Fetch only this super_column
read_consistency_level : ConsistencyLevel
Affects the guaranteed replication factor before returning from
any read operation
Returns
-------
Class instance
"""
if 'columns' not in kwargs and not self.column_family.super and not self.raw_columns:
kwargs['columns'] = self.columns.keys()
columns = self.column_family.get(key, *args, **kwargs)
if self.column_family.super:
if 'super_column' not in kwargs:
vals = self.dict_class()
for super_column, subcols in columns.iteritems():
combined = self.combine_columns(subcols)
vals[super_column] = create_instance(self.cls, key=key, super_column=super_column, **combined)
return vals
combined = self.combine_columns(columns)
return create_instance(self.cls, key=key, super_column=kwargs['super_column'], **combined)
combined = self.combine_columns(columns)
return create_instance(self.cls, key=key, **combined)
def get_indexed_slices(self, instance=None, *args, **kwargs):
"""
Fetches a list of KeySlices from a Cassandra server based on an index clause
Parameters
----------
index_clause : IndexClause
Limits the keys that are returned based on expressions that compare
the value of a column to a given value. At least one of the
expressions in the IndexClause must be on an indexed column.
See index_clause.create_index_clause() and create_index_expression().
columns : [str]
Limit the columns or super_columns fetched to the specified list
column_start : str
Only fetch when a column or super_column is >= column_start
column_finish : str
Only fetch when a column or super_column is <= column_finish
column_reversed : bool
Fetch the columns or super_columns in reverse order. This will do
nothing unless you passed a dict_class to the constructor.
column_count : int
Limit the number of columns or super_columns fetched per key
include_timestamp : bool
If true, return a (value, timestamp) tuple for each column
super_column : str
Return columns only in this super_column
read_consistency_level : ConsistencyLevel
Affects the guaranteed replication factor before returning from
any read operation
Returns
-------
Class instance
"""
if 'columns' not in kwargs and not self.column_family.super and not self.raw_columns:
kwargs['columns'] = self.columns.keys()
# Autopack the index clause's values
if instance is not None:
new_exprs = []
for expr in kwargs['index_clause'].expressions:
new_expr = IndexExpression(expr.column_name, expr.op,
value=self.columns[expr.column_name].pack(instance.__dict__[expr.column_name]))
new_exprs.append(new_expr)
kwargs['index_clause'].expressions = new_exprs
keyslice_map = self.column_family.get_indexed_slices(*args, **kwargs)
ret = self.dict_class()
for key, columns in keyslice_map.iteritems():
if self.column_family.super:
if 'super_column' not in kwargs:
vals = self.dict_class()
for super_column, subcols in columns.iteritems():
combined = self.combine_columns(subcols)
vals[super_column] = create_instance(self.cls, key=key, super_column=super_column, **combined)
ret[key] = vals
else:
combined = self.combine_columns(columns)
ret[key] = create_instance(self.cls, key=key, super_column=kwargs['super_column'], **combined)
else:
combined = self.combine_columns(columns)
ret[key] = create_instance(self.cls, key=key, **combined)
return ret
def multiget(self, *args, **kwargs):
"""
Fetch multiple key from a Cassandra server
Parameters
----------
keys : [str]
A list of keys to fetch
columns : [str]
Limit the columns or super_columns fetched to the specified list
column_start : str
Only fetch when a column or super_column is >= column_start
column_finish : str
Only fetch when a column or super_column is <= column_finish
column_reversed : bool
Fetch the columns or super_columns in reverse order. This will do
nothing unless you passed a dict_class to the constructor.
column_count : int
Limit the number of columns or super_columns fetched per key
super_column : str
Fetch only this super_column
read_consistency_level : ConsistencyLevel
Affects the guaranteed replication factor before returning from
any read operation
Returns
-------
{'key': Class instance}
"""
if 'columns' not in kwargs and not self.column_family.super and not self.raw_columns:
kwargs['columns'] = self.columns.keys()
kcmap = self.column_family.multiget(*args, **kwargs)
ret = self.dict_class()
for key, columns in kcmap.iteritems():
if self.column_family.super:
if 'super_column' not in kwargs:
vals = self.dict_class()
for super_column, subcols in columns.iteritems():
combined = self.combine_columns(subcols)
vals[super_column] = create_instance(self.cls, key=key, super_column=super_column, **combined)
ret[key] = vals
else:
combined = self.combine_columns(columns)
ret[key] = create_instance(self.cls, key=key, super_column=kwargs['super_column'], **combined)
else:
combined = self.combine_columns(columns)
ret[key] = create_instance(self.cls, key=key, **combined)
return ret
def get_count(self, *args, **kwargs):
"""
Count the number of columns for a key
Parameters
----------
key : str
The key with which to count columns
Returns
-------
int Count of columns
"""
return self.column_family.get_count(*args, **kwargs)
def get_range(self, *args, **kwargs):
"""
Get an iterator over keys in a specified range
Parameters
----------
start : str
Start from this key (inclusive)
finish : str
End at this key (inclusive)
columns : [str]
Limit the columns or super_columns fetched to the specified list
column_start : str
Only fetch when a column or super_column is >= column_start
column_finish : str
Only fetch when a column or super_column is <= column_finish
column_reversed : bool
Fetch the columns or super_columns in reverse order. This will do
nothing unless you passed a dict_class to the constructor.
column_count : int
Limit the number of columns or super_columns fetched per key
row_count : int
Limit the number of rows fetched
super_column : str
Fetch only this super_column
read_consistency_level : ConsistencyLevel
Affects the guaranteed replication factor before returning from
any read operation
Returns
-------
iterator over Class instance
"""
if 'columns' not in kwargs and not self.column_family.super and not self.raw_columns:
kwargs['columns'] = self.columns.keys()
for key, columns in self.column_family.get_range(*args, **kwargs):
if self.column_family.super:
if 'super_column' not in kwargs:
vals = self.dict_class()
for super_column, subcols in columns.iteritems():
combined = self.combine_columns(subcols)
vals[super_column] = create_instance(self.cls, key=key, super_column=super_column, **combined)
yield vals
else:
combined = self.combine_columns(columns)
yield create_instance(self.cls, key=key, super_column=kwargs['super_column'], **combined)
else:
combined = self.combine_columns(columns)
yield create_instance(self.cls, key=key, **combined)
def insert(self, instance, columns=None):
"""
Insert or update columns for a key
Parameters
----------
instance : Class instance
The key to insert or update the columns at
columns : ['column']
Limit the columns inserted to this list
Returns
-------
int timestamp
"""
insert_dict = {}
if columns is None:
columns = self.columns.keys()
for column in columns:
if instance.__dict__.has_key(column) and instance.__dict__[column] is not None:
insert_dict[column] = self.columns[column].pack(instance.__dict__[column])
if self.column_family.super:
insert_dict = {instance.super_column: insert_dict}
return self.column_family.insert(instance.key, insert_dict)
def remove(self, instance, column=None):
"""
Remove this instance
Parameters
----------
instance : Class instance
Remove the instance where the key is instance.key
column : str
If set, remove only this Column. Doesn't do anything for SuperColumns
Returns
-------
int timestamp
"""
# Hmm, should we only remove the columns specified on construction?
# It's slower, so we'll leave it out.
if self.column_family.super:
return self.column_family.remove(instance.key, column=instance.super_column)
return self.column_family.remove(instance.key, column)