public DatabaseVersionHeader() { this.date = new Date(); this.vectorClock = new VectorClock(); this.client = "UnknownMachine"; }
protected VectorClock getVectorClockByDatabaseVersionId(int databaseVersionId) throws SQLException { PreparedStatement preparedStatement = getStatement("databaseversion.select.all.getVectorClockByDatabaseVersionId.sql"); preparedStatement.setInt(1, databaseVersionId); ResultSet resultSet = preparedStatement.executeQuery(); VectorClock vectorClock = new VectorClock(); while (resultSet.next()) { vectorClock.setClock(resultSet.getString("client"), resultSet.getLong("logicaltime")); } resultSet.close(); preparedStatement.close(); return vectorClock; }
public VectorClock getHighestKnownDatabaseFilenameNumbers() { VectorClock highestKnownDatabaseFilenameNumbers = new VectorClock(); try (PreparedStatement preparedStatement = getStatement("application.select.all.getHighestKnownDatabaseFilenameNumbers.sql")) { try (ResultSet resultSet = preparedStatement.executeQuery()) { while (resultSet.next()) { String clientName = resultSet.getString("client"); int fileNumber = resultSet.getInt("filenumber"); highestKnownDatabaseFilenameNumbers.put(clientName, (long) fileNumber); } return highestKnownDatabaseFilenameNumbers; } } catch (Exception e) { throw new RuntimeException(e); } }
VectorClock vectorClock = new VectorClock();
public static VectorClock createVectorClock(String vectorClockString) throws Exception { String[] vectorClockElements = vectorClockString.split(","); VectorClock vectorClock = new VectorClock(); for (String vectorClockElement : vectorClockElements) { if ("".equals(vectorClockElement.trim())) { continue; } Matcher vectorClockElementMatcher = vectorClockElementPattern.matcher(vectorClockElement); if (!vectorClockElementMatcher.matches()) { throw new Exception("Invalid vector clock element string: " + vectorClockElement); } String vectorClockMachineName = vectorClockElementMatcher.group(1); long vectorClockTime = Long.parseLong(vectorClockElementMatcher.group(2)); vectorClock.setClock(vectorClockMachineName, vectorClockTime); } return vectorClock; }
@Test public void testNonExistingVectorClockUsage() { VectorClock vc = new VectorClock(); assertEquals("Expected clock value to be different.", 0L, (long) vc.getClock("NonExistingUnit")); }
@Test public void testIncrementNonExistingUnit() { VectorClock vc = new VectorClock(); vc.incrementClock("NonExistingUnit"); assertEquals("Expected clock value to be different.", 1L, (long) vc.getClock("NonExistingUnit")); }
@Test public void testCompareSmallerClocksWithSameUnitCount() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = new VectorClock(); vc2.setClock("UnitA", 4L); vc2.setClock("UnitB", 100000L); // greater! assertEquals("Expected clock 1 to be smaller than clock 2.", VectorClockComparison.SMALLER, VectorClock.compare(vc1, vc2)); assertEquals("Expected clock 2 to be greater than clock 1.", VectorClockComparison.GREATER, VectorClock.compare(vc2, vc1)); }
@Test public void testSimultaneousClocks() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = new VectorClock(); vc2.setClock("UnitC", 1L); vc2.setClock("UnitD", 2L); assertEquals("Expected clock 1 and 2 to be simulataneous.", VectorClockComparison.SIMULTANEOUS, VectorClock.compare(vc1, vc2)); assertEquals("Expected clock 2 and 1 to be simulataneous.", VectorClockComparison.SIMULTANEOUS, VectorClock.compare(vc2, vc1)); }
@Test public void testCompareEqualClocks() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = new VectorClock(); vc2.setClock("UnitA", 4L); // same vc2.setClock("UnitB", 5L); // same assertEquals("Expected clock 1 and 2 to be equal.", VectorClockComparison.EQUAL, VectorClock.compare(vc1, vc2)); assertEquals("Expected clock 2 and 1 to be equal.", VectorClockComparison.EQUAL, VectorClock.compare(vc2, vc1)); }
@Test public void testCompareGreaterClocksWithSameUnitCount() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = new VectorClock(); vc2.setClock("UnitA", 4L); vc2.setClock("UnitB", 100000L); // greater! assertEquals("Expected clock 2 to be greater than clock 1.", VectorClockComparison.GREATER, VectorClock.compare(vc2, vc1)); assertEquals("Expected clock 1 to be smaller than clock 2.", VectorClockComparison.SMALLER, VectorClock.compare(vc1, vc2)); }
@Test public void testCompareGreaterClocksWithDifferentUnitCount() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = new VectorClock(); vc2.setClock("UnitA", 4L); // same vc2.setClock("UnitB", 5L); // same vc2.setClock("UnitC", 1L); // not in vc1 assertEquals("Expected clock 2 to be greater than clock 1.", VectorClockComparison.GREATER, VectorClock.compare(vc2, vc1)); assertEquals("Expected clock 1 to be smaller than clock 2.", VectorClockComparison.SMALLER, VectorClock.compare(vc1, vc2)); }
@Test public void testCompareSmallerClocksWithDifferentUnitCount() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = new VectorClock(); vc2.setClock("UnitA", 4L); // same vc2.setClock("UnitB", 5L); // same vc2.setClock("UnitC", 100000L); // not in vc1 assertEquals("Expected clock 1 to be smaller than clock 2.", VectorClockComparison.SMALLER, VectorClock.compare(vc1, vc2)); assertEquals("Expected clock 2 to be greater than clock 1.", VectorClockComparison.GREATER, VectorClock.compare(vc2, vc1)); }
@Test public void testToString() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitBBB", 5L); vc1.setClock("UnitAAA", 4L); assertEquals("Expected different serialization", "(UnitAAA4,UnitBBB5)", vc1.toString()); }
VectorClock lastVectorClock = (lastDatabaseVersionHeader != null) ? lastDatabaseVersionHeader.getVectorClock() : new VectorClock();
@Before public void setUp() { // Set up basic DatabaseVersionHeaders and VectorClocks. vc1 = new VectorClock(); vc1.setClock("A", 5); vc1.setClock("B", 7); dbvh1 = new DatabaseVersionHeader(); dbvh1.setClient("A"); dbvh1.setDate(new Date(0xDEADDEAD)); dbvh1.setVectorClock(vc1); vc2 = new VectorClock(); vc2.setClock("A", 5); vc2.setClock("B", 7); dbvh2 = new DatabaseVersionHeader(); dbvh2.setClient("A"); dbvh2.setDate(new Date(0xDEADDEAD)); dbvh2.setVectorClock(vc2); }
public static DatabaseVersion createDatabaseVersion(DatabaseVersionHeader basedOnDatabaseVersionHeader, Date date) { VectorClock vectorClock = (basedOnDatabaseVersionHeader != null) ? basedOnDatabaseVersionHeader.getVectorClock().clone() : new VectorClock(); vectorClock.incrementClock("someclient"); DatabaseVersion databaseVersion = new DatabaseVersion(); databaseVersion.setClient("someclient"); databaseVersion.setTimestamp(date); databaseVersion.setVectorClock(vectorClock); return databaseVersion; } }
@Test public void testClone() { VectorClock vc1 = new VectorClock(); vc1.setClock("UnitA", 4L); vc1.setClock("UnitB", 5L); VectorClock vc2 = vc1.clone(); assertEquals("Expected clock value of cloned clock to be different.", 4L, (long) vc2.getClock("UnitA")); assertEquals("Expected clock value of cloned clock to be different.", 5L, (long) vc2.getClock("UnitB")); } }
@Test public void testIncrementUnit() { VectorClock vc = new VectorClock(); vc.setClock("UnitA", 1); vc.incrementClock("UnitA"); // 2 vc.incrementClock("UnitA"); // 3 vc.setClock("UnitB", 2); vc.incrementClock("UnitB"); // 3 assertEquals("Expected clock value to be different.", 3L, (long) vc.getClock("UnitA")); assertEquals("Expected clock value to be different.", 3L, (long) vc.getClock("UnitB")); }
@Test public void testNormalVectorClockUsage() { VectorClock vc = new VectorClock(); vc.setClock("UnitA", 1); vc.setClock("UnitB", 2); vc.setClock("UnitC", 3); assertEquals("Expected clock value to be different.", 1L, (long) vc.getClock("UnitA")); assertEquals("Expected clock value to be different.", 2L, (long) vc.getClock("UnitB")); assertEquals("Expected clock value to be different.", 3L, (long) vc.getClock("UnitC")); }