Package org.syncany.database

Examples of org.syncany.database.DatabaseVersionHeader


    System.out.println("----------------");
   
    /// Input data ///
    String localMachineName = "C";
   
    DatabaseVersionHeader currentLocalVersion = null;

    DatabaseBranches allBranches = new DatabaseBranches();
   
    // B
    allBranches.put("B", TestDatabaseUtil.createBranch(new String[] {
View Full Code Here


    System.out.println("Winners winner test");
    System.out.println("----------------");
   
    /// Input data ///
    String localMachineName = "B";
    DatabaseVersionHeader currentLocalVersion = TestDatabaseUtil.createFromString("A/(A1,C4)/T=8");
    DatabaseBranches allBranches = new DatabaseBranches();
   
    // A
    allBranches.put("A", TestDatabaseUtil.createBranch(new String[] {     
      "A/(A1,C4)/T=8",     // last common
View Full Code Here

    System.out.println("Winners winner test");
    System.out.println("----------------");
   
    /// Input data ///
    String localMachineName = "D";
    DatabaseVersionHeader currentLocalVersion = TestDatabaseUtil.createFromString("A/(A1,C4)/T=8");
    DatabaseBranches allBranches = new DatabaseBranches();
   
    // A
    allBranches.put("A", TestDatabaseUtil.createBranch(new String[] {
      "A/(A2,C4)/T=9",     // first conflicting, wins
View Full Code Here

    /* Scenario: The local machine ("C") has only one local database version
     */
   
    /// Input data ///
    String localMachineName = "C";
    DatabaseVersionHeader currentLocalVersion = TestDatabaseUtil.createFromString("A/(A1)/T=1376074225169");
    DatabaseBranches allBranches = new DatabaseBranches();

    allBranches.put("A", TestDatabaseUtil.createBranch(new String[] {
      "A/(A1)/T=1376074225169",
      "A/(A2)/T=1376074225230",
View Full Code Here

     *           --> A should win over B (alphabetical order)
     */
   
    /// Input data ///
    String localMachineName = "C";
    DatabaseVersionHeader currentLocalVersion = TestDatabaseUtil.createFromString("A/(A2)/T=1376074225230");
    DatabaseBranches allBranches = new DatabaseBranches();

    allBranches.put("A", TestDatabaseUtil.createBranch(new String[] {
      "A/(A1)/T=1376074225169",
      "A/(A2)/T=1376074225230",
View Full Code Here

   * @return Returns the branch of the winning client
   */
  public Map.Entry<String, DatabaseBranch> findWinnerBranch(String localMachineName, DatabaseBranch localBranch, DatabaseBranches allStitchedBranches)
      throws Exception {
   
    DatabaseVersionHeader lastCommonHeader = findLastCommonDatabaseVersionHeader(localBranch, allStitchedBranches);
    TreeMap<String, DatabaseVersionHeader> firstConflictHeaders = findFirstConflictingDatabaseVersionHeader(lastCommonHeader, allStitchedBranches);
    TreeMap<String, DatabaseVersionHeader> winningFirstConflictHeaders = findWinningFirstConflictingDatabaseVersionHeaders(firstConflictHeaders);
    Entry<String, DatabaseVersionHeader> winnersLastHeader = findWinnersLastDatabaseVersionHeader(winningFirstConflictHeaders, allStitchedBranches);

    String winnersName = winnersLastHeader.getKey();
View Full Code Here

   * @param remoteBranches All remote branches of the other clients
   * @return Returns the last common database version header, or <tt>null</tt> if there is none
   */
  // TODO [medium] This is very inefficient; Runtime O(n^3)!
  public DatabaseVersionHeader findLastCommonDatabaseVersionHeader(DatabaseBranch localBranch, DatabaseBranches remoteBranches) {
    DatabaseVersionHeader lastCommonDatabaseVersionHeader = null;

    for (DatabaseBranchIterator localBranchIterator = localBranch.iteratorLast(); localBranchIterator.hasPrevious();) {
      DatabaseVersionHeader currentLocalDatabaseVersionHeader = localBranchIterator.previous();

      if (isGreaterOrEqualDatabaseVersionHeaderInAllDatabaseBranches(currentLocalDatabaseVersionHeader, remoteBranches)) {
        lastCommonDatabaseVersionHeader = currentLocalDatabaseVersionHeader;
        break;
      }
View Full Code Here

    nextClient: for (String remoteMachineName : allDatabaseBranches.getClients()) {
      DatabaseBranch remoteMachineBranch = allDatabaseBranches.getBranch(remoteMachineName);

      for (Iterator<DatabaseVersionHeader> i = remoteMachineBranch.iteratorFirst(); i.hasNext();) {
        DatabaseVersionHeader thisDatabaseVersionHeader = i.next();

        if (thisDatabaseVersionHeader.equals(lastCommonHeader)) {
          if (i.hasNext()) {
            DatabaseVersionHeader firstConflictingInBranch = i.next();
            firstConflictingDatabaseVersionHeaders.put(remoteMachineName, firstConflictingInBranch);
          }
          else {
            // No conflict here!
          }

          continue nextClient;
        }
      }

      // Last common header not found; Add first as conflict
      if (remoteMachineBranch.size() > 0) {
        DatabaseVersionHeader firstConflictingInBranch = remoteMachineBranch.get(0);
        firstConflictingDatabaseVersionHeaders.put(remoteMachineName, firstConflictingInBranch);
      }
    }

    return firstConflictingDatabaseVersionHeaders;
View Full Code Here

   *         is first conflicting database version header.
   */
  public TreeMap<String, DatabaseVersionHeader> findWinningFirstConflictingDatabaseVersionHeaders(
      TreeMap<String, DatabaseVersionHeader> firstConflictingDatabaseVersionHeaders) {
   
    DatabaseVersionHeader winningFirstConflictingDatabaseVersionHeader = null;

    // (1) Compare all first conflicting ones and take the one with the EARLIEST timestamp
    for (DatabaseVersionHeader databaseVersionHeader : firstConflictingDatabaseVersionHeaders.values()) {
      if (winningFirstConflictingDatabaseVersionHeader == null) {
        winningFirstConflictingDatabaseVersionHeader = databaseVersionHeader;
      }
      else if (databaseVersionHeader.getDate().before(winningFirstConflictingDatabaseVersionHeader.getDate())) {
        winningFirstConflictingDatabaseVersionHeader = databaseVersionHeader;
      }
    }

    // (2) Find all first conflicting entries with the SAME timestamp as the
    // EARLIEST one (= multiple winning entries possible)
    TreeMap<String, DatabaseVersionHeader> winningFirstConflictingDatabaseVersionHeaders = new TreeMap<String, DatabaseVersionHeader>();

    for (Map.Entry<String, DatabaseVersionHeader> entry : firstConflictingDatabaseVersionHeaders.entrySet()) {
      if (winningFirstConflictingDatabaseVersionHeader.equals(entry.getValue())) {
        winningFirstConflictingDatabaseVersionHeaders.put(entry.getKey(), entry.getValue());
      }
    }

    return winningFirstConflictingDatabaseVersionHeaders;
View Full Code Here

      throws Exception {

    // 1. If there is only one conflicting database version header, return it (no need for a complex algorithm)
    if (winningFirstConflictingDatabaseVersionHeaders.size() == 1) {
      String winningMachineName = winningFirstConflictingDatabaseVersionHeaders.firstKey();
      DatabaseVersionHeader winnersWinnersLastDatabaseVersionHeader = allBranches.getBranch(winningMachineName).getLast();

      return new AbstractMap.SimpleEntry<String, DatabaseVersionHeader>(winningMachineName, winnersWinnersLastDatabaseVersionHeader);
    }

    // 2. Find position of first conflicting header in branch (per client)
    Map<String, Integer> machineInBranchPosition = findWinningFirstConflictDatabaseVersionHeaderPerClientPosition(
        winningFirstConflictingDatabaseVersionHeaders, allBranches);

    // 3. Compare all, go forward if all are identical
    int machineInRaceCount = winningFirstConflictingDatabaseVersionHeaders.size();   
   
    while (machineInRaceCount > 1) {
      String firstMachineName = null;
      DatabaseVersionHeader firstMachineDatabaseVersionHeader = null;

      for (Map.Entry<String, Integer> secondMachineNamePositionEntry : machineInBranchPosition.entrySet()) {
        // 3a. Get second machine and make sure we can use it (= it hasn't been eliminated before)
       
        // - Get machine name, position of next database version to be compared, and the machine branch
        String secondMachineName = secondMachineNamePositionEntry.getKey();
        Integer secondMachinePosition = secondMachineNamePositionEntry.getValue();       

        // - If machine position is 'null', it has been marked 'eliminated'
        if (secondMachinePosition == null) {
          continue;
        }

        // - If machine position is greater than the machine's branch size (out of bound),
        //   eliminate the machine (= position to 'null')
        DatabaseBranch secondMachineBranch = allBranches.getBranch(secondMachineName);

        if (secondMachinePosition >= secondMachineBranch.size()) {
          machineInBranchPosition.put(secondMachineName, null);
          machineInRaceCount--;

          continue;
        }

        DatabaseVersionHeader secondMachineDatabaseVersionHeader = secondMachineBranch.get(secondMachinePosition);
       
        // 3b. Now compare 'firstMachine*' and 'secondMachine*'
       
        // If this is the first iteration of the loop, there is nothing to compare it to.
        if (firstMachineDatabaseVersionHeader == null) {
          firstMachineName = secondMachineName;
          firstMachineDatabaseVersionHeader = secondMachineDatabaseVersionHeader;
        }       
        else {
          // Compare the two machines 'firstMachine*' and 'secondMachine*'
          // Keep the winner, eliminate the loser
         
          VectorClockComparison comparison = VectorClock.compare(firstMachineDatabaseVersionHeader.getVectorClock(),
              secondMachineDatabaseVersionHeader.getVectorClock());

          if (comparison != VectorClockComparison.EQUAL) {
            Boolean eliminateFirstMachine = determineEliminateMachine(firstMachineName, firstMachineDatabaseVersionHeader,
                secondMachineName, secondMachineDatabaseVersionHeader);

            if (eliminateFirstMachine) {
              machineInBranchPosition.put(firstMachineName, null);
              machineInRaceCount--;

              firstMachineName = secondMachineName;
              firstMachineDatabaseVersionHeader = secondMachineDatabaseVersionHeader;
            }
            else {
              machineInBranchPosition.put(secondMachineName, null);
              machineInRaceCount--;
            }
          }
        }
      }

      // 3c. If more than one machine are still in the race, increase positions
      if (machineInRaceCount > 1) {
        increaseBranchPosition(machineInBranchPosition);       
      }
    }

    // 4. Return the last remaining machine and its last database version header (= winner!)
    for (String machineName : machineInBranchPosition.keySet()) {
      Integer machineCurrentPosition = machineInBranchPosition.get(machineName);

      if (machineCurrentPosition != null) {
        DatabaseVersionHeader winnersWinnersLastDatabaseVersionHeader = allBranches.getBranch(machineName).getLast();
        return new AbstractMap.SimpleEntry<String, DatabaseVersionHeader>(machineName, winnersWinnersLastDatabaseVersionHeader);
      }
    }

    return null;
View Full Code Here

TOP

Related Classes of org.syncany.database.DatabaseVersionHeader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.