-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FusionIndexBase.java
184 lines (173 loc) · 6.51 KB
/
FusionIndexBase.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
/*
* 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.kernel.impl.index.schema.fusion;
import java.lang.reflect.Array;
import java.util.Arrays;
import org.neo4j.collection.primitive.PrimitiveIntCollections;
import org.neo4j.function.ThrowingConsumer;
import org.neo4j.function.ThrowingFunction;
import org.neo4j.kernel.api.index.IndexProvider;
/**
* Acting as a simplifier for the multiplexing that is going in inside a fusion index. A fusion index consists of multiple parts,
* each handling one or more value groups. Each instance, be it a reader, populator or accessor should extend this class
* to get that multiplexing at a low cost. All parts will live in an array with specific slot constants to each specific part.
*
* @param <T> type of instances
*/
public abstract class FusionIndexBase<T>
{
static final int INSTANCE_COUNT = 5;
static final int STRING = 0;
static final int NUMBER = 1;
static final int SPATIAL = 2;
static final int TEMPORAL = 3;
static final int LUCENE = 4;
final T[] instances;
final FusionIndexProvider.Selector selector;
FusionIndexBase( T[] instances, FusionIndexProvider.Selector selector )
{
assert instances.length == INSTANCE_COUNT;
this.instances = instances;
this.selector = selector;
}
<R,E extends Exception> R[] instancesAs( Class<R> cls, ThrowingFunction<T,R,E> converter ) throws E
{
return instancesAs( instances, cls, converter );
}
static <T,R,E extends Exception> R[] instancesAs( T[] instances, Class<R> cls, ThrowingFunction<T,R,E> converter ) throws E
{
R[] result = (R[]) Array.newInstance( cls, instances.length );
for ( int i = 0; i < instances.length; i++ )
{
result[i] = converter.apply( instances[i] );
}
return result;
}
/**
* NOTE: duplicate of {@link #forAll(ThrowingConsumer, Iterable)} to avoid having to wrap subjects of one form into another.
* There are some real use cases for passing in an array instead of {@link Iterable} out there...
*
* Method for calling a lambda function on many objects when it is expected that the function might
* throw an exception. First exception will be thrown and subsequent will be suppressed.
*
* For example, in FusionIndexAccessor:
* <pre>
* public void drop() throws IOException
* {
* forAll( IndexAccessor::drop, firstAccessor, secondAccessor, thirdAccessor );
* }
* </pre>
*
* @param consumer lambda function to call on each object passed
* @param subjects varargs array of objects to call the function on
* @param <E> the type of exception anticipated, inferred from the lambda
* @throws E if consumption fails with this exception
*/
@SafeVarargs
public static <T, E extends Exception> void forAll( ThrowingConsumer<T,E> consumer, T... subjects ) throws E
{
E exception = null;
for ( T subject : subjects )
{
try
{
consumer.accept( subject );
}
catch ( Throwable t )
{
E e = (E) t;
if ( exception == null )
{
exception = e;
}
else
{
exception.addSuppressed( e );
}
}
}
if ( exception != null )
{
throw exception;
}
}
/**
* See {@link #forAll(ThrowingConsumer, Object[])}
* NOTE: duplicate of {@link #forAll(ThrowingConsumer, Object[])} to avoid having to wrap subjects of one form into another.
* There are some real use cases for passing in an Iterable instead of array out there...
*
* Method for calling a lambda function on many objects when it is expected that the function might
* throw an exception. First exception will be thrown and subsequent will be suppressed.
*
* For example, in FusionIndexAccessor:
* <pre>
* public void drop() throws IOException
* {
* forAll( IndexAccessor::drop, firstAccessor, secondAccessor, thirdAccessor );
* }
* </pre>
*
* @param consumer lambda function to call on each object passed
* @param subjects varargs array of objects to call the function on
* @param <E> the type of exception anticipated, inferred from the lambda
* @throws E if consumption fails with this exception
*/
public static <T, E extends Exception> void forAll( ThrowingConsumer<T,E> consumer, Iterable<T> subjects ) throws E
{
E exception = null;
for ( T subject : subjects )
{
try
{
consumer.accept( subject );
}
catch ( Throwable t )
{
E e = (E) t;
if ( exception == null )
{
exception = e;
}
else
{
exception.addSuppressed( e );
}
}
}
if ( exception != null )
{
throw exception;
}
}
static void validateSelectorInstances( Object[] instances, int... aliveIndex )
{
for ( int i = 0; i < instances.length; i++ )
{
boolean expected = PrimitiveIntCollections.contains( aliveIndex, i );
boolean actual = instances[i] != IndexProvider.EMPTY;
if ( expected != actual )
{
throw new IllegalArgumentException(
String.format( "Only indexes expected to be separated from IndexProvider.EMPTY are %s but was %s",
Arrays.toString( aliveIndex ), Arrays.toString( instances ) ) );
}
}
}
}