@@ -38,73 +38,70 @@ public KVStoreSnapshotter(KVStoreSerializer serializer) {
3838 this .serializer = serializer ;
3939 }
4040
41- public void dump (KVStore store , File snapshotFile ) throws Exception {
42- DataOutputStream output = new DataOutputStream (new FileOutputStream (snapshotFile ));
43-
41+ /** Dump current KVStore to the output stream - caller should close the output stream. */
42+ public void dump (KVStore store , DataOutputStream snapshotStream ) throws Exception {
4443 // store metadata if it exists
4544 Class <?> metadataType = store .metadataType ();
4645 if (metadataType != null ) {
47- writeClassName (metadataType , output );
46+ writeClassName (metadataType , snapshotStream );
4847 Object metadata = store .getMetadata (metadataType );
49- writeObject (metadata , output );
50- writeEndOfType (output );
48+ writeObject (metadata , snapshotStream );
49+ writeEndOfType (snapshotStream );
5150 } else {
52- writeEndOfType (output );
51+ writeEndOfType (snapshotStream );
5352 }
5453
5554 Set <Class <?>> types = store .types ();
5655 for (Class <?> clazz : types ) {
57- writeClassName (clazz , output );
56+ writeClassName (clazz , snapshotStream );
5857
5958 KVStoreView <?> view = store .view (clazz );
6059 for (Object obj : view ) {
61- writeObject (obj , output );
60+ writeObject (obj , snapshotStream );
6261 }
6362
64- writeEndOfType (output );
63+ writeEndOfType (snapshotStream );
6564 }
6665
67- writeEndOfFile (output );
68- output .close ();
66+ writeEndOfFile (snapshotStream );
6967 }
7068
71- public void restore (File snapshotFile , KVStore store ) throws Exception {
72- DataInputStream input = new DataInputStream (new FileInputStream (snapshotFile ));
73-
69+ /** Restore current KVStore from the input stream - caller should close the input stream. */
70+ public void restore (DataInputStream snapshotStream , KVStore store ) throws Exception {
7471 // first one would be metadata
75- int metadataClazzLen = input .readInt ();
72+ int metadataClazzLen = snapshotStream .readInt ();
7673 if (metadataClazzLen > 0 ) {
77- Class <?> metadataClazz = readClassName (input , metadataClazzLen );
74+ Class <?> metadataClazz = readClassName (snapshotStream , metadataClazzLen );
7875 // metadata presented
79- int objLen = input .readInt ();
80- Object metadata = readObj (input , metadataClazz , objLen );
76+ int objLen = snapshotStream .readInt ();
77+ Object metadata = readObj (snapshotStream , metadataClazz , objLen );
8178 store .setMetadata (metadata );
8279
8380 // additionally read -2 as end of type
84- consumeEndOfType (input );
81+ consumeEndOfType (snapshotStream );
8582 }
8683
8784 boolean eof = false ;
8885 while (!eof ) {
89- int typeClazzNameLen = input .readInt ();
86+ int typeClazzNameLen = snapshotStream .readInt ();
9087 if (typeClazzNameLen == MARKER_END_OF_FILE ) {
9188 eof = true ;
9289 } else {
93- Class <?> typeClazz = readClassName (input , typeClazzNameLen );
90+ Class <?> typeClazz = readClassName (snapshotStream , typeClazzNameLen );
9491 boolean eot = false ;
9592 while (!eot ) {
96- int objLen = input .readInt ();
93+ int objLen = snapshotStream .readInt ();
9794 if (objLen == MARKER_END_OF_TYPE ) {
9895 eot = true ;
9996 } else {
100- Object obj = readObj (input , typeClazz , objLen );
97+ Object obj = readObj (snapshotStream , typeClazz , objLen );
10198 store .write (obj );
10299 }
103100 }
104101 }
105102 }
106103
107- input .close ();
104+ snapshotStream .close ();
108105 }
109106
110107 private void writeClassName (Class <?> clazz , DataOutputStream output ) throws IOException {
0 commit comments