This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
ColumnFamily.java
559 lines (495 loc) · 16.7 KB
/
ColumnFamily.java
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.db;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.cassandra.config.DatabaseDescriptor;
import org.apache.cassandra.utils.FBUtilities;
/**
* Author : Avinash Lakshman ( alakshman@facebook.com) & Prashant Malik ( pmalik@facebook.com )
*/
public final class ColumnFamily
{
/* The column serializer for this Column Family. Create based on config. */
private static ICompactSerializer2<ColumnFamily> serializer_;
public static final short utfPrefix_ = 2;
private static Logger logger_ = Logger.getLogger( ColumnFamily.class );
private static Map<String, String> columnTypes_ = new HashMap<String, String>();
private static Map<String, String> indexTypes_ = new HashMap<String, String>();
private String type_;
static
{
serializer_ = new ColumnFamilySerializer();
/* TODO: These are the various column types. Hard coded for now. */
columnTypes_.put("Standard", "Standard");
columnTypes_.put("Super", "Super");
indexTypes_.put("Name", "Name");
indexTypes_.put("Time", "Time");
}
public static ICompactSerializer2<ColumnFamily> serializer()
{
return serializer_;
}
/*
* This method returns the serializer whose methods are
* preprocessed by a dynamic proxy.
*/
public static ICompactSerializer2<ColumnFamily> serializerWithIndexes()
{
return (ICompactSerializer2<ColumnFamily>)Proxy.newProxyInstance( ColumnFamily.class.getClassLoader(), new Class[]{ICompactSerializer2.class}, new CompactSerializerInvocationHandler<ColumnFamily>(serializer_) );
}
public static String getColumnType(String key)
{
if ( key == null )
return columnTypes_.get("Standard");
return columnTypes_.get(key);
}
public static String getColumnSortProperty(String columnIndexProperty)
{
if ( columnIndexProperty == null )
return indexTypes_.get("Time");
return indexTypes_.get(columnIndexProperty);
}
private transient AbstractColumnFactory columnFactory_;
private String name_;
private transient ICompactSerializer2<IColumn> columnSerializer_;
private long markedForDeleteAt = Long.MIN_VALUE;
private AtomicInteger size_ = new AtomicInteger(0);
private EfficientBidiMap columns_;
private Comparator<IColumn> columnComparator_;
private Comparator<IColumn> getColumnComparator(String cfName, String columnType)
{
if(columnComparator_ == null)
{
/*
* if this columnfamily has supercolumns or there is an index on the column name,
* then sort by name
*/
if("Super".equals(columnType) || DatabaseDescriptor.isNameSortingEnabled(cfName))
{
columnComparator_ = ColumnComparatorFactory.getComparator(ColumnComparatorFactory.ComparatorType.NAME);
}
/* if this columnfamily has simple columns, and no index on name sort by timestamp */
else
{
columnComparator_ = ColumnComparatorFactory.getComparator(ColumnComparatorFactory.ComparatorType.TIMESTAMP);
}
}
return columnComparator_;
}
public ColumnFamily(String cfName, String columnType)
{
name_ = cfName;
type_ = columnType;
createColumnFactoryAndColumnSerializer(columnType);
}
void createColumnFactoryAndColumnSerializer(String columnType)
{
if ( columnFactory_ == null )
{
columnFactory_ = AbstractColumnFactory.getColumnFactory(columnType);
columnSerializer_ = columnFactory_.createColumnSerializer();
if(columns_ == null)
columns_ = new EfficientBidiMap(getColumnComparator(name_, columnType));
}
}
void createColumnFactoryAndColumnSerializer()
{
String columnType = DatabaseDescriptor.getColumnFamilyType(name_);
if ( columnType == null )
{
List<String> tables = DatabaseDescriptor.getTables();
if ( tables.size() > 0 )
{
String table = tables.get(0);
columnType = Table.open(table).getColumnFamilyType(name_);
}
}
createColumnFactoryAndColumnSerializer(columnType);
}
ColumnFamily cloneMe()
{
ColumnFamily cf = new ColumnFamily(name_, type_);
cf.markedForDeleteAt = markedForDeleteAt;
cf.columns_ = columns_.cloneMe();
return cf;
}
public String name()
{
return name_;
}
/*
* We need to go through each column
* in the column family and resolve it before adding
*/
void addColumns(ColumnFamily cf)
{
for (IColumn column : cf.getAllColumns())
{
addColumn(column);
}
}
public ICompactSerializer2<IColumn> getColumnSerializer()
{
createColumnFactoryAndColumnSerializer();
return columnSerializer_;
}
public void addColumn(String name)
{
addColumn(columnFactory_.createColumn(name));
}
int getColumnCount()
{
int count = 0;
Map<String, IColumn> columns = columns_.getColumns();
if( columns != null )
{
if(!isSuper())
{
count = columns.size();
}
else
{
Collection<IColumn> values = columns.values();
for(IColumn column: values)
{
count += column.getObjectCount();
}
}
}
return count;
}
public boolean isSuper()
{
return type_.equals("Super");
}
public void addColumn(String name, byte[] value)
{
addColumn(name, value, 0);
}
public void addColumn(String name, byte[] value, long timestamp)
{
addColumn(name, value, timestamp, false);
}
public void addColumn(String name, byte[] value, long timestamp, boolean deleted)
{
IColumn column = columnFactory_.createColumn(name, value, timestamp, deleted);
addColumn(column);
}
void clear()
{
columns_.clear();
}
/*
* If we find an old column that has the same name
* the ask it to resolve itself else add the new column .
*/
void addColumn(IColumn column)
{
String name = column.name();
IColumn oldColumn = columns_.get(name);
if (oldColumn != null)
{
if (oldColumn instanceof SuperColumn)
{
int oldSize = oldColumn.size();
((SuperColumn) oldColumn).putColumn(column);
size_.addAndGet(oldColumn.size() - oldSize);
}
else
{
if (oldColumn.timestamp() <= column.timestamp())
{
columns_.put(name, column);
size_.addAndGet(column.size());
}
}
}
else
{
size_.addAndGet(column.size());
columns_.put(name, column);
}
}
public IColumn getColumn(String name)
{
return columns_.get( name );
}
public SortedSet<IColumn> getAllColumns()
{
return columns_.getSortedColumns();
}
Map<String, IColumn> getColumns()
{
return columns_.getColumns();
}
public void remove(String columnName)
{
columns_.remove(columnName);
}
void delete(long timestamp)
{
markedForDeleteAt = timestamp;
}
public boolean isMarkedForDelete()
{
return markedForDeleteAt > Long.MIN_VALUE;
}
/*
* This function will repair a list of columns
* If there are any columns in the external list which are not present
* internally then they are added ( this might have to change depending on
* how we implement delete repairs).
* Also if there are any columns in teh internal and not in the external
* they are kept intact.
* Else the one with the greatest timestamp is considered latest.
*/
void repair(ColumnFamily columnFamily)
{
for (IColumn column : columnFamily.getAllColumns()) {
addColumn(column);
}
}
/*
* This function will calculate the differnce between 2 column families
* the external input is considered the superset of internal
* so there are no deletes in the diff.
*/
ColumnFamily diff(ColumnFamily columnFamily)
{
ColumnFamily cfDiff = new ColumnFamily(columnFamily.name(), columnFamily.type_);
Map<String, IColumn> columns = columnFamily.getColumns();
Set<String> cNames = columns.keySet();
for ( String cName : cNames )
{
IColumn columnInternal = columns_.get(cName);
IColumn columnExternal = columns.get(cName);
if( columnInternal == null )
{
cfDiff.addColumn(columnExternal);
}
else
{
IColumn columnDiff = columnInternal.diff(columnExternal);
if(columnDiff != null)
{
cfDiff.addColumn(columnDiff);
}
}
}
if(cfDiff.getColumns().size() != 0)
return cfDiff;
else
return null;
}
int size()
{
if ( size_.get() == 0 )
{
Set<String> cNames = columns_.getColumns().keySet();
for ( String cName : cNames )
{
size_.addAndGet(columns_.get(cName).size());
}
}
return size_.get();
}
public int hashCode()
{
return name().hashCode();
}
public boolean equals(Object o)
{
if ( !(o instanceof ColumnFamily) )
return false;
ColumnFamily cf = (ColumnFamily)o;
return name().equals(cf.name());
}
public String toString()
{
StringBuilder sb = new StringBuilder();
sb.append("ColumnFamily(");
sb.append(name_);
if (isMarkedForDelete()) {
sb.append(" -delete at " + getMarkedForDeleteAt() + "-");
}
sb.append(" [");
sb.append(StringUtils.join(getAllColumns(), ", "));
sb.append("])");
return sb.toString();
}
public byte[] digest()
{
Set<IColumn> columns = columns_.getSortedColumns();
byte[] xorHash = null;
for(IColumn column : columns)
{
if(xorHash == null)
{
xorHash = column.digest();
}
else
{
xorHash = FBUtilities.xor(xorHash, column.digest());
}
}
return xorHash;
}
public long getMarkedForDeleteAt() {
return markedForDeleteAt;
}
public String type()
{
return type_;
}
public static class ColumnFamilySerializer implements ICompactSerializer2<ColumnFamily>
{
/*
* We are going to create indexes, and write out that information as well. The format
* of the data serialized is as follows.
*
* 1) Without indexes:
* // written by the data
* <boolean false (index is not present)>
* <column family id>
* <is marked for delete>
* <total number of columns>
* <columns data>
* <boolean true (index is present)>
*
* This part is written by the column indexer
* <size of index in bytes>
* <list of column names and their offsets relative to the first column>
*
* <size of the cf in bytes>
* <column family id>
* <is marked for delete>
* <total number of columns>
* <columns data>
*/
public void serialize(ColumnFamily columnFamily, DataOutputStream dos) throws IOException
{
Collection<IColumn> columns = columnFamily.getAllColumns();
/* write the column family id */
dos.writeUTF(columnFamily.name());
/* write if this cf is marked for delete */
dos.writeLong(columnFamily.getMarkedForDeleteAt());
/* write the size is the number of columns */
dos.writeInt(columns.size());
/* write the column data */
for ( IColumn column : columns )
{
columnFamily.getColumnSerializer().serialize(column, dos);
}
}
/*
* Use this method to create a bare bones Column Family. This column family
* does not have any of the Column information.
*/
private ColumnFamily defreezeColumnFamily(DataInputStream dis) throws IOException
{
String name = dis.readUTF();
ColumnFamily cf = new ColumnFamily(name, DatabaseDescriptor.getColumnFamilyType(name));
cf.delete(dis.readLong());
return cf;
}
public ColumnFamily deserialize(DataInputStream dis) throws IOException
{
ColumnFamily cf = defreezeColumnFamily(dis);
int size = dis.readInt();
IColumn column = null;
for ( int i = 0; i < size; ++i )
{
column = cf.getColumnSerializer().deserialize(dis);
if(column != null)
{
cf.addColumn(column);
}
}
return cf;
}
/*
* This version of deserialize is used when we need a specific set if columns for
* a column family specified in the name cfName parameter.
*/
public ColumnFamily deserialize(DataInputStream dis, IFilter filter) throws IOException
{
ColumnFamily cf = defreezeColumnFamily(dis);
int size = dis.readInt();
IColumn column = null;
for ( int i = 0; i < size; ++i )
{
column = cf.getColumnSerializer().deserialize(dis, filter);
if(column != null)
{
cf.addColumn(column);
column = null;
if(filter.isDone())
{
break;
}
}
}
return cf;
}
/*
* Deserialize a particular column or super column or the entire columnfamily given a : seprated name
* name could be of the form cf:superColumn:column or cf:column or cf
*/
public ColumnFamily deserialize(DataInputStream dis, String name, IFilter filter) throws IOException
{
String[] names = RowMutation.getColumnAndColumnFamily(name);
String columnName = "";
if ( names.length == 1 )
return deserialize(dis, filter);
if( names.length == 2 )
columnName = names[1];
if( names.length == 3 )
columnName = names[1]+ ":" + names[2];
ColumnFamily cf = defreezeColumnFamily(dis);
/* read the number of columns */
int size = dis.readInt();
for ( int i = 0; i < size; ++i )
{
IColumn column = cf.getColumnSerializer().deserialize(dis, columnName, filter);
if ( column != null )
{
cf.addColumn(column);
break;
}
}
return cf;
}
public void skip(DataInputStream dis) throws IOException
{
throw new UnsupportedOperationException("This operation is not yet supported.");
}
}
}