-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ValueCreatorUtil.java
266 lines (233 loc) · 9.39 KB
/
ValueCreatorUtil.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
/*
* Copyright (c) 2002-2018 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.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.apache.commons.lang3.ArrayUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.neo4j.helpers.collection.PrefetchingIterator;
import org.neo4j.internal.kernel.api.IndexQuery;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.storageengine.api.schema.IndexDescriptor;
import org.neo4j.storageengine.api.schema.StoreIndexDescriptor;
import org.neo4j.test.rule.RandomRule;
import org.neo4j.values.storable.RandomValues;
import org.neo4j.values.storable.Value;
import org.neo4j.values.storable.ValueType;
import org.neo4j.values.storable.Values;
class ValueCreatorUtil<KEY extends NativeIndexKey<KEY>, VALUE extends NativeIndexValue>
{
static final double FRACTION_DUPLICATE_UNIQUE = 0;
static final double FRACTION_DUPLICATE_NON_UNIQUE = 0.1;
private static final double FRACTION_EXTREME_VALUE = 0.25;
private static final Comparator<IndexEntryUpdate<IndexDescriptor>> UPDATE_COMPARATOR = ( u1, u2 ) ->
Values.COMPARATOR.compare( u1.values()[0], u2.values()[0] );
private static final int N_VALUES = 10;
final StoreIndexDescriptor indexDescriptor;
private final ValueType[] supportedTypes;
private final double fractionDuplicates;
ValueCreatorUtil( ValueCreatorUtil delegate )
{
this( delegate.indexDescriptor, delegate.supportedTypes, delegate.fractionDuplicates );
}
ValueCreatorUtil( StoreIndexDescriptor indexDescriptor, ValueType[] supportedTypes, double fractionDuplicates )
{
this.indexDescriptor = indexDescriptor;
this.supportedTypes = supportedTypes;
this.fractionDuplicates = fractionDuplicates;
}
int compareIndexedPropertyValue( KEY key1, KEY key2 )
{
return Values.COMPARATOR.compare( key1.asValues()[0], key2.asValues()[0] );
}
ValueType[] supportedTypes()
{
return supportedTypes;
}
private double fractionDuplicates()
{
return fractionDuplicates;
}
IndexQuery rangeQuery( Value from, boolean fromInclusive, Value to, boolean toInclusive )
{
return IndexQuery.range( 0, from, fromInclusive, to, toInclusive );
}
StoreIndexDescriptor indexDescriptor()
{
return indexDescriptor;
}
IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule randomRule )
{
return someUpdates( randomRule, supportedTypes(), fractionDuplicates() );
}
IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule random, ValueType[] types, boolean allowDuplicates )
{
double fractionDuplicates = allowDuplicates ? FRACTION_DUPLICATE_NON_UNIQUE : FRACTION_DUPLICATE_UNIQUE;
return someUpdates( random, types, fractionDuplicates );
}
private IndexEntryUpdate<IndexDescriptor>[] someUpdates( RandomRule random, ValueType[] types, double fractionDuplicates )
{
RandomValueGenerator valueGenerator = new RandomValueGenerator( random.randomValues(), types, fractionDuplicates );
RandomUpdateGenerator randomUpdateGenerator = new RandomUpdateGenerator( valueGenerator );
//noinspection unchecked
IndexEntryUpdate<IndexDescriptor>[] result = new IndexEntryUpdate[N_VALUES];
for ( int i = 0; i < N_VALUES; i++ )
{
result[i] = randomUpdateGenerator.next();
}
return result;
}
IndexEntryUpdate<IndexDescriptor>[] someUpdatesWithDuplicateValues( RandomRule randomRule )
{
Iterator<Value> valueIterator = new RandomValueGenerator( randomRule.randomValues(), supportedTypes(), fractionDuplicates() );
Value[] someValues = new Value[N_VALUES];
for ( int i = 0; i < N_VALUES; i++ )
{
someValues[i] = valueIterator.next();
}
return generateAddUpdatesFor( ArrayUtils.addAll( someValues, someValues ) );
}
Iterator<IndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator( RandomRule randomRule )
{
return randomUpdateGenerator( randomRule, supportedTypes() );
}
Iterator<IndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator( RandomRule random, ValueType[] types )
{
Iterator<Value> valueIterator = new RandomValueGenerator( random.randomValues(), types, fractionDuplicates() );
return new RandomUpdateGenerator( valueIterator );
}
IndexEntryUpdate<IndexDescriptor>[] generateAddUpdatesFor( Value[] values )
{
//noinspection unchecked
IndexEntryUpdate<IndexDescriptor>[] indexEntryUpdates = new IndexEntryUpdate[values.length];
for ( int i = 0; i < indexEntryUpdates.length; i++ )
{
indexEntryUpdates[i] = add( i, values[i] );
}
return indexEntryUpdates;
}
Value[] extractValuesFromUpdates( IndexEntryUpdate<IndexDescriptor>[] updates )
{
Value[] values = new Value[updates.length];
for ( int i = 0; i < updates.length; i++ )
{
if ( updates[i].values().length > 1 )
{
throw new UnsupportedOperationException( "This method does not support composite entries" );
}
values[i] = updates[i].values()[0];
}
return values;
}
protected IndexEntryUpdate<IndexDescriptor> add( long nodeId, Value value )
{
return IndexEntryUpdate.add( nodeId, indexDescriptor, value );
}
static int countUniqueValues( IndexEntryUpdate<IndexDescriptor>[] updates )
{
return Stream.of( updates ).map( update -> update.values()[0] ).collect( Collectors.toSet() ).size();
}
static int countUniqueValues( Object[] updates )
{
return Stream.of( updates ).collect( Collectors.toSet() ).size();
}
void sort( IndexEntryUpdate<IndexDescriptor>[] updates )
{
Arrays.sort( updates, UPDATE_COMPARATOR );
}
void copyValue( VALUE value, VALUE intoValue )
{ // no-op until we decide to use value for something
}
private class RandomValueGenerator extends PrefetchingIterator<Value>
{
private final Set<Value> uniqueCompareValues;
private final List<Value> uniqueValues;
private final ValueType[] types;
private final double fractionDuplicates;
private final RandomValues randomValues;
RandomValueGenerator( RandomValues randomValues, ValueType[] types, double fractionDuplicates )
{
this.types = types;
this.fractionDuplicates = fractionDuplicates;
this.randomValues = randomValues;
this.uniqueCompareValues = new HashSet<>();
this.uniqueValues = new ArrayList<>();
}
@Override
protected Value fetchNextOrNull()
{
Value value;
if ( fractionDuplicates > 0 && !uniqueValues.isEmpty() &&
randomValues.nextFloat() < fractionDuplicates )
{
value = randomValues.among( uniqueValues );
}
else
{
value = newUniqueValue( randomValues, uniqueCompareValues, uniqueValues );
}
return value;
}
private Value newUniqueValue( RandomValues random, Set<Value> uniqueCompareValues, List<Value> uniqueValues )
{
int attempts = 0;
int maxAttempts = 10; // To avoid infinite loop on booleans
Value value;
do
{
attempts++;
ValueType type = randomValues.among( types );
boolean useExtremeValue = attempts == 1 && randomValues.nextDouble() < FRACTION_EXTREME_VALUE;
if ( useExtremeValue )
{
value = randomValues.among( type.extremeValues() );
}
else
{
value = random.nextValueOfType( type );
}
}
while ( attempts < maxAttempts && !uniqueCompareValues.add( value ) );
uniqueValues.add( value );
return value;
}
}
private class RandomUpdateGenerator extends PrefetchingIterator<IndexEntryUpdate<IndexDescriptor>>
{
private final Iterator<Value> valueIterator;
private long currentEntityId;
RandomUpdateGenerator( Iterator<Value> valueIterator )
{
this.valueIterator = valueIterator;
}
@Override
protected IndexEntryUpdate<IndexDescriptor> fetchNextOrNull()
{
Value value = valueIterator.next();
return add( currentEntityId++, value );
}
}
}