-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Layout.java
290 lines (255 loc) · 9.73 KB
/
Layout.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
/*
* 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.index.internal.gbptree;
import java.io.File;
import java.util.Comparator;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.PageCursor;
import static java.lang.String.format;
/**
* Main point of interaction for customizing a {@link GBPTree}, how its keys and values are represented
* as bytes and what keys and values contains.
* <p>
* Additionally custom meta data can be supplied, which will be persisted in {@link GBPTree}.
* <p>
* Rather extend {@link Adapter} as to get standard implementation of e.g. {@link Adapter#toString()}.
*
* @param <KEY> type of key
* @param <VALUE> type of value
*/
public interface Layout<KEY, VALUE> extends Comparator<KEY>
{
/**
* @return new key instance.
*/
KEY newKey();
/**
* Copies contents of {@code key} to {@code into}.
*
* @param key key (left unchanged as part of this call) to copy contents from.
* @param into key (changed as part of this call) to copy contents into.
* @return the provided {@code into} instance for convenience.
*/
KEY copyKey( KEY key, KEY into );
/**
* @return new value instance.
*/
VALUE newValue();
/**
* @return size, in bytes, of a key.
*/
int keySize();
/**
* @return size, in bytes, of a value.
*/
int valueSize();
/**
* Writes contents of {@code key} into {@code cursor} at its current offset.
*
* @param cursor {@link PageCursor} to write into, at current offset.
* @param key key containing data to write.
*/
void writeKey( PageCursor cursor, KEY key );
/**
* Writes contents of {@code value} into {@code cursor} at its current offset.
*
* @param cursor {@link PageCursor} to write into, at current offset.
* @param value value containing data to write.
*/
void writeValue( PageCursor cursor, VALUE value );
/**
* Reads key contents at {@code cursor} at its current offset into {@code key}.
*
* @param cursor {@link PageCursor} to read from, at current offset.
* @param into key instances to read into.
*/
void readKey( PageCursor cursor, KEY into );
/**
* Reads value contents at {@code cursor} at its current offset into {@code value}.
*
* @param cursor {@link PageCursor} to read from, at current offset.
* @param into value instances to read into.
*/
void readValue( PageCursor cursor, VALUE into );
/**
* Used as a checksum for when loading an index after creation, to verify that the same layout is used,
* as the one it was initially created with.
*
* @return a long acting as an identifier, written in the header of an index.
*/
long identifier();
/**
* @return major version of layout. Will be compared to version written into meta page when opening index.
*/
int majorVersion();
/**
* @return minor version of layout. Will be compared to version written into meta page when opening index.
*/
int minorVersion();
/**
* Writes meta data specific to this layout instance to {@code cursor} at its current offset.
*
* @param cursor {@link PageCursor} to write into, at its current offset.
*/
default void writeMetaData( PageCursor cursor )
{ // no meta-data by default
}
/**
* Reads meta data specific to this layout instance from {@code cursor} at its current offset.
* The read meta data must also be verified against meta data provided in constructor of this Layout.
* Constructor-provided meta data can be {@code null} to skip this verification.
* if read meta data doesn't match with the meta data provided in constructor
* {@link PageCursor#setCursorException(String)} should be called with appropriate error message.
*
* @param cursor {@link PageCursor} to read from, at its current offset.
*/
default void readMetaData( PageCursor cursor )
{ // no meta-data by default
}
/**
* Utility method for generating an {@link #identifier()}.
*
* @param name name to be part of this identifier, must at most be 4 characters.
* @param checksum checksum to include into the identifier.
* @return a long which is a combination of {@code name} and {@code checksum}.
*/
static long namedIdentifier( String name, int checksum )
{
char[] chars = name.toCharArray();
if ( chars.length > 4 )
{
throw new IllegalArgumentException( "Maximum 4 character name, was '" + name + "'" );
}
long upperInt = 0;
for ( char aChar : chars )
{
byte byteValue = (byte) (((byte) aChar) ^ ((byte) (aChar >> 8)));
upperInt <<= 8;
upperInt |= byteValue & 0xFF;
}
return (upperInt << Integer.SIZE) | checksum;
}
/**
* Typically, layout is compatible with given identifier, major and minor version if
* <ul>
* <li>{@code layoutIdentifier == this.identifier()}</li>
* <li>{@code majorVersion == this.majorVersion()}</li>
* <li>{@code minorVersion == this.minorVersion()}</li>
* </ul>
* <p>
* Edge case: When only want to {@link GBPTree#readHeader(PageCache, File, Layout, Header.Reader)} read header} of index and not
* actually use it there may be multiple different combinations that is considered to be valid.
*
* @param layoutIdentifier the stored layout identifier we want to check compatibility against.
* @param majorVersion the stored major version we want to check compatibility against.
* @param minorVersion the stored minor version we want to check compatibility against.
* @return true if this layout is compatible with combination of identifier, major and minor version, false otherwise.
*/
boolean compatibleWith( long layoutIdentifier, int majorVersion, int minorVersion );
/**
* Adapter for {@link Layout}, which contains convenient standard implementations of some methods.
*
* @param <KEY> type of key
* @param <VALUE> type of value
*/
abstract class Adapter<KEY, VALUE> implements Layout<KEY,VALUE>
{
@Override
public String toString()
{
return format( "%s[version:%d.%d, identifier:%d, keySize:%d, valueSize:%d]",
getClass().getSimpleName(), majorVersion(), minorVersion(), identifier(),
keySize(), valueSize() );
}
@Override
public boolean compatibleWith( long layoutIdentifier, int majorVersion, int minorVersion )
{
return layoutIdentifier == identifier() && majorVersion == majorVersion() && minorVersion == minorVersion();
}
}
abstract class ReadOnlyMetaLayout<KEY, VALUE> implements Layout<KEY,VALUE>
{
@Override
public KEY newKey()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public KEY copyKey( KEY key, KEY into )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public VALUE newValue()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public int keySize()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public int valueSize()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public void writeKey( PageCursor cursor, KEY key )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public void writeValue( PageCursor cursor, VALUE value )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public void readKey( PageCursor cursor, KEY into )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public void readValue( PageCursor cursor, VALUE into )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public long identifier()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public int majorVersion()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public int minorVersion()
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
@Override
public int compare( KEY o1, KEY o2 )
{
throw new UnsupportedOperationException( "Not allowed with read only layout" );
}
}
}