Thursday, April 29, 2021

Synchronization of state with remote (continued...)

 

The key features of data synchronization include the following:

1.      Data scoping and partitioning:   Typically, the enterprise stores contain a lot more data than what is needed by client devices and their applications. This calls for scoping of data that needs to be synchronized. There are two ways to do this. First, we restrict the data to only those tables that pertain to the user from that client. If there are any data that does not pertain to the user, we can skip those. Second, the data that needs to be synchronized from those tables is minimized.
Partitioning can also be used to reduce the data that is synchronized. As is typical to partitioning, it can be horizontal or vertical. Usually, the vertical partitioning is done prior to horizontal because it trims the columns that need to be synchronized. The set of columns are easily found by comparing what is required for that user versus what is used by the application. After the columns are decided, a filter can be applied to reduce the rowset. Again, this can be done with the help of predicates that involves the user clause. Also reducing the scope and partition of the data, also reduces the errors introduced by way of conflicts which also improves performance.

2.      Data compression:  Another way to reduce an already scoped and partitioned data is to reduce the number of bytes that is used for its transfer. Data compression reduces this size. It is useful for reducing both time and cost although it incurs some overhead via compression and decompression routines. Some of these routines may be expensive for a mobile device more than it is for the server. Also, some data types are easy to compress while others aren’t. Therefore, the compression helps only in cases where those data types are used.

3.      Data transformation: By the same argument as above, it is easy for the server to process and transform the data because of its compute and storage resources. Therefore, conversion to format that is suitable for mobile devices is easier done on the server side. Such transformations might even include conversion to data types that are compression friendly. Also, numerical data might be converted to string if the mobile devices find it easier to handle string.

4.      Transactional integrity: This means that either all the change are committed or none of the changes are committed. Transactional changes occur in isolation. It should not affect others. Once the transaction is committed, its effects are persistent even against failures. Maintaining transactional behavior over network involves retries and this is not efficient. It is easier to enforce transactions within the store. If the synchronization involves remote transactions, then the rollbacks on the remote will require rollback of the entire synchronization and retry during the next synchronization. When databases allow net changes to be captured, there is an option to not look at transaction log if the individual transactions in the log are not important and the initial and final state are what matters. If the order of the changes made is also important, then transaction logs are important as well. A transaction log can be read in chronological order and executed on destination database.

#c#codingexercise

Given clock hands positions for different points of time as pairs A[I][0] and A[I][1] where the order of the hands does not matter but their angle enclosed, count the number of pairs of points of time where the angles are the same 

    public static int[] getClockHandsDelta(int[][] A) { 

        int[] angles = new int[A.length]; 

        for (int i = 0; i < A.length; i++){ 

            angles[i] = Math.max(A[i][0], A[i][1]) - Math.min(A[i][0],A[i][1]); 

        } 

        return angles; 

    } 

    public static int NChooseK(int n, int k) 

    { 

        if (k < 0 || k > n || n == 0) return 0; 

        if ( k == 0 || k == n) return 1; 

        return Factorial(n) / (Factorial(n-k) * Factorial(k)); 

    } 

  

    public static int Factorial(int n) { 

        if (n <= 1) return 1; 

        return n * Factorial(n-1); 

    } 

 

    public static int countPairsWithIdenticalAnglesDelta(int[] angles){ 

        Arrays.sort(angles); 

        int count = 1; 

        int result = 0; 

        for (int i = 1; i < angles.length; i++) { 

            if (angles[i] == angles[i-1]) { 

                count += 1; 

            } else { 

                if (count > 0) { 

                    result += NChooseK(count, 2); 

                } 

                count = 1; 

            } 

        } 

        if (count > 0) { 

            result += NChooseK(count, 2); 

            count = 0; 

        } 

        return result; 

    } 

 

        int [][] A = new int[5][2]; 
         A[0][0] = 1;    A[0][1] = 2; 
         A[1][0] = 2;    A[1][1] = 4; 
         A[2][0] = 4;    A[2][1] = 3; 
         A[3][0] = 2;    A[3][1] = 3; 
         A[4][0] = 1;    A[4][1] = 3; 
1 2 1 1 2  
1 1 1 2 2  
4 




No comments:

Post a Comment