private static <A extends Annotation, E extends Throwable> long callLong( AnnotationInterceptor<A> annotationInterceptor, int annotationId, A[] annotations, CallContext context, Arguments currentArguments, ToLongFunction<Arguments> terminalInvokeFun) throws E { A annotation = annotations[annotationId]; if (annotationId == annotations.length - 1) { // last annotation return annotationInterceptor.onCall(annotation, context, new SimpleLongInterceptionHandler(currentArguments, terminalInvokeFun)); } else { return annotationInterceptor.onCall(annotation, context, new SimpleLongInterceptionHandler(currentArguments, (args) -> callLong(annotationInterceptor, annotationId + 1, annotations, context, args, terminalInvokeFun))); } }
@Override public final LongStream mapToLong(ToLongFunction<? super P_OUT> mapper) { Objects.requireNonNull(mapper); return new LongPipeline.StatelessOp<P_OUT>(this, StreamShape.REFERENCE, StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT) { @Override Sink<P_OUT> opWrapSink(int flags, Sink<Long> sink) { return new Sink.ChainedReference<P_OUT, Long>(sink) { @Override public void accept(P_OUT u) { downstream.accept(mapper.applyAsLong(u)); } }; } }; }
public void testLongComparator() { Thing[] things = new Thing[longValues.length]; for (int i=0; i<longValues.length; i++) things[i] = new Thing(0, longValues[i], 0.0, null); Comparator<Thing> comp = Comparator.comparingLong(new ToLongFunction<Thing>() { @Override public long applyAsLong(Thing thing) { return thing.getLongField(); } }); assertComparisons(things, comp, comparisons); }
/** * Maps the specified column to longs using the mapper function provided * @param frame the frame reference * @param colKey the column key to apply mapper function to * @param mapper the mapper function to apply * @return the newly created content, with update column */ @SuppressWarnings("unchecked") final XDataFrameContent<R,C> mapToLongs(XDataFrame<R,C> frame, C colKey, ToLongFunction<DataFrameValue<R,C>> mapper) { if (!isColumnStore()) { throw new DataFrameException("Cannot apply columns of a transposed DataFrame"); } else { final int rowCount = rowKeys.size(); final boolean parallel = frame.isParallel(); final int colIndex = colKeys.getIndexForKey(colKey); return new XDataFrameContent<>(rowKeys, colKeys, true, Mapper.apply(data, parallel, (index, array) -> { if (index != colIndex) { return array; } else { final int colOrdinal = colKeys.getOrdinalForKey(colKey); final Array<?> targetValues = Array.of(Long.class, array.length()); final Cursor cursor = new Cursor(frame, rowKeys.isEmpty() ? -1 : 0, colOrdinal); for (int i = 0; i < rowCount; ++i) { cursor.atRowOrdinal(i); final long value = mapper.applyAsLong(cursor); targetValues.setLong(cursor.rowIndex, value); } return targetValues; } })); } }
@Test public void testBinaryInputEstimation() { OptimizationContext optimizationContext = mock(OptimizationContext.class); when(optimizationContext.getConfiguration()).thenReturn(new Configuration()); CardinalityEstimate inputEstimate1 = new CardinalityEstimate(50, 60, 0.8); CardinalityEstimate inputEstimate2 = new CardinalityEstimate(10, 100, 0.4); final ToLongFunction<long[]> singlePointEstimator = inputEstimates -> (long) Math.ceil(0.8 * inputEstimates[0] * inputEstimates[1]); CardinalityEstimator estimator = new DefaultCardinalityEstimator( 0.9, 2, false, singlePointEstimator ); CardinalityEstimate estimate = estimator.estimate(optimizationContext, inputEstimate1, inputEstimate2); Assert.assertEquals(0.9 * 0.4, estimate.getCorrectnessProbability(), 0.001); Assert.assertEquals(singlePointEstimator.applyAsLong(new long[]{50, 10}), estimate.getLowerEstimate()); Assert.assertEquals(singlePointEstimator.applyAsLong(new long[]{60, 100}), estimate.getUpperEstimate()); }
/** * */ @Test @SuppressWarnings(CompilerWarnings.UNCHECKED) public void shouldUseCallWrappedFunction() { // given final ConcurrentMap<String, Long> cache = new ConcurrentHashMap<>(); final ToLongFunction<String> function = Mockito.mock(ToLongFunction.class); final Function<String, String> keyFunction = Function.identity(); // when final ConcurrentMapBasedToLongFunctionMemoizer<String, String> memoizer = new ConcurrentMapBasedToLongFunctionMemoizer<>( cache, keyFunction, function); // then memoizer.applyAsLong("123"); Mockito.verify(function).applyAsLong("123"); }
@Override protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { DropwizardFunctionTimer ft = new DropwizardFunctionTimer<>(id, clock, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits, getBaseTimeUnit()); registry.register(hierarchicalName(id), ft.getDropwizardMeter()); return ft; }
public CumulativeFunctionTimer(Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits, TimeUnit baseTimeUnit) { this.id = id; this.ref = new WeakReference<>(obj); this.countFunction = countFunction; this.totalTimeFunction = totalTimeFunction; this.totalTimeFunctionUnits = totalTimeFunctionUnits; this.baseTimeUnit = baseTimeUnit; }
public StepFunctionTimer(Id id, Clock clock, long stepMillis, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits, TimeUnit baseTimeUnit) { this.id = id; this.ref = new WeakReference<>(obj); this.countFunction = countFunction; this.totalTimeFunction = totalTimeFunction; this.totalTimeFunctionUnits = totalTimeFunctionUnits; this.baseTimeUnit = baseTimeUnit; this.count = new StepLong(clock, stepMillis); this.total = new StepDouble(clock, stepMillis); }
/** * A timer that tracks monotonically increasing functions for count and totalTime. */ public <T> FunctionTimer timer(String name, Iterable<Tag> tags, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { return globalRegistry.more().timer(name, tags, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits); }
private Builder(String name, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { this.name = name; this.obj = obj; this.countFunction = countFunction; this.totalTimeFunction = totalTimeFunction; this.totalTimeFunctionUnits = totalTimeFunctionUnits; }
CompositeFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { super(id); this.ref = new WeakReference<>(obj); this.countFunction = countFunction; this.totalTimeFunction = totalTimeFunction; this.totalTimeFunctionUnits = totalTimeFunctionUnits; }
StatsdFunctionTimer(Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits, TimeUnit baseTimeUnit, StatsdLineBuilder lineBuilder, Subscriber<String> publisher) { super(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits, baseTimeUnit); this.lineBuilder = lineBuilder; this.publisher = publisher; }
@Override protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { StatsdFunctionTimer ft = new StatsdFunctionTimer<>(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits, getBaseTimeUnit(), lineBuilder(id), publisher); pollableMeters.add(ft); return ft; }
@Override protected <T> FunctionTimer newFunctionTimer(Meter.Id id, T obj, ToLongFunction<T> countFunction, ToDoubleFunction<T> totalTimeFunction, TimeUnit totalTimeFunctionUnits) { MicrometerCollector collector = collectorByName(id, Collector.Type.SUMMARY); FunctionTimer ft = new CumulativeFunctionTimer<>(id, obj, countFunction, totalTimeFunction, totalTimeFunctionUnits, getBaseTimeUnit()); List<String> tagValues = tagValues(id); collector.add((conventionName, tagKeys) -> Stream.of( new Collector.MetricFamilySamples.Sample(conventionName + "_count", tagKeys, tagValues, ft.count()), new Collector.MetricFamilySamples.Sample(conventionName + "_sum", tagKeys, tagValues, ft.totalTime(TimeUnit.SECONDS)) )); return ft; }
private static <E extends Throwable> long callLong(int interceptorId, Interceptor[] interceptors, CallContext context, Arguments currentArguments, ToLongFunction<Arguments> terminalInvokeFun) throws E { Interceptor interceptor = interceptors[interceptorId]; if (interceptorId == interceptors.length - 1) { // last interceptor return interceptor.onCall(context, new SimpleLongInterceptionHandler(currentArguments, terminalInvokeFun)); } else { return interceptor.onCall(context, new SimpleLongInterceptionHandler(currentArguments, (args) -> callLong(interceptorId + 1, interceptors, context, args, terminalInvokeFun))); } }
MapReduceKeysToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceKeysToLongTask<K,V> nextRight, ToLongFunction<? super K> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToLongFunction<? super K> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceKeysToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p.key)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceKeysToLongTask<K,V> t = (MapReduceKeysToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceValuesToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceValuesToLongTask<K,V> nextRight, ToLongFunction<? super V> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToLongFunction<? super V> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceValuesToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p.val)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceValuesToLongTask<K,V> t = (MapReduceValuesToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }
MapReduceEntriesToLongTask (BulkTask<K,V,?> p, int b, int i, int f, Node<K,V>[] t, MapReduceEntriesToLongTask<K,V> nextRight, ToLongFunction<Map.Entry<K,V>> transformer, long basis, LongBinaryOperator reducer) { super(p, b, i, f, t); this.nextRight = nextRight; this.transformer = transformer; this.basis = basis; this.reducer = reducer; }
public final void compute() { final ToLongFunction<Map.Entry<K,V>> transformer; final LongBinaryOperator reducer; if ((transformer = this.transformer) != null && (reducer = this.reducer) != null) { long r = this.basis; for (int i = baseIndex, f, h; batch > 0 && (h = ((f = baseLimit) + i) >>> 1) > i;) { addToPendingCount(1); (rights = new MapReduceEntriesToLongTask<K,V> (this, batch >>>= 1, baseLimit = h, f, tab, rights, transformer, r, reducer)).fork(); } for (Node<K,V> p; (p = advance()) != null; ) r = reducer.applyAsLong(r, transformer.applyAsLong(p)); result = r; CountedCompleter<?> c; for (c = firstComplete(); c != null; c = c.nextComplete()) { @SuppressWarnings("unchecked") MapReduceEntriesToLongTask<K,V> t = (MapReduceEntriesToLongTask<K,V>)c, s = t.rights; while (s != null) { t.result = reducer.applyAsLong(t.result, s.result); s = t.rights = s.nextRight; } } } }