-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
StateHandlingStatementOperationsTest.java
464 lines (398 loc) · 20.5 KB
/
StateHandlingStatementOperationsTest.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
* Copyright (c) 2002-2016 "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.api.state;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.util.Collections;
import java.util.Set;
import org.neo4j.collection.pool.Pool;
import org.neo4j.collection.primitive.PrimitiveLongCollections;
import org.neo4j.collection.primitive.PrimitiveLongIterator;
import org.neo4j.cursor.Cursor;
import org.neo4j.helpers.collection.IteratorUtil;
import org.neo4j.kernel.api.constraints.NodePropertyConstraint;
import org.neo4j.kernel.api.constraints.PropertyConstraint;
import org.neo4j.kernel.api.constraints.UniquenessConstraint;
import org.neo4j.kernel.api.exceptions.index.IndexNotFoundKernelException;
import org.neo4j.kernel.api.index.IndexDescriptor;
import org.neo4j.kernel.api.txstate.TransactionState;
import org.neo4j.kernel.impl.api.AutoIndexing;
import org.neo4j.kernel.impl.api.IndexReaderFactory;
import org.neo4j.kernel.impl.api.KernelStatement;
import org.neo4j.kernel.impl.api.StateHandlingStatementOperations;
import org.neo4j.kernel.impl.api.store.StoreStatement;
import org.neo4j.kernel.impl.index.LegacyIndexStore;
import org.neo4j.kernel.impl.locking.ReentrantLockService;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.util.Cursors;
import org.neo4j.kernel.impl.util.diffsets.DiffSets;
import org.neo4j.storageengine.api.LabelItem;
import org.neo4j.storageengine.api.NodeItem;
import org.neo4j.storageengine.api.StorageStatement;
import org.neo4j.storageengine.api.StoreReadLayer;
import org.neo4j.storageengine.api.schema.IndexReader;
import org.neo4j.storageengine.api.schema.LabelScanReader;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.neo4j.helpers.collection.IteratorUtil.asIterable;
import static org.neo4j.helpers.collection.IteratorUtil.asSet;
import static org.neo4j.kernel.api.properties.Property.intProperty;
import static org.neo4j.kernel.impl.api.StatementOperationsTestHelper.mockedState;
import static org.neo4j.kernel.impl.api.state.StubCursors.asNodeCursor;
import static org.neo4j.kernel.impl.api.state.StubCursors.asPropertyCursor;
public class StateHandlingStatementOperationsTest
{
// Note: Most of the behavior of this class is tested in separate classes,
// based on the category of state being
// tested. This contains general tests or things that are common to all
// types of state.
StoreReadLayer inner = mock( StoreReadLayer.class );
@Test
public void shouldNeverDelegateWrites() throws Exception
{
KernelStatement state = mockedState();
when( state.txState() ).thenReturn( new TxState() );
StoreStatement storeStatement = mock( StoreStatement.class );
when( state.getStoreStatement() ).thenReturn( storeStatement );
when( storeStatement.acquireSingleNodeCursor( anyLong() ) ).
thenReturn( asNodeCursor( 0 ) );
StateHandlingStatementOperations ctx = newTxStateOps( inner );
// When
ctx.indexCreate( state, 0, 0 );
ctx.nodeAddLabel( state, 0, 0 );
ctx.indexDrop( state, new IndexDescriptor( 0, 0 ) );
ctx.nodeRemoveLabel( state, 0, 0 );
// one for add and one for remove
verify( storeStatement, times( 2 ) ).acquireSingleNodeCursor( 0 );
verifyNoMoreInteractions( storeStatement );
}
@Test
public void shouldNotAddConstraintAlreadyExistsInTheStore() throws Exception
{
// given
PropertyConstraint constraint = new UniquenessConstraint( 10, 66 );
TransactionState txState = mock( TransactionState.class );
when( txState.nodesWithLabelChanged( anyInt() ) ).thenReturn( new DiffSets<Long>() );
when( txState.hasChanges() ).thenReturn( true );
KernelStatement state = mockedState( txState );
when( inner.constraintsGetForLabelAndPropertyKey( 10, 66 ) )
.thenAnswer( invocation -> IteratorUtil.iterator( constraint ) );
StateHandlingStatementOperations context = newTxStateOps( inner );
// when
context.uniquePropertyConstraintCreate( state, 10, 66 );
// then
verify( txState ).constraintIndexDoUnRemove( any( IndexDescriptor.class ) );
}
@Test
public void shouldGetConstraintsByLabelAndProperty() throws Exception
{
// given
PropertyConstraint constraint = new UniquenessConstraint( 10, 66 );
TransactionState txState = new TxState();
KernelStatement state = mockedState( txState );
when( inner.constraintsGetForLabelAndPropertyKey( 10, 66 ) )
.thenAnswer( invocation -> IteratorUtil.emptyIterator() );
StateHandlingStatementOperations context = newTxStateOps( inner );
context.uniquePropertyConstraintCreate( state, 10, 66 );
// when
Set<NodePropertyConstraint> result = asSet(
asIterable( context.constraintsGetForLabelAndPropertyKey( state, 10, 66 ) ) );
// then
assertEquals( asSet( constraint ), result );
}
@Test
public void shouldGetConstraintsByLabel() throws Exception
{
// given
PropertyConstraint constraint2 = new UniquenessConstraint( 11, 99 );
PropertyConstraint constraint1 = new UniquenessConstraint( 11, 66 );
TransactionState txState = new TxState();
KernelStatement state = mockedState( txState );
when( inner.constraintsGetForLabelAndPropertyKey( 10, 66 ) )
.thenAnswer( invocation -> IteratorUtil.emptyIterator() );
when( inner.constraintsGetForLabelAndPropertyKey( 11, 99 ) )
.thenAnswer( invocation -> IteratorUtil.emptyIterator() );
when( inner.constraintsGetForLabel( 10 ) )
.thenAnswer( invocation -> IteratorUtil.emptyIterator() );
when( inner.constraintsGetForLabel( 11 ) )
.thenAnswer( invocation -> IteratorUtil.iterator( constraint1 ) );
StateHandlingStatementOperations context = newTxStateOps( inner );
context.uniquePropertyConstraintCreate( state, 10, 66 );
context.uniquePropertyConstraintCreate( state, 11, 99 );
// when
Set<NodePropertyConstraint> result = asSet( asIterable( context.constraintsGetForLabel( state, 11 ) ) );
// then
assertEquals( asSet( constraint1, constraint2 ), result );
}
@Test
public void shouldGetAllConstraints() throws Exception
{
// given
PropertyConstraint constraint1 = new UniquenessConstraint( 10, 66 );
PropertyConstraint constraint2 = new UniquenessConstraint( 11, 99 );
TransactionState txState = new TxState();
KernelStatement state = mockedState( txState );
when( inner.constraintsGetForLabelAndPropertyKey( 10, 66 ) )
.thenAnswer( invocation -> IteratorUtil.emptyIterator() );
when( inner.constraintsGetForLabelAndPropertyKey( 11, 99 ) )
.thenAnswer( invocation -> IteratorUtil.emptyIterator() );
when( inner.constraintsGetAll() )
.thenAnswer( invocation -> IteratorUtil.iterator( constraint2 ) );
StateHandlingStatementOperations context = newTxStateOps( inner );
context.uniquePropertyConstraintCreate( state, 10, 66 );
// when
Set<PropertyConstraint> result = asSet( asIterable( context.constraintsGetAll( state ) ) );
// then
assertEquals( asSet( constraint1, constraint2 ), result );
}
@Test
public void shouldConsiderTransactionStateDuringIndexScan() throws Exception
{
// Given
TransactionState txState = mock( TransactionState.class );
KernelStatement statement = mock( KernelStatement.class );
when( statement.hasTxStateWithChanges() ).thenReturn( true );
when( statement.txState() ).thenReturn( txState );
IndexDescriptor index = new IndexDescriptor( 1, 2 );
when( txState.indexUpdatesForScanOrSeek( index, null ) ).thenReturn(
new DiffSets<>( Collections.singleton( 42L ), Collections.singleton( 44L ) )
);
when( txState.addedAndRemovedNodes() ).thenReturn(
new DiffSets<>( Collections.singleton( 45L ), Collections.singleton( 46L ) )
);
StoreReadLayer storeReadLayer = mock( StoreReadLayer.class );
IndexReader indexReader = addMockedIndexReader( statement );
when( indexReader.scan() ).thenReturn(
PrimitiveLongCollections.resourceIterator( PrimitiveLongCollections.iterator( 43L, 44L, 46L ), null )
);
StateHandlingStatementOperations context = newTxStateOps( storeReadLayer );
// When
PrimitiveLongIterator results = context.nodesGetFromIndexScan( statement, index );
// Then
assertEquals( asSet( 42L, 43L ), PrimitiveLongCollections.toSet( results ) );
}
@Test
public void shouldConsiderTransactionStateDuringIndexSeek() throws Exception
{
// Given
TransactionState txState = mock( TransactionState.class );
KernelStatement statement = mock( KernelStatement.class );
when( statement.hasTxStateWithChanges() ).thenReturn( true );
when( statement.txState() ).thenReturn( txState );
IndexDescriptor index = new IndexDescriptor( 1, 2 );
when( txState.indexUpdatesForScanOrSeek( index, "value" ) ).thenReturn(
new DiffSets<>( Collections.singleton( 42L ), Collections.singleton( 44L ) )
);
when( txState.addedAndRemovedNodes() ).thenReturn(
new DiffSets<>( Collections.singleton( 45L ), Collections.singleton( 46L ) )
);
StoreReadLayer storeReadLayer = mock( StoreReadLayer.class );
IndexReader indexReader = addMockedIndexReader( statement );
when( indexReader.seek( "value" ) ).thenReturn(
PrimitiveLongCollections.resourceIterator( PrimitiveLongCollections.iterator( 43L, 44L, 46L ), null ) );
StateHandlingStatementOperations context = newTxStateOps( storeReadLayer );
// When
PrimitiveLongIterator results = context.nodesGetFromIndexSeek( statement, index, "value" );
// Then
assertEquals( asSet( 42L, 43L ), PrimitiveLongCollections.toSet( results ) );
}
@Test
public void shouldConsiderTransactionStateDuringIndexRangeSeekByPrefix() throws Exception
{
// Given
TransactionState txState = mock( TransactionState.class );
KernelStatement statement = mock( KernelStatement.class );
when( statement.hasTxStateWithChanges() ).thenReturn( true );
when( statement.txState() ).thenReturn( txState );
IndexDescriptor index = new IndexDescriptor( 1, 2 );
when( txState.indexUpdatesForRangeSeekByPrefix( index, "prefix" ) ).thenReturn(
new DiffSets<>( Collections.singleton( 42L ), Collections.singleton( 44L ) )
);
when( txState.addedAndRemovedNodes() ).thenReturn(
new DiffSets<>( Collections.singleton( 45L ), Collections.singleton( 46L ) )
);
StoreReadLayer storeReadLayer = mock( StoreReadLayer.class );
IndexReader indexReader = addMockedIndexReader( statement );
when( indexReader.rangeSeekByPrefix( "prefix" ) )
.thenReturn( PrimitiveLongCollections.resourceIterator( PrimitiveLongCollections.iterator( 43L, 44L, 46L ), null ) );
StateHandlingStatementOperations context = newTxStateOps( storeReadLayer );
// When
PrimitiveLongIterator results = context.nodesGetFromIndexRangeSeekByPrefix( statement, index, "prefix" );
// Then
assertEquals( asSet( 42L, 43L ), PrimitiveLongCollections.toSet( results ) );
}
@SuppressWarnings( "unchecked" )
@Test
public void shouldConsiderTransactionStateDuringIndexBetweenRangeSeekByNumber() throws Exception
{
// Given
final int propertyKey = 2;
final int inRange = 15;
int lower = 10;
int upper = 20;
TransactionState txState = mock( TransactionState.class );
KernelStatement statement = mock( KernelStatement.class );
when( statement.hasTxStateWithChanges() ).thenReturn( true );
when( statement.txState() ).thenReturn( txState );
StorageStatement storageStatement = mock( StorageStatement.class );
when( statement.getStoreStatement() ).thenReturn( storageStatement );
IndexDescriptor index = new IndexDescriptor( 1, propertyKey );
when( txState.indexUpdatesForRangeSeekByNumber( index, lower, true, upper, false ) ).thenReturn(
new DiffSets<>( Collections.singleton( 42L ), Collections.singleton( 44L ) )
);
when( txState.addedAndRemovedNodes() ).thenReturn(
new DiffSets<>( Collections.singleton( 45L ), Collections.singleton( 46L ) )
);
when( txState.augmentSingleNodeCursor( any( Cursor.class ), anyLong() ) ).thenAnswer(
new Answer<Cursor<NodeItem>>()
{
@Override
public Cursor<NodeItem> answer( InvocationOnMock invocationOnMock ) throws Throwable
{
long nodeId = (long) invocationOnMock.getArguments()[1];
return asNodeCursor( nodeId, asPropertyCursor( intProperty( propertyKey, inRange ) ),
Cursors.<LabelItem>empty() );
}
} );
StoreReadLayer storeReadLayer = mock( StoreReadLayer.class );
IndexReader indexReader = addMockedIndexReader( storageStatement );
when( indexReader.rangeSeekByNumberInclusive( lower, upper ) ).thenReturn(
PrimitiveLongCollections.resourceIterator( PrimitiveLongCollections.iterator( 43L, 44L, 46L ), null )
);
when( storageStatement.acquireSingleNodeCursor( anyLong() ) ).thenAnswer(
new Answer<Cursor<NodeItem>>()
{
@Override
public Cursor<NodeItem> answer( InvocationOnMock invocationOnMock ) throws Throwable
{
long nodeId = (long) invocationOnMock.getArguments()[0];
return asNodeCursor( nodeId, asPropertyCursor( intProperty( propertyKey, inRange ) ),
Cursors.<LabelItem>empty() );
}
} );
StateHandlingStatementOperations context = newTxStateOps( storeReadLayer );
// When
PrimitiveLongIterator results = context.nodesGetFromIndexRangeSeekByNumber( statement, index, lower, true,
upper, false );
// Then
assertEquals( asSet( 42L, 43L ), PrimitiveLongCollections.toSet( results ) );
}
@Test
public void shouldConsiderTransactionStateDuringIndexBetweenRangeSeekByString() throws Exception
{
// Given
TransactionState txState = mock( TransactionState.class );
KernelStatement statement = mock( KernelStatement.class );
when( statement.hasTxStateWithChanges() ).thenReturn( true );
when( statement.txState() ).thenReturn( txState );
IndexDescriptor index = new IndexDescriptor( 1, 2 );
when( txState.indexUpdatesForRangeSeekByString( index, "Anne", true, "Bill", false ) ).thenReturn(
new DiffSets<>( Collections.singleton( 42L ), Collections.singleton( 44L ) )
);
when( txState.addedAndRemovedNodes() ).thenReturn(
new DiffSets<>( Collections.singleton( 45L ), Collections.singleton( 46L ) )
);
StoreReadLayer storeReadLayer = mock( StoreReadLayer.class );
IndexReader indexReader = addMockedIndexReader( statement );
when( indexReader.rangeSeekByString( "Anne", true, "Bill", false ) ) .thenReturn(
PrimitiveLongCollections.resourceIterator( PrimitiveLongCollections.iterator( 43L, 44L, 46L ), null )
);
StateHandlingStatementOperations context = newTxStateOps( storeReadLayer );
// When
PrimitiveLongIterator results = context.nodesGetFromIndexRangeSeekByString( statement, index, "Anne", true, "Bill", false );
// Then
assertEquals( asSet( 42L, 43L ), PrimitiveLongCollections.toSet( results ) );
}
@Test
public void nodeGetFromUniqueIndexSeekClosesIndexReader() throws Exception
{
KernelStatement kernelStatement = mock( KernelStatement.class );
StoreStatement storeStatement = mock( StoreStatement.class );
IndexReader indexReader = mock( IndexReader.class );
when( indexReader.seek( any() ) ).thenReturn( PrimitiveLongCollections.emptyIterator() );
when( storeStatement.getFreshIndexReader( any() ) ).thenReturn( indexReader );
when( kernelStatement.getStoreStatement() ).thenReturn( storeStatement );
StateHandlingStatementOperations operations = newTxStateOps( mock( StoreReadLayer.class ) );
operations.nodeGetFromUniqueIndexSeek( kernelStatement, new IndexDescriptor( 1, 1 ), "foo" );
verify( indexReader ).close();
}
@Test
public void nodeCursorGetFromUniqueIndexSeekClosesIndexReader() throws Exception
{
KernelStatement kernelStatement = mock( KernelStatement.class );
IndexReader indexReader = mock( IndexReader.class );
StoreStatement storeStatement = new StoreStatementWithSingleFreshIndexReader( indexReader );
when( indexReader.seek( any() ) ).thenReturn( PrimitiveLongCollections.emptyIterator() );
when( kernelStatement.getStoreStatement() ).thenReturn( storeStatement );
StateHandlingStatementOperations operations = newTxStateOps( mock( StoreReadLayer.class ) );
try ( Cursor<NodeItem> cursor = operations.nodeCursorGetFromUniqueIndexSeek( kernelStatement,
new IndexDescriptor( 1, 1 ), "foo" ) )
{
while ( cursor.next() )
{
fail( "Cursor should be empty" );
}
}
verify( indexReader ).close();
}
private StateHandlingStatementOperations newTxStateOps( StoreReadLayer delegate )
{
return new StateHandlingStatementOperations( delegate,
mock( AutoIndexing.class ), mock( ConstraintIndexCreator.class ),
mock( LegacyIndexStore.class ) );
}
private IndexReader addMockedIndexReader( KernelStatement kernelStatement ) throws IndexNotFoundKernelException
{
StorageStatement storageStatement = mock( StorageStatement.class );
when( kernelStatement.getStoreStatement() ).thenReturn( storageStatement );
return addMockedIndexReader( storageStatement );
}
private IndexReader addMockedIndexReader( StorageStatement storeStatement )
throws IndexNotFoundKernelException
{
IndexReader indexReader = mock( IndexReader.class );
when( storeStatement.getIndexReader( any( IndexDescriptor.class ) ) ).thenReturn( indexReader );
return indexReader;
}
private static class StoreStatementWithSingleFreshIndexReader extends StoreStatement
{
final IndexReader reader;
StoreStatementWithSingleFreshIndexReader( IndexReader reader )
{
super( mock( NeoStores.class ), new ReentrantLockService(), () -> mock( IndexReaderFactory.class ),
() -> mock( LabelScanReader.class ), mock( Pool.class ) );
this.reader = reader;
}
@Override
public IndexReader getFreshIndexReader( IndexDescriptor descriptor ) throws IndexNotFoundKernelException
{
return reader;
}
}
}