private static void printValue( PrintStream out, long value, String description, LongFunction<String> toStringConverter ) { if ( value > 0 ) { printIndented( out, description + ": " + toStringConverter.apply( value ) ); } }
@Override public T readObject(ObjectInput input) throws IOException, ClassNotFoundException { return this.reader.apply(input.readLong()); }
private static <T> void readNonNullLongColumn(Object[] vals, int fieldIdx, LongColumnVector vector, int childCount, LongFunction<T> reader) { if (vector.isRepeating) { // fill complete column with first value T repeatingValue = reader.apply(vector.vector[0]); fillColumnWithRepeatingValue(vals, fieldIdx, repeatingValue, childCount); } else { if (fieldIdx == -1) { // set as an object for (int i = 0; i < childCount; i++) { vals[i] = reader.apply(vector.vector[i]); } } else { // set as a field of Row Row[] rows = (Row[]) vals; for (int i = 0; i < childCount; i++) { rows[i].setField(fieldIdx, reader.apply(vector.vector[i])); } } } }
@Override public T next() { return mapFunction.apply( source.next() ); }
@Override public T next() { return mapper.apply( iterator.next() ); } };
@Override public T next() { return mapFunction.apply( source.next() ); }
@Override public void initialize(LayeredZoneRegionFunction parent) { noise = seededNoiseFunction.apply(parent.getSeed()); }
@Override public boolean next(Predicate<? super T> consumer) { return source.next(e -> consumer.test(mapper.apply(e))); }
default VALUE computeIfAbsent( long key, LongFunction<VALUE> function ) { VALUE value = get( key ); if ( value == null ) { value = function.apply( key ); put( key, value ); } return value; } }
@Override public synchronized boolean next( InputChunk chunk ) { if ( numberOfBatches > 1 ) { numberOfBatches--; long batch = nextBatch++; ((Chunk) chunk).initialize( states.apply( batch ), batch, batchSize ); return true; } else if ( numberOfBatches == 1 ) { numberOfBatches--; int rest = toIntExact( totalCount % batchSize ); int size = rest != 0 ? rest : batchSize; long batch = nextBatch++; ((Chunk) chunk).initialize( states.apply( batch ), batch, size ); return true; } return false; }
@Override protected T fetchNextOrNull() { if ( cursor.next() ) { return mapper.apply( cursor.nodeReference() ); } else { close(); return null; } }
@Override protected T fetchNextOrNull() { if ( cursor.next() ) { return mapper.apply( cursor.relationshipReference() ); } else { close(); return null; } }
private static void failQuery( LongFunction<Entity> getEntity, String index, String query, MutableLongSet ids, long[] expectedIds, MutableLongSet actualIds, String msg ) { StringBuilder message = new StringBuilder( msg ).append( '\n' ); MutableLongIterator itr = ids.longIterator(); while ( itr.hasNext() ) { long id = itr.next(); Entity entity = getEntity.apply( id ); message.append( '\t' ).append( entity ).append( entity.getAllProperties() ).append( '\n' ); } message.append( "for query: '" ).append( query ).append( "'\nin index: " ).append( index ).append( '\n' ); message.append( "all expected ids: " ).append( Arrays.toString( expectedIds ) ).append( '\n' ); message.append( "actual ids: " ).append( actualIds ); itr = actualIds.longIterator(); while ( itr.hasNext() ) { long id = itr.next(); Entity entity = getEntity.apply( id ); message.append( "\n\t" ).append( entity ).append( entity.getAllProperties() ); } fail( message.toString() ); }
@Test public void shouldReturnIdImmediatelyAfterRecordId() { // given PrepareIdSequence idSequence = new SecondaryUnitPrepareIdSequence(); IdSequence actual = mock( IdSequence.class ); // when long recordId = 10; IdSequence prepared = idSequence.apply( actual ).apply( recordId ); long nextRecordId = prepared.nextId(); // then assertEquals( 10 + 1, nextRecordId ); verifyNoMoreInteractions( actual ); }
@Test public void shouldCopeWithCollisionsBasedOnDifferentInputIds() { // GIVEN Monitor monitor = mock( Monitor.class ); Encoder encoder = mock( Encoder.class ); when( encoder.encode( any() ) ).thenReturn( 12345L ); IdMapper mapper = mapper( encoder, Radix.STRING, monitor ); LongFunction<Object> ids = values( "10", "9" ); for ( int i = 0; i < 2; i++ ) { mapper.put( ids.apply( i ), i, GLOBAL ); } // WHEN ProgressListener progress = mock( ProgressListener.class ); Collector collector = mock( Collector.class ); mapper.prepare( ids, collector, progress ); // THEN verifyNoMoreInteractions( collector ); verify( monitor ).numberOfCollisions( 2 ); assertEquals( 0L, mapper.get( "10", GLOBAL ) ); assertEquals( 1L, mapper.get( "9", GLOBAL ) ); // 7 times since SPLIT+SORT+DETECT+RESOLVE+SPLIT+SORT,DEDUPLICATE verify( progress, times( 7 ) ).started( anyString() ); verify( progress, times( 7 ) ).done(); }
@Test public void shouldReportCollisionsForSameInputId() { // GIVEN IdMapper mapper = mapper( new StringEncoder(), Radix.STRING, NO_MONITOR ); LongFunction<Object> values = values( "10", "9", "10" ); for ( int i = 0; i < 3; i++ ) { mapper.put( values.apply( i ), i, GLOBAL ); } // WHEN Collector collector = mock( Collector.class ); mapper.prepare( values, collector, NONE ); // THEN verify( collector, times( 1 ) ).collectDuplicateNode( "10", 2, GLOBAL.name() ); verifyNoMoreInteractions( collector ); }
@Override protected void process( RECORD[] batch, BatchSender sender ) { LongFunction<IdSequence> idSequence = prepareIdSequence.apply( store ); int recordsUpdatedInThisBatch = 0; for ( RECORD record : batch ) { if ( record != null && record.inUse() && !IdValidator.isReservedId( record.getId() ) ) { store.prepareForCommit( record, idSequence.apply( record.getId() ) ); store.updateRecord( record ); recordsUpdatedInThisBatch++; } } recordsUpdated.add( recordsUpdatedInThisBatch ); }
@Override public void run() throws FAILURE { entityCursor.scan(); try ( EntityIdIterator entityIdIterator = getEntityIdIterator() ) { continueScanning = true; while ( continueScanning && entityIdIterator.hasNext() ) { long id = entityIdIterator.next(); try ( Lock ignored = lockFunction.apply( id ) ) { count++; if ( process( entityCursor ) ) { entityIdIterator.invalidateCache(); } } } } finally { IOUtils.closeAllUnchecked( propertyCursor, entityCursor, storageReader ); } }
@Test public void shouldBeAbleToHaveDuplicateInputIdButInDifferentGroups() { // GIVEN Monitor monitor = mock( Monitor.class ); Group firstGroup = groups.getOrCreate( "first" ); Group secondGroup = groups.getOrCreate( "second" ); IdMapper mapper = mapper( new StringEncoder(), Radix.STRING, monitor ); LongFunction<Object> ids = values( "10", "9", "10" ); int id = 0; // group 0 mapper.put( ids.apply( id ), id++, firstGroup ); mapper.put( ids.apply( id ), id++, firstGroup ); // group 1 mapper.put( ids.apply( id ), id++, secondGroup ); Collector collector = mock( Collector.class ); mapper.prepare( ids, collector, NONE ); // WHEN/THEN verifyNoMoreInteractions( collector ); verify( monitor ).numberOfCollisions( 0 ); assertEquals( 0L, mapper.get( "10", firstGroup ) ); assertEquals( 1L, mapper.get( "9", firstGroup ) ); assertEquals( 2L, mapper.get( "10", secondGroup ) ); assertFalse( mapper.leftOverDuplicateNodesIds().hasNext() ); }
@Test public void shouldOnlyFindInputIdsInSpecificGroup() { // GIVEN Group firstGroup = groups.getOrCreate( "first" ); Group secondGroup = groups.getOrCreate( "second" ); Group thirdGroup = groups.getOrCreate( "third" ); IdMapper mapper = mapper( new StringEncoder(), Radix.STRING, NO_MONITOR ); LongFunction<Object> ids = values( "8", "9", "10" ); int id = 0; mapper.put( ids.apply( id ), id++, firstGroup ); mapper.put( ids.apply( id ), id++, secondGroup ); mapper.put( ids.apply( id ), id++, thirdGroup ); mapper.prepare( ids, mock( Collector.class ), NONE ); // WHEN/THEN assertEquals( 0L, mapper.get( "8", firstGroup ) ); assertEquals( ID_NOT_FOUND, mapper.get( "8", secondGroup ) ); assertEquals( ID_NOT_FOUND, mapper.get( "8", thirdGroup ) ); assertEquals( ID_NOT_FOUND, mapper.get( "9", firstGroup ) ); assertEquals( 1L, mapper.get( "9", secondGroup ) ); assertEquals( ID_NOT_FOUND, mapper.get( "9", thirdGroup ) ); assertEquals( ID_NOT_FOUND, mapper.get( "10", firstGroup ) ); assertEquals( ID_NOT_FOUND, mapper.get( "10", secondGroup ) ); assertEquals( 2L, mapper.get( "10", thirdGroup ) ); }