Update of /cvsroot/freenet/freenet/src/freenet/node/rt
In directory sc8-pr-cvs1:/tmp/cvs-serv27534/src/freenet/node/rt

Modified Files:
        ResponseTimeEstimator.java 
Log Message:
Merge the three RoutingPointdata storaage arrays into one single RoutingPoint array.
Synchronize RoutingStore methods as appropriate.

Index: ResponseTimeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/ResponseTimeEstimator.java,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -w -r1.25 -r1.26
--- ResponseTimeEstimator.java  1 Nov 2003 20:16:10 -0000       1.25
+++ ResponseTimeEstimator.java  1 Nov 2003 21:15:27 -0000       1.26
@@ -832,9 +832,10 @@
           }
        
        protected class RoutingPointStore {
-               BigInteger key[];
-               double sensitivity[];
-               int time[];
+               //BigInteger key[];
+               //double sensitivity[];
+               //int time[];
+               RoutingPoint points[];
                final RoutingPointStore.RoutingPointKeySorter ms = new 
RoutingPointKeySorter();
                
                //Represents a point in a routing graph
@@ -843,6 +844,11 @@
                        BigInteger key;
                        int time;
                        double sensitivity;
+                       RoutingPoint() {
+                               key = null;
+                               time = 0;
+                               sensitivity = 0;
+                       }
                        RoutingPoint(BigInteger key, int time, double sensitivity) {
                                this.key = key;
                                this.time = time;
@@ -860,13 +866,13 @@
                //Sorts the store in order of the RoutingPoint keys.
                private final class RoutingPointKeySorter implements Sortable {
                        public final int compare(int index1, int index2) {
-                               return key[index1].compareTo(key[index2]);
+                               return 
points[index1].key.compareTo(points[index2].key);
                        }
 
                        public final void swap(int index1, int index2) {
-                               BigInteger bi = key[index1];
-                               key[index1] = key[index2];
-                               key[index2] = bi;
+                               RoutingPoint p = points[index1];
+                               points[index1] = points[index2];
+                               points[index2] = p;
                        }
 
                        public final int size() {
@@ -874,11 +880,12 @@
                        }
                }
 
-               //Create fresh, empty routing store with space for 'accuracy' 
RoutingPoints
+               //Create fresh routing store with 'accuracy' number of default-valued 
RoutingPoints
                public RoutingPointStore(int accuracy) {
-                       key = new BigInteger[accuracy];
-                       time = new int[accuracy];
-                       sensitivity = new double[accuracy];
+                       points = new RoutingPoint[accuracy];
+                       for (int x = 0; x < points.length; x++) {
+                               points[x] = new RoutingPoint();
+                       }
                }
 
                //Create a store by reading a serialization
@@ -888,33 +895,33 @@
                                throw new IOException("failed read");
                        if (accuracy == 0 || accuracy > MAX_ACCURACY) // seems 
reasonable bounds
                                throw new IOException("Invalid accuracy word " + 
accuracy);
+                       points = new RoutingPoint[accuracy];
                        
-                       key = new BigInteger[accuracy];
-                       time = new int[accuracy];
-                       sensitivity = new double[accuracy];
                        
                        BigInteger prev = BigInteger.ZERO;
 
-                       for (int x = 0; x < accuracy; x++) {
-                               time[x] = i.readInt();
-                               if (time[x] < 0)
+                       for (int x = 0; x < points.length; x++) {
+                               int time = i.readInt();
+                               //time[x] = i.readInt();
+                               if (time < 0)
                                        throw new IOException("negative value");
                                byte[] b = new byte[KEYBYTES];
                                i.readFully(b);
-                               key[x] = new BigInteger(1, b);
-                               if (key[x].signum() == -1)
+                               BigInteger key = new BigInteger(1, b);
+                               if (key.signum() == -1)
                                        throw new IOException("negative key");
-                               if (key[x].compareTo(keyspace) == 1)
+                               if (key.compareTo(keyspace) == 1)
                                        throw new IOException("exceeded keyspace");
-                               if (key[x].compareTo(prev) == -1)
+                               if (key.compareTo(prev) == -1)
                                        throw new IOException("smaller than prev");
-                               prev = key[x];
-                               sensitivity[x] = i.readDouble();
+                               prev = key;
+                               double sensitivity = i.readDouble();
+                               points[x] = new RoutingPoint(key,time,sensitivity);
                        }
                }
-
+               //TODO: Documentation
                public RoutingPointStore(Key k, int high, int low, int accuracy) {
-                       
+                       this(accuracy); 
                        if (high < 0 || low < 0)
                                throw new IllegalArgumentException("negative high " + 
high + " or low " + low);
                        // Center on k
@@ -930,7 +937,7 @@
                                throw new IllegalStateException("argh");
                        int kOffset = dr[0].intValue();
                        for (int i = 0; i < accuracy; i++) {
-                               key[i] = c;
+                               points[i].key = c;
                                c.add(b);
                        }
                        // Now the times
@@ -938,22 +945,23 @@
                        int valStep = (high - low) / accuracy;
                        for (int i = 0; i < accuracy; i++) {
                                int offset = (kOffset + i) % accuracy;
-                               time[offset] = val;
+                               points[offset].time = val;
                                if (i <= accuracy / 2)
                                        val -= valStep;
                                else
                                        val += valStep;
                        }
+                       //TODO: What about the sensitivities?
                }
 
                //Returns the offset of the biggest key that's still less than n.
-               final int search(BigInteger n) {
-                       int mid, low = 0, high = key.length - 1;
+               final synchronized int search(BigInteger n) {
+                       int mid, low = 0, high = points.length - 1;
 
                        for (;;) {
                                mid = (high + low) >> 1;
-                               int i = key[mid].compareTo(n);
-                               if (i <= 0 && key[mid + 1].compareTo(n) > 0)
+                               int i = points[mid].key.compareTo(n);
+                               if (i <= 0 && points[mid+1].key.compareTo(n) > 0)
                                        return mid;
                                if (i > 0)
                                        high = mid;
@@ -965,26 +973,24 @@
                }
 
                //Sets all times to the supplied value
-               private void setAllTimes(int newtime) {
-                       for (int i = 0; i < time.length; i++) {
-                               time[i] = newtime;
+               private synchronized void setAllTimes(int newtime) {
+                       for (int i = 0; i < points.length; i++) {
+                               points[i].time = newtime;
                        }
                }
                
                //Returns the RoutingPoint at the position 'index' in the store
-               RoutingPoint get(int index) {
-                       return new RoutingPoint(key[index], time[index], 
sensitivity[index]);
+               synchronized RoutingPoint get(int index) {
+                       return points[index];
                }
                
-               void set(int index, RoutingPoint value) {
-                       key[index] = value.key;
-                       time[index] = value.time;
-                       sensitivity[index] = value.sensitivity;
+               synchronized void set(int index, RoutingPoint value) {
+                       points[index] = value;
                }
                //Finds the position of the first key in the store, before the 
position 'startAt', that is smaller than n
                //Returns -1 if no such key is present in the store
-               protected int findFirstSmaller_reverse(BigInteger n, int startAt) {
-                       while (key[startAt].compareTo(n) == 1) {
+               protected synchronized int findFirstSmaller_reverse(BigInteger n, int 
startAt) {
+                       while (points[startAt].key.compareTo(n) == 1) {
                                startAt--;
                                if (startAt < 0)
                                        return -1;
@@ -993,30 +999,30 @@
                }
                //Finds the position of the first key in the store, after the position 
'startAt', that is larger than n
                //Returns -1 if no such key is present in the store
-               protected int findFirstLarger(BigInteger n, int startAt) {
-                       while (key[startAt].compareTo(n) == -1) {
+               protected synchronized int findFirstLarger(BigInteger n, int startAt) {
+                       while (points[startAt].key.compareTo(n) == -1) {
                                startAt++;
-                               if (startAt >= key.length)
+                               if (startAt >= points.length)
                                        return -1;
                        }
                        return startAt;
                }
 
                int getDataLength() {
-                       return 4 + key.length * (4 + KEYBYTES);
+                       return 4 + points.length * (4 + KEYBYTES);
                }
                
                //Returns the number of items in the store
                int length() {
-                       return key.length;
+                       return points.length;
                }
-               public void dumpLog() {
+               public synchronized void dumpLog() {
                        if (Core.logger.shouldLog(Logger.MINOR, this)) {
                                StringBuffer sb = new StringBuffer();
-                               for (int x = 0; x < key.length; x++) {
-                                       sb.append(key[x].toString(16));
+                               for (int x = 0; x < points.length; x++) {
+                                       sb.append(points[x].key.toString(16));
                                        sb.append(": ");
-                                       int t = time[x];
+                                       int t = points[x].time;
                                        sb.append(Integer.toString(t));
                                        if (t < 0)
                                                Core.logger.log(this, "time[" + x + 
"]=" + t + " - NEGATIVE (" + this +")", Logger.ERROR);
@@ -1025,42 +1031,42 @@
                                Core.logger.log(this, "Dump of " + this +": \n" + 
sb.toString(), Logger.MINOR);
                        } else {
                                //Do only the sanity check if we wont log the contents 
of the estimator
-                               for (int x = 0; x < key.length; x++) {
-                                       int t = time[x];
+                               for (int x = 0; x < points.length; x++) {
+                                       int t = points[x].time;
                                        if (t < 0)
                                                Core.logger.log(this, "time[" + x + 
"]=" + t + " - NEGATIVE (" + this +")", Logger.ERROR);
                                }
                        }
                }
-               void dumpHtml(PrintWriter pw) {
-                       for (int x = 0; x < key.length; x++) {
-                               pw.println("<tr><td>" + x + "</td><td>" + 
key[x].toString(16) + "</td><td>" + time[x] + "</td><td>" + sensitivity[x] + 
"</td></tr>");
-                               if (time[x] < 0)
-                                       Core.logger.log(this, "time[" + x + "]=" + 
time[x] + " - NEGATIVE (" + this +")", Logger.ERROR);
+               synchronized void dumpHtml(PrintWriter pw) {
+                       for (int x = 0; x < points.length; x++) {
+                               pw.println("<tr><td>" + x + "</td><td>" + 
points[x].key.toString(16) + "</td><td>" + points[x].time + "</td><td>" + 
points[x].sensitivity + "</td></tr>");
+                               if (points[x].time < 0)
+                                       Core.logger.log(this, "time[" + x + "]=" + 
points[x].time + " - NEGATIVE (" + this +")", Logger.ERROR);
                        }
                }
                //Returns the value (time) of the highest-valued RoutingPoint
-               public long highestRaw() {
+               public synchronized long highestRaw() {
                        int highest = 0;
-                       for (int x = 0; x < time.length; x++) {
-                               highest = Math.max(highest,time[x]);
+                       for (int x = 0; x < points.length; x++) {
+                               highest = Math.max(highest,points[x].time);
                        }
                        return highest;
                }
                //Returns the value (time) of the lowest-valued RoutingPoint
-               public long lowestRaw() {
+               public synchronized long lowestRaw() {
                        int lowest = Integer.MAX_VALUE;
-                       for (int x = 0; x < time.length; x++) {
-                               lowest = Math.min(lowest,time[x]);
+                       for (int x = 0; x < points.length; x++) {
+                               lowest = Math.min(lowest,points[x].time);
                        }
                        return lowest;
                }
-               public void print() {
-                       for (int i = 0; i < key.length; i++)
-                               System.out.println(i + ":\t" + key[i] + "\t" + 
time[i]);
+               public synchronized void print() {
+                       for (int i = 0; i < points.length; i++)
+                               System.out.println(i + ":\t" + points[i].key + "\t" + 
points[i].time);
                }
                //Sorts the RoutingStore in order of its RoutingPoint keys
-               protected void sortByKey() {
+               protected synchronized void sortByKey() {
                        if (logDEBUG)
                                Core.logger.log(this, "Reordering keys", Logger.DEBUG);
                        dumpLog();
@@ -1074,14 +1080,14 @@
                }
 
                //Serializes the store to the supplied DataOutputStream
-               public void writeTo(DataOutputStream o) throws IOException {
+               public synchronized void writeTo(DataOutputStream o) throws 
IOException {
                        logDEBUG = Core.logger.shouldLog(Logger.DEBUG, this);
                        if (logDEBUG)
-                               logDEBUG("Serializing store to disk, accuracy " + 
time.length);
-                       o.writeInt(time.length);
-                       for (int i = 0; i < time.length; i++) {
-                               o.writeInt(time[i]);
-                               byte[] b = key[i].toByteArray();
+                               logDEBUG("Serializing store to disk, accuracy " + 
points.length);
+                       o.writeInt(points.length);
+                       for (int i = 0; i < points.length; i++) {
+                               o.writeInt(points[i].time);
+                               byte[] b = points[i].key.toByteArray();
                                if (b.length > KEYBYTES + 1)
                                        throw new IllegalStateException("Key too long 
in serializing: " + b.length);
                                if (b.length < KEYBYTES) {
@@ -1092,29 +1098,29 @@
                                } else
                                        o.write(b, b.length - KEYBYTES, KEYBYTES);
                                // in case of zero padding
-                               o.writeDouble(sensitivity[i]);
+                               o.writeDouble(points[i].sensitivity);
                        }
                }
 
                //Returns the RoutingPoint which has the highest key
                private RoutingPoint lastPoint() {
-                       return get(key.length - 1);
+                       return get(points.length - 1);
                }
                //Returns the RoutingPoint which has the lowest key
                private RoutingPoint firstPoint() {
                        return get(0);
                }
                //distributes the keys evenly throughout all of the keyspace
-               private void distributeKeysEvenly() {
+               private synchronized void distributeKeysEvenly() {
                        BigInteger a = keyspace.subtract(BigInteger.ONE);
-                       BigInteger b = a.divide(BigInteger.valueOf(key.length));
-                       for (int i = key.length; --i >= 0; a = a.subtract(b))
-                               key[i] = a;
+                       BigInteger b = a.divide(BigInteger.valueOf(points.length));
+                       for (int i = points.length; --i >= 0; a = a.subtract(b))
+                               points[i].key = a;
                }
 
                //Locates the RoutingPoints who are keywise located right before and 
right after the supplied BigInteger
                //Handles keyspace wrap situations. The returned class contains 
information wheter or not a wrap occurred
-               NeighbourRoutingPointPair findKeyBeforeAndAfter(BigInteger n) {
+               synchronized NeighbourRoutingPointPair 
findKeyBeforeAndAfter(BigInteger n) {
                        NeighbourRoutingPointPair retval = new 
NeighbourRoutingPointPair();
                        int pos = store.search(n);
 
@@ -1126,10 +1132,9 @@
                                retval.includesKeyspaceWrap = true;
                                pos = store.length() - 1;
                                //                              Yes, beforeKey should 
be negative
-                               retval.left = new 
RoutingPoint(store.lastPoint().key.subtract(keyspace), time[pos], sensitivity[pos]);
+                               retval.left = new 
RoutingPoint(store.lastPoint().key.subtract(keyspace), points[pos].time, 
points[pos].sensitivity);
                                retval.right = get(0);
                        } else {//else in the middle
-
                                retval.includesKeyspaceWrap = false;
                                retval.left = get(pos);
                                retval.right = get(pos+1);

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to