@Override public boolean delete(ByteArray key, Version maxVersion) throws PersistenceFailureException { StoreUtils.assertValidKey(key); Connection conn = null; PreparedStatement selectStmt = null; ResultSet rs = null; String select = "select version_ from " + getName() + " where key_ = ? for update"; try { conn = datasource.getConnection(); selectStmt = conn.prepareStatement(select); selectStmt.setBytes(1, key.get()); rs = selectStmt.executeQuery(); boolean deletedSomething = false; while(rs.next()) { byte[] version = rs.getBytes("version_"); if((maxVersion == null) || (new VectorClock(version).compare(maxVersion) == Occurred.BEFORE)) { delete(conn, key.get(), version); deletedSomething = true; } } return deletedSomething; } catch(SQLException e) { throw new PersistenceFailureException("Fix me!", e); } finally { tryClose(rs); tryClose(selectStmt); tryClose(conn); } }
@Test public void testComparisons() { assertTrue("The empty clock should not happen before itself.", getClock().compare(getClock()) != Occurred.CONCURRENTLY); assertTrue("A clock should not happen before an identical clock.", getClock(1, 1, 2).compare(getClock(1, 1, 2)) != Occurred.CONCURRENTLY); assertTrue(" A clock should happen before an identical clock with a single additional event.", getClock(1, 1, 2).compare(getClock(1, 1, 2, 3)) == Occurred.BEFORE); assertTrue("Clocks with different events should be concurrent.", getClock(1).compare(getClock(2)) == Occurred.CONCURRENTLY); assertTrue("Clocks with different events should be concurrent.", getClock(1, 1, 2).compare(getClock(1, 1, 3)) == Occurred.CONCURRENTLY); assertTrue("Clocks with different events should be concurrent.", getClock(1, 2, 3, 3).compare(getClock(1, 1, 2, 3)) == Occurred.CONCURRENTLY); assertTrue(getClock(2, 2).compare(getClock(1, 2, 2, 3)) == Occurred.BEFORE && getClock(1, 2, 2, 3).compare(getClock(2, 2)) == Occurred.AFTER); }
@Test public void testPutIncrementsVersionZZZ() throws Exception { Store<ByteArray, byte[], byte[]> store = getZonedStore(); VectorClock clock = new VectorClock(); VectorClock copy = clock.clone(); store.put(aKey, new Versioned<byte[]>(getValue(), clock), aTransform); List<Versioned<byte[]>> found = store.get(aKey, aTransform); assertEquals("Invalid number of items found.", 1, found.size()); assertEquals("Version not incremented properly", Occurred.BEFORE, copy.compare(found.get(0).getVersion())); }
@Test public void testPutIncrementsVersion() throws Exception { Store<ByteArray, byte[], byte[]> store = getStore(); VectorClock clock = new VectorClock(); VectorClock copy = clock.clone(); store.put(aKey, new Versioned<byte[]>(getValue(), clock), aTransform); List<Versioned<byte[]>> found = store.get(aKey, aTransform); assertEquals("Invalid number of items found.", 1, found.size()); assertEquals("Version not incremented properly", Occurred.BEFORE, copy.compare(found.get(0).getVersion())); }