-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CountsComputerTest.java
409 lines (363 loc) · 16.3 KB
/
CountsComputerTest.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
/*
* 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.store.counts;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import java.io.File;
import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseBuilder;
import org.neo4j.graphdb.factory.GraphDatabaseSettings;
import org.neo4j.graphdb.mockfs.UncloseableDelegatingFileSystemAbstraction;
import org.neo4j.io.fs.FileSystemAbstraction;
import org.neo4j.io.pagecache.PageCache;
import org.neo4j.io.pagecache.tracing.cursor.context.EmptyVersionContextSupplier;
import org.neo4j.kernel.configuration.Config;
import org.neo4j.kernel.impl.store.CountsComputer;
import org.neo4j.kernel.impl.store.MetaDataStore;
import org.neo4j.kernel.impl.store.NeoStores;
import org.neo4j.kernel.impl.store.NodeStore;
import org.neo4j.kernel.impl.store.RelationshipStore;
import org.neo4j.kernel.impl.store.StoreFactory;
import org.neo4j.kernel.impl.store.counts.keys.CountsKey;
import org.neo4j.kernel.impl.store.id.DefaultIdGeneratorFactory;
import org.neo4j.kernel.impl.store.id.IdGeneratorFactory;
import org.neo4j.kernel.impl.transaction.log.TransactionIdStore;
import org.neo4j.kernel.impl.util.monitoring.ProgressReporter;
import org.neo4j.kernel.impl.util.monitoring.SilentProgressReporter;
import org.neo4j.kernel.internal.GraphDatabaseAPI;
import org.neo4j.kernel.lifecycle.Lifespan;
import org.neo4j.logging.NullLogProvider;
import org.neo4j.register.Register;
import org.neo4j.register.Registers;
import org.neo4j.test.TestGraphDatabaseFactory;
import org.neo4j.test.rule.PageCacheRule;
import org.neo4j.test.rule.TestDirectory;
import org.neo4j.test.rule.fs.EphemeralFileSystemRule;
import org.neo4j.unsafe.impl.batchimport.cache.NumberArrayFactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory.nodeKey;
import static org.neo4j.kernel.impl.store.counts.keys.CountsKeyFactory.relationshipKey;
import static org.neo4j.kernel.impl.transaction.log.TransactionIdStore.BASE_TX_ID;
public class CountsComputerTest
{
private static final String COUNTS_STORE_BASE = MetaDataStore.DEFAULT_NAME + StoreFactory.COUNTS_STORE;
private static final NullLogProvider LOG_PROVIDER = NullLogProvider.getInstance();
private static final Config CONFIG = Config.defaults();
private final EphemeralFileSystemRule fsRule = new EphemeralFileSystemRule();
private final PageCacheRule pcRule = new PageCacheRule();
private final TestDirectory testDir = TestDirectory.testDirectory( fsRule.get() );
@Rule
public RuleChain ruleChain = RuleChain.outerRule( pcRule ).around( fsRule ).around( testDir );
private FileSystemAbstraction fs;
private File dir;
private GraphDatabaseBuilder dbBuilder;
private PageCache pageCache;
@Before
public void setup()
{
fs = fsRule.get();
dir = testDir.directory( "dir" ).getAbsoluteFile();
dbBuilder = new TestGraphDatabaseFactory().setFileSystem( new UncloseableDelegatingFileSystemAbstraction( fs ) )
.newImpermanentDatabaseBuilder( dir );
pageCache = pcRule.getPageCache( fs );
}
@Test
public void skipPopulationWhenNodeAndRelationshipStoresAreEmpty()
{
GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
InvocationTrackingProgressReporter progressReporter = new InvocationTrackingProgressReporter();
rebuildCounts( lastCommittedTransactionId, progressReporter );
checkEmptyCountStore();
assertTrue( progressReporter.isCompleteInvoked() );
assertFalse( progressReporter.isStartInvoked() );
}
@Test
public void shouldCreateAnEmptyCountsStoreFromAnEmptyDatabase()
{
@SuppressWarnings( "deprecation" )
final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
rebuildCounts( lastCommittedTransactionId );
checkEmptyCountStore();
}
@Test
public void shouldCreateACountsStoreWhenThereAreNodesInTheDB()
{
@SuppressWarnings( "deprecation" )
final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
db.createNode( Label.label( "A" ) );
db.createNode( Label.label( "C" ) );
db.createNode( Label.label( "D" ) );
db.createNode();
tx.success();
}
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
rebuildCounts( lastCommittedTransactionId );
try ( Lifespan life = new Lifespan() )
{
CountsTracker store = life.add( createCountsTracker() );
assertEquals( BASE_TX_ID + 1 + 1 + 1 + 1, store.txId() );
assertEquals( 4, store.totalEntriesStored() );
assertEquals( 4, get( store, nodeKey( -1 ) ) );
assertEquals( 1, get( store, nodeKey( 0 ) ) );
assertEquals( 1, get( store, nodeKey( 1 ) ) );
assertEquals( 1, get( store, nodeKey( 2 ) ) );
assertEquals( 0, get( store, nodeKey( 3 ) ) );
}
}
@Test
public void shouldCreateACountsStoreWhenThereAreUnusedNodeRecordsInTheDB()
{
@SuppressWarnings( "deprecation" )
final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
db.createNode( Label.label( "A" ) );
db.createNode( Label.label( "C" ) );
Node node = db.createNode( Label.label( "D" ) );
db.createNode();
node.delete();
tx.success();
}
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
rebuildCounts( lastCommittedTransactionId );
try ( Lifespan life = new Lifespan() )
{
CountsTracker store = life.add( createCountsTracker() );
assertEquals( BASE_TX_ID + 1 + 1 + 1 + 1, store.txId() );
assertEquals( 3, store.totalEntriesStored() );
assertEquals( 3, get( store, nodeKey( -1 ) ) );
assertEquals( 1, get( store, nodeKey( 0 ) ) );
assertEquals( 1, get( store, nodeKey( 1 ) ) );
assertEquals( 0, get( store, nodeKey( 2 ) ) );
assertEquals( 0, get( store, nodeKey( 3 ) ) );
}
}
@Test
public void shouldCreateACountsStoreWhenThereAreUnusedRelationshipRecordsInTheDB()
{
@SuppressWarnings( "deprecation" )
final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
Node nodeA = db.createNode( Label.label( "A" ) );
Node nodeC = db.createNode( Label.label( "C" ) );
Relationship rel = nodeA.createRelationshipTo( nodeC, RelationshipType.withName( "TYPE1" ) );
nodeC.createRelationshipTo( nodeA, RelationshipType.withName( "TYPE2" ) );
rel.delete();
tx.success();
}
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
rebuildCounts( lastCommittedTransactionId );
try ( Lifespan life = new Lifespan() )
{
CountsTracker store = life.add( createCountsTracker() );
assertEquals( BASE_TX_ID + 1 + 1 + 1 + 1 + 1, store.txId() );
assertEquals( 9, store.totalEntriesStored() );
assertEquals( 2, get( store, nodeKey( -1 ) ) );
assertEquals( 1, get( store, nodeKey( 0 ) ) );
assertEquals( 1, get( store, nodeKey( 1 ) ) );
assertEquals( 0, get( store, nodeKey( 2 ) ) );
assertEquals( 0, get( store, nodeKey( 3 ) ) );
assertEquals( 0, get( store, relationshipKey( -1, 0, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 1, -1 ) ) );
}
}
@Test
public void shouldCreateACountsStoreWhenThereAreNodesAndRelationshipsInTheDB()
{
@SuppressWarnings( "deprecation" )
final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
Node nodeA = db.createNode( Label.label( "A" ) );
Node nodeC = db.createNode( Label.label( "C" ) );
Node nodeD = db.createNode( Label.label( "D" ) );
Node node = db.createNode();
nodeA.createRelationshipTo( nodeD, RelationshipType.withName( "TYPE" ) );
node.createRelationshipTo( nodeC, RelationshipType.withName( "TYPE2" ) );
tx.success();
}
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
rebuildCounts( lastCommittedTransactionId );
try ( Lifespan life = new Lifespan() )
{
CountsTracker store = life.add( createCountsTracker() );
assertEquals( BASE_TX_ID + 1 + 1 + 1 + 1 + 1 + 1, store.txId() );
assertEquals( 13, store.totalEntriesStored() );
assertEquals( 4, get( store, nodeKey( -1 ) ) );
assertEquals( 1, get( store, nodeKey( 0 ) ) );
assertEquals( 1, get( store, nodeKey( 1 ) ) );
assertEquals( 1, get( store, nodeKey( 2 ) ) );
assertEquals( 0, get( store, nodeKey( 3 ) ) );
assertEquals( 2, get( store, relationshipKey( -1, -1, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 0, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 1, -1 ) ) );
assertEquals( 0, get( store, relationshipKey( -1, 2, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 1, 1 ) ) );
assertEquals( 0, get( store, relationshipKey( -1, 0, 1 ) ) );
}
}
@Test
public void shouldCreateACountStoreWhenDBContainsDenseNodes()
{
@SuppressWarnings( "deprecation" )
final GraphDatabaseAPI db = (GraphDatabaseAPI) dbBuilder.
setConfig( GraphDatabaseSettings.dense_node_threshold, "2" ).newGraphDatabase();
try ( Transaction tx = db.beginTx() )
{
Node nodeA = db.createNode( Label.label( "A" ) );
Node nodeC = db.createNode( Label.label( "C" ) );
Node nodeD = db.createNode( Label.label( "D" ) );
nodeA.createRelationshipTo( nodeA, RelationshipType.withName( "TYPE1" ) );
nodeA.createRelationshipTo( nodeC, RelationshipType.withName( "TYPE2" ) );
nodeA.createRelationshipTo( nodeD, RelationshipType.withName( "TYPE3" ) );
nodeD.createRelationshipTo( nodeC, RelationshipType.withName( "TYPE4" ) );
tx.success();
}
long lastCommittedTransactionId = getLastTxId( db );
db.shutdown();
rebuildCounts( lastCommittedTransactionId );
try ( Lifespan life = new Lifespan() )
{
CountsTracker store = life.add( createCountsTracker() );
assertEquals( BASE_TX_ID + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1, store.txId() );
assertEquals( 22, store.totalEntriesStored() );
assertEquals( 3, get( store, nodeKey( -1 ) ) );
assertEquals( 1, get( store, nodeKey( 0 ) ) );
assertEquals( 1, get( store, nodeKey( 1 ) ) );
assertEquals( 1, get( store, nodeKey( 2 ) ) );
assertEquals( 0, get( store, nodeKey( 3 ) ) );
assertEquals( 4, get( store, relationshipKey( -1, -1, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 0, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 1, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 2, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 3, -1 ) ) );
assertEquals( 0, get( store, relationshipKey( -1, 4, -1 ) ) );
assertEquals( 1, get( store, relationshipKey( -1, 1, 1 ) ) );
assertEquals( 2, get( store, relationshipKey( -1, -1, 1 ) ) );
assertEquals( 3, get( store, relationshipKey( 0, -1, -1 ) ) );
}
}
private File alphaStoreFile()
{
return new File( dir, COUNTS_STORE_BASE + CountsTracker.LEFT );
}
private File betaStoreFile()
{
return new File( dir, COUNTS_STORE_BASE + CountsTracker.RIGHT );
}
private long getLastTxId( @SuppressWarnings( "deprecation" ) GraphDatabaseAPI db )
{
return db.getDependencyResolver().resolveDependency( TransactionIdStore.class ).getLastCommittedTransactionId();
}
private void checkEmptyCountStore()
{
try ( Lifespan life = new Lifespan() )
{
CountsTracker store = life.add( createCountsTracker() );
assertEquals( BASE_TX_ID, store.txId() );
assertEquals( 0, store.totalEntriesStored() );
}
}
private void cleanupCountsForRebuilding()
{
fs.deleteFile( alphaStoreFile() );
fs.deleteFile( betaStoreFile() );
}
private CountsTracker createCountsTracker()
{
return new CountsTracker( LOG_PROVIDER, fs, pageCache,
CONFIG, new File( dir, COUNTS_STORE_BASE ), EmptyVersionContextSupplier.EMPTY );
}
private void rebuildCounts( long lastCommittedTransactionId )
{
rebuildCounts( lastCommittedTransactionId, SilentProgressReporter.INSTANCE );
}
private void rebuildCounts( long lastCommittedTransactionId, ProgressReporter progressReporter )
{
cleanupCountsForRebuilding();
IdGeneratorFactory idGenFactory = new DefaultIdGeneratorFactory( fs );
StoreFactory storeFactory = new StoreFactory( dir, CONFIG, idGenFactory, pageCache, fs, LOG_PROVIDER, EmptyVersionContextSupplier.EMPTY );
try ( Lifespan life = new Lifespan();
NeoStores neoStores = storeFactory.openAllNeoStores() )
{
NodeStore nodeStore = neoStores.getNodeStore();
RelationshipStore relationshipStore = neoStores.getRelationshipStore();
int highLabelId = (int) neoStores.getLabelTokenStore().getHighId();
int highRelationshipTypeId = (int) neoStores.getRelationshipTypeTokenStore().getHighId();
CountsComputer countsComputer = new CountsComputer(
lastCommittedTransactionId, nodeStore, relationshipStore, highLabelId, highRelationshipTypeId, NumberArrayFactory.AUTO_WITHOUT_PAGECACHE,
progressReporter );
CountsTracker countsTracker = createCountsTracker();
life.add( countsTracker.setInitializer( countsComputer ) );
}
}
private long get( CountsTracker store, CountsKey key )
{
Register.DoubleLongRegister value = Registers.newDoubleLongRegister();
store.get( key, value );
return value.readSecond();
}
private static class InvocationTrackingProgressReporter implements ProgressReporter
{
private boolean startInvoked;
private boolean completeInvoked;
@Override
public void start( long max )
{
startInvoked = true;
}
@Override
public void progress( long add )
{
}
@Override
public void completed()
{
completeInvoked = true;
}
boolean isStartInvoked()
{
return startInvoked;
}
boolean isCompleteInvoked()
{
return completeInvoked;
}
}
}