public static boolean chainsEqual(Chain chain1, Chain chain2) { Iterator<Element> it1 = chain1.iterator(); Iterator<Element> it2 = chain2.iterator(); while (it1.hasNext() && it2.hasNext()) { Element next1 = it1.next(); Element next2 = it2.next(); if (!next1.getPayload().equals(next2.getPayload())) { return false; } } return !it1.hasNext() && !it2.hasNext(); }
private Long createAttachedChain(ByteBuffer readKeyBuffer, int hash, Iterator<Element> iterator) { Long address = createAttachedChain(readKeyBuffer, hash, iterator.next().getPayload()); if (address == null) { return null; } if (iterator.hasNext()) { try (AttachedInternalChain chain = new AttachedInternalChain(address)) { do { if (!chain.append(iterator.next().getPayload())) { chain.free(); return null; } } while (iterator.hasNext()); } } return address; }
@Override protected boolean matchesSafely(Element item) { return item.getPayload().equals(buffer(i)); }
@Override protected boolean matchesSafely(Element item) { return item.getPayload().equals(buffer(i)); }
private boolean shouldBePinned(Chain chain) { for (Element element : chain) { if (OperationsCodec.getOperationCode(element.getPayload()).shouldBePinned()) { return true; } } return false; } }
private List<Operation<Long, String>> getOperationsListFromChain(Chain chain) { List<Operation<Long, String>> list = new ArrayList<>(); for (Element element : chain) { Operation<Long, String> operation = codec.decode(element.getPayload()); list.add(operation); } return list; }
@Override public ChainBuilder newChainBuilder() { return elements -> { ByteBuffer[] buffers = new ByteBuffer[elements.length]; for (int i = 0; i < buffers.length; i++) { buffers[i] = elements[i].getPayload(); } return OffHeapChainMap.chain(buffers); }; }
@Override protected boolean matchesSafely(Element item) { return operation.equals(codec.decode(item.getPayload())); }
@Override protected boolean matchesSafely(Element item) { return operation.equals(codec.decode(item.getPayload())); }
/** * Compacts the given chain by resolving every key within. * * @param chain a compacted heterogenous {@code Chain} * @param now time when the chain is being resolved * @return a compacted chain */ public Chain applyOperation(Chain chain, long now) { //absent hash-collisions this should always be a 1 entry map Map<K, PutOperation<K, V>> compacted = new HashMap<>(2); for (Element element : chain) { ByteBuffer payload = element.getPayload(); Operation<K, V> operation = codec.decode(payload); compacted.compute(operation.getKey(), (k, v) -> applyOperation(k, v, operation, now)); } ChainBuilder builder = new ChainBuilder(); for (PutOperation<K, V> operation : compacted.values()) { builder = builder.add(codec.encode(operation)); } return builder.build(); }
private static void assertChainAndReverseChainOnlyHave(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); Iterator<Element> reverseElements = chain.reverseIterator(); for (int i = payLoads.length -1; i >= 0; i--) { assertThat(readPayLoad(reverseElements.next().getPayload()), is(Long.valueOf(payLoads[i]))); } assertThat(reverseElements.hasNext(), is(false)); }
public static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); } }
private static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(Util.readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); } }
private static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); }
public static void encode(StructEncoder<?> encoder, Chain chain) { StructArrayEncoder<? extends StructEncoder<?>> elementsEncoder = encoder.structs("elements"); for (Element element : chain) { StructEncoder<?> elementEncoder = elementsEncoder.add(); if (element instanceof SequencedElement) { elementEncoder.int64("sequence", ((SequencedElement) element).getSequenceNumber()); } elementEncoder.byteBuffer("payload", element.getPayload()); elementEncoder.end(); } elementsEncoder.end(); }
@Test public void testChainWithSingleElement() { Chain chain = getChain(false, createPayload(1L)); assertThat(chain.isEmpty(), is(false)); Iterator<Element> chainIterator = chain.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); chainIterator = decoded.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); }
private Map<Long, String> convert(Chain chain, OperationsCodec<Long, String> codec, ChainResolver<Long, String> resolver, TimeSource timeSource) { Map<Long, String> result = new HashMap<>(); for (Element element : chain) { ByteBuffer payload = element.getPayload(); Operation<Long, String> operation = codec.decode(payload); Long key = operation.getKey(); PutOperation<Long, String> opResult = resolver.applyOperation(key, null, operation, timeSource.getTimeMillis()); result.put(key, opResult.getValue()); } return result; }
@Test public void testChainWithSingleSequencedElement() { Chain chain = getChain(true, createPayload(1L)); assertThat(chain.isEmpty(), is(false)); Iterator<Element> chainIterator = chain.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); chainIterator = decoded.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); assertSameSequenceChain(chain, decoded); }
@Test public void testAppendKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testAppendKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testAppendKeyNotPresent", clientEntity, mock(ServerCallback.class)); serverStoreProxy.append(2, createPayload(2)); Chain chain = serverStoreProxy.get(2); assertThat(chain.isEmpty(), is(false)); assertThat(readPayLoad(chain.iterator().next().getPayload()), is(2L)); }
@Test public void testGetAndAppendMappingExists() throws Exception { ServerStore store = newStore(); populateStore(store); Chain chain = store.getAndAppend(1, createPayload(22)); for (Element element : chain) { assertThat(readPayLoad(element.getPayload()), is(Long.valueOf(1))); } chain = store.get(1); assertChainAndReverseChainOnlyHave(chain, 1, 22); }