Adam Lydick writes:
        > > I just had a thought. Why, exactly, do we want JVM
        > > independent serialization of the datastore? So we can
        > > switch JVMs without deleting the datastore? That`s not very
        > > useful. Just delete your datastore and start over.

        As the data stores get larger, it will be nice to have a useful
        node after making changes to the server while retaining a useful
        datastore.

Well, I just done wrote JVM-independent datastore storing -- I even made
the code backwards-compatible, while I was at it.  Enclosed are the
patches -- I'd like them reviewed before I commit them.


Index: StandardDataStore.java
===================================================================
RCS file: /cvsroot/freenet/Freenet/node/StandardDataStore.java,v
retrieving revision 1.12
diff -u -r1.12 StandardDataStore.java
--- StandardDataStore.java      2000/04/13 04:23:06     1.12
+++ StandardDataStore.java      2000/04/14 23:37:40
@@ -59,30 +59,68 @@
     */
   public static DataStore makeDataStore(Node node,  long maxData, int maxItems)
   {
-    Object o=null;
+    DataStore result;
+    
+    if ((result = readV1DataStore(maxData, maxItems)) == null &&
+               (result = readObjectDataStore()) == null) {
+       Logger.log("DataStore.java", 
+                              "Couldn't load DataStore from "+filename, 
+                              Logger.NORMAL);
+       result = new StandardDataStore(maxData, maxItems);
+    }
+
+    write_interval = 1000 * Node.params.getlong("checkpointInterval", 5 * 60);
+    Node.timer.add(23, write_interval, result);
+    return result;
+  }

+  public static DataStore readObjectDataStore() {
+    Object o = null;
+    
     try
     {
-      System.out.println(filename);
       FileInputStream stream=new FileInputStream(filename);
       ObjectInputStream ostream=new ObjectInputStream(stream);
       o=ostream.readObject();
       ostream.close();
       if(!(o instanceof DataStore))
-        o=new StandardDataStore(maxData, maxItems);
+        return null;
     }
     catch(Exception e) 
        {
-           Logger.log("DataStore.java", 
-                      "Couldn't load DataStore from "+filename, 
-                      Logger.NORMAL);
-           o=new StandardDataStore(maxData, maxItems); 
+           if (o == null) {
+                   Logger.log("DataStore.java", "Not a serialized DataStore", 
+                              Logger.MINOR);
+           }
        }

-    write_interval=1000*Node.params.getlong("checkpointInterval",5*60);
-    Node.timer.add(23, write_interval, (Callback)o);
     return (DataStore)o;
   }
+  
+  public static DataStore readV1DataStore(long maxData, int maxItems) {
+       StandardDataStore result = null;
+       try {
+               FileInputStream fstream = new FileInputStream(filename);
+               DataInputStream stream = new DataInputStream(fstream);
+               
+               if (!"VERSION 1".equals(stream.readUTF())) {
+                       Logger.log("DataStore.java", "not version 1",
+                                       Logger.MINOR);
+                       return null;
+               }
+               result = new StandardDataStore(maxData, maxItems);
+               while (stream.available() > 0)
+                       result.put(new DataStoreItem(stream));
+               stream.close();
+               fstream.close();
+       }
+       catch (Exception e) {
+               if (result == null)
+                       Logger.log("DataStore.java", "not a version 1 
datastore",
+                                       Logger.MINOR);
+       }
+       return result;
+  }

   public void tofile(String fname) throws IOException
   {
@@ -98,17 +136,24 @@
        public void doWrite(StandardDataStore store, String filename) throws 
IOException {
                Logger.log("DataStore.java","Writing datastore to 
disk",Logger.MINOR);

-               FileOutputStream stream=new FileOutputStream(filename);
-               ObjectOutputStream ostream=new ObjectOutputStream(stream);
+               FileOutputStream fstream = new FileOutputStream(filename);
+               DataOutputStream stream = new DataOutputStream(fstream);

+               stream.writeUTF("VERSION 1");
+
                synchronized(store) {
-                       /* Set this *before* the write -- after all, we won't 
need to
-                        * write it right after it has been read! 
-                        */
+                       CyclicArray ar = store.ar;
+                       for (int i = ar.length() - 1; i >= 0; i--) {
+                               DataStoreItem item = (DataStoreItem) ar.get(i);
+                               if (item == null)
+                                       continue;
+                               item.toFile(stream);
+                       }
+
                        store.needsWriting = false;
-                       ostream.writeObject(store);
                }
-               ostream.close();
+               stream.close();
+               fstream.close();
        }
   }

@@ -270,17 +315,19 @@
     **/

   public void put(Key k, Address r, Data d) {
-    synchronized (this) {
-      DataStoreItem dsi = new DataStoreItem(k, r, d);
+    put(new DataStoreItem(k, r, d));
+  }
+
+  synchronized public void put(DataStoreItem dsi) {
+      Key k = dsi.key;
       DataStoreItem old = (DataStoreItem) ar.put(dsi);
       if (old != null)
          h.remove(old.key);
       h.put(k, dsi);
       needsWriting = true;
       cleanUpData();
-      }
-    }
-
+  }
+  
   // Protected Methods

   /**
@@ -351,6 +398,19 @@
     data = d;
     }

+  public DataStoreItem(DataInputStream stream) throws IOException {
+       key = new StringKey(stream.readUTF());
+       ref = new Address(stream.readUTF());
+       String dataString = stream.readUTF();
+       if (!dataString.equals(""))
+               data = new Data(new File(dataString));
+  }
+  public void toFile(DataOutputStream stream) throws IOException {
+       stream.writeUTF(key.toString());
+       stream.writeUTF(ref.toString());
+       stream.writeUTF(data == null ? "" : data.toString());
+  }
+
   public String toString() {
     return "Key: "+key + " Ref: "+ref + " Data:"+data;
     }

_______________________________________________
Freenet-dev mailing list
Freenet-dev at lists.sourceforge.net
http://lists.sourceforge.net/mailman/listinfo/freenet-dev

Reply via email to