-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Layout.java
152 lines (134 loc) · 4.99 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
/*
* Copyright (c) 2002-2016 "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.gbptree;
import java.util.Comparator;
import org.neo4j.io.pagecache.PageCursor;
/**
* 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}.
*
* @param <KEY> type of key
* @param <VALUE> type of value
*/
public interface Layout<KEY,VALUE> extends Comparator<KEY>
{
/**
* @return new key instances.
*/
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.
*/
void writeMetaData( PageCursor cursor );
/**
* 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.
*
* @param cursor {@link PageCursor} to read from, at its current offset.
* @throws IllegalArgumentException if read meta data doesn't match with the meta data provided in constructor.
*/
void readMetaData( PageCursor cursor );
/**
* Utility method for generating an {@link #identifier()}.
*/
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 ( int i = 0; i < chars.length; i++ )
{
byte byteValue = (byte) (((byte) chars[i]) ^ ((byte) (chars[i] >> 8)));
upperInt <<= 8;
upperInt |= (byteValue & 0xFF);
}
return upperInt << Integer.SIZE | (checksum & 0xFFFFFFFF);
}
}