-
Notifications
You must be signed in to change notification settings - Fork 5
/
GSBuffer.java
817 lines (690 loc) · 17.1 KB
/
GSBuffer.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
package org.genericsystem.common;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.invoke.MethodHandles;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.stream.Collectors;
import org.genericsystem.api.core.ApiStatics;
import org.genericsystem.api.core.AxedPropertyClass;
import org.genericsystem.api.core.exceptions.ConcurrencyControlException;
import org.genericsystem.api.core.exceptions.OptimisticLockConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.netty.buffer.ByteBuf;
import io.netty.util.CharsetUtil;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
/**
* @author Nicolas Feybesse
*
*/
public class GSBuffer implements Buffer {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
private int index = 0;
private final Buffer internal;
public GSBuffer() {
this(Buffer.buffer());
}
public GSBuffer(Buffer internal) {
this.internal = internal;
}
@Override
public void writeToBuffer(Buffer buffer) {
internal.writeToBuffer(buffer);
}
@Override
public int readFromBuffer(int pos, Buffer buffer) {
return internal.readFromBuffer(pos, buffer);
}
@Override
public String toString(String enc) {
return internal.toString(enc);
}
@Override
public byte getByte(int pos) {
return internal.getByte(pos);
}
@Override
public int getInt(int pos) {
return internal.getInt(pos);
}
@Override
public long getLong(int pos) {
return internal.getLong(pos);
}
@Override
public double getDouble(int pos) {
return internal.getDouble(pos);
}
@Override
public float getFloat(int pos) {
return internal.getFloat(pos);
}
@Override
public short getShort(int pos) {
return internal.getShort(pos);
}
@Override
public byte[] getBytes() {
return internal.getBytes();
}
@Override
public byte[] getBytes(int start, int end) {
return internal.getBytes(start, end);
}
@Override
public GSBuffer getBuffer(int start, int end) {
return new GSBuffer(internal.getBuffer(start, end));
}
@Override
public String getString(int start, int end, String enc) {
return internal.getString(start, end, enc);
}
@Override
public String getString(int start, int end) {
return internal.getString(start, end);
}
@Override
public Buffer appendBuffer(Buffer buff) {
return internal.appendBuffer(buff);
}
@Override
public Buffer appendBuffer(Buffer buff, int offset, int len) {
return internal.appendBuffer(buff, offset, len);
}
@Override
public Buffer appendBytes(byte[] bytes) {
return internal.appendBytes(bytes);
}
@Override
public Buffer appendBytes(byte[] bytes, int offset, int len) {
return internal.appendBytes(bytes, offset, len);
}
@Override
public GSBuffer appendByte(byte b) {
internal.appendByte(b);
return this;
}
@Override
public GSBuffer appendInt(int i) {
internal.appendInt(i);
return this;
}
@Override
public GSBuffer appendLong(long l) {
internal.appendLong(l);
return this;
}
@Override
public GSBuffer appendShort(short s) {
internal.appendShort(s);
return this;
}
@Override
public GSBuffer appendFloat(float f) {
internal.appendFloat(f);
return this;
}
@Override
public GSBuffer appendDouble(double d) {
internal.appendDouble(d);
return this;
}
@Override
public GSBuffer appendString(String str, String enc) {
internal.appendString(str, enc);
return this;
}
@Override
public GSBuffer appendString(String str) {
internal.appendString(str);
return this;
}
public GSBuffer appendGSString(String string) {
return appendGSBytes(string.getBytes(CharsetUtil.UTF_8));
}
public GSBuffer appendGSVertex(Vertex vertex) {
appendGSClazz(vertex.getClazz());
appendLong(vertex.getTs());
appendLong(vertex.getMeta());
appendGSLongList(vertex.getSupers());
appendGSValue(vertex.getValue());
appendGSLongList(vertex.getComponents());
appendLong(vertex.getBirthTs());
return this;
}
public GSBuffer appendGSSignature(long meta, List<Long> supers, Serializable value, List<Long> components) {
appendLong(meta);
appendGSLongList(supers);
appendGSValue(value);
appendGSLongList(components);
return this;
}
public GSBuffer appendGSVertexArray(Vertex[] vertexArray) {
appendInt(vertexArray.length);
for (Vertex v : vertexArray)
appendGSVertex(v);
return this;
}
public GSBuffer appendGSLongList(List<Long> array) {
appendInt(array.size());
for (long l : array)
appendLong(l);
return this;
}
public GSBuffer appendGSLongArray(long[] array) {
appendInt(array.length);
for (long l : array)
appendLong(l);
return this;
}
public GSBuffer appendGSValue(Serializable value) {
// System.out.println("Append value : " + value);
for (Entry<Integer, Class<?>> entry : ApiStatics.SUPPORTED_VALUE_CLASSES.entrySet()) {
if (entry.getValue().isInstance(value)) {
appendInt(entry.getKey());
switch (entry.getKey()) {
case 0: {
appendGSClazz(((AxedPropertyClass) value).getClazz());
appendInt(((AxedPropertyClass) value).getAxe());
return this;
}
case 1: {
appendInt(((Boolean) value).booleanValue() ? 1 : 0);
return this;
}
case 2: {
appendGSBytes((byte[]) value);
return this;
}
case 3: {
appendDouble((Double) value);
return this;
}
case 4: {
appendFloat((Float) value);
return this;
}
case 5: {
appendInt((Integer) value);
return this;
}
case 6: {
appendLong((Long) value);
return this;
}
case 7: {
appendShort((Short) value);
return this;
}
case 8: {
appendGSString((String) value);
return this;
}
case 9: {
appendGSClazz((Class<?>) value);
return this;
}
default:
throw new IllegalStateException("unknown class code in appendGSValue" + entry.getKey());
}
}
}
return this;
}
public GSBuffer appendGSBytes(byte[] bytes) {
appendInt(bytes.length);
appendBytes(bytes);
return this;
}
public GSBuffer appendGSClazz(Class<?> clazz) {
appendGSString(clazz != null ? clazz.getName() : "");
return this;
}
public Object getIntThrowException() {
return getInt() == 1 ? getInt() : getGSSerializable();
}
public Object getLongThrowException() {
return getInt() == 1 ? getLong() : getGSSerializable();
}
public Object getGSVertexThrowException() {
return getInt() == 1 ? getGSVertex() : getGSSerializable();
}
public Object getGSVertexArrayThrowException() {
return getInt() == 1 ? getGSVertexArray() : getGSSerializable();
}
@FunctionalInterface
public interface ConcurrentSupplier<T> {
T get() throws ConcurrencyControlException, OptimisticLockConstraintViolationException;
}
public GSBuffer appendIntThrowException(ConcurrentSupplier<Integer> supplier) {
try {
int result = supplier.get();
appendInt(1);
appendInt(result);
} catch (Throwable t) {
appendInt(0);
logger.warn("Exception while appending to buffer.", t);
appendGSSerializable(t);
}
return this;
}
public GSBuffer appendLongThrowException(ConcurrentSupplier<Long> supplier) {
try {
long result = supplier.get();
appendInt(1);
appendLong(result);
} catch (Throwable t) {
appendInt(0);
logger.warn("Exception while appending to buffer.", t);
appendGSSerializable(t);
}
return this;
}
public GSBuffer appendGSVertexThrowException(ConcurrentSupplier<Vertex> vertex) {
try {
Vertex result = vertex.get();
appendInt(1);
appendGSVertex(result);
} catch (Throwable t) {
appendInt(0);
logger.warn("Exception while appending to buffer.", t);
appendGSSerializable(t);
}
return this;
}
public GSBuffer appendGSVertexArrayThrowException(ConcurrentSupplier<Vertex[]> vertexArray) {
try {
Vertex[] result = vertexArray.get();
appendInt(1);
appendGSVertexArray(result);
} catch (Throwable t) {
appendInt(0);
logger.warn("Exception while appending to buffer.", t);
appendGSSerializable(t);
}
return this;
}
public GSBuffer appendGSSerializable(Serializable serializable) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(serializable);
} catch (IOException e) {
logger.error("Exception while writing object.", e);
throw new IllegalStateException(e);
}
appendGSBytes(bos.toByteArray());
return this;
}
public Serializable getGSSerializable() {
ByteArrayInputStream bis = new ByteArrayInputStream(getGSBytes());
Serializable serializable;
try {
ObjectInputStream ois = new ObjectInputStream(bis);
serializable = (Serializable) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
logger.error("Exception while reading object.", e);
throw new IllegalStateException();
}
return serializable;
}
public int getInt() {
int result = getInt(index);
index += 4;
return result;
}
public long getLong() {
long result = getLong(index);
index += 8;
return result;
}
public double getDouble() {
double result = getDouble(index);
index += 16;
return result;
}
public float getFloat() {
float result = getFloat(index);
index += 8;
return result;
}
public short getShort() {
short result = getShort(index);
index += 2;
return result;
}
public String getGSString() {
byte[] result = getGSBytes();
return new String(result);
}
public Vertex getGSVertex() {
Class<?> clazz = getGSClazz();
long ts = getLong();
long meta = getLong();
List<Long> supers = Arrays.stream(getGSLongArray()).mapToObj(l -> l).collect(Collectors.toList());
Serializable value = getGSValue();
List<Long> components = Arrays.stream(getGSLongArray()).mapToObj(l -> l).collect(Collectors.toList());
long birthTs = getLong();
return new Vertex(clazz, ts, meta, supers, value, components, birthTs);
}
public Vertex[] getGSVertexArray() {
Vertex[] result = new Vertex[getInt()];
for (int i = 0; i < result.length; i++)
result[i] = getGSVertex();
return result;
}
public long[] getGSLongArray() {
long[] result = new long[getInt()];
for (int i = 0; i < result.length; i++)
result[i] = getLong();
return result;
}
public Serializable getGSValue() {
int code = getInt();
switch (code) {
case 0:
return new AxedPropertyClass((Class) getGSClazz(), getInt());
case 1:
return (boolean) (getInt() == 1);
case 2:
return getGSBytes();
case 3:
return getDouble();
case 4:
return getFloat();
case 5:
return getInt();
case 6:
return getLong();
case 7:
return getShort();
case 8:
return getGSString();
case 9:
return getGSClazz();
case 10:
return null;
default:
throw new IllegalStateException("unknowned class code");
}
}
public Class<?> getGSClazz() {
String string = getGSString();
try {
return string.isEmpty() ? null : Class.forName(string);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
public byte[] getGSBytes() {
int length = getInt();
byte[] result = getBytes(index, index + length);
if (result.length != length)
throw new IllegalStateException(result.length + " " + length);
index += length;
return result;
}
public byte getByte() {
byte result = getByte(index);
index++;
return result;
}
@Override
public GSBuffer setByte(int pos, byte b) {
internal.setByte(pos, b);
return this;
}
@Override
public GSBuffer setInt(int pos, int i) {
internal.setInt(pos, i);
return this;
}
@Override
public GSBuffer setLong(int pos, long l) {
internal.setLong(pos, l);
return this;
}
@Override
public GSBuffer setDouble(int pos, double d) {
internal.setDouble(pos, d);
return this;
}
@Override
public GSBuffer setFloat(int pos, float f) {
internal.setFloat(pos, f);
return this;
}
@Override
public GSBuffer setShort(int pos, short s) {
internal.setShort(pos, s);
return this;
}
@Override
public GSBuffer setBuffer(int pos, Buffer b) {
internal.setBuffer(pos, b);
return this;
}
@Override
public GSBuffer setBuffer(int pos, Buffer b, int offset, int len) {
internal.setBuffer(pos, b, offset, len);
return this;
}
@Override
public GSBuffer setBytes(int pos, ByteBuffer b) {
internal.setBytes(pos, b);
return this;
}
@Override
public GSBuffer setBytes(int pos, byte[] b) {
internal.setBytes(pos, b);
return this;
}
@Override
public GSBuffer setBytes(int pos, byte[] b, int offset, int len) {
internal.setBytes(pos, b, offset, len);
return this;
}
@Override
public GSBuffer setString(int pos, String str) {
internal.setString(pos, str);
return this;
}
@Override
public GSBuffer setString(int pos, String str, String enc) {
internal.setString(pos, str, enc);
return this;
}
@Override
public int length() {
return internal.length();
}
@Override
public GSBuffer copy() {
return new GSBuffer(internal.copy());
}
@Override
public GSBuffer slice() {
internal.slice();
return this;
}
@Override
public Buffer slice(int start, int end) {
return internal.slice(start, end);
}
@Override
public ByteBuf getByteBuf() {
return internal.getByteBuf();
}
@Override
public GSBuffer appendUnsignedByte(short arg0) {
internal.appendUnsignedByte(arg0);
return this;
}
@Override
public GSBuffer appendUnsignedInt(long arg0) {
internal.appendUnsignedInt(arg0);
return this;
}
@Override
public GSBuffer appendUnsignedShort(int arg0) {
internal.appendUnsignedShort(arg0);
return this;
}
@Override
public short getUnsignedByte(int arg0) {
return internal.getUnsignedByte(arg0);
}
@Override
public long getUnsignedInt(int arg0) {
return internal.getUnsignedInt(arg0);
}
@Override
public int getUnsignedShort(int arg0) {
return internal.getUnsignedShort(arg0);
}
@Override
public GSBuffer setUnsignedByte(int arg0, short arg1) {
internal.setUnsignedByte(arg0, arg1);
return this;
}
@Override
public GSBuffer setUnsignedInt(int arg0, long arg1) {
internal.setUnsignedInt(arg0, arg1);
return this;
}
@Override
public GSBuffer setUnsignedShort(int arg0, int arg1) {
internal.setUnsignedShort(arg0, arg1);
return this;
}
@Override
public GSBuffer getBytes(byte[] arg0) {
return new GSBuffer(internal.getBytes(arg0));
}
@Override
public GSBuffer getBytes(byte[] arg0, int arg1) {
return new GSBuffer(internal.getBytes(arg0, arg1));
}
@Override
public GSBuffer getBytes(int arg0, int arg1, byte[] arg2) {
return new GSBuffer(internal.getBytes(arg0, arg1, arg2));
}
@Override
public GSBuffer getBytes(int arg0, int arg1, byte[] arg2, int arg3) {
return new GSBuffer(internal.getBytes(arg0, arg1, arg2, arg3));
}
@Override
public JsonArray toJsonArray() {
return internal.toJsonArray();
}
@Override
public JsonObject toJsonObject() {
return internal.toJsonObject();
}
@Override
public String toString(Charset arg0) {
return internal.toString();
}
@Override
public int getIntLE(int pos) {
return internal.getIntLE(pos);
}
@Override
public long getUnsignedIntLE(int pos) {
return internal.getUnsignedIntLE(pos);
}
@Override
public long getLongLE(int pos) {
return internal.getLongLE(pos);
}
@Override
public short getShortLE(int pos) {
return internal.getShortLE(pos);
}
@Override
public int getUnsignedShortLE(int pos) {
return internal.getUnsignedShortLE(pos);
}
@Override
public int getMedium(int pos) {
return internal.getMedium(pos);
}
@Override
public int getMediumLE(int pos) {
return internal.getMediumLE(pos);
}
@Override
public int getUnsignedMedium(int pos) {
return internal.getUnsignedMedium(pos);
}
@Override
public int getUnsignedMediumLE(int pos) {
return internal.getUnsignedMediumLE(pos);
}
@Override
public Buffer appendIntLE(int i) {
return internal.appendIntLE(i);
}
@Override
public Buffer appendUnsignedIntLE(long i) {
return internal.appendUnsignedIntLE(i);
}
@Override
public Buffer appendMedium(int i) {
return internal.appendMedium(i);
}
@Override
public Buffer appendMediumLE(int i) {
return internal.appendMediumLE(i);
}
@Override
public Buffer appendLongLE(long l) {
return internal.appendLongLE(l);
}
@Override
public Buffer appendShortLE(short s) {
return internal.appendShortLE(s);
}
@Override
public Buffer appendUnsignedShortLE(int s) {
return internal.appendUnsignedShortLE(s);
}
@Override
public Buffer setIntLE(int pos, int i) {
return internal.setInt(pos, i);
}
@Override
public Buffer setUnsignedIntLE(int pos, long i) {
return internal.setUnsignedIntLE(pos, i);
}
@Override
public Buffer setMedium(int pos, int i) {
return internal.setMedium(pos, i);
}
@Override
public Buffer setMediumLE(int pos, int i) {
return internal.setMediumLE(pos, i);
}
@Override
public Buffer setLongLE(int pos, long l) {
return internal.setLongLE(pos, l);
}
@Override
public Buffer setShortLE(int pos, short s) {
return internal.setShortLE(pos, s);
}
@Override
public Buffer setUnsignedShortLE(int pos, int s) {
return internal.setUnsignedShortLE(pos, s);
}
}