-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PropertyType.java
323 lines (296 loc) · 8.71 KB
/
PropertyType.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
/*
* Copyright (c) 2002-2017 "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.store;
import java.util.Arrays;
import org.neo4j.kernel.impl.store.format.standard.PropertyRecordFormat;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.Values;
/**
* Defines valid property types.
*/
@SuppressWarnings( "UnnecessaryBoxing" )
public enum PropertyType
{
BOOL( 1 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.booleanValue( getValue( block.getSingleValueLong() ) );
}
private boolean getValue( long propBlock )
{
return ( propBlock & 0x1 ) == 1;
}
},
BYTE( 2 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.byteValue( block.getSingleValueByte() );
}
},
SHORT( 3 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.shortValue( block.getSingleValueShort() );
}
},
CHAR( 4 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.charValue( (char) block.getSingleValueShort() );
}
},
INT( 5 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.intValue( block.getSingleValueInt() );
}
},
LONG( 6 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
long firstBlock = block.getSingleValueBlock();
long value = valueIsInlined( firstBlock ) ? (block.getSingleValueLong() >>> 1) : block.getValueBlocks()[1];
return Values.longValue( value );
}
private boolean valueIsInlined( long firstBlock )
{
// [][][][][ i,tttt][kkkk,kkkk][kkkk,kkkk][kkkk,kkkk]
return (firstBlock & 0x10000000L) > 0;
}
@Override
public int calculateNumberOfBlocksUsed( long firstBlock )
{
return valueIsInlined( firstBlock ) ? 1 : 2;
}
},
FLOAT( 7 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.floatValue( Float.intBitsToFloat( block.getSingleValueInt() ) );
}
},
DOUBLE( 8 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.doubleValue( Double.longBitsToDouble( block.getValueBlocks()[1] ) );
}
@Override
public int calculateNumberOfBlocksUsed( long firstBlock )
{
return 2;
}
},
STRING( 9 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return Values.stringValue( store.getStringFor( block ) );
}
@Override
public byte[] readDynamicRecordHeader( byte[] recordBytes )
{
return EMPTY_BYTE_ARRAY;
}
},
ARRAY( 10 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return store.getArrayFor( block );
}
@Override
public byte[] readDynamicRecordHeader( byte[] recordBytes )
{
byte itemType = recordBytes[0];
if ( itemType == STRING.byteValue() )
{
return headOf( recordBytes, DynamicArrayStore.STRING_HEADER_SIZE );
}
else if ( itemType <= DOUBLE.byteValue() )
{
return headOf( recordBytes, DynamicArrayStore.NUMBER_HEADER_SIZE );
}
else if ( itemType <= GEOMETRY.byteValue() )
{
return headOf( recordBytes, DynamicArrayStore.GEOMETRY_HEADER_SIZE );
}
throw new IllegalArgumentException( "Unknown array type " + itemType );
}
private byte[] headOf( byte[] bytes, int length )
{
return Arrays.copyOf( bytes, length );
}
},
SHORT_STRING( 11 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return LongerShortString.decode( block );
}
@Override
public int calculateNumberOfBlocksUsed( long firstBlock )
{
return LongerShortString.calculateNumberOfBlocksUsed( firstBlock );
}
},
SHORT_ARRAY( 12 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return ShortArray.decode( block );
}
@Override
public int calculateNumberOfBlocksUsed( long firstBlock )
{
return ShortArray.calculateNumberOfBlocksUsed( firstBlock );
}
},
GEOMETRY( 13 )
{
@Override
public Value value( PropertyBlock block, PropertyStore store )
{
return GeometryType.decode( block );
}
@Override
public int calculateNumberOfBlocksUsed( long firstBlock )
{
return GeometryType.calculateNumberOfBlocksUsed( firstBlock );
}
};
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final int BLOCKS_USED_FOR_BAD_TYPE_OR_ENCODING = -1;
// TODO In wait of a better place
private static int payloadSize = PropertyRecordFormat.DEFAULT_PAYLOAD_SIZE;
private final int type;
PropertyType( int type )
{
this.type = type;
}
/**
* Returns an int value representing the type.
*
* @return The int value for this property type
*/
public int intValue()
{
return type;
}
/**
* Returns a byte value representing the type. As long as there are
* < 128 PropertyTypes, this should be equal to intValue(). When this
* statement no longer holds, this method should be removed.
*
* @return The byte value for this property type
*/
public byte byteValue()
{
return (byte) type;
}
public abstract Value value( PropertyBlock block, PropertyStore store );
public static PropertyType getPropertyTypeOrNull( long propBlock )
{
// [][][][][ ,tttt][kkkk,kkkk][kkkk,kkkk][kkkk,kkkk]
int type = typeIdentifier( propBlock );
switch ( type )
{
case 1:
return BOOL;
case 2:
return BYTE;
case 3:
return SHORT;
case 4:
return CHAR;
case 5:
return INT;
case 6:
return LONG;
case 7:
return FLOAT;
case 8:
return DOUBLE;
case 9:
return STRING;
case 10:
return ARRAY;
case 11:
return SHORT_STRING;
case 12:
return SHORT_ARRAY;
case 13:
return GEOMETRY;
default:
return null;
}
}
private static int typeIdentifier( long propBlock )
{
return (int) ((propBlock & 0x000000000F000000L) >> 24);
}
public static PropertyType getPropertyTypeOrThrow( long propBlock )
{
PropertyType type = getPropertyTypeOrNull( propBlock );
if ( type == null )
{
throw new InvalidRecordException( "Unknown property type for type " + typeIdentifier( propBlock ) );
}
return type;
}
// TODO In wait of a better place
public static int getPayloadSize()
{
return payloadSize;
}
// TODO In wait of a better place
public static int getPayloadSizeLongs()
{
return payloadSize >>> 3;
}
public int calculateNumberOfBlocksUsed( long firstBlock )
{
return 1;
}
public byte[] readDynamicRecordHeader( byte[] recordBytes )
{
throw new UnsupportedOperationException();
}
}