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 Register.DoubleLongRegister answer( InvocationOnMock invocationOnMock ) throws Throwable { Register.DoubleLongRegister r = invocationOnMock.getArgument( 1 ); r.write( updates[i], 0 ); i = (i + 1) % updates.length; return r; } } );
@Override public long countsForRelationship( int startLabelId, int typeId, int endLabelId ) { long count = countsForRelationshipWithoutTxState( startLabelId, typeId, endLabelId ); if ( ktx.hasTxStateWithChanges() ) { CountsRecordState counts = new CountsRecordState(); try { TransactionState txState = ktx.txState(); txState.accept( new TransactionCountingStateVisitor( EMPTY, storageReader, txState, counts ) ); if ( counts.hasChanges() ) { count += counts.relationshipCount( startLabelId, typeId, endLabelId, newDoubleLongRegister() ) .readSecond(); } } catch ( ConstraintValidationException | CreateConstraintFailureException e ) { throw new IllegalArgumentException( "Unexpected error: " + e.getMessage() ); } } return count; }
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 long countsForNode( int labelId ) { long count = countsForNodeWithoutTxState( labelId ); if ( ktx.hasTxStateWithChanges() ) { CountsRecordState counts = new CountsRecordState(); try { TransactionState txState = ktx.txState(); txState.accept( new TransactionCountingStateVisitor( EMPTY, storageReader, txState, counts ) ); if ( counts.hasChanges() ) { count += counts.nodeCount( labelId, newDoubleLongRegister() ).readSecond(); } } catch ( ConstraintValidationException | CreateConstraintFailureException e ) { throw new IllegalArgumentException( "Unexpected error: " + e.getMessage() ); } } return count; }
private void verify( CountsKey key, long actualFirst, long actualSecond ) { DoubleLongRegister expected = counts.remove( key ); if ( expected == null ) { if ( actualFirst != 0 || actualSecond != 0 ) { differences.add( new Difference( key, 0, 0, actualFirst, actualSecond ) ); } } else { long expectedFirst = expected.readFirst(); long expectedSecond = expected.readSecond(); if ( expectedFirst != actualFirst || expectedSecond != actualSecond ) { differences.add( new Difference( key, expectedFirst, expectedSecond, actualFirst, actualSecond ) ); } } }
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); } }
@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(); }
@Override public long countsForNode( int labelId ) { return counts.nodeCount( labelId, newDoubleLongRegister() ).readSecond(); }
@Override public long indexSize( SchemaDescriptor descriptor ) throws IndexNotFoundKernelException { Register.DoubleLongRegister result = indexService.indexUpdatesAndSize( descriptor ); return result.readSecond(); }
@Override public void incrementRelationshipCount( long startLabelId, int typeId, long endLabelId, long delta ) { if ( delta != 0 ) { counts( relationshipKey( startLabelId, typeId, endLabelId ) ).increment( 0L, delta ); } }
@Override protected Register.DoubleLongRegister parseValue( ReadableBuffer value ) { target.write( value.getLong( 0 ), value.getLong( 8 ) ); return target; }
private static long readUpdates( IndexReference index, SchemaRead schemaRead, Register.DoubleLongRegister register ) throws IndexNotFoundKernelException { schemaRead.indexUpdatesAndSize( index, register ); return register.readFirst(); } }
@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; } };
public List<Difference> differences() { for ( Map.Entry<CountsKey, DoubleLongRegister> entry : counts.entrySet() ) { DoubleLongRegister value = entry.getValue(); differences.add( new Difference( entry.getKey(), value.readFirst(), value.readSecond(), 0, 0 ) ); } counts.clear(); return differences; } }
@Override public DoubleLongRegister nodeCount( int labelId, DoubleLongRegister target ) { counts( nodeKey( labelId ) ).copyTo( target ); return target; }
@Override protected Register.DoubleLongRegister defaultValue() { target.write( 0, 0 ); return target; } }
@Override public void accept( CountsVisitor visitor ) { for ( Map.Entry<CountsKey, DoubleLongRegister> entry : counts.entrySet() ) { DoubleLongRegister register = entry.getValue(); entry.getKey().accept( visitor, register.readFirst(), register.readSecond() ); } }
@Override public void incrementIndexUpdates( long indexId, long delta ) { counts( indexStatisticsKey( indexId ) ).increment( delta, 0L ); }
@Override public void incrementNodeCount( long labelId, long delta ) { counts( nodeKey( labelId ) ).increment( 0L, delta ); }