/
Converters.java
546 lines (464 loc) · 15.5 KB
/
Converters.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
package com.alvazan.orm.api.z8spi.conv;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.ISODateTimeFormat;
import com.eaio.uuid.UUID;
public class Converters {
public static final StringConverter STRING_CONVERTER = new StringConverter();
public static final BooleanConverter BOOLEAN_CONVERTER = new BooleanConverter();
public static final ShortConverter SHORT_CONVERTER = new ShortConverter();
public static final IntConverter INT_CONVERTER = new IntConverter();
public static final LongConverter LONG_CONVERTER = new LongConverter();
public static final FloatConverter FLOAT_CONVERTER = new FloatConverter();
public static final DoubleConverter DOUBLE_CONVERTER = new DoubleConverter();
public static final ByteConverter BYTE_CONVERTER = new ByteConverter();
public static final ByteArrayConverter BYTE_ARRAY_CONVERTER = new ByteArrayConverter();
public static final BaseConverter BIGINTEGER_CONVERTER = new BigIntegerConverter();
public static final BaseConverter BIGDECIMAL_CONVERTER = new BigDecimalConverter();
public static final BaseConverter LOCAL_DATE_TIME = new LocalDateTimeConverter();
public static final BaseConverter DATE_TIME = new DateTimeConverter();
public static final BaseConverter LOCAL_DATE = new LocalDateConverter();
public static final BaseConverter LOCAL_TIME = new LocalTimeConverter();
public static final BaseConverter UUID_CONVERTER = new UUIDConverter();
private static byte[] intToBytes(int val) {
try {
ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
DataOutputStream out = new DataOutputStream(outBytes);
out.writeInt(val);
byte[] outData = outBytes.toByteArray();
return outData;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static class BigDecimalConverter extends BaseConverter {
@Override
public byte[] convertToNoSqlImpl(Object input) {
BigDecimal value = (BigDecimal) input;
BigInteger bi = value.unscaledValue();
Integer scale = value.scale();
byte[] bibytes = bi.toByteArray();
byte[] sbytes = intToBytes(scale);
byte[] bytes = new byte[bi.toByteArray().length+4];
for (int i = 0 ; i < 4 ; i++) bytes[i] = sbytes[i];
for (int i = 4 ; i < bibytes.length+4 ; i++) bytes[i] = bibytes[i-4];
return bytes;
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
ByteBuffer buf = ByteBuffer.wrap(value);
int scale = buf.getInt();
byte[] bibytes = new byte[buf.remaining()];
buf.get(bibytes);
BigInteger bi = new BigInteger(bibytes);
return new BigDecimal(bi,scale);
}
@Override
public Object convertToType(String value) {
return new BigDecimal(value);
}
}
public static class BigIntegerConverter extends BaseConverter {
@Override
public byte[] convertToNoSqlImpl(Object value) {
BigInteger val = (BigInteger)value;
return val.toByteArray();
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
return new BigInteger(value);
}
@Override
public Object convertToType(String value) {
return new BigInteger(value);
}
}
public static class ByteArrayConverter extends BaseConverter {
@Override
public byte[] convertToNoSqlImpl(Object value) {
return (byte[])value;
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
return value;
}
@Override
public String convertToString(Object dbValue) {
byte[] data = (byte[]) dbValue;
return new String(Hex.encodeHex(data));
}
@Override
public Object convertToType(String value) {
try {
return Hex.decodeHex(value.toCharArray());
} catch (DecoderException e) {
throw new RuntimeException("could not decode", e);
}
}
}
public static abstract class BaseConverter implements Converter {
public final byte[] convertToNoSql(Object value) {
if(value == null)
return null;
return convertToNoSqlImpl(value);
}
public final Object convertFromNoSql(byte[] value) {
if(value == null)
return null;
else if(value.length == 0)
return null;
return convertFromNoSqlImpl(value);
}
public final Object convertStringToType(String value) {
if(value == null)
return null;
return convertToType(value);
}
public final String convertTypeToString(Object value) {
if(value == null)
return null;
return convertToString(value);
}
protected abstract Object convertToType(String value);
protected abstract byte[] convertToNoSqlImpl(Object value);
protected abstract Object convertFromNoSqlImpl(byte[] value);
//This is overloaded for stuff like byte[] types which need to convert to Hex
//or for LocalDateTime as well to use correct formatter.
protected String convertToString(Object value) {
return value+"";
}
}
public static abstract class DecimalConverter extends BaseConverter {
public byte[] convertToNoSqlImpl(Object value) {
BigDecimal dec = convertToForSmallStorage(value);
return BIGDECIMAL_CONVERTER.convertToNoSql(dec);
}
public Object convertFromNoSqlImpl(byte[] data) {
BigDecimal bigD = (BigDecimal) BIGDECIMAL_CONVERTER.convertFromNoSql(data);
return convertFromForSmallStorage(bigD);
}
protected abstract Object convertFromForSmallStorage(BigDecimal bigD);
protected abstract BigDecimal convertToForSmallStorage(Object value);
}
public static abstract class IntegerConverter extends BaseConverter {
public byte[] convertToNoSqlImpl(Object value) {
BigInteger dec = convertToForSmallStorage(value);
return BIGINTEGER_CONVERTER.convertToNoSql(dec);
}
public Object convertFromNoSqlImpl(byte[] data) {
BigInteger bigD = (BigInteger) BIGINTEGER_CONVERTER.convertFromNoSql(data);
return convertFromForSmallStorage(bigD);
}
protected abstract Object convertFromForSmallStorage(BigInteger bigD);
protected abstract BigInteger convertToForSmallStorage(Object value);
}
public static class StringConverter extends BaseConverter {
@Override
public byte[] convertToNoSqlImpl(Object value) {
try {
String temp = (String) value;
return temp.getBytes("UTF8");
} catch(IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Object convertFromNoSqlImpl(byte[] bytes) {
try {
return new String(bytes, "UTF8");
} catch(IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected Object convertToType(String value) {
return value;
}
}
public static class ByteConverter extends IntegerConverter {
@Override
protected Object convertToType(String value) {
return Byte.parseByte(value);
}
@Override
protected Object convertFromForSmallStorage(BigInteger bigD) {
return bigD.byteValue();
}
@Override
protected BigInteger convertToForSmallStorage(Object value) {
Byte b = (Byte)value;
return BigInteger.valueOf(b.byteValue());
}
}
public static class ShortConverter extends IntegerConverter {
@Override
protected Object convertToType(String value) {
return Short.parseShort(value);
}
@Override
protected Object convertFromForSmallStorage(BigInteger bigD) {
return bigD.shortValue();
}
@Override
protected BigInteger convertToForSmallStorage(Object value) {
Short s = (Short)value;
return BigInteger.valueOf(s);
}
}
public static class IntConverter extends IntegerConverter {
@Override
protected Object convertToType(String value) {
return Integer.parseInt(value);
}
@Override
protected Object convertFromForSmallStorage(BigInteger bigD) {
return bigD.intValue();
}
@Override
protected BigInteger convertToForSmallStorage(Object value) {
Integer val = (Integer)value;
return BigInteger.valueOf(val.intValue());
}
}
public static class LongConverter extends IntegerConverter {
@Override
protected Object convertToType(String value) {
return Long.parseLong(value);
}
@Override
protected Object convertFromForSmallStorage(BigInteger bigD) {
return bigD.longValue();
}
@Override
protected BigInteger convertToForSmallStorage(Object value) {
Long val = (Long)value;
return BigInteger.valueOf(val);
}
/* This is issue #83
@Override
public byte[] convertToNoSqlImpl(Object value) {
// Cassandra expects an 8-byte Long value, but some versions
// of Java produce fewer bytes (eg 6 bytes in Java 1.7.0)
// We need to detect when this happens extend the array size
// We cannot use Arrays.copyOf(byte[], int newLength) because
// this right-pads the new array, but we need a left-padded
// array due to being big-endian.
// How many bytes does Cassandra expect?
int targetLength = 8;
byte[] b1 = super.convertToNoSqlImpl(value);
// Exit early if we already have an array of the right length
if (b1.length == targetLength) return b1;
// We need to copy the bytes offset by the delta length
byte[] b2 = new byte[targetLength];
// Left pad the array
int deltaLength = targetLength - b1.length;
for (int y = 0; y < b1.length; y++) {
b2[y + deltaLength] = b1[y];
}
return b2;
}*/
}
public static class FloatConverter extends DecimalConverter {
@Override
protected Object convertToType(String value) {
return Float.parseFloat(value);
}
@Override
protected Object convertFromForSmallStorage(BigDecimal bigD) {
return bigD.floatValue();
}
@Override
protected BigDecimal convertToForSmallStorage(Object value) {
Float f = (Float)value;
//NOTE: Must convert to string here or when you feed BigDecimal, the
//2.33 that you had given the float becomes 2.329999999991234...
return new BigDecimal(f.floatValue()+"");
}
}
public static class DoubleConverter extends DecimalConverter {
@Override
protected Object convertToType(String value) {
return Double.parseDouble(value);
}
@Override
protected Object convertFromForSmallStorage(BigDecimal bigD) {
return bigD.doubleValue();
}
@Override
protected BigDecimal convertToForSmallStorage(Object value) {
Double d = (Double)value;
//NOTE: Must convert to string here or when you feed BigDecimal, the
//2.33 that you had given the float becomes 2.329999999991234...
return new BigDecimal(""+d.doubleValue());
}
}
public static class BooleanConverter extends BaseConverter {
@Override
public byte[] convertToNoSqlImpl(Object value) {
Boolean b = (Boolean) value;
if(b)
return StandardConverters.convertToBytes(1);
return StandardConverters.convertToBytes(0);
}
@Override
public Object convertFromNoSqlImpl(byte[] bytes) {
Integer value = StandardConverters.convertFromBytes(Integer.class, bytes);
if(value == 1)
return true;
return false;
}
@Override
protected Object convertToType(String value) {
return Boolean.parseBoolean(value);
}
}
public static class LocalDateTimeConverter extends BaseConverter {
private DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
@Override
public byte[] convertToNoSqlImpl(Object value) {
LocalDateTime dt = (LocalDateTime) value;
long milliseconds = dt.toDate().getTime();
return StandardConverters.convertToBytes(milliseconds);
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
Long time = StandardConverters.convertFromBytes(Long.class, value);
LocalDateTime dt = new LocalDateTime(time);
return dt;
}
@Override
protected Object convertToType(String value) {
LocalDateTime dt = fmt.parseLocalDateTime(value);
return dt;
}
@Override
protected String convertToString(Object value) {
LocalDateTime dt = (LocalDateTime) value;
return fmt.print(dt);
}
}
public static class LocalDateConverter extends BaseConverter {
private DateTimeFormatter fmt = ISODateTimeFormat.date();
@Override
public byte[] convertToNoSqlImpl(Object value) {
LocalDate dt = (LocalDate) value;
long milliseconds = dt.toDate().getTime();
return StandardConverters.convertToBytes(milliseconds);
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
Long time = StandardConverters.convertFromBytes(Long.class, value);
LocalDate dt = new LocalDate(time);
return dt;
}
@Override
protected Object convertToType(String value) {
LocalDate dt = fmt.parseLocalDate(value);
return dt;
}
@Override
protected String convertToString(Object value) {
LocalDate dt = (LocalDate) value;
return fmt.print(dt);
}
}
public static class LocalTimeConverter extends BaseConverter {
private DateTimeFormatter fmt = ISODateTimeFormat.time();
@Override
public byte[] convertToNoSqlImpl(Object value) {
LocalTime dt = (LocalTime) value;
long milliseconds = dt.getMillisOfDay();
return StandardConverters.convertToBytes(milliseconds);
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
Long time = StandardConverters.convertFromBytes(Long.class, value);
LocalTime dt = LocalTime.fromMillisOfDay(time);
return dt;
}
@Override
protected Object convertToType(String value) {
LocalTime dt = fmt.parseLocalTime(value);
return dt;
}
@Override
protected String convertToString(Object value) {
LocalTime dt = (LocalTime) value;
return fmt.print(dt);
}
}
public static class DateTimeConverter extends BaseConverter {
private DateTimeFormatter fmt = ISODateTimeFormat.dateTime();
@Override
public byte[] convertToNoSqlImpl(Object value) {
DateTime dt = (DateTime) value;
long milliseconds = dt.toDate().getTime();
return StandardConverters.convertToBytes(milliseconds);
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
Long time = StandardConverters.convertFromBytes(Long.class, value);
DateTime dt = new DateTime(time);
return dt;
}
@Override
protected Object convertToType(String value) {
DateTime dt = fmt.parseDateTime(value);
return dt;
}
@Override
protected String convertToString(Object value) {
DateTime dt = (DateTime) value;
return fmt.print(dt);
}
}
public static class UUIDConverter extends BaseConverter {
@Override
public byte[] convertToNoSqlImpl(Object value) {
UUID uid = (UUID) value;
long time = uid.getTime();
long clockSeqAndNode = uid.getClockSeqAndNode();
byte[] timeArray = LONG_CONVERTER.convertToNoSql(time);
byte[] nodeArray = LONG_CONVERTER.convertToNoSql(clockSeqAndNode);
byte[] combinedUUID = new byte[timeArray.length + nodeArray.length];
System.arraycopy(timeArray,0,combinedUUID,0 ,timeArray.length);
System.arraycopy(nodeArray,0,combinedUUID,timeArray.length,nodeArray.length);
return combinedUUID;
}
@Override
public Object convertFromNoSqlImpl(byte[] value) {
try {
byte[] timeArray = new byte[8];
byte[] clockSeqAndNodeArray=new byte[8];
System.arraycopy(value,0,timeArray,0,8);
System.arraycopy(value,8,clockSeqAndNodeArray,0,8);
long time = StandardConverters.convertFromBytes(Long.class, timeArray);
long clockSeqAndNode = StandardConverters.convertFromBytes(Long.class, clockSeqAndNodeArray);
UUID ud = new UUID(time,clockSeqAndNode);
return ud;
} catch(Exception e) {
throw new RuntimeException("value in len="+value.length, e);
}
}
@Override
protected Object convertToType(String value) {
UUID ud = new UUID(value);
return ud;
}
@Override
protected String convertToString(Object value) {
return value.toString();
}
}
}