public static Register.DoubleLongRegister newDoubleLongRegister() { return newDoubleLongRegister( -1L, -1L ); }
private Predicate<Long> createSamplingPredicate() { return new Predicate<Long>() { private final DoubleLongRegister output = newDoubleLongRegister(); @Override public boolean test( Long indexId ) { storeView.indexUpdatesAndSize( indexId, output ); long updates = output.readFirst(); long size = output.readSecond(); long threshold = Math.round( config.updateRatio() * size ); return updates > threshold; } }; }
private DoubleLongRegister counts( CountsKey key ) { return counts.computeIfAbsent( key, k -> Registers.newDoubleLongRegister( DEFAULT_FIRST_VALUE, DEFAULT_SECOND_VALUE ) ); }
private IndexSamplingController.RecoveryCondition createIndexRecoveryCondition( final LogProvider logProvider, final TokenNameLookup tokenNameLookup ) { return new IndexSamplingController.RecoveryCondition() { private final Log log = logProvider.getLog( IndexSamplingController.class ); private final DoubleLongRegister register = newDoubleLongRegister(); @Override public boolean test( StoreIndexDescriptor descriptor ) { boolean result = storeView.indexSample( descriptor.getId(), register ).readSecond() == 0; if ( result ) { log.debug( "Recovering index sampling for index %s", descriptor.schema().userDescription( tokenNameLookup ) ); } return result; } }; } }
@Override public long countsForRelationship( int startLabelId, int typeId, int endLabelId ) { if ( !(startLabelId == StatementConstants.ANY_LABEL || endLabelId == StatementConstants.ANY_LABEL) ) { throw new UnsupportedOperationException( "not implemented" ); } return counts.relationshipCount( startLabelId, typeId, endLabelId, newDoubleLongRegister() ).readSecond(); }
public DoubleLongRegister indexUpdatesAndSize( SchemaDescriptor descriptor ) throws IndexNotFoundKernelException { final long indexId = indexMapRef.getOnlineIndexId( descriptor ); final DoubleLongRegister output = Registers.newDoubleLongRegister(); storeView.indexUpdatesAndSize( indexId, output ); return output; }
@Override public long countsForNode( int labelId ) { return counts.nodeCount( labelId, newDoubleLongRegister() ).readSecond(); }
private void mockLabelNodeCount( CountsTracker countStore, int labelId ) { Register.DoubleLongRegister register = Registers.newDoubleLongRegister( labelId, labelId ); when( countStore.nodeCount( eq( labelId ), any( Register.DoubleLongRegister.class ) ) ).thenReturn( register ); }
private static ThrowingFunction<CountsTracker,Void,RuntimeException> countStoreLookup( AtomicBoolean workerContinueFlag, AtomicLong lookups ) { return countsTracker -> { while ( workerContinueFlag.get() ) { Register.DoubleLongRegister register = Registers.newDoubleLongRegister(); countsTracker.get( CountsKeyFactory.nodeKey( 0 ), register ); lookups.incrementAndGet(); } return null; }; }
public double indexUniqueValuesPercentage( SchemaDescriptor descriptor ) throws IndexNotFoundKernelException { final long indexId = indexMapRef.getOnlineIndexId( descriptor ); final DoubleLongRegister output = Registers.newDoubleLongRegister(); storeView.indexSample( indexId, output ); long unique = output.readFirst(); long size = output.readSecond(); if ( size == 0 ) { return 1.0d; } else { return ((double) unique) / ((double) size); } }
private long get( CountsTracker store, CountsKey key ) { Register.DoubleLongRegister value = Registers.newDoubleLongRegister(); store.get( key, value ); return value.readSecond(); }
private static List<Map<String,Object>> indexes( TokenRead tokens, SchemaRead schemaRead, Anonymizer anonymizer ) throws IndexNotFoundKernelException { List<Map<String,Object>> indexes = new ArrayList<>(); SilentTokenNameLookup tokenLookup = new SilentTokenNameLookup( tokens ); Iterator<IndexReference> iterator = schemaRead.indexesGetAll(); while ( iterator.hasNext() ) { IndexReference index = iterator.next(); Map<String,Object> data = new HashMap<>(); data.put( "labels", map( index.schema().getEntityTokenIds(), id -> anonymizer.label( tokenLookup.labelGetName( id ), id ) ) ); data.put( "properties", map( index.schema().getPropertyIds(), id -> anonymizer.propertyKey( tokenLookup.propertyKeyGetName( id ), id ) ) ); Register.DoubleLongRegister register = Registers.newDoubleLongRegister(); schemaRead.indexUpdatesAndSize( index, register ); data.put( "totalSize", register.readSecond() ); data.put( "updatesSinceEstimation", register.readFirst() ); schemaRead.indexSample( index, register ); data.put( "estimatedUniqueSize", register.readFirst() ); indexes.add( data ); } return indexes; }
@Override public void visitRelationshipCount( int startLabelId, int typeId, int endLabelId, long count ) { long expected = tracker.relationshipCount( startLabelId, typeId, endLabelId, newDoubleLongRegister() ).readSecond(); assertEquals( "Should be able to read visited state.", expected, count ); }
@Override public void visitNodeCount( int labelId, long count ) { long expected = tracker.nodeCount( labelId, newDoubleLongRegister() ).readSecond(); assertEquals( "Should be able to read visited state.", expected, count ); }
@Test public void shouldSnapshotOnlineIndexes() throws Exception { // GIVEN int indexId = 1; int indexId2 = 2; StoreIndexDescriptor rule1 = storeIndex( indexId, 2, 3, PROVIDER_DESCRIPTOR ); StoreIndexDescriptor rule2 = storeIndex( indexId2, 4, 5, PROVIDER_DESCRIPTOR ); IndexAccessor indexAccessor = mock( IndexAccessor.class ); IndexingService indexing = newIndexingServiceWithMockedDependencies( mock( IndexPopulator.class ), indexAccessor, new DataUpdates( ), rule1, rule2 ); File theFile = new File( "Blah" ); when( indexAccessor.snapshotFiles()).thenAnswer( newResourceIterator( theFile ) ); when( indexProvider.getInitialState( rule1 ) ).thenReturn( ONLINE ); when( indexProvider.getInitialState( rule2 ) ).thenReturn( ONLINE ); when( storeView.indexSample( anyLong(), any( DoubleLongRegister.class ) ) ) .thenReturn( newDoubleLongRegister( 32L, 32L ) ); life.start(); // WHEN ResourceIterator<File> files = indexing.snapshotIndexFiles(); // THEN // We get a snapshot per online index assertThat( asCollection( files ), equalTo( asCollection( iterator( theFile, theFile ) ) ) ); }
@Override public void visitIndexStatistics( long indexId, long updates, long size ) { Register.DoubleLongRegister output = tracker.indexUpdatesAndSize( indexId, newDoubleLongRegister() ); assertEquals( "Should be able to read visited state.", output.readFirst(), updates ); assertEquals( "Should be able to read visited state.", output.readSecond(), size ); }
@Override public void visitIndexSample( long indexId, long unique, long size ) { Register.DoubleLongRegister output = tracker.indexSample( indexId, newDoubleLongRegister() ); assertEquals( "Should be able to read visited state.", output.readFirst(), unique ); assertEquals( "Should be able to read visited state.", output.readSecond(), size ); } } );
@Test public void detectInMemoryDirtyVersionRead() { int labelId = 1; long lastClosedTransactionId = 11L; long writeTransactionId = 22L; TransactionVersionContextSupplier versionContextSupplier = new TransactionVersionContextSupplier(); versionContextSupplier.init( () -> lastClosedTransactionId ); VersionContext versionContext = versionContextSupplier.getVersionContext(); try ( Lifespan life = new Lifespan() ) { CountsTracker tracker = life.add( newTracker( versionContextSupplier ) ); try ( CountsAccessor.Updater updater = tracker.apply( writeTransactionId ).get() ) { updater.incrementNodeCount( labelId, 1 ); } versionContext.initRead(); tracker.nodeCount( labelId, Registers.newDoubleLongRegister() ); assertTrue( versionContext.isDirty() ); } }
@Test public void allowNonDirtyInMemoryDirtyVersionRead() { int labelId = 1; long lastClosedTransactionId = 15L; long writeTransactionId = 13L; TransactionVersionContextSupplier versionContextSupplier = new TransactionVersionContextSupplier(); versionContextSupplier.init( () -> lastClosedTransactionId ); VersionContext versionContext = versionContextSupplier.getVersionContext(); try ( Lifespan life = new Lifespan() ) { CountsTracker tracker = life.add( newTracker( versionContextSupplier ) ); try ( CountsAccessor.Updater updater = tracker.apply( writeTransactionId ).get() ) { updater.incrementNodeCount( labelId, 1 ); } versionContext.initRead(); tracker.nodeCount( labelId, Registers.newDoubleLongRegister() ); assertFalse( versionContext.isDirty() ); } }
private DoubleLongRegister fetchIndexSizeValues( GraphDatabaseService db ) throws IndexNotFoundKernelException, TransactionFailureException { try { // Then db = new TestGraphDatabaseFactory().newEmbeddedDatabase( testDirectory.storeDir() ); @SuppressWarnings( "deprecation" ) GraphDatabaseAPI api = (GraphDatabaseAPI) db; try ( org.neo4j.internal.kernel.api.Transaction tx = api.getDependencyResolver().resolveDependency( Kernel.class ) .beginTransaction( explicit, AUTH_DISABLED ) ) { return tx.schemaRead().indexUpdatesAndSize( indexId( tx ), Registers.newDoubleLongRegister() ); } } finally { if ( db != null ) { db.shutdown(); } } }