-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
LayoutTestUtil.java
176 lines (148 loc) · 5.84 KB
/
LayoutTestUtil.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
/*
* 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 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.Values;
abstract class LayoutTestUtil<KEY extends NativeIndexKey<KEY>, VALUE extends NativeIndexValue>
{
private static final Comparator<IndexEntryUpdate<IndexDescriptor>> UPDATE_COMPARATOR = ( u1, u2 ) ->
Values.COMPARATOR.compare( u1.values()[0], u2.values()[0] );
final StoreIndexDescriptor indexDescriptor;
LayoutTestUtil( IndexDescriptor indexDescriptor )
{
this( indexDescriptor.withId( 0 ) );
}
LayoutTestUtil( StoreIndexDescriptor indexDescriptor )
{
this.indexDescriptor = indexDescriptor;
}
abstract IndexLayout<KEY,VALUE> createLayout();
abstract IndexEntryUpdate<IndexDescriptor>[] someUpdates();
abstract RandomValues.Type[] supportedTypes();
protected double fractionDuplicates()
{
return 0.1;
}
IndexQuery rangeQuery( Value from, boolean fromInclusive, Value to, boolean toInclusive )
{
return IndexQuery.range( 0, from, fromInclusive, to, toInclusive );
}
abstract int compareIndexedPropertyValue( KEY key1, KEY key2 );
StoreIndexDescriptor indexDescriptor()
{
return indexDescriptor;
}
void copyValue( VALUE value, VALUE intoValue )
{
}
Iterator<IndexEntryUpdate<IndexDescriptor>> randomUpdateGenerator( RandomRule random )
{
double fractionDuplicates = fractionDuplicates();
return new PrefetchingIterator<IndexEntryUpdate<IndexDescriptor>>()
{
private final Set<Object> uniqueCompareValues = new HashSet<>();
private final List<Value> uniqueValues = new ArrayList<>();
private long currentEntityId;
@Override
protected IndexEntryUpdate<IndexDescriptor> fetchNextOrNull()
{
Value value;
if ( fractionDuplicates > 0 && !uniqueValues.isEmpty() &&
random.nextFloat() < fractionDuplicates )
{
value = existingNonUniqueValue( random );
}
else
{
value = newUniqueValue( random.randomValues(), uniqueCompareValues, uniqueValues );
}
return add( currentEntityId++, value );
}
private Value existingNonUniqueValue( RandomRule randomRule )
{
return uniqueValues.get( randomRule.nextInt( uniqueValues.size() ) );
}
};
}
abstract Value newUniqueValue( RandomValues random, Set<Object> uniqueCompareValues, List<Value> uniqueValues );
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;
}
abstract IndexEntryUpdate<IndexDescriptor>[] someUpdatesNoDuplicateValues();
abstract IndexEntryUpdate<IndexDescriptor>[] someUpdatesWithDuplicateValues();
IndexEntryUpdate<IndexDescriptor>[] generateAddUpdatesFor( Object[] values )
{
return generateAddUpdatesFor( Arrays.stream( values )
.map( Values::of )
.toArray( Value[]::new ) );
}
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;
}
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 );
}
}