/************************************************************************* * @(#) Replication.groovy 1.0 * * Copyright (c) 2007 John Miller * All Right Reserved *------------------------------------------------------------------------ * @version 1.0, 9 February 2007 * @author John Miller */ /************************************************************************* * This class is used to find the optimal allocation of fragment replicas * to sites in a distributed database. The model takes into account the * expected cost of doing remote queries and updates to each frament as * well as the probabilities of each site querying/updating each fragment. */ public class Replication { /** Expected cost of a remote query to fragment i. */ private static double [] cq = [ 1.0, 1.0, 1.5 ]; /** Expected cost of a remote update to fragment i. */ private static double [] cu = [ 1.5, 1.5, 2.0 ]; /** Probability that fragment i is queried by site j. */ private static double [][] pq = [ [ 1.0, 1.0, 0.3, 0.0 ], [ 0.0, 0.3, 0.7, 1.0 ], [ 0.0, 1.0, 1.0, 0.0 ] ]; /** Probability that fragment i is updated by site j. */ private static double [][] pu = [ [ 0.6, 0.4, 0.0, 0.0 ], [ 0.0, 0.0, 0.4, 0.6 ], [ 0.0, 0.5, 0.5, 0.0 ] ]; /********************************************************************* * Evalaute the objective function for the ith fragment. * @param x Array indicating if fragment allocated to site j. * @param n Number of sites. * @param i Fragment i. * @return double The value of the objective function. */ private static double eval (int [] x, int n, int i) { double cost = 0.0; for (j in 0 .. n - 1) { double xSum = 0.0; for (k in 0 .. n - 1) { if (k != j) xSum += x[k]; } // for cost += pq[i][j] * cq[i] * (1 - x[j]) + pu[i][j] * cu[i] * xSum; } // for cost; } // eval /********************************************************************* * Copy replica to site j based on value of counter. * @param x Array indicating fragment if allocated to site j. * @param n Number of sites. * @param jCount Counter used to get all subsets of sites. */ private static void assignX (int [] x, int n, int jCount) { for (j in 0 .. n - 1) { x[j] = (jCount & (1 << j)) ? 1 : 0; } // for } // assignX /********************************************************************* * Main method to find optimal replication and allocation. * @param args Command-line arguments. */ public static void main (String [] args) { int m = pq.length; int n = pq [0].length; int [] x = new int [n]; int [] minX; double min; double value; for (i in 0 .. m - 1) { min = 1E100; println ("\n *** Find optimal allocation for fragment $i"); // for (all possible non-empty subsets of sites) { assignX (x, n, jCount); value = eval (x, n, i); println ("x = $x , value = $value"); if (value < min) { minX = x.clone (); min = value; } // if } // for println ("\nminX = $minX , min = $min"); } // for } // main } // Replication class