Mercurial > jhg
comparison src/org/tmatesoft/hg/repo/Revlog.java @ 74:6f1b88693d48
Complete refactoring to org.tmatesoft
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Mon, 24 Jan 2011 03:14:45 +0100 |
parents | src/com/tmate/hgkit/ll/Revlog.java@576d6e8a09f6 |
children | c677e1593919 |
comparison
equal
deleted
inserted
replaced
73:0d279bcc4442 | 74:6f1b88693d48 |
---|---|
1 /* | |
2 * Copyright (c) 2010-2011 TMate Software Ltd | |
3 * | |
4 * This program is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; version 2 of the License. | |
7 * | |
8 * This program is distributed in the hope that it will be useful, | |
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 * GNU General Public License for more details. | |
12 * | |
13 * For information on how to redistribute this software under | |
14 * the terms of a license other than GNU General Public License | |
15 * contact TMate Software at support@svnkit.com | |
16 */ | |
17 package org.tmatesoft.hg.repo; | |
18 | |
19 import static org.tmatesoft.hg.repo.HgRepository.TIP; | |
20 | |
21 import java.util.Arrays; | |
22 import java.util.Collection; | |
23 import java.util.Collections; | |
24 import java.util.HashMap; | |
25 import java.util.LinkedHashSet; | |
26 import java.util.Map; | |
27 import java.util.Set; | |
28 | |
29 import org.tmatesoft.hg.core.Nodeid; | |
30 | |
31 | |
32 /** | |
33 * | |
34 * @author Artem Tikhomirov | |
35 * @author TMate Software Ltd. | |
36 */ | |
37 abstract class Revlog { | |
38 | |
39 private final HgRepository hgRepo; | |
40 protected final RevlogStream content; | |
41 | |
42 protected Revlog(HgRepository hgRepo, RevlogStream content) { | |
43 if (hgRepo == null) { | |
44 throw new IllegalArgumentException(); | |
45 } | |
46 if (content == null) { | |
47 throw new IllegalArgumentException(); | |
48 } | |
49 this.hgRepo = hgRepo; | |
50 this.content = content; | |
51 } | |
52 | |
53 public final HgRepository getRepo() { | |
54 return hgRepo; | |
55 } | |
56 | |
57 public int getRevisionCount() { | |
58 return content.revisionCount(); | |
59 } | |
60 | |
61 public int getLocalRevisionNumber(Nodeid nid) { | |
62 int revision = content.findLocalRevisionNumber(nid); | |
63 if (revision == Integer.MIN_VALUE) { | |
64 throw new IllegalArgumentException(String.format("%s doesn't represent a revision of %s", nid.toString(), this /*XXX HgDataFile.getPath might be more suitable here*/)); | |
65 } | |
66 return revision; | |
67 } | |
68 | |
69 // Till now, i follow approach that NULL nodeid is never part of revlog | |
70 public boolean isKnown(Nodeid nodeid) { | |
71 final int rn = content.findLocalRevisionNumber(nodeid); | |
72 if (Integer.MIN_VALUE == rn) { | |
73 return false; | |
74 } | |
75 if (rn < 0 || rn >= content.revisionCount()) { | |
76 // Sanity check | |
77 throw new IllegalStateException(); | |
78 } | |
79 return true; | |
80 } | |
81 | |
82 /** | |
83 * Access to revision data as is (decompressed, but otherwise unprocessed, i.e. not parsed for e.g. changeset or manifest entries) | |
84 * @param nodeid | |
85 */ | |
86 public byte[] content(Nodeid nodeid) { | |
87 return content(getLocalRevisionNumber(nodeid)); | |
88 } | |
89 | |
90 /** | |
91 * @param revision - repo-local index of this file change (not a changelog revision number!) | |
92 */ | |
93 public byte[] content(int revision) { | |
94 final byte[][] dataPtr = new byte[1][]; | |
95 Revlog.Inspector insp = new Revlog.Inspector() { | |
96 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { | |
97 dataPtr[0] = data; | |
98 } | |
99 }; | |
100 content.iterate(revision, revision, true, insp); | |
101 return dataPtr[0]; | |
102 } | |
103 | |
104 /** | |
105 * XXX perhaps, return value Nodeid[2] and boolean needNodeids is better (and higher level) API for this query? | |
106 * | |
107 * @param revision - revision to query parents, or {@link HgRepository#TIP} | |
108 * @param parentRevisions - int[2] to get local revision numbers of parents (e.g. {6, -1}) | |
109 * @param parent1 - byte[20] or null, if parent's nodeid is not needed | |
110 * @param parent2 - byte[20] or null, if second parent's nodeid is not needed | |
111 * @return | |
112 */ | |
113 public void parents(int revision, int[] parentRevisions, byte[] parent1, byte[] parent2) { | |
114 if (revision != TIP && !(revision >= 0 && revision < content.revisionCount())) { | |
115 throw new IllegalArgumentException(String.valueOf(revision)); | |
116 } | |
117 if (parentRevisions == null || parentRevisions.length < 2) { | |
118 throw new IllegalArgumentException(String.valueOf(parentRevisions)); | |
119 } | |
120 if (parent1 != null && parent1.length < 20) { | |
121 throw new IllegalArgumentException(parent1.toString()); | |
122 } | |
123 if (parent2 != null && parent2.length < 20) { | |
124 throw new IllegalArgumentException(parent2.toString()); | |
125 } | |
126 class ParentCollector implements Revlog.Inspector { | |
127 public int p1 = -1; | |
128 public int p2 = -1; | |
129 public byte[] nodeid; | |
130 | |
131 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { | |
132 p1 = parent1Revision; | |
133 p2 = parent2Revision; | |
134 this.nodeid = new byte[20]; | |
135 // nodeid arg now comes in 32 byte from (as in file format description), however upper 12 bytes are zeros. | |
136 System.arraycopy(nodeid, nodeid.length > 20 ? nodeid.length - 20 : 0, this.nodeid, 0, 20); | |
137 } | |
138 }; | |
139 ParentCollector pc = new ParentCollector(); | |
140 content.iterate(revision, revision, false, pc); | |
141 parentRevisions[0] = pc.p1; | |
142 parentRevisions[1] = pc.p2; | |
143 if (parent1 != null) { | |
144 if (parentRevisions[0] == -1) { | |
145 Arrays.fill(parent1, 0, 20, (byte) 0); | |
146 } else { | |
147 content.iterate(parentRevisions[0], parentRevisions[0], false, pc); | |
148 System.arraycopy(pc.nodeid, 0, parent1, 0, 20); | |
149 } | |
150 } | |
151 if (parent2 != null) { | |
152 if (parentRevisions[1] == -1) { | |
153 Arrays.fill(parent2, 0, 20, (byte) 0); | |
154 } else { | |
155 content.iterate(parentRevisions[1], parentRevisions[1], false, pc); | |
156 System.arraycopy(pc.nodeid, 0, parent2, 0, 20); | |
157 } | |
158 } | |
159 } | |
160 | |
161 // FIXME byte[] data might be too expensive, for few usecases it may be better to have intermediate Access object (when we don't need full data | |
162 // instantly - e.g. calculate hash, or comparing two revisions | |
163 // XXX seems that RevlogStream is better place for this class. | |
164 public interface Inspector { | |
165 // XXX boolean retVal to indicate whether to continue? | |
166 // TODO specify nodeid and data length, and reuse policy (i.e. if revlog stream doesn't reuse nodeid[] for each call) | |
167 void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[/*20*/] nodeid, byte[] data); | |
168 } | |
169 | |
170 /* | |
171 * XXX think over if it's better to do either: | |
172 * pw = getChangelog().new ParentWalker(); pw.init() and pass pw instance around as needed | |
173 * or | |
174 * add Revlog#getParentWalker(), static class, make cons() and #init package-local, and keep SoftReference to allow walker reuse. | |
175 * | |
176 * and yes, walker is not a proper name | |
177 */ | |
178 public final class ParentWalker { | |
179 private Map<Nodeid, Nodeid> firstParent; | |
180 private Map<Nodeid, Nodeid> secondParent; | |
181 private Set<Nodeid> allNodes; | |
182 | |
183 public ParentWalker() { | |
184 firstParent = secondParent = Collections.emptyMap(); | |
185 allNodes = Collections.emptySet(); | |
186 } | |
187 | |
188 public void init() { | |
189 final RevlogStream stream = Revlog.this.content; | |
190 final int revisionCount = stream.revisionCount(); | |
191 firstParent = new HashMap<Nodeid, Nodeid>(revisionCount); | |
192 secondParent = new HashMap<Nodeid, Nodeid>(firstParent.size() >> 1); // assume branches/merges are less frequent | |
193 allNodes = new LinkedHashSet<Nodeid>(); | |
194 | |
195 Inspector insp = new Inspector() { | |
196 final Nodeid[] sequentialRevisionNodeids = new Nodeid[revisionCount]; | |
197 int ix = 0; | |
198 public void next(int revisionNumber, int actualLen, int baseRevision, int linkRevision, int parent1Revision, int parent2Revision, byte[] nodeid, byte[] data) { | |
199 if (ix != revisionNumber) { | |
200 // XXX temp code, just to make sure I understand what's going on here | |
201 throw new IllegalStateException(); | |
202 } | |
203 if (parent1Revision >= revisionNumber || parent2Revision >= revisionNumber) { | |
204 throw new IllegalStateException(); // sanity, revisions are sequential | |
205 } | |
206 final Nodeid nid = new Nodeid(nodeid, true); | |
207 sequentialRevisionNodeids[ix++] = nid; | |
208 allNodes.add(nid); | |
209 if (parent1Revision != -1) { | |
210 firstParent.put(nid, sequentialRevisionNodeids[parent1Revision]); | |
211 if (parent2Revision != -1) { | |
212 secondParent.put(nid, sequentialRevisionNodeids[parent2Revision]); | |
213 } | |
214 } | |
215 } | |
216 }; | |
217 stream.iterate(0, -1, false, insp); | |
218 } | |
219 | |
220 public Set<Nodeid> allNodes() { | |
221 return Collections.unmodifiableSet(allNodes); | |
222 } | |
223 | |
224 // FIXME need to decide whether Nodeid(00 * 20) is always known or not | |
225 public boolean knownNode(Nodeid nid) { | |
226 return allNodes.contains(nid); | |
227 } | |
228 | |
229 // null if none | |
230 public Nodeid firstParent(Nodeid nid) { | |
231 return firstParent.get(nid); | |
232 } | |
233 | |
234 // never null, Nodeid.NULL if none known | |
235 public Nodeid safeFirstParent(Nodeid nid) { | |
236 Nodeid rv = firstParent(nid); | |
237 return rv == null ? Nodeid.NULL : rv; | |
238 } | |
239 | |
240 public Nodeid secondParent(Nodeid nid) { | |
241 return secondParent.get(nid); | |
242 } | |
243 | |
244 public Nodeid safeSecondParent(Nodeid nid) { | |
245 Nodeid rv = secondParent(nid); | |
246 return rv == null ? Nodeid.NULL : rv; | |
247 } | |
248 | |
249 public boolean appendParentsOf(Nodeid nid, Collection<Nodeid> c) { | |
250 Nodeid p1 = firstParent(nid); | |
251 boolean modified = false; | |
252 if (p1 != null) { | |
253 modified = c.add(p1); | |
254 Nodeid p2 = secondParent(nid); | |
255 if (p2 != null) { | |
256 modified = c.add(p2) || modified; | |
257 } | |
258 } | |
259 return modified; | |
260 } | |
261 } | |
262 } |