-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
UTF8StringValue.java
554 lines (505 loc) · 16.1 KB
/
UTF8StringValue.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
/*
* Copyright (c) 2002-2018 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.values.storable;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.neo4j.hashing.HashFunction;
/*
* Just as a normal StringValue but is backed by a byte array and does string
* serialization lazily when necessary.
*
*/
public final class UTF8StringValue extends StringValue
{
/** Used for removing the high order bit from byte. */
private static final int HIGH_BIT_MASK = 0b0111_1111;
/** Used for detecting non-continuation bytes. For example {@code 0b10xx_xxxx}. */
private static final int NON_CONTINUATION_BIT_MASK = 0b0100_0000;
private volatile String value;
private final byte[] bytes;
private final int offset;
private final int byteLength;
UTF8StringValue( byte[] bytes, int offset, int length )
{
assert bytes != null;
this.bytes = bytes;
this.offset = offset;
this.byteLength = length;
}
@Override
public <E extends Exception> void writeTo( ValueWriter<E> writer ) throws E
{
writer.writeUTF8( bytes, offset, byteLength );
}
@Override
public boolean equals( Value value )
{
if ( value instanceof UTF8StringValue )
{
UTF8StringValue other = (UTF8StringValue) value;
if ( byteLength != other.byteLength )
{
return false;
}
for ( int i = offset, j = other.offset; i < byteLength; i++, j++ )
{
if ( bytes[i] != other.bytes[j] )
{
return false;
}
}
return true;
}
else
{
return super.equals( value );
}
}
@Override
String value()
{
String s = value;
if ( s == null )
{
synchronized ( this )
{
s = value;
if ( s == null )
{
value = s = new String( bytes, offset, byteLength, StandardCharsets.UTF_8 );
}
}
}
return s;
}
@Override
public int length()
{
return numberOfCodePoints( bytes, offset, byteLength );
}
private static int numberOfCodePoints( byte[] bytes, int offset, int byteLength )
{
int count = 0, i = offset, len = offset + byteLength;
while ( i < len )
{
byte b = bytes[i];
//If high bit is zero (equivalent to the byte being positive in two's complement)
//we are dealing with an ascii value and use a single byte for storing the value.
if ( b >= 0 )
{
i++;
count++;
continue;
}
//The number of high bits tells us how many bytes we use to store the value
//e.g. 110xxxx -> need two bytes, 1110xxxx -> need three bytes, 11110xxx -> needs
//four bytes
while ( b < 0 )
{
i++;
b = (byte) (b << 1);
}
count++;
}
return count;
}
@Override
public int computeHash()
{
if ( bytes.length == 0 || byteLength == 0 )
{
return 0;
}
CodePointCursor cpc = new CodePointCursor();
cpc.values = bytes;
cpc.i = offset;
int hash = 1;
int len = offset + byteLength;
while ( cpc.i < len )
{
hash = 31 * hash + (int) cpc.nextCodePoint();
}
return hash;
}
@Override
public long updateHash( HashFunction hashFunction, long hash )
{
CodePointCursor cpc = new CodePointCursor();
cpc.values = bytes;
cpc.i = offset;
int len = offset + byteLength;
while ( cpc.i < len )
{
long codePointA = cpc.nextCodePoint() << 32;
long codePointB = 0L;
if ( cpc.i < len )
{
codePointB = cpc.nextCodePoint();
}
hash = hashFunction.update( hash, codePointA + codePointB );
}
return hashFunction.update( hash, cpc.codePointCount );
}
private static class CodePointCursor
{
byte[] values;
int i;
int codePointCount;
long nextCodePoint()
{
codePointCount++;
byte b = values[i];
//If high bit is zero (equivalent to the byte being positive in two's complement)
//we are dealing with an ascii value and use a single byte for storing the value.
if ( b >= 0 )
{
i++;
return b;
}
//We can now have one of three situations.
//Byte1 Byte2 Byte3 Byte4
//110xxxxx 10xxxxxx
//1110xxxx 10xxxxxx 10xxxxxx
//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//Figure out how many bytes we need by reading the number of leading bytes
int bytesNeeded = 0;
while ( b < 0 )
{
bytesNeeded++;
b = (byte) (b << 1);
}
int codePoint = codePoint( values, b, i, bytesNeeded );
i += bytesNeeded;
return codePoint;
}
}
@Override
public TextValue substring( int start, int length )
{
if ( start < 0 || length < 0 )
{
throw new IndexOutOfBoundsException( "Cannot handle negative start index nor negative length" );
}
if ( length == 0 )
{
return StringValue.EMTPY;
}
int end = start + length;
byte[] values = bytes;
int count = 0, byteStart = -1, byteEnd = -1, i = offset, len = offset + byteLength;
while ( i < len )
{
if ( count == start )
{
byteStart = i;
}
if ( count == end )
{
byteEnd = i;
break;
}
byte b = values[i];
//If high bit is zero (equivalent to the byte being positive in two's complement)
//we are dealing with an ascii value and use a single byte for storing the value.
if ( b >= 0 )
{
i++;
}
while ( b < 0 )
{
i++;
b = (byte) (b << 1);
}
count++;
}
if ( byteEnd < 0 )
{
byteEnd = len;
}
if ( byteStart < 0 )
{
return StringValue.EMTPY;
}
return new UTF8StringValue( values, byteStart, byteEnd - byteStart );
}
@Override
public TextValue trim()
{
byte[] values = bytes;
if ( values.length == 0 || byteLength == 0 )
{
return this;
}
int startIndex = trimLeftIndex();
int endIndex = trimRightIndex();
if ( startIndex > endIndex )
{
return StringValue.EMTPY;
}
return new UTF8StringValue( values, startIndex, Math.max( endIndex + 1 - startIndex, 0 ) );
}
@Override
public TextValue ltrim()
{
byte[] values = bytes;
if ( values.length == 0 || byteLength == 0 )
{
return this;
}
int startIndex = trimLeftIndex();
if ( startIndex >= values.length )
{
return StringValue.EMTPY;
}
return new UTF8StringValue( values, startIndex, values.length - startIndex );
}
@Override
public TextValue rtrim()
{
byte[] values = bytes;
if ( values.length == 0 || byteLength == 0 )
{
return this;
}
int endIndex = trimRightIndex();
if ( endIndex < 0 )
{
return StringValue.EMTPY;
}
return new UTF8StringValue( values, offset, endIndex + 1 - offset );
}
@Override
public TextValue reverse()
{
byte[] values = bytes;
if ( values.length == 0 || byteLength == 0 )
{
return StringValue.EMTPY;
}
int i = offset, len = offset + byteLength;
byte[] newValues = new byte[byteLength];
while ( i < len )
{
byte b = values[i];
//If high bit is zero (equivalent to the byte being positive in two's complement)
//we are dealing with an ascii value and use a single byte for storing the value.
if ( b >= 0 )
{
//a single byte is trivial to reverse
//just put it at the opposite end of the new array
newValues[len - 1 - i] = b;
i++;
continue;
}
//We can now have one of three situations.
//Byte1 Byte2 Byte3 Byte4
//110xxxxx 10xxxxxx
//1110xxxx 10xxxxxx 10xxxxxx
//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//Figure out how many bytes we need by reading the number of leading bytes
int bytesNeeded = 0;
while ( b < 0 )
{
bytesNeeded++;
b = (byte) (b << 1);
}
//reversing when multiple bytes are needed for the code point we cannot just reverse
//since we need to preserve the code point while moving it,
//e.g. [A, b1,b2, B] -> [B, b1,b2, A]
System.arraycopy( values, i, newValues, len - i - bytesNeeded, bytesNeeded );
i += bytesNeeded;
}
return new UTF8StringValue( newValues, 0, newValues.length );
}
@Override
public int compareTo( TextValue other )
{
if ( !(other instanceof UTF8StringValue) )
{
return super.compareTo( other );
}
UTF8StringValue otherUTF8 = (UTF8StringValue) other;
return byteArrayCompare( bytes, offset, byteLength, otherUTF8.bytes, otherUTF8.offset, otherUTF8.byteLength );
}
public static int byteArrayCompare( byte[] value1, byte[] value2 )
{
return byteArrayCompare( value1, 0, value1.length, value2, 0, value2.length );
}
public static int byteArrayCompare( byte[] value1, int value1Offset, int value1Length,
byte[] value2, int value2Offset, int value2Length )
{
int len1 = value1Length;
int len2 = value2Length;
int lim = Math.min( len1, len2 );
int i = 0;
while ( i < lim )
{
int b1 = ((int) value1[i + value1Offset]) & 0xFF;
int b2 = ((int) value2[i + value2Offset]) & 0xFF;
if ( b1 != b2 )
{
return b1 - b2;
}
i++;
}
return len1 - len2;
}
@Override
Matcher matcher( Pattern pattern )
{
return pattern.matcher( value() ); // TODO: can we do better here?
}
private static int codePointAt( byte[] bytes, int i )
{
assert i < bytes.length;
byte b = bytes[i];
if ( b >= 0 )
{
return b;
}
int bytesNeeded = 0;
while ( b < 0 )
{
bytesNeeded++;
b = (byte) (b << 1);
}
switch ( bytesNeeded )
{
case 2:
return (b << 4) | (bytes[i + 1] & HIGH_BIT_MASK);
case 3:
return (b << 9) | ((bytes[i + 1] & HIGH_BIT_MASK) << 6) | (bytes[i + 2] & HIGH_BIT_MASK);
case 4:
return (b << 14) | ((bytes[i + 1] & HIGH_BIT_MASK) << 12) |
((bytes[i + 2] & HIGH_BIT_MASK) << 6)
| (bytes[i + 3] & HIGH_BIT_MASK);
default:
throw new IllegalArgumentException( "Malformed UTF8 value " + bytesNeeded );
}
}
/**
* Returns the left-most index into the underlying byte array that does not belong to a whitespace code point
*/
private int trimLeftIndex()
{
int i = offset, len = offset + byteLength;
while ( i < len )
{
byte b = bytes[i];
//If high bit is zero (equivalent to the byte being positive in two's complement)
//we are dealing with an ascii value and use a single byte for storing the value.
if ( b >= 0 )
{
if ( !Character.isWhitespace( b ) )
{
return i;
}
i++;
continue;
}
//We can now have one of three situations.
//Byte1 Byte2 Byte3 Byte4
//110xxxxx 10xxxxxx
//1110xxxx 10xxxxxx 10xxxxxx
//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//Figure out how many bytes we need by reading the number of leading bytes
int bytesNeeded = 0;
while ( b < 0 )
{
bytesNeeded++;
b = (byte) (b << 1);
}
int codePoint = codePoint( bytes, b, i, bytesNeeded );
if ( !Character.isWhitespace( codePoint ) )
{
return i;
}
i += bytesNeeded;
}
return i;
}
/**
* Returns the right-most index into the underlying byte array that does not belong to a whitespace code point
*/
private int trimRightIndex()
{
int index = offset + byteLength - 1;
while ( index >= 0 )
{
byte b = bytes[index];
//If high bit is zero (equivalent to the byte being positive in two's complement)
//we are dealing with an ascii value and use a single byte for storing the value.
if ( b >= 0 )
{
if ( !Character.isWhitespace( b ) )
{
return index;
}
index--;
continue;
}
//We can now have one of three situations.
//Byte1 Byte2 Byte3 Byte4
//110xxxxx 10xxxxxx
//1110xxxx 10xxxxxx 10xxxxxx
//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
int bytesNeeded = 1;
while ( (b & NON_CONTINUATION_BIT_MASK) == 0 )
{
bytesNeeded++;
b = bytes[--index];
}
int codePoint = codePoint( bytes, (byte) (b << bytesNeeded), index, bytesNeeded );
if ( !Character.isWhitespace( codePoint ) )
{
return Math.min( index + bytesNeeded - 1, bytes.length - 1 );
}
index--;
}
return index;
}
public byte[] bytes()
{
return bytes;
}
static int codePoint( byte[] bytes, byte currentByte, int i, int bytesNeeded )
{
int codePoint;
byte[] values = bytes;
switch ( bytesNeeded )
{
case 2:
codePoint = (currentByte << 4) | (values[i + 1] & HIGH_BIT_MASK);
break;
case 3:
codePoint = (currentByte << 9) | ((values[i + 1] & HIGH_BIT_MASK) << 6) | (values[i + 2] & HIGH_BIT_MASK);
break;
case 4:
codePoint = (currentByte << 14) | ((values[i + 1] & HIGH_BIT_MASK) << 12) |
((values[i + 2] & HIGH_BIT_MASK) << 6)
| (values[i + 3] & HIGH_BIT_MASK);
break;
default:
throw new IllegalArgumentException( "Malformed UTF8 value" );
}
return codePoint;
}
}