public void replaceAtHead(K key, Chain expected, Chain replacement) { final Lock lock = heads.writeLock(); lock.lock(); try { while (true) { InternalChain chain = heads.get(key); if (chain == null) { if (expected.isEmpty()) { throw new IllegalArgumentException("Empty expected sequence"); } else { return; } } else { try { if (chain.replace(expected, replacement)) { return; } else { evict(); } } finally { chain.close(); } } } } finally { lock.unlock(); } }
if (matched) { Chain newChain = newChainBuilder.build(); return new ResolvedChain.Impl<>(newChain, key, null, chain.length() - newChain.length(), Long.MAX_VALUE); } else { return new ResolvedChain.Impl<>(chain, key, null, 0, Long.MAX_VALUE); return new ResolvedChain.Impl<>(newChain, key, result, chain.length() - newChain.length(), result.expirationTime());
private Chain dropLastElement(Chain chain) { if (!chain.isEmpty()) { List<Element> elements = StreamSupport.stream(chain.spliterator(), false) .collect(Collectors.toList()); elements.remove(elements.size() - 1); // remove last return Util.getChain(elements); } else { return chain; } }
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)); }
@Override protected Iterator<Element> iterator() { return chain.iterator(); } }
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(); }
try { chain = store.get(key); if (chain.isEmpty()) {
@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)); }
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)); } }
@Test public void testLock() throws Exception { ClusterTierClientEntity clusterTierClientEntity = mock(ClusterTierClientEntity.class); LockManagerImpl lockManager = new LockManagerImpl(clusterTierClientEntity); LockSuccess lockSuccess = getLockSuccessResponse(); when(clusterTierClientEntity.invokeAndWaitForComplete(any(LockMessage.class), anyBoolean())) .thenReturn(lockSuccess); Chain lock = lockManager.lock(2L); assertThat(lock, notNullValue()); assertThat(lock.length(), is(3)); }
public void put(K key, Chain chain) { final Lock lock = heads.writeLock(); lock.lock(); try { InternalChain current = heads.get(key); if (current != null) { try { replaceAtHead(key, current.detach(), chain); } finally { current.close(); } } else { if (!chain.isEmpty()) { heads.put(key, chainStorage.newChain(chain)); } } } finally { lock.unlock(); } }
@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); }
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)); }
@Test public void testLockWhenFailure() throws Exception { ClusterTierClientEntity clusterTierClientEntity = mock(ClusterTierClientEntity.class); LockManagerImpl lockManager = new LockManagerImpl(clusterTierClientEntity); LockSuccess lockSuccess = getLockSuccessResponse(); when(clusterTierClientEntity.invokeAndWaitForComplete(any(LockMessage.class), anyBoolean())) .thenReturn(lockFailure(), lockFailure(), lockFailure(), lockSuccess); Chain lock = lockManager.lock(2L); assertThat(lock, notNullValue()); assertThat(lock.length(), is(3)); }
@Test public void testGetKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetKeyNotPresent", clientEntity, mock(ServerCallback.class)); Chain chain = serverStoreProxy.get(1); assertThat(chain.isEmpty(), is(true)); }
@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)); }
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)); } }
if (newChain.length() > chainCompactionLimit) { requestChainResolution(clientDescriptor, key, newChain);
@Test public void testGetAndAppendKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetAndAppendKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetAndAppendKeyNotPresent", clientEntity, mock(ServerCallback.class)); Chain chain = serverStoreProxy.getAndAppend(4L, createPayload(4L)); assertThat(chain.isEmpty(), is(true)); chain = serverStoreProxy.get(4L); assertThat(chain.isEmpty(), is(false)); assertChainHas(chain, 4L); }