This repository has been archived by the owner on Sep 18, 2021. It is now read-only.
/
ColumnFamily.scala
394 lines (350 loc) · 15.4 KB
/
ColumnFamily.scala
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package com.twitter.cassie
import clocks.MicrosecondEpochClock
import codecs.{Codec, Utf8Codec}
import connection.ClientProvider
import org.apache.cassandra.finagle.thrift
import com.twitter.logging.Logger
import java.nio.ByteBuffer
import java.util.Collections.{singleton => singletonSet}
import java.util.{ArrayList, HashMap, Iterator, List, Map, Set}
import org.apache.cassandra.finagle.thrift.Mutation
import scala.collection.JavaConversions._
import com.twitter.util.Future
/**
* A readable, writable column family with batching capabilities. This is a
* lightweight object: it inherits a connection pool from the Keyspace.
*
* TODO: remove (insert/get)As methods in favor of copying the CF to allow for alternate types.
*/
case class ColumnFamily[Key, Name, Value](
keyspace: String,
name: String,
provider: ClientProvider,
defaultKeyCodec: Codec[Key],
defaultNameCodec: Codec[Name],
defaultValueCodec: Codec[Value],
readConsistency: ReadConsistency = ReadConsistency.Quorum,
writeConsistency: WriteConsistency = WriteConsistency.Quorum) {
val log = Logger.get
val clock = MicrosecondEpochClock
import ColumnFamily._
def keysAs[K](codec: Codec[K]): ColumnFamily[K, Name, Value] = copy(defaultKeyCodec = codec)
def namesAs[N](codec: Codec[N]): ColumnFamily[Key, N, Value] = copy(defaultNameCodec = codec)
def valuesAs[V](codec: Codec[V]): ColumnFamily[Key, Name, V] = copy(defaultValueCodec = codec)
def consistency(rc: ReadConsistency) = copy(readConsistency = rc)
def consistency(wc: WriteConsistency) = copy(writeConsistency = wc)
/**
* @Java
* Creates a new Column with.
*/
def newColumn[N, V](n: N, v: V) = Column(n, v)
/**
* Returns the optional value of a given column for a given key as the given
* types.
*/
def getColumnAs[K, N, V](key: K,
columnName: N)
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): Future[Option[Column[N, V]]] = {
getColumnsAs(key, singletonSet(columnName))(keyCodec, nameCodec, valueCodec)
.map { resmap => Option(resmap.get(columnName)) }
}
/**
* Returns the optional value of a given column for a given key as the default
* types.
*/
def getColumn(key: Key,
columnName: Name): Future[Option[Column[Name, Value]]] = {
getColumnAs[Key, Name, Value](key, columnName)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Returns a map of all column names to the columns for a given key as the
* given types. If your rows contain a huge number of columns, this will be
* slow and horrible.
*/
def getRowAs[K, N, V](key: K)
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): Future[Map[N, Column[N, V]]] = {
getRowSliceAs[K, N, V](key, None, None, Int.MaxValue, Order.Normal)(keyCodec, nameCodec, valueCodec)
}
/**
* Returns a map of all column names to the columns for a given key as the
* default types. If your rows contain a huge number of columns, this will be
* slow and horrible.
*/
def getRow(key: Key): Future[Map[Name, Column[Name, Value]]] = {
getRowAs[Key, Name, Value](key)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Returns a slice of all columns of a row as the given types.
*/
def getRowSliceAs[K, N, V](key: K,
startColumnName: Option[N],
endColumnName: Option[N],
count: Int,
order: Order)
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]) = {
val startBytes = startColumnName.map { c => nameCodec.encode(c) }.getOrElse(EMPTY)
val endBytes = endColumnName.map { c => nameCodec.encode(c) }.getOrElse(EMPTY)
val pred = new thrift.SlicePredicate()
pred.setSlice_range(new thrift.SliceRange(startBytes, endBytes, order.reversed, count))
getSlice(key, pred, keyCodec, nameCodec, valueCodec)
}
/**
* Returns a slice of all columns of a row as the default types.
*/
def getRowSlice(key: Key,
startColumnName: Option[Name],
endColumnName: Option[Name],
count: Int,
order: Order): Future[Map[Name, Column[Name, Value]]] = {
getRowSliceAs[Key, Name, Value](key, startColumnName, endColumnName, count, order)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Returns a map of the given column names to the columns for a given key as
* the given types.
*/
def getColumnsAs[K, N, V](key: K,
columnNames: Set[N])
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): Future[Map[N, Column[N, V]]] = {
val pred = new thrift.SlicePredicate()
pred.setColumn_names(encodeSet(columnNames))
getSlice(key, pred, keyCodec, nameCodec, valueCodec)
}
/**
* Returns a map of the given column names to the columns for a given key as
* the default types.
*/
def getColumns(key: Key,
columnNames: Set[Name]): Future[Map[Name, Column[Name, Value]]] = {
getColumnsAs[Key, Name, Value](key, columnNames)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Returns a map of keys to given column for a set of keys as the given types.
*/
def multigetColumnAs[K, N, V](keys: Set[K],
columnName: N)
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): Future[Map[K, Column[N, V]]] = {
multigetColumnsAs[K, N, V](keys, singletonSet(columnName))(keyCodec, nameCodec, valueCodec).map { rows =>
val cols: Map[K, Column[N, V]] = new HashMap(rows.size)
for (rowEntry <- asScalaIterable(rows.entrySet))
if (!rowEntry.getValue.isEmpty)
cols.put(rowEntry.getKey, rowEntry.getValue.get(columnName))
cols
}
}
/**
* Returns a map of keys to given column for a set of keys as the default
* types.
*/
def multigetColumn(keys: Set[Key],
columnName: Name): Future[Map[Key, Column[Name, Value]]] = {
multigetColumnAs[Key, Name, Value](keys, columnName)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Returns a map of keys to a map of column names to the columns for a given
* set of keys and columns as the given types.
*/
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
def multigetColumnsAs[K, N, V](keys: Set[K],
columnNames: Set[N])
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): Future[Map[K, Map[N, Column[N, V]]]] = {
val cp = new thrift.ColumnParent(name)
val pred = new thrift.SlicePredicate()
pred.setColumn_names(encodeSet(columnNames))
log.debug("multiget_slice(%s, %s, %s, %s, %s)", keyspace, keys, cp, pred, readConsistency.level)
val encodedKeys = encodeSet(keys)
provider.map {
_.multiget_slice(encodedKeys, cp, pred, readConsistency.level)
}.map { result =>
// decode result
val rows: Map[K, Map[N, Column[N, V]]] = new HashMap(result.size)
for (rowEntry <- asScalaIterable(result.entrySet)) {
val cols: Map[N, Column[N, V]] = new HashMap(rowEntry.getValue.size)
for (cosc <- asScalaIterable(rowEntry.getValue)) {
val col = Column.convert(nameCodec, valueCodec, cosc)
cols.put(col.name, col)
}
rows.put(keyCodec.decode(rowEntry.getKey), cols)
}
rows
}
}
/**
* Returns a map of keys to a map of column names to the columns for a given
* set of keys and columns as the default types.
*/
def multigetColumns(keys: Set[Key], columnNames: Set[Name]) = {
multigetColumnsAs[Key, Name, Value](keys, columnNames)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Inserts a column.
*/
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
def insert(key: Key, column: Column[Name, Value]) = {
insertAs(key, column)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
def insertAs[K, N, V](key: K, column: Column[N, V])
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]) = {
val cp = new thrift.ColumnParent(name)
val col = Column.convert(nameCodec, valueCodec, clock, column)
log.debug("insert(%s, %s, %s, %s, %d, %s)", keyspace, key, cp, column.value,
col.timestamp, writeConsistency.level)
provider.map {
_.insert(keyCodec.encode(key), cp, col, writeConsistency.level)
}
}
def truncate() = provider.map(_.truncate(name))
/**
* Removes a column from a key.
*/
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
def removeColumn(key: Key, columnName: Name) = {
val cp = new thrift.ColumnPath(name)
val timestamp = clock.timestamp
cp.setColumn(defaultNameCodec.encode(columnName))
log.debug("remove(%s, %s, %s, %d, %s)", keyspace, key, cp, timestamp, writeConsistency.level)
provider.map { _.remove(defaultKeyCodec.encode(key), cp, timestamp, writeConsistency.level) }
}
/**
* Removes a set of columns from a key.
*/
def removeColumns(key: Key, columnNames: Set[Name]): Future[Void] = {
batch()
.removeColumns(key, columnNames)
.execute()
}
/**
* Removes a set of columns from a key.
*/
def removeColumns(key: Key, columnNames: Set[Name], timestamp: Long): Future[Void] = {
batch()
.removeColumns(key, columnNames, timestamp)
.execute()
}
/**
* Removes a key.
*/
def removeRow(key: Key) = {
removeRowWithTimestamp(key, clock.timestamp)
}
/**
* Removes a key with a specific timestamp.
*/
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
def removeRowWithTimestamp(key: Key, timestamp: Long) = {
val cp = new thrift.ColumnPath(name)
log.debug("remove(%s, %s, %s, %d, %s)", keyspace, key, cp, timestamp, writeConsistency.level)
provider.map { _.remove(defaultKeyCodec.encode(key), cp, timestamp, writeConsistency.level) }
}
/**
* @return A Builder that can be used to execute multiple actions in a single
* request.
*/
def batch() = new BatchMutationBuilder(this)
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
private[cassie] def batch(mutations: java.util.Map[ByteBuffer, java.util.Map[String, java.util.List[Mutation]]]) = {
log.debug("batch_mutate(%s, %s, %s", keyspace, mutations, writeConsistency.level)
provider.map { _.batch_mutate(mutations, writeConsistency.level) }
}
/**
* Returns a column iterator which iterates over all columns of all rows in
* the column family with the given batch size as the given types.
*/
def rowIterateeAs[K, N, V](batchSize: Int)
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): ColumnIteratee[K, N, V] = {
val pred = new thrift.SlicePredicate
pred.setSlice_range(new thrift.SliceRange(EMPTY, EMPTY, false, Int.MaxValue))
new ColumnIteratee(this, EMPTY, EMPTY, batchSize, pred, keyCodec, nameCodec, valueCodec)
}
/**
* Returns a column iterator which iterates over all columns of all rows in
* the column family with the given batch size as the default types.
*/
def rowIteratee(batchSize: Int): ColumnIteratee[Key, Name, Value] = {
rowIterateeAs[Key, Name, Value](batchSize)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
/**
* Returns a column iterator which iterates over the given column of all rows
* in the column family with the given batch size as the given types.
*/
def columnIterateeAs[K, N, V](batchSize: Int, columnName: N)
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): ColumnIteratee[K, N, V] =
columnsIterateeAs(batchSize, singletonSet(columnName))(keyCodec, nameCodec, valueCodec)
/**
* Returns a column iterator which iterates over the given column of all rows
* in the column family with the given batch size as the default types.
*/
def columnIteratee(batchSize: Int,
columnName: Name): ColumnIteratee[Key, Name, Value] =
columnIterateeAs[Key, Name, Value](batchSize, columnName)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
/**
* Returns a column iterator which iterates over the given columns of all rows
* in the column family with the given batch size as the given types.
*/
def columnsIterateeAs[K, N, V](batchSize: Int,
columnNames: Set[N])
(implicit keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): ColumnIteratee[K, N, V] = {
val pred = new thrift.SlicePredicate
pred.setColumn_names(encodeSet(columnNames))
new ColumnIteratee(this, EMPTY, EMPTY, batchSize, pred, keyCodec, nameCodec, valueCodec)
}
/**
* Returns a column iterator which iterates over the given columns of all rows
* in the column family with the given batch size as the default types.
*/
def columnsIteratee(batchSize: Int,
columnNames: Set[Name]): ColumnIteratee[Key, Name, Value] = {
columnsIterateeAs[Key, Name, Value](batchSize, columnNames)(defaultKeyCodec, defaultNameCodec, defaultValueCodec)
}
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
private def getSlice[K, N, V](key: K,
pred: thrift.SlicePredicate,
keyCodec: Codec[K], nameCodec: Codec[N], valueCodec: Codec[V]): Future[Map[N,Column[N,V]]] = {
val cp = new thrift.ColumnParent(name)
log.debug("get_slice(%s, %s, %s, %s, %s)", keyspace, key, cp, pred, readConsistency.level)
provider.map { _.get_slice(keyCodec.encode(key), cp, pred, readConsistency.level) }
.map { result =>
val cols: Map[N,Column[N,V]] = new HashMap(result.size)
for (cosc <- result.iterator) {
val col = Column.convert(nameCodec, valueCodec, cosc)
cols.put(col.name, col)
}
cols
}
}
@throws(classOf[thrift.TimedOutException])
@throws(classOf[thrift.UnavailableException])
@throws(classOf[thrift.InvalidRequestException])
private[cassie] def getRangeSlice(startKey: ByteBuffer,
endKey: ByteBuffer,
count: Int,
predicate: thrift.SlicePredicate) = {
val cp = new thrift.ColumnParent(name)
val range = new thrift.KeyRange(count)
range.setStart_key(startKey)
range.setEnd_key(endKey)
log.debug("get_range_slices(%s, %s, %s, %s, %s)", keyspace, cp, predicate, range, readConsistency.level)
provider.map { _.get_range_slices(cp, predicate, range, readConsistency.level) }
}
def encodeSet[V](values: Set[V])(implicit codec: Codec[V]): List[ByteBuffer] = {
val output = new ArrayList[ByteBuffer](values.size)
for (value <- asScalaIterable(values))
output.add(codec.encode(value))
output
}
}
private[cassie] object ColumnFamily
{
val EMPTY = ByteBuffer.allocate(0)
}