comparison src/org/tmatesoft/hg/repo/HgBundle.java @ 157:d5268ca7715b

Merged branch wrap-data-access into default for resource-friendly data access. Updated API to promote that friendliness to clients (channels, not byte[]). More exceptions
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 09 Mar 2011 05:22:17 +0100
parents src/com/tmate/hgkit/ll/HgBundle.java@9429c7bd1920 src/com/tmate/hgkit/ll/HgBundle.java@ba2bf656f00f
children 8c8e3f372fa1
comparison
equal deleted inserted replaced
156:643ddec3be36 157:d5268ca7715b
19 import java.io.File; 19 import java.io.File;
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.util.LinkedList; 21 import java.util.LinkedList;
22 import java.util.List; 22 import java.util.List;
23 23
24 import org.tmatesoft.hg.core.HgException;
24 import org.tmatesoft.hg.core.Nodeid; 25 import org.tmatesoft.hg.core.Nodeid;
26 import org.tmatesoft.hg.internal.ByteArrayChannel;
27 import org.tmatesoft.hg.internal.ByteArrayDataAccess;
25 import org.tmatesoft.hg.internal.DataAccess; 28 import org.tmatesoft.hg.internal.DataAccess;
26 import org.tmatesoft.hg.internal.DataAccessProvider; 29 import org.tmatesoft.hg.internal.DataAccessProvider;
27 import org.tmatesoft.hg.internal.DigestHelper; 30 import org.tmatesoft.hg.internal.DigestHelper;
28 import org.tmatesoft.hg.internal.RevlogStream; 31 import org.tmatesoft.hg.internal.RevlogStream;
29 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; 32 import org.tmatesoft.hg.repo.HgChangelog.RawChangeset;
33 import org.tmatesoft.hg.util.CancelledException;
30 34
31 35
32 /** 36 /**
33 * @see http://mercurial.selenic.com/wiki/BundleFormat 37 * @see http://mercurial.selenic.com/wiki/BundleFormat
34 * 38 *
43 public HgBundle(DataAccessProvider dap, File bundle) { 47 public HgBundle(DataAccessProvider dap, File bundle) {
44 accessProvider = dap; 48 accessProvider = dap;
45 bundleFile = bundle; 49 bundleFile = bundle;
46 } 50 }
47 51
48 public void changes(HgRepository hgRepo) throws IOException { 52 public void changes(HgRepository hgRepo) throws HgException, IOException {
49 DataAccess da = accessProvider.create(bundleFile); 53 DataAccess da = accessProvider.create(bundleFile);
50 DigestHelper dh = new DigestHelper(); 54 DigestHelper dh = new DigestHelper();
51 try { 55 try {
52 List<GroupElement> changelogGroup = readGroup(da); 56 List<GroupElement> changelogGroup = readGroup(da);
53 if (changelogGroup.isEmpty()) { 57 if (changelogGroup.isEmpty()) {
60 throw new IllegalArgumentException("unknown parent"); 64 throw new IllegalArgumentException("unknown parent");
61 } 65 }
62 // BundleFormat wiki says: 66 // BundleFormat wiki says:
63 // Each Changelog entry patches the result of all previous patches 67 // Each Changelog entry patches the result of all previous patches
64 // (the previous, or parent patch of a given patch p is the patch that has a node equal to p's p1 field) 68 // (the previous, or parent patch of a given patch p is the patch that has a node equal to p's p1 field)
65 byte[] baseRevContent = hgRepo.getChangelog().content(base); 69 ByteArrayChannel bac = new ByteArrayChannel();
70 hgRepo.getChangelog().rawContent(base, bac); // FIXME get DataAccess directly, to avoid
71 // extra byte[] (inside ByteArrayChannel) duplication just for the sake of subsequent ByteArrayDataChannel wrap.
72 ByteArrayDataAccess baseRevContent = new ByteArrayDataAccess(bac.toArray());
66 for (GroupElement ge : changelogGroup) { 73 for (GroupElement ge : changelogGroup) {
67 byte[] csetContent = RevlogStream.apply(baseRevContent, -1, ge.patches); 74 byte[] csetContent = RevlogStream.apply(baseRevContent, -1, ge.patches);
68 dh = dh.sha1(ge.firstParent(), ge.secondParent(), csetContent); // XXX ge may give me access to byte[] content of nodeid directly, perhaps, I don't need DH to be friend of Nodeid? 75 dh = dh.sha1(ge.firstParent(), ge.secondParent(), csetContent); // XXX ge may give me access to byte[] content of nodeid directly, perhaps, I don't need DH to be friend of Nodeid?
69 if (!ge.node().equalsTo(dh.asBinary())) { 76 if (!ge.node().equalsTo(dh.asBinary())) {
70 throw new IllegalStateException("Integrity check failed on " + bundleFile + ", node:" + ge.node()); 77 throw new IllegalStateException("Integrity check failed on " + bundleFile + ", node:" + ge.node());
71 } 78 }
72 RawChangeset cs = RawChangeset.parse(csetContent, 0, csetContent.length); 79 ByteArrayDataAccess csetDataAccess = new ByteArrayDataAccess(csetContent);
80 RawChangeset cs = RawChangeset.parse(csetDataAccess);
73 System.out.println(cs.toString()); 81 System.out.println(cs.toString());
74 baseRevContent = csetContent; 82 baseRevContent = csetDataAccess.reset();
75 } 83 }
84 } catch (CancelledException ex) {
85 System.out.println("Operation cancelled");
76 } finally { 86 } finally {
77 da.done(); 87 da.done();
78 } 88 }
79 } 89 }
80 90