/** * Returns a hash code of the <code>Mutable</code>'s value. */ @Override public int hashCode() { T v = get(); if(v == null) return 0; return v.hashCode(); }
/** * Returns <code>true</code> if this <code>Mutable</code> * is equal to the given object. */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Mutable<?>)) return false; final Mutable<?> other = (Mutable<?>) obj; T v = get(); Object otherV = other.get(); if (v == null) { if (otherV != null) return false; } else if (!v.equals(otherV)) return false; return true; }
@Nullable public static <T> T getFirst(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept(1, AbortingVisitors.<T, RuntimeException>batching(item -> { ret.set(item); return false; })); return ret.get(); }
@Nullable public static <T> T getLast(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept(DEFAULT_BATCH_SIZE, AbortingVisitors.<T, RuntimeException>batching(item -> { ret.set(item); return true; })); return ret.get(); }
return ret.get();
return ret.get();
@Override public boolean visit(T item) throws RuntimeException { if (hasSeenFirst) { ret.set(o.min(ret.get(), item)); } else { // Call o.max here so it will throw if item is null and this // ordering hates on nulls. ret.set(o.min(item, item)); hasSeenFirst = true; } return true; } }));
@Override public boolean visit(T item) throws RuntimeException { if (hasSeenFirst) { ret.set(o.max(ret.get(), item)); } else { // Call o.max here so it will throw if item is null and this // ordering hates on nulls. ret.set(o.max(item, item)); hasSeenFirst = true; } return true; } }));
private byte[] internalCopyRange(BatchingVisitable<RowResult<byte[]>> bv, final long maxBytes, final Transaction txn) { final Mutable<byte[]> lastRowName = Mutables.newMutable(null); final MutableLong bytesPut = new MutableLong(0L); bv.batchAccept(readBatchSize, AbortingVisitors.batching( // Replacing this with a lambda results in an unreported exception compile error // even though no exception can be thrown :-( new AbortingVisitor<RowResult<byte[]>, RuntimeException>() { @Override public boolean visit(RowResult<byte[]> rr) throws RuntimeException { return TransactionRangeMigrator.this.internalCopyRow(rr, maxBytes, txn, bytesPut, lastRowName); } })); return lastRowName.get(); }
private byte[] internalCopyRange(BatchingVisitable<RowResult<byte[]>> bv, final long maxBytes, @Output final Map<Cell, byte[]> writeMap) { final Mutable<byte[]> lastRowName = Mutables.newMutable(null); final MutableLong bytesPut = new MutableLong(0L); bv.batchAccept(readBatchSize, AbortingVisitors.batching( // Replacing this with a lambda results in an unreported exception compile error // even though no exception can be thrown :-( new AbortingVisitor<RowResult<byte[]>, RuntimeException>() { @Override public boolean visit(RowResult<byte[]> rr) throws RuntimeException { return KvsRangeMigrator.this.internalCopyRow(rr, maxBytes, writeMap, bytesPut, lastRowName); } })); return lastRowName.get(); }
@Test public void testBatchWrap2() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); final Mutable<Boolean> hasTripped = Mutables.newMutable(); AbortingVisitor<List<? extends Long>, RuntimeException> bv = item -> { hasTripped.set(true); assertEquals("batched item had wrong size", 8, item.size()); return false; }; AbortingVisitor<List<Long>, RuntimeException> wrap = AbortingVisitors.wrapBatching(bv); BatchingVisitableView.of(visitor).batchAccept(1000, wrap); assertTrue("should have been tripped!", hasTripped.get()); }
@Test public void testBatchWrap() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); final Mutable<Boolean> hasTripped = Mutables.newMutable(); AbortingVisitor<List<Object>, RuntimeException> bv = item -> { hasTripped.set(true); assertEquals("batched item had wrong size", 8, item.size()); return false; }; AbortingVisitor<List<Long>, RuntimeException> wrap = AbortingVisitors.wrapBatching(bv); BatchingVisitableView.of(visitor).batchAccept(1000, wrap); assertTrue("should have been tripped!", hasTripped.get()); }
@Test public void testBatchHints() { BatchingVisitable<Long> visitor = ListVisitor.create(Lists.newArrayList(0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L)); Function<List<Long>, List<String>> trans = (input) -> { assertEquals("batched item had wrong size", 2, input.size()); return Lists.transform(input, Functions.toStringFunction()); }; BatchingVisitableView<String> visitable = BatchingVisitableView.of(visitor).transformBatch(trans) .hintBatchSize(2); final Mutable<Boolean> hasTripped = Mutables.newMutable(); visitable.batchAccept(10000, item -> { hasTripped.set(true); assertEquals("batched item had wrong size", 8, item.size()); return false; }); assertTrue("should have been tripped!", hasTripped.get()); }
/** * Returns a hash code of the <code>Mutable</code>'s value. */ @Override public int hashCode() { T v = get(); if(v == null) return 0; return v.hashCode(); }
@Nullable public static <T> T getFirst(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept(1, AbortingVisitors.<T, RuntimeException>batching(item -> { ret.set(item); return false; })); return ret.get(); }
@Nullable public static <T> T getLast(BatchingVisitable<T> visitable, @Nullable T defaultElement) { final Mutable<T> ret = Mutables.newMutable(defaultElement); visitable.batchAccept(DEFAULT_BATCH_SIZE, AbortingVisitors.<T, RuntimeException>batching(item -> { ret.set(item); return true; })); return ret.get(); }
@Override public boolean visit(T item) throws RuntimeException { if (hasSeenFirst) { ret.set(o.min(ret.get(), item)); } else { // Call o.max here so it will throw if item is null and this // ordering hates on nulls. ret.set(o.min(item, item)); hasSeenFirst = true; } return true; } }));
private byte[] internalCopyRange(BatchingVisitable<RowResult<byte[]>> bv, final long maxBytes, @Output final Map<Cell, byte[]> writeMap) { final Mutable<byte[]> lastRowName = Mutables.newMutable(null); final MutableLong bytesPut = new MutableLong(0L); bv.batchAccept(readBatchSize, AbortingVisitors.batching( // Replacing this with a lambda results in an unreported exception compile error // even though no exception can be thrown :-( new AbortingVisitor<RowResult<byte[]>, RuntimeException>() { @Override public boolean visit(RowResult<byte[]> rr) throws RuntimeException { return KvsRangeMigrator.this.internalCopyRow(rr, maxBytes, writeMap, bytesPut, lastRowName); } })); return lastRowName.get(); }