Search in sources :

Example 21 with VectorClock

use of org.syncany.database.VectorClock in project syncany by syncany.

the class TestDatabaseUtil method createVectorClock.

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;
}
Also used : Matcher(java.util.regex.Matcher) VectorClock(org.syncany.database.VectorClock) IOException(java.io.IOException)

Example 22 with VectorClock

use of org.syncany.database.VectorClock in project syncany by syncany.

the class TestDatabaseUtil method createDatabaseVersion.

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;
}
Also used : VectorClock(org.syncany.database.VectorClock) DatabaseVersion(org.syncany.database.DatabaseVersion)

Example 23 with VectorClock

use of org.syncany.database.VectorClock in project syncany by syncany.

the class DatabaseVersionSqlDao method getVectorClockByDatabaseVersionId.

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;
}
Also used : VectorClock(org.syncany.database.VectorClock) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 24 with VectorClock

use of org.syncany.database.VectorClock in project syncany by syncany.

the class DatabaseFileReader method next.

/**
 * Loads the winner's database branch into the memory in a {@link MemoryDatabase} object, by using
 * the already downloaded list of remote database files.
 *
 * <p>Because database files can contain multiple {@link DatabaseVersion}s per client, a range for which
 * to load the database versions must be determined.
 *
 * <p><b>Example 1:</b><br />
 * <pre>
 *  db-A-0001   (A1)     Already known             Not loaded
 *  db-A-0005   (A2)     Already known             Not loaded
 *              (A3)     Already known             Not loaded
 *              (A4)     Part of winner's branch   Loaded
 *              (A5)     Purge database version    Ignored (only DEFAULT)
 *  db-B-0001   (A5,B1)  Part of winner's branch   Loaded
 *  db-A-0006   (A6,B1)  Part of winner's branch   Loaded
 * </pre>
 *
 * <p>In example 1, only (A4)-(A5) must be loaded from db-A-0005, and not all four database versions.
 *
 * <p><b>Other example:</b><br />
 * <pre>
 *  db-A-0005   (A1)     Part of winner's branch   Loaded
 *  db-A-0005   (A2)     Part of winner's branch   Loaded
 *  db-B-0001   (A2,B1)  Part of winner's branch   Loaded
 *  db-A-0005   (A3,B1)  Part of winner's branch   Loaded
 *  db-A-0005   (A4,B1)  Part of winner's branch   Loaded
 *  db-A-0005   (A5,B1)  Purge database version    Ignored (only DEFAULT)
 * </pre>
 *
 * <p>In example 2, (A1)-(A5,B1) [except (A2,B1)] are contained in db-A-0005 (after merging!), so
 * db-A-0005 must be processed twice; each time loading separate parts of the file. In this case:
 * First load (A1)-(A2) from db-A-0005, then load (A2,B1) from db-B-0001, then load (A3,B1)-(A4,B1)
 * from db-A-0005, and ignore (A5,B1).
 * @param databaseFileList
 * @param ignoredMostRecentPurgeVersions
 *
 * @return Returns a loaded memory database containing all metadata from the winner's branch
 */
@Override
public MemoryDatabase next() {
    MemoryDatabase winnerBranchDatabase = new MemoryDatabase();
    String rangeClientName = null;
    VectorClock rangeVersionFrom = null;
    VectorClock rangeVersionTo = null;
    while (branchIndex < winnersApplyBranchList.size() && winnerBranchDatabase.getFileHistories().size() < MAX_FILES) {
        DatabaseVersionHeader currentDatabaseVersionHeader = winnersApplyBranchList.get(branchIndex);
        DatabaseVersionHeader nextDatabaseVersionHeader = (branchIndex + 1 < winnersApplyBranchList.size()) ? winnersApplyBranchList.get(branchIndex + 1) : null;
        // First of range for this client
        if (rangeClientName == null) {
            rangeClientName = currentDatabaseVersionHeader.getClient();
            rangeVersionFrom = currentDatabaseVersionHeader.getVectorClock();
            rangeVersionTo = currentDatabaseVersionHeader.getVectorClock();
        } else // Still in range for this client
        {
            rangeVersionTo = currentDatabaseVersionHeader.getVectorClock();
        }
        // Now load this stuff from the database file (or not)
        // - If the database file exists, load the range and reset it
        // - If not, only force a load if this is the range end
        File databaseVersionFile = databaseVersionLocations.get(currentDatabaseVersionHeader);
        if (databaseVersionFile == null) {
            throw new RuntimeException("Could not find file corresponding to " + currentDatabaseVersionHeader + ", while it is in the winners branch.");
        }
        boolean lastDatabaseVersionHeader = nextDatabaseVersionHeader == null;
        boolean nextDatabaseVersionInSameFile = lastDatabaseVersionHeader || databaseVersionFile.equals(databaseVersionLocations.get(nextDatabaseVersionHeader));
        boolean rangeEnds = lastDatabaseVersionHeader || !nextDatabaseVersionInSameFile;
        if (rangeEnds) {
            try {
                databaseSerializer.load(winnerBranchDatabase, databaseVersionFile, rangeVersionFrom, rangeVersionTo, DatabaseReadType.FULL);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
            rangeClientName = null;
        }
        branchIndex++;
    }
    return winnerBranchDatabase;
}
Also used : VectorClock(org.syncany.database.VectorClock) MemoryDatabase(org.syncany.database.MemoryDatabase) IOException(java.io.IOException) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) File(java.io.File)

Example 25 with VectorClock

use of org.syncany.database.VectorClock in project syncany by syncany.

the class DatabaseVersionHeaderComparatorTest method setUp.

@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);
}
Also used : VectorClock(org.syncany.database.VectorClock) DatabaseVersionHeader(org.syncany.database.DatabaseVersionHeader) Date(java.util.Date) Before(org.junit.Before)

Aggregations

VectorClock (org.syncany.database.VectorClock)28 Test (org.junit.Test)14 IOException (java.io.IOException)5 Date (java.util.Date)5 DatabaseVersion (org.syncany.database.DatabaseVersion)5 DatabaseVersionHeader (org.syncany.database.DatabaseVersionHeader)5 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 MemoryDatabase (org.syncany.database.MemoryDatabase)3 File (java.io.File)2 Matcher (java.util.regex.Matcher)2 PartialFileHistory (org.syncany.database.PartialFileHistory)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 ChunkEntry (org.syncany.database.ChunkEntry)1