diff src/org/tmatesoft/hg/repo/HgChangelog.java @ 312:f9f3e9b67ccc

Facilitate cancellation and progress reporting in changelog and manifest iterations
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 27 Sep 2011 05:29:12 +0200
parents a6d19adc2636
children 09628675bcee
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/repo/HgChangelog.java	Mon Sep 26 04:06:04 2011 +0200
+++ b/src/org/tmatesoft/hg/repo/HgChangelog.java	Tue Sep 27 05:29:12 2011 +0200
@@ -33,8 +33,12 @@
 import org.tmatesoft.hg.core.HgBadStateException;
 import org.tmatesoft.hg.core.Nodeid;
 import org.tmatesoft.hg.internal.DataAccess;
+import org.tmatesoft.hg.internal.IterateControlMediator;
+import org.tmatesoft.hg.internal.Lifecycle;
 import org.tmatesoft.hg.internal.Pool;
 import org.tmatesoft.hg.internal.RevlogStream;
+import org.tmatesoft.hg.util.CancelSupport;
+import org.tmatesoft.hg.util.ProgressSupport;
 
 /**
  * Representation of the Mercurial changelog file (list of ChangeSets)
@@ -338,16 +342,19 @@
 		}
 	}
 
-	private static class RawCsetParser implements RevlogStream.Inspector {
+	private static class RawCsetParser implements RevlogStream.Inspector, Lifecycle {
 		
 		private final Inspector inspector;
 		private final Pool<String> usersPool;
 		private final RawChangeset cset = new RawChangeset();
+		private final ProgressSupport progressHelper;
+		private IterateControlMediator iterateControl;
 
 		public RawCsetParser(HgChangelog.Inspector delegate) {
 			assert delegate != null;
 			inspector = delegate;
 			usersPool = new Pool<String>();
+			progressHelper = ProgressSupport.Factory.get(delegate);
 		}
 
 		public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, DataAccess da) {
@@ -356,9 +363,23 @@
 				cset.init(data, 0, data.length, usersPool);
 				// XXX there's no guarantee for Changeset.Callback that distinct instance comes each time, consider instance reuse
 				inspector.next(revisionNumber, Nodeid.fromBinary(nodeid, 0), cset);
+				progressHelper.worked(1);
 			} catch (Exception ex) {
 				throw new HgBadStateException(ex); // FIXME exception handling
 			}
+			if (iterateControl != null) {
+				iterateControl.checkCancelled();
+			}
+		}
+
+		public void start(int count, Callback callback, Object token) {
+			CancelSupport cs = CancelSupport.Factory.get(inspector, null);
+			iterateControl = cs == null ? null : new IterateControlMediator(cs, callback);
+			progressHelper.start(count);
+		}
+
+		public void finish(Object token) {
+			progressHelper.done();
 		}
 	}
 }