-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StorePropertyPayloadCursor.java
364 lines (326 loc) · 11.5 KB
/
StorePropertyPayloadCursor.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
/*
* Copyright (c) 2002-2015 "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.kernel.impl.api.store;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;
import org.neo4j.cursor.GenericCursor;
import org.neo4j.helpers.UTF8;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.impl.store.AbstractDynamicStore;
import org.neo4j.kernel.impl.store.DynamicArrayStore;
import org.neo4j.kernel.impl.store.DynamicStringStore;
import org.neo4j.kernel.impl.store.LongerShortString;
import org.neo4j.kernel.impl.store.PropertyStore;
import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.ShortArray;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;
import org.neo4j.kernel.impl.store.record.DynamicRecord;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.util.Bits;
import static org.neo4j.kernel.impl.store.PropertyType.ARRAY;
import static org.neo4j.kernel.impl.store.PropertyType.BOOL;
import static org.neo4j.kernel.impl.store.PropertyType.BYTE;
import static org.neo4j.kernel.impl.store.PropertyType.CHAR;
import static org.neo4j.kernel.impl.store.PropertyType.DOUBLE;
import static org.neo4j.kernel.impl.store.PropertyType.FLOAT;
import static org.neo4j.kernel.impl.store.PropertyType.INT;
import static org.neo4j.kernel.impl.store.PropertyType.LONG;
import static org.neo4j.kernel.impl.store.PropertyType.SHORT;
import static org.neo4j.kernel.impl.store.PropertyType.SHORT_ARRAY;
import static org.neo4j.kernel.impl.store.PropertyType.SHORT_STRING;
import static org.neo4j.kernel.impl.store.PropertyType.STRING;
/**
* Cursor that provides a view on property blocks of a particular property record.
* This cursor is reusable and can be re-initialized with
* {@link #init(PageCursor)} method and cleaned up using {@link #clear()} method.
* <p/>
* During initialization {@link #MAX_NUMBER_OF_PAYLOAD_LONG_ARRAY} number of longs is read from
* the given {@linkplain PageCursor}. This is done eagerly to avoid reading property blocks from different versions
* of the page.
* <p/>
* Internally, this cursor is mainly an array of {@link #MAX_NUMBER_OF_PAYLOAD_LONG_ARRAY} and a current-position
* pointer.
*/
class StorePropertyPayloadCursor
{
static final int MAX_NUMBER_OF_PAYLOAD_LONG_ARRAY = PropertyStore.DEFAULT_PAYLOAD_SIZE / 8;
private static final long PROPERTY_KEY_ID_BITMASK = 0xFFFFFFL;
private static final int MAX_BYTES_IN_SHORT_STRING_OR_SHORT_ARRAY = 32;
private static final int INTERNAL_BYTE_ARRAY_SIZE = 4096;
private static final int INITIAL_POSITION = -1;
/**
* Reusable initial buffer for reading of dynamic records.
*/
private final ByteBuffer cachedBuffer = ByteBuffer.allocate( INTERNAL_BYTE_ARRAY_SIZE );
private final DynamicStringStore stringStore;
private final DynamicArrayStore arrayStore;
private AbstractDynamicStore.DynamicRecordCursor stringRecordCursor;
private AbstractDynamicStore.DynamicRecordCursor arrayRecordCursor;
private ByteBuffer buffer = cachedBuffer;
private final long[] data = new long[MAX_NUMBER_OF_PAYLOAD_LONG_ARRAY];
private int position = INITIAL_POSITION;
StorePropertyPayloadCursor( DynamicStringStore stringStore, DynamicArrayStore arrayStore )
{
this.stringStore = stringStore;
this.arrayStore = arrayStore;
}
void init( PageCursor cursor )
{
for ( int i = 0; i < data.length; i++ )
{
data[i] = cursor.getLong();
}
}
void clear()
{
position = INITIAL_POSITION;
buffer = cachedBuffer;
// Array of data should be filled with '0' because it is possible to call next() without calling init().
// In such case 'false' should be returned, which might not be the case if there is stale data in the buffer.
Arrays.fill( data, 0 );
}
boolean next()
{
if ( position == INITIAL_POSITION )
{
position = 0;
}
else
{
position += currentBlocksUsed();
}
if ( position >= data.length )
{
return false;
}
return type() != null;
}
PropertyType type()
{
return PropertyType.getPropertyType( currentHeader(), true );
}
int propertyKeyId()
{
return (int) (currentHeader() & PROPERTY_KEY_ID_BITMASK);
}
boolean booleanValue()
{
assertOfType( BOOL );
return PropertyBlock.fetchByte( currentHeader() ) == 1;
}
byte byteValue()
{
assertOfType( BYTE );
return PropertyBlock.fetchByte( currentHeader() );
}
short shortValue()
{
assertOfType( SHORT );
return PropertyBlock.fetchShort( currentHeader() );
}
char charValue()
{
assertOfType( CHAR );
return (char) PropertyBlock.fetchShort( currentHeader() );
}
int intValue()
{
assertOfType( INT );
return PropertyBlock.fetchInt( currentHeader() );
}
float floatValue()
{
assertOfType( FLOAT );
return Float.intBitsToFloat( PropertyBlock.fetchInt( currentHeader() ) );
}
long longValue()
{
assertOfType( LONG );
if ( PropertyBlock.valueIsInlined( currentHeader() ) )
{
return PropertyBlock.fetchLong( currentHeader() ) >>> 1;
}
return data[position + 1];
}
double doubleValue()
{
assertOfType( DOUBLE );
return Double.longBitsToDouble( data[position + 1] );
}
String shortStringValue()
{
assertOfType( SHORT_STRING );
Bits bits = valueAsBits();
return LongerShortString.decode( bits );
}
String stringValue()
{
assertOfType( STRING );
try
{
if ( stringRecordCursor == null )
{
stringRecordCursor = stringStore.newDynamicRecordCursor();
}
readFromStore( stringStore, stringRecordCursor );
}
catch ( IOException e )
{
throw new UnderlyingStorageException( "Unable to read string value", e );
}
buffer.flip();
return UTF8.decode( buffer.array(), 0, buffer.limit() );
}
Object shortArrayValue()
{
assertOfType( SHORT_ARRAY );
Bits bits = valueAsBits();
return ShortArray.decode( bits );
}
Object arrayValue()
{
assertOfType( ARRAY );
try
{
if ( arrayRecordCursor == null )
{
arrayRecordCursor = arrayStore.newDynamicRecordCursor();
}
readFromStore( arrayStore, arrayRecordCursor );
}
catch ( IOException e )
{
throw new UnderlyingStorageException( "Unable to read array value", e );
}
buffer.flip();
return readArrayFromBuffer( buffer );
}
private long currentHeader()
{
return data[position];
}
private int currentBlocksUsed()
{
return type().calculateNumberOfBlocksUsed( currentHeader() );
}
private Bits valueAsBits()
{
Bits bits = Bits.bits( MAX_BYTES_IN_SHORT_STRING_OR_SHORT_ARRAY );
int blocksUsed = currentBlocksUsed();
for ( int i = 0; i < blocksUsed; i++ )
{
bits.put( data[position + i] );
}
return bits;
}
private void readFromStore( AbstractDynamicStore store, AbstractDynamicStore.DynamicRecordCursor cursor )
throws IOException
{
buffer.clear();
long startBlockId = PropertyBlock.fetchLong( currentHeader() );
try ( GenericCursor<DynamicRecord> records = store.getRecordsCursor( startBlockId, true, cursor ) )
{
while ( records.next() )
{
DynamicRecord dynamicRecord = records.get();
byte[] data = dynamicRecord.getData();
if ( buffer.remaining() < data.length )
{
buffer.flip();
ByteBuffer newBuffer = newBiggerBuffer( data.length );
newBuffer.put( buffer );
buffer = newBuffer;
}
buffer.put( data, 0, data.length );
}
}
}
private ByteBuffer newBiggerBuffer( int requiredCapacity )
{
int newCapacity;
do
{
newCapacity = buffer.capacity() * 2;
}
while ( newCapacity - buffer.limit() < requiredCapacity );
return ByteBuffer.allocate( newCapacity ).order( ByteOrder.LITTLE_ENDIAN );
}
private static Object readArrayFromBuffer( ByteBuffer buffer )
{
if ( buffer.limit() <= 0 )
{
throw new IllegalStateException( "Given buffer is empty" );
}
byte typeId = buffer.get();
buffer.order( ByteOrder.BIG_ENDIAN );
try
{
if ( typeId == PropertyType.STRING.intValue() )
{
int arrayLength = buffer.getInt();
String[] result = new String[arrayLength];
for ( int i = 0; i < arrayLength; i++ )
{
int byteLength = buffer.getInt();
result[i] = UTF8.decode( buffer.array(), buffer.position(), byteLength );
buffer.position( buffer.position() + byteLength );
}
return result;
}
else
{
ShortArray type = ShortArray.typeOf( typeId );
int bitsUsedInLastByte = buffer.get();
int requiredBits = buffer.get();
if ( requiredBits == 0 )
{
return type.createEmptyArray();
}
Object result;
if ( type == ShortArray.BYTE && requiredBits == Byte.SIZE )
{ // Optimization for byte arrays (probably large ones)
byte[] byteArray = new byte[buffer.limit() - buffer.position()];
buffer.get( byteArray );
result = byteArray;
}
else
{ // Fallback to the generic approach, which is a slower
Bits bits = Bits.bitsFromBytes( buffer.array(), buffer.position() );
int length = ((buffer.limit() - buffer.position()) * 8 - (8 - bitsUsedInLastByte)) / requiredBits;
result = type.createArray( length, bits, requiredBits );
}
return result;
}
}
finally
{
buffer.order( ByteOrder.LITTLE_ENDIAN );
}
}
private void assertOfType( PropertyType expected )
{
if ( type() != expected )
{
throw new IllegalStateException( "Expected type " + expected + " but was " + type() );
}
}
}