This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
forked from hector-client/hector
-
Notifications
You must be signed in to change notification settings - Fork 31
/
AbstractComposite.java
741 lines (616 loc) · 20.4 KB
/
AbstractComposite.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package me.prettyprint.hector.api.beans;
import static me.prettyprint.hector.api.ddl.ComparatorType.ASCIITYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.BYTESTYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.INTEGERTYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.LEXICALUUIDTYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.LONGTYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.TIMEUUIDTYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.UTF8TYPE;
import static me.prettyprint.hector.api.ddl.ComparatorType.UUIDTYPE;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.charset.CharacterCodingException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import me.prettyprint.cassandra.serializers.AsciiSerializer;
import me.prettyprint.cassandra.serializers.BigIntegerSerializer;
import me.prettyprint.cassandra.serializers.ByteBufferSerializer;
import me.prettyprint.cassandra.serializers.LongSerializer;
import me.prettyprint.cassandra.serializers.SerializerTypeInferer;
import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.serializers.UUIDSerializer;
import me.prettyprint.cassandra.utils.ByteBufferOutputStream;
import me.prettyprint.hector.api.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Charsets;
import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableClassToInstanceMap;
/**
* Parent class of Composite and DynamicComposite. Acts as a list of objects
* that get serialized into a composite column name. Unless
* setAutoDeserialize(true) is called, it's going to try to match serializers to
* Cassandra comparator types.
*
* @author edanuff
*/
@SuppressWarnings("rawtypes")
public abstract class AbstractComposite extends AbstractList<Object> implements
Comparable<AbstractComposite> {
public static Logger log = LoggerFactory.getLogger(AbstractComposite.class);
public enum ComponentEquality {
LESS_THAN_EQUAL((byte) -1), EQUAL((byte) 0), GREATER_THAN_EQUAL((byte) 1);
private final byte equality;
ComponentEquality(byte equality) {
this.equality = equality;
}
public byte toByte() {
return equality;
}
public static ComponentEquality fromByte(byte equality) {
if (equality > 0) {
return GREATER_THAN_EQUAL;
}
if (equality < 0) {
return LESS_THAN_EQUAL;
}
return EQUAL;
}
}
public static final BiMap<Class<? extends Serializer>, String> DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder<Class<? extends Serializer>, String>()
.put(AsciiSerializer.class,
AsciiSerializer.get().getComparatorType().getTypeName())
.put(BigIntegerSerializer.class,
BigIntegerSerializer.get().getComparatorType().getTypeName())
.put(LongSerializer.class,
LongSerializer.get().getComparatorType().getTypeName())
.put(StringSerializer.class,
StringSerializer.get().getComparatorType().getTypeName())
.put(UUIDSerializer.class,
UUIDSerializer.get().getComparatorType().getTypeName()).build();
static final ImmutableClassToInstanceMap<Serializer> SERIALIZERS = new ImmutableClassToInstanceMap.Builder<Serializer>()
.put(AsciiSerializer.class, AsciiSerializer.get())
.put(BigIntegerSerializer.class, BigIntegerSerializer.get())
.put(ByteBufferSerializer.class, ByteBufferSerializer.get())
.put(LongSerializer.class, LongSerializer.get())
.put(StringSerializer.class, StringSerializer.get())
.put(UUIDSerializer.class, UUIDSerializer.get()).build();
public static final BiMap<Byte, String> DEFAULT_ALIAS_TO_COMPARATOR_MAPPING = new ImmutableBiMap.Builder<Byte, String>()
.put((byte) 'a', ASCIITYPE.getTypeName())
.put((byte) 'b', BYTESTYPE.getTypeName())
.put((byte) 'i', INTEGERTYPE.getTypeName())
.put((byte) 'x', LEXICALUUIDTYPE.getTypeName())
.put((byte) 'l', LONGTYPE.getTypeName())
.put((byte) 't', TIMEUUIDTYPE.getTypeName())
.put((byte) 's', UTF8TYPE.getTypeName())
.put((byte) 'u', UUIDTYPE.getTypeName()).build();
BiMap<Class<? extends Serializer>, String> serializerToComparatorMapping = DEFAULT_SERIALIZER_TO_COMPARATOR_MAPPING;
BiMap<Byte, String> aliasToComparatorMapping = DEFAULT_ALIAS_TO_COMPARATOR_MAPPING;
final boolean dynamic;
List<Serializer<?>> serializersByPosition = null;
List<String> comparatorsByPosition = null;
public class Component<T> {
final Serializer<T> serializer;
final T value;
final ByteBuffer bytes;
final String comparator;
final ComponentEquality equality;
public Component(T value, ByteBuffer bytes, Serializer<T> serializer,
String comparator, ComponentEquality equality) {
this.serializer = serializer;
this.value = value;
this.bytes = bytes;
this.comparator = comparator;
this.equality = equality;
}
public Serializer<T> getSerializer() {
return serializer;
}
@SuppressWarnings("unchecked")
public <A> A getValue(Serializer<A> s) {
if (s == null) {
s = (Serializer<A>) serializer;
}
if ((value == null) && (bytes != null) && (s != null)) {
ByteBuffer cb = bytes.duplicate();
if (cb.hasRemaining()) {
return s.fromByteBuffer(cb);
}
}
if (value instanceof ByteBuffer) {
return (A) ((ByteBuffer) value).duplicate();
}
return (A) value;
}
public T getValue() {
return getValue(serializer);
}
@SuppressWarnings("unchecked")
public <A> ByteBuffer getBytes(Serializer<A> s) {
if (bytes == null) {
if (value instanceof ByteBuffer) {
return ((ByteBuffer) value).duplicate();
}
if (value == null) {
return null;
}
if (s == null) {
s = (Serializer<A>) serializer;
}
if (s != null) {
return s.toByteBuffer((A) value).duplicate();
}
}
return bytes.duplicate();
}
public ByteBuffer getBytes() {
return getBytes(serializer);
}
public String getComparator() {
return comparator;
}
public ComponentEquality getEquality() {
return equality;
}
@Override
public String toString() {
return "Component [" + getValue() + "]";
}
}
List<Component<?>> components = new ArrayList<Component<?>>();
ComponentEquality equality = ComponentEquality.EQUAL;
ByteBuffer serialized = null;
public AbstractComposite(boolean dynamic) {
this.dynamic = dynamic;
}
public AbstractComposite(boolean dynamic, Object... o) {
this.dynamic = dynamic;
this.addAll(Arrays.asList(o));
}
public AbstractComposite(boolean dynamic, List<?> l) {
this.dynamic = dynamic;
this.addAll(l);
}
public List<Component<?>> getComponents() {
return components;
}
public void setComponents(List<Component<?>> components) {
serialized = null;
this.components = components;
}
public Map<Class<? extends Serializer>, String> getSerializerToComparatorMapping() {
return serializerToComparatorMapping;
}
public void setSerializerToComparatorMapping(
Map<Class<? extends Serializer>, String> serializerToComparatorMapping) {
serialized = null;
this.serializerToComparatorMapping = new ImmutableBiMap.Builder<Class<? extends Serializer>, String>()
.putAll(serializerToComparatorMapping).build();
}
public Map<Byte, String> getAliasesToComparatorMapping() {
return aliasToComparatorMapping;
}
public void setAliasesToComparatorMapping(
Map<Byte, String> aliasesToComparatorMapping) {
serialized = null;
aliasToComparatorMapping = new ImmutableBiMap.Builder<Byte, String>()
.putAll(aliasesToComparatorMapping).build();
}
public boolean isDynamic() {
return dynamic;
}
public List<Serializer<?>> getSerializersByPosition() {
return serializersByPosition;
}
public void setSerializersByPosition(List<Serializer<?>> serializersByPosition) {
this.serializersByPosition = serializersByPosition;
}
public void setSerializersByPosition(Serializer<?>... serializers) {
serializersByPosition = Arrays.asList(serializers);
}
public void setSerializerByPosition(int index, Serializer<?> s) {
if (serializersByPosition == null) {
serializersByPosition = new ArrayList<Serializer<?>>();
}
while (serializersByPosition.size() <= index) {
serializersByPosition.add(null);
}
serializersByPosition.set(index, s);
}
public List<String> getComparatorsByPosition() {
return comparatorsByPosition;
}
public void setComparatorsByPosition(List<String> comparatorsByPosition) {
this.comparatorsByPosition = comparatorsByPosition;
}
public void setComparatorsByPosition(String... comparators) {
comparatorsByPosition = Arrays.asList(comparators);
}
public void setComparatorByPosition(int index, String c) {
if (comparatorsByPosition == null) {
comparatorsByPosition = new ArrayList<String>();
}
while (comparatorsByPosition.size() <= index) {
comparatorsByPosition.add(null);
}
comparatorsByPosition.set(index, c);
}
@Override
public int compareTo(AbstractComposite o) {
return serialize().compareTo(o.serialize());
}
private String comparatorForSerializer(Serializer<?> s) {
String comparator = serializerToComparatorMapping.get(s.getClass());
if (comparator != null) {
return comparator;
}
return BYTESTYPE.getTypeName();
}
private Serializer<?> serializerForComparator(String c) {
int p = c.indexOf('(');
if (p >= 0) {
c = c.substring(0, p);
}
if (LEXICALUUIDTYPE.getTypeName().equals(c)
|| TIMEUUIDTYPE.getTypeName().equals(c)) {
return UUIDSerializer.get();
}
Serializer<?> s = SERIALIZERS.getInstance(serializerToComparatorMapping
.inverse().get(c));
if (s != null) {
return s;
}
return ByteBufferSerializer.get();
}
private Serializer<?> serializerForPosition(int i) {
if (serializersByPosition == null) {
return null;
}
if (i >= serializersByPosition.size()) {
return null;
}
return serializersByPosition.get(i);
}
private Serializer<?> getSerializer(int i, String c) {
Serializer<?> s = serializerForPosition(i);
if (s != null) {
return s;
}
return serializerForComparator(c);
}
private String comparatorForPosition(int i) {
if (comparatorsByPosition == null) {
return null;
}
if (i >= comparatorsByPosition.size()) {
return null;
}
return comparatorsByPosition.get(i);
}
private String getComparator(int i, ByteBuffer bb) {
String name = comparatorForPosition(i);
if (name != null) {
return name;
}
if (!dynamic) {
if (bb.hasRemaining()) {
return BYTESTYPE.getTypeName();
} else {
return null;
}
}
if (bb.hasRemaining()) {
try {
int header = getShortLength(bb);
if ((header & 0x8000) == 0) {
name = Charsets.UTF_8.newDecoder().decode(getBytes(bb, header).duplicate()).toString();
} else {
byte a = (byte) (header & 0xFF);
name = aliasToComparatorMapping.get(a);
if (name == null) {
a = (byte) Character.toLowerCase((char) a);
name = aliasToComparatorMapping.get(a);
if (name != null) {
name += "(reversed=true)";
}
}
}
} catch (CharacterCodingException e) {
throw new RuntimeException(e);
}
}
if ((name != null) && (name.length() == 0)) {
name = null;
}
return name;
}
@Override
public void clear() {
serialized = null;
components = new ArrayList<Component<?>>();
}
@Override
public int size() {
return components.size();
}
public <T> AbstractComposite addComponent(T value, Serializer<T> s) {
addComponent(value, s, comparatorForSerializer(s));
return this;
}
public <T> AbstractComposite addComponent(T value, Serializer<T> s,
String comparator) {
addComponent(value, s, comparator, ComponentEquality.EQUAL);
return this;
}
public <T> AbstractComposite addComponent(T value, Serializer<T> s,
String comparator, ComponentEquality equality) {
addComponent(-1, value, s, comparator, equality);
return this;
}
@SuppressWarnings("unchecked")
public <T> AbstractComposite addComponent(int index, T value,
Serializer<T> s, String comparator, ComponentEquality equality) {
serialized = null;
if (index < 0) {
index = components.size();
}
while (components.size() < index) {
components.add(null);
}
components.add(index, new Component(value, null, s, comparator, equality));
return this;
}
private static Object mapIfNumber(Object o) {
if ((o instanceof Byte) || (o instanceof Integer) || (o instanceof Short)) {
return BigInteger.valueOf(((Number) o).longValue());
}
return o;
}
@SuppressWarnings({ "unchecked" })
private static Collection<?> flatten(Collection<?> c) {
if (c instanceof AbstractComposite) {
return ((AbstractComposite) c).getComponents();
}
boolean hasCollection = false;
for (Object o : c) {
if (o instanceof Collection) {
hasCollection = true;
break;
}
}
if (!hasCollection) {
return c;
}
List newList = new ArrayList();
for (Object o : c) {
if (o instanceof Collection) {
newList.addAll(flatten((Collection) o));
} else {
newList.add(o);
}
}
return newList;
}
@Override
public boolean addAll(Collection<? extends Object> c) {
return super.addAll(flatten(c));
}
@Override
public boolean containsAll(Collection<?> c) {
return super.containsAll(flatten(c));
}
@Override
public boolean removeAll(Collection<?> c) {
return super.removeAll(flatten(c));
}
@Override
public boolean retainAll(Collection<?> c) {
return super.retainAll(flatten(c));
}
@Override
public boolean addAll(int i, Collection<? extends Object> c) {
return super.addAll(i, flatten(c));
}
@SuppressWarnings("unchecked")
@Override
public void add(int index, Object element) {
serialized = null;
if (element instanceof Component) {
components.add(index, (Component<?>) element);
return;
}
element = mapIfNumber(element);
Serializer s = serializerForPosition(index);
if (s == null) {
s = SerializerTypeInferer.getSerializer(element);
}
String c = comparatorForPosition(index);
if (c == null) {
c = comparatorForSerializer(s);
}
components.add(index, new Component(element, null, s, c,
ComponentEquality.EQUAL));
}
@Override
public Object remove(int index) {
serialized = null;
Component prev = components.remove(index);
if (prev != null) {
return prev.getValue();
}
return null;
}
public <T> AbstractComposite setComponent(int index, T value, Serializer<T> s) {
setComponent(index, value, s, comparatorForSerializer(s));
return this;
}
public <T> AbstractComposite setComponent(int index, T value,
Serializer<T> s, String comparator) {
setComponent(index, value, s, comparator, ComponentEquality.EQUAL);
return this;
}
@SuppressWarnings("unchecked")
public <T> AbstractComposite setComponent(int index, T value,
Serializer<T> s, String comparator, ComponentEquality equality) {
serialized = null;
while (components.size() <= index) {
components.add(null);
}
components.set(index, new Component(value, null, s, comparator, equality));
return this;
}
@SuppressWarnings("unchecked")
@Override
public Object set(int index, Object element) {
serialized = null;
if (element instanceof Component) {
Component prev = components.set(index, (Component<?>) element);
if (prev != null) {
return prev.getValue();
}
return null;
}
element = mapIfNumber(element);
Serializer s = serializerForPosition(index);
if (s == null) {
s = SerializerTypeInferer.getSerializer(element);
}
String c = comparatorForPosition(index);
if (c == null) {
c = comparatorForSerializer(s);
}
Component prev = components.set(index, new Component(element, null, s, c,
ComponentEquality.EQUAL));
if (prev != null) {
return prev.getValue();
}
return null;
}
@Override
public Object get(int i) {
Component c = components.get(i);
if (c != null) {
return c.getValue();
}
return null;
}
public <T> T get(int i, Serializer<T> s) throws ClassCastException {
T value = null;
Component<?> c = components.get(i);
if (c != null) {
value = c.getValue(s);
}
return value;
}
public Component getComponent(int i) {
if (i >= components.size()) {
return null;
}
Component c = components.get(i);
return c;
}
public Iterator<Component<?>> componentsIterator() {
return components.iterator();
}
@SuppressWarnings("unchecked")
public ByteBuffer serialize() {
if (serialized != null) {
return serialized.duplicate();
}
ByteBufferOutputStream out = new ByteBufferOutputStream();
int i = 0;
for (Component c : components) {
Serializer<?> s = serializerForPosition(i);
ByteBuffer cb = c.getBytes(s);
if (cb == null) {
cb = ByteBuffer.allocate(0);
}
if (dynamic) {
String comparator = comparatorForPosition(i);
if (comparator == null) {
comparator = c.getComparator();
}
if (comparator == null) {
comparator = BYTESTYPE.getTypeName();
}
int p = comparator.indexOf("(reversed=true)");
boolean desc = false;
if (p >= 0) {
comparator = comparator.substring(0, p);
desc = true;
}
if (aliasToComparatorMapping.inverse().containsKey(comparator)) {
byte a = aliasToComparatorMapping.inverse().get(comparator);
if (desc) {
a = (byte) Character.toUpperCase((char) a);
}
out.writeShort((short) (0x8000 | a));
} else {
out.writeShort((short) comparator.length());
out.write(ByteBuffer.wrap(comparator.getBytes(Charsets.UTF_8)));
}
// if (comparator.equals(BYTESTYPE.getTypeName()) && (cb.remaining() ==
// 0)) {
// log.warn("Writing zero-length BytesType, probably an error");
// }
}
out.writeShort((short) cb.remaining());
out.write(cb.slice());
if ((i == (components.size() - 1))
&& (equality != ComponentEquality.EQUAL)) {
out.write(equality.toByte());
} else {
out.write(c.getEquality().toByte());
}
i++;
}
serialized = out.getByteBuffer();
return serialized.duplicate();
}
@SuppressWarnings("unchecked")
public void deserialize(ByteBuffer b) {
serialized = b.duplicate();
components = new ArrayList<Component<?>>();
String comparator = null;
int i = 0;
while ((comparator = getComparator(i, b)) != null) {
ByteBuffer data = getWithShortLength(b);
if (data != null) {
Serializer<?> s = getSerializer(i, comparator);
ComponentEquality equality = ComponentEquality.fromByte(b.get());
components.add(new Component(null, data.slice(), s, comparator,
equality));
} else {
throw new RuntimeException("Missing component data in composite type");
}
i++;
}
}
protected static int getShortLength(ByteBuffer bb) {
int length = (bb.get() & 0xFF) << 8;
return length | (bb.get() & 0xFF);
}
protected static ByteBuffer getBytes(ByteBuffer bb, int length) {
ByteBuffer copy = bb.duplicate();
copy.limit(copy.position() + length);
bb.position(bb.position() + length);
return copy;
}
protected static ByteBuffer getWithShortLength(ByteBuffer bb) {
int length = getShortLength(bb);
return getBytes(bb, length);
}
public ComponentEquality getEquality() {
return equality;
}
public void setEquality(ComponentEquality equality) {
serialized = null;
this.equality = equality;
}
}