-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SchemaNumberKey.java
177 lines (157 loc) · 5.46 KB
/
SchemaNumberKey.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
/*
* 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.index.schema;
import org.neo4j.index.internal.gbptree.GBPTree;
import org.neo4j.values.NumberValue;
import org.neo4j.values.Value;
import org.neo4j.values.ValueWriter;
import org.neo4j.values.Values;
import static java.lang.String.format;
/**
* Includes value and entity id (to be able to handle non-unique values).
* A value can be any {@link Number} and is represented as a {@code long} to store the raw bits and a type
* to say if it's a long, double or float.
*
* Distinction between double and float exists because coersions between each other and long may differ.
* TODO this should be figured out and potentially reduced to long, double types only.
*/
class SchemaNumberKey extends ValueWriter.Adapter
{
static final int SIZE =
Byte.BYTES + /* type of value */
Long.BYTES + /* raw value bits */
// TODO this could use 6 bytes instead and have the highest 2 bits stored in the type byte
Long.BYTES; /* entityId */
byte type;
long rawValueBits;
long entityId;
/**
* Marks that comparisons with this key requires also comparing entityId, this allows functionality
* of inclusive/exclusive bounds of range queries.
* This is because {@link GBPTree} only support from inclusive and to exclusive.
* <p>
* Note that {@code entityIdIsSpecialTieBreaker} is only an in memory state.
*/
boolean entityIdIsSpecialTieBreaker;
void from( long entityId, Value... values )
{
extractRawBitsAndType( assertValidSingleNumber( values ) );
this.entityId = entityId;
entityIdIsSpecialTieBreaker = false;
}
private static NumberValue assertValidSingleNumber( Value... values )
{
// TODO: support multiple values, right?
if ( values.length > 1 )
{
throw new IllegalArgumentException( "Tried to create composite key with non-composite schema key layout" );
}
if ( values.length < 1 )
{
throw new IllegalArgumentException( "Tried to create key without value" );
}
if ( !Values.isNumberValue( values[0] ) )
{
throw new IllegalArgumentException(
"Key layout does only support numbers, tried to create key from " + values[0] );
}
return (NumberValue) values[0];
}
String propertiesAsString()
{
return RawBits.asNumberValue( rawValueBits, type ).toString();
}
void initAsLowest()
{
rawValueBits = Double.doubleToLongBits( Double.NEGATIVE_INFINITY );
type = RawBits.DOUBLE;
entityId = Long.MIN_VALUE;
entityIdIsSpecialTieBreaker = true;
}
void initAsHighest()
{
rawValueBits = Double.doubleToLongBits( Double.POSITIVE_INFINITY );
type = RawBits.DOUBLE;
entityId = Long.MAX_VALUE;
entityIdIsSpecialTieBreaker = true;
}
/**
* Compares the value of this key to that of another key.
* This method is expected to be called in scenarios where inconsistent reads may happen (and later retried).
*
* @param other the {@link SchemaNumberKey} to compare to.
* @return comparison against the {@code other} {@link SchemaNumberKey}.
*/
int compareValueTo( SchemaNumberKey other )
{
return RawBits.compare( rawValueBits, type, other.rawValueBits, other.type );
}
/**
* Extracts raw bits and type from a {@link NumberValue} and store as state of this {@link SchemaNumberKey} instance.
*
* @param value actual {@link NumberValue} value.
*/
private void extractRawBitsAndType( NumberValue value )
{
value.writeTo( this );
}
@Override
public String toString()
{
return format( "type=%d,rawValue=%d,value=%s,entityId=%d",
type, rawValueBits, RawBits.asNumberValue( rawValueBits, type ), entityId );
}
@Override
public void writeInteger( byte value )
{
type = RawBits.BYTE;
rawValueBits = value;
}
@Override
public void writeInteger( short value )
{
type = RawBits.SHORT;
rawValueBits = value;
}
@Override
public void writeInteger( int value )
{
type = RawBits.INT;
rawValueBits = value;
}
@Override
public void writeInteger( long value )
{
type = RawBits.LONG;
rawValueBits = value;
}
@Override
public void writeFloatingPoint( float value )
{
type = RawBits.FLOAT;
rawValueBits = Float.floatToIntBits( value );
}
@Override
public void writeFloatingPoint( double value )
{
type = RawBits.DOUBLE;
rawValueBits = Double.doubleToLongBits( value );
}
}