3737public class SnapshotManager implements RevokingDatabase {
3838
3939 private static final int DEFAULT_STACK_MAX_SIZE = 256 ;
40- public static final int DEFAULT_MAX_FLUSH_COUNT = 1000 ;
40+ public static final int DEFAULT_MAX_FLUSH_COUNT = 500 ;
4141 public static final int DEFAULT_MIN_FLUSH_COUNT = 1 ;
4242 @ Getter
4343 private List <RevokingDBWithCachingNewValue > dbs = new ArrayList <>();
@@ -80,7 +80,7 @@ public synchronized ISession buildSession(boolean forceEnable) {
8080
8181 if (size > maxSize .get ()) {
8282 flushCount = flushCount + (size - maxSize .get ());
83- merge (size - maxSize .get ());
83+ updateSolidity (size - maxSize .get ());
8484 size = maxSize .get ();
8585 flush ();
8686 }
@@ -90,11 +90,6 @@ public synchronized ISession buildSession(boolean forceEnable) {
9090 return new Session (this , disableOnExit );
9191 }
9292
93- @ Override
94- public void setMode (boolean mode ) {
95- dbs .forEach (db -> db .setMode (mode ));
96- }
97-
9893 @ Override
9994 public void add (IRevokingDB db ) {
10095 RevokingDBWithCachingNewValue revokingDB = (RevokingDBWithCachingNewValue ) db ;
@@ -222,18 +217,10 @@ public void shutdown() {
222217 System .err .println ("******** end to pop revokingDb ********" );
223218 }
224219
225- private void merge (int hops ) {
220+ public void updateSolidity (int hops ) {
226221 for (int i = 0 ; i < hops ; i ++) {
227222 for (RevokingDBWithCachingNewValue db : dbs ) {
228- Snapshot root = db .getHead ().getRoot ();
229- Snapshot next = root .getNext ();
230- root .merge (root .getNext ());
231- if (db .getHead () == next ) {
232- db .setHead (root );
233- } else {
234- next .getNext ().setPrevious (root );
235- root .setNext (next .getNext ());
236- }
223+ db .getHead ().updateSolidity ();
237224 }
238225 }
239226 }
@@ -258,7 +245,28 @@ private void refresh() {
258245 }
259246
260247 private void refreshOne (RevokingDBWithCachingNewValue db ) {
261- ((SnapshotRoot ) db .getHead ().getRoot ()).flush ();
248+ if (Snapshot .isRoot (db .getHead ())) {
249+ return ;
250+ }
251+
252+ List <Snapshot > snapshots = new ArrayList <>();
253+
254+ SnapshotRoot root = (SnapshotRoot ) db .getHead ().getRoot ();
255+ Snapshot next = root ;
256+ for (int i = 0 ; i < flushCount ; ++i ) {
257+ next = next .getNext ();
258+ snapshots .add (next );
259+ }
260+
261+ root .merge (snapshots );
262+
263+ root .resetSolidity ();
264+ if (db .getHead () == next ) {
265+ db .setHead (root );
266+ } else {
267+ next .getNext ().setPrevious (root );
268+ root .setNext (next .getNext ());
269+ }
262270 }
263271
264272 public void flush () {
@@ -285,14 +293,22 @@ private void createCheckPoint() {
285293 Map <WrappedByteArray , WrappedByteArray > batch = new HashMap <>();
286294 for (RevokingDBWithCachingNewValue db : dbs ) {
287295 Snapshot head = db .getHead ();
296+ if (Snapshot .isRoot (head )) {
297+ return ;
298+ }
299+
288300 String dbName = db .getDbName ();
289- SnapshotRoot snapshot = (SnapshotRoot ) head .getRoot ();
290- DB <Key , Value > keyValueDB = snapshot .getCache ();
291- for (Map .Entry <Key , Value > e : keyValueDB ) {
292- Key k = e .getKey ();
293- Value v = e .getValue ();
294- batch .put (WrappedByteArray .of (Bytes .concat (simpleEncode (dbName ), k .getBytes ())),
295- WrappedByteArray .of (v .encode ()));
301+ Snapshot next = head .getRoot ();
302+ for (int i = 0 ; i < flushCount ; ++i ) {
303+ next = next .getNext ();
304+ SnapshotImpl snapshot = (SnapshotImpl ) next ;
305+ DB <Key , Value > keyValueDB = snapshot .getDb ();
306+ for (Map .Entry <Key , Value > e : keyValueDB ) {
307+ Key k = e .getKey ();
308+ Value v = e .getValue ();
309+ batch .put (WrappedByteArray .of (Bytes .concat (simpleEncode (dbName ), k .getBytes ())),
310+ WrappedByteArray .of (v .encode ()));
311+ }
296312 }
297313 }
298314
@@ -346,7 +362,7 @@ public void check() {
346362
347363 }
348364
349- dbs .forEach (db -> (( SnapshotRoot ) db .getHead ().getRoot ()) .merge (db .getHead (), true ));
365+ dbs .forEach (db -> db .getHead ().getRoot ().merge (db .getHead ()));
350366 retreat ();
351367 }
352368
0 commit comments