-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
IndexCRUDIT.java
273 lines (245 loc) · 10 KB
/
IndexCRUDIT.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
267
268
269
270
271
272
273
/*
* 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.api.index;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.internal.kernel.api.TokenRead;
import org.neo4j.internal.kernel.api.schema.LabelSchemaDescriptor;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.kernel.api.KernelTransaction;
import org.neo4j.kernel.api.index.IndexAccessor;
import org.neo4j.kernel.api.index.IndexEntryUpdate;
import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.IndexProvider;
import org.neo4j.kernel.api.index.IndexUpdater;
import org.neo4j.kernel.api.index.NodePropertyAccessor;
import org.neo4j.kernel.api.schema.SchemaDescriptorFactory;
import org.neo4j.kernel.api.schema.index.StoreIndexDescriptor;
import org.neo4j.kernel.extension.KernelExtensionFactory;
import org.neo4j.kernel.impl.api.index.sampling.IndexSamplingConfig;
import org.neo4j.kernel.impl.core.ThreadToStatementContextBridge;
import org.neo4j.kernel.impl.storemigration.StoreMigrationParticipant;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.storageengine.api.schema.IndexSample;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import org.neo4j.values.storable.Values;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.neo4j.helpers.collection.Iterators.asSet;
import static org.neo4j.helpers.collection.MapUtil.map;
import static org.neo4j.kernel.impl.api.index.SchemaIndexTestHelper.singleInstanceIndexProviderFactory;
import static org.neo4j.kernel.impl.api.index.TestIndexProviderDescriptor.PROVIDER_DESCRIPTOR;
import static org.neo4j.test.mockito.matcher.Neo4jMatchers.createIndex;
public class IndexCRUDIT
{
@Test
public void addingANodeWithPropertyShouldGetIndexed() throws Exception
{
// Given
String indexProperty = "indexProperty";
GatheringIndexWriter writer = newWriter();
createIndex( db, myLabel, indexProperty );
// When
int value1 = 12;
String otherProperty = "otherProperty";
int otherValue = 17;
Node node = createNode( map( indexProperty, value1, otherProperty, otherValue ), myLabel );
// Then, for now, this should trigger two NodePropertyUpdates
try ( Transaction tx = db.beginTx() )
{
KernelTransaction ktx =
ctxSupplier.getKernelTransactionBoundToThisThread( true );
TokenRead tokenRead = ktx.tokenRead();
int propertyKey1 = tokenRead.propertyKey( indexProperty );
int label = tokenRead.nodeLabel( myLabel.name() );
LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel( label, propertyKey1 );
assertThat( writer.updatesCommitted, equalTo( asSet(
IndexEntryUpdate.add( node.getId(), descriptor, Values.of( value1 ) ) ) ) );
tx.success();
}
// We get two updates because we both add a label and a property to be indexed
// in the same transaction, in the future, we should optimize this down to
// one NodePropertyUpdate.
}
@Test
public void addingALabelToPreExistingNodeShouldGetIndexed() throws Exception
{
// GIVEN
String indexProperty = "indexProperty";
GatheringIndexWriter writer = newWriter();
createIndex( db, myLabel, indexProperty );
// WHEN
String otherProperty = "otherProperty";
int value = 12;
int otherValue = 17;
Node node = createNode( map( indexProperty, value, otherProperty, otherValue ) );
// THEN
assertThat( writer.updatesCommitted.size(), equalTo( 0 ) );
// AND WHEN
try ( Transaction tx = db.beginTx() )
{
node.addLabel( myLabel );
tx.success();
}
// THEN
try ( Transaction tx = db.beginTx() )
{
KernelTransaction ktx =
ctxSupplier.getKernelTransactionBoundToThisThread( true );
TokenRead tokenRead = ktx.tokenRead();
int propertyKey1 = tokenRead.propertyKey( indexProperty );
int label = tokenRead.nodeLabel( myLabel.name() );
LabelSchemaDescriptor descriptor = SchemaDescriptorFactory.forLabel( label, propertyKey1 );
assertThat( writer.updatesCommitted, equalTo( asSet(
IndexEntryUpdate.add( node.getId(), descriptor, Values.of( value ) ) ) ) );
tx.success();
}
}
private GraphDatabaseAPI db;
@Rule
public EphemeralFileSystemRule fs = new EphemeralFileSystemRule();
private final IndexProvider mockedIndexProvider = mock( IndexProvider.class );
private final KernelExtensionFactory<?> mockedIndexProviderFactory =
singleInstanceIndexProviderFactory( "none", mockedIndexProvider );
private ThreadToStatementContextBridge ctxSupplier;
private final Label myLabel = Label.label( "MYLABEL" );
private Node createNode( Map<String, Object> properties, Label ... labels )
{
try ( Transaction tx = db.beginTx() )
{
Node node = db.createNode( labels );
for ( Map.Entry<String, Object> prop : properties.entrySet() )
{
node.setProperty( prop.getKey(), prop.getValue() );
}
tx.success();
return node;
}
}
@Before
public void before()
{
when( mockedIndexProvider.getProviderDescriptor() ).thenReturn( PROVIDER_DESCRIPTOR );
when( mockedIndexProvider.storeMigrationParticipant( any( FileSystemAbstraction.class ), any( PageCache.class ) ) )
.thenReturn( StoreMigrationParticipant.NOT_PARTICIPATING );
TestGraphDatabaseFactory factory = new TestGraphDatabaseFactory();
factory.setFileSystem( fs.get() );
factory.setKernelExtensions(
Collections.singletonList( mockedIndexProviderFactory ) );
db = (GraphDatabaseAPI) factory.newImpermanentDatabase();
ctxSupplier = db.getDependencyResolver().resolveDependency( ThreadToStatementContextBridge.class );
}
private GatheringIndexWriter newWriter() throws IOException
{
GatheringIndexWriter writer = new GatheringIndexWriter();
when( mockedIndexProvider.getPopulator( any( StoreIndexDescriptor.class ), any( IndexSamplingConfig.class ) )
).thenReturn( writer );
when( mockedIndexProvider.getOnlineAccessor( any( StoreIndexDescriptor.class ), any( IndexSamplingConfig.class )
) ).thenReturn( writer );
when( mockedIndexProvider.compareTo( any( IndexProvider.class ) ) )
.thenReturn( 1 ); // always pretend to have highest priority
return writer;
}
@After
public void after()
{
db.shutdown();
}
private class GatheringIndexWriter extends IndexAccessor.Adapter implements IndexPopulator
{
private final Set<IndexEntryUpdate<?>> updatesCommitted = new HashSet<>();
private final Map<Object,Set<Long>> indexSamples = new HashMap<>();
@Override
public void create()
{
}
@Override
public void add( Collection<? extends IndexEntryUpdate<?>> updates )
{
updatesCommitted.addAll( updates );
}
@Override
public void verifyDeferredConstraints( NodePropertyAccessor nodePropertyAccessor )
{
}
@Override
public IndexUpdater newPopulatingUpdater( NodePropertyAccessor nodePropertyAccessor )
{
return newUpdater( IndexUpdateMode.ONLINE );
}
@Override
public IndexUpdater newUpdater( final IndexUpdateMode mode )
{
return new CollectingIndexUpdater()
{
@Override
public void close()
{
updatesCommitted.addAll( updates );
}
};
}
@Override
public void close( boolean populationCompletedSuccessfully )
{
}
@Override
public void markAsFailed( String failure )
{
}
@Override
public void includeSample( IndexEntryUpdate<?> update )
{
addValueToSample( update.getEntityId(), update.values()[0] );
}
@Override
public IndexSample sampleResult()
{
long indexSize = 0;
for ( Set<Long> nodeIds : indexSamples.values() )
{
indexSize += nodeIds.size();
}
return new IndexSample( indexSize, indexSamples.size(), indexSize );
}
private void addValueToSample( long nodeId, Object propertyValue )
{
Set<Long> nodeIds = indexSamples.computeIfAbsent( propertyValue, k -> new HashSet<>() );
nodeIds.add( nodeId );
}
}
}