-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SimpleLongLayout.java
277 lines (237 loc) · 6.85 KB
/
SimpleLongLayout.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
/*
* 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.index.internal.gbptree;
import org.apache.commons.lang3.mutable.MutableLong;
import org.neo4j.io.pagecache.PageCursor;
import static java.nio.charset.StandardCharsets.UTF_8;
class SimpleLongLayout extends TestLayout<MutableLong,MutableLong>
{
private final int keyPadding;
private String customNameAsMetaData;
private final boolean fixedSize;
private final int identifier;
private final int majorVersion;
private final int minorVersion;
static class Builder
{
private int keyPadding;
private int identifier = 999;
private int majorVersion;
private int minorVersion;
private String customNameAsMetaData = "test";
private boolean fixedSize = true;
Builder withKeyPadding( int keyPadding )
{
this.keyPadding = keyPadding;
return this;
}
Builder withIdentifier( int identifier )
{
this.identifier = identifier;
return this;
}
Builder withMajorVersion( int majorVersion )
{
this.majorVersion = majorVersion;
return this;
}
Builder withMinorVersion( int minorVersion )
{
this.minorVersion = minorVersion;
return this;
}
Builder withCustomerNameAsMetaData( String customNameAsMetaData )
{
this.customNameAsMetaData = customNameAsMetaData;
return this;
}
Builder withFixedSize( boolean fixedSize )
{
this.fixedSize = fixedSize;
return this;
}
SimpleLongLayout build()
{
return new SimpleLongLayout( keyPadding, customNameAsMetaData, fixedSize, identifier, majorVersion, minorVersion );
}
}
static Builder longLayout()
{
return new Builder();
}
SimpleLongLayout( int keyPadding, String customNameAsMetaData, boolean fixedSize, int identifier, int majorVersion, int minorVersion )
{
this.keyPadding = keyPadding;
this.customNameAsMetaData = customNameAsMetaData;
this.fixedSize = fixedSize;
this.identifier = identifier;
this.majorVersion = majorVersion;
this.minorVersion = minorVersion;
}
@Override
public int compare( MutableLong o1, MutableLong o2 )
{
return Long.compare( o1.longValue(), o2.longValue() );
}
@Override
int compareValue( MutableLong v1, MutableLong v2 )
{
return compare( v1, v2 );
}
@Override
public MutableLong newKey()
{
return new MutableLong();
}
@Override
public MutableLong copyKey( MutableLong key, MutableLong into )
{
into.setValue( key.longValue() );
return into;
}
@Override
public MutableLong newValue()
{
return new MutableLong();
}
@Override
public int keySize( MutableLong key )
{
// pad the key here to affect the max key count, useful to get odd or even max key count
return Long.BYTES + keyPadding;
}
@Override
public int valueSize( MutableLong value )
{
return Long.BYTES;
}
@Override
public void writeKey( PageCursor cursor, MutableLong key )
{
cursor.putLong( key.longValue() );
}
@Override
public void writeValue( PageCursor cursor, MutableLong value )
{
cursor.putLong( value.longValue() );
}
@Override
public void readKey( PageCursor cursor, MutableLong into, int keySize )
{
into.setValue( cursor.getLong() );
}
@Override
public void readValue( PageCursor cursor, MutableLong into, int valueSize )
{
into.setValue( cursor.getLong() );
}
@Override
public boolean fixedSize()
{
return fixedSize;
}
@Override
public long identifier()
{
return identifier;
}
@Override
public int majorVersion()
{
return majorVersion;
}
@Override
public int minorVersion()
{
return minorVersion;
}
@Override
public void writeMetaData( PageCursor cursor )
{
writeString( cursor, customNameAsMetaData );
cursor.putInt( keyPadding );
}
private static void writeString( PageCursor cursor, String string )
{
byte[] bytes = string.getBytes( UTF_8 );
cursor.putInt( string.length() );
cursor.putBytes( bytes );
}
@Override
public void readMetaData( PageCursor cursor )
{
String name = readString( cursor );
if ( name == null )
{
return;
}
if ( customNameAsMetaData != null )
{
if ( !name.equals( customNameAsMetaData ) )
{
cursor.setCursorException( "Name '" + name +
"' doesn't match expected '" + customNameAsMetaData + "'" );
return;
}
}
customNameAsMetaData = name;
int readKeyPadding = cursor.getInt();
if ( readKeyPadding != keyPadding )
{
cursor.setCursorException( "Key padding " + readKeyPadding + " doesn't match expected " + keyPadding );
}
}
private static String readString( PageCursor cursor )
{
int length = cursor.getInt();
if ( length < 0 || length >= cursor.getCurrentPageSize() )
{
cursor.setCursorException( "Unexpected length of string " + length );
return null;
}
byte[] bytes = new byte[length];
cursor.getBytes( bytes );
return new String( bytes, UTF_8 );
}
@Override
public MutableLong key( long seed )
{
MutableLong key = newKey();
key.setValue( seed );
return key;
}
@Override
public MutableLong value( long seed )
{
MutableLong value = newValue();
value.setValue( seed );
return value;
}
@Override
public long keySeed( MutableLong key )
{
return key.getValue();
}
@Override
public long valueSeed( MutableLong value )
{
return value.getValue();
}
}