comparison src/org/tmatesoft/hg/repo/HgDataFile.java @ 215:41a778e3fd31

Issue 5: Facilities for progress and cancellation. More specific exceptions. Exceptions from callbacks as RuntimeException
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 17 May 2011 00:56:54 +0200
parents a5198b6487dc
children 1d389c0cb0a5
comparison
equal deleted inserted replaced
214:4252faa556cd 215:41a778e3fd31
134 insp = new ContentPipe(sink, 0); 134 insp = new ContentPipe(sink, 0);
135 } else if (metadata.known(revision)) { 135 } else if (metadata.known(revision)) {
136 insp = new ContentPipe(sink, metadata.dataOffset(revision)); 136 insp = new ContentPipe(sink, metadata.dataOffset(revision));
137 } else { 137 } else {
138 // do not know if there's metadata 138 // do not know if there's metadata
139 insp = new MetadataContentPipe(sink, metadata); 139 insp = new MetadataContentPipe(sink, metadata, getPath());
140 } 140 }
141 insp.checkCancelled(); 141 insp.checkCancelled();
142 super.content.iterate(revision, revision, true, insp); 142 super.content.iterate(revision, revision, true, insp);
143 try { 143 try {
144 insp.checkFailed(); 144 insp.checkFailed();
145 } catch (HgDataStreamException ex) { 145 } catch (HgDataStreamException ex) {
146 throw ex; 146 throw ex;
147 } catch (HgException ex) { 147 } catch (HgException ex) {
148 // shall not happen, unless we changed ContentPipe or its subclass 148 // shall not happen, unless we changed ContentPipe or its subclass
149 throw new HgDataStreamException(ex.getClass().getName(), ex); 149 throw new HgDataStreamException(getPath(), ex.getClass().getName(), ex);
150 } 150 }
151 } 151 }
152 152
153 public void history(HgChangelog.Inspector inspector) { 153 public void history(HgChangelog.Inspector inspector) {
154 history(0, getLastRevision(), inspector); 154 history(0, getLastRevision(), inspector);
208 } 208 }
209 }); 209 });
210 } catch (CancelledException ex) { 210 } catch (CancelledException ex) {
211 // it's ok, we did that 211 // it's ok, we did that
212 } catch (Exception ex) { 212 } catch (Exception ex) {
213 throw new HgDataStreamException("Can't initialize metadata", ex); 213 throw new HgDataStreamException(getPath(), "Can't initialize metadata", ex);
214 } 214 }
215 } 215 }
216 if (!metadata.known(0)) { 216 if (!metadata.known(0)) {
217 return false; 217 return false;
218 } 218 }
317 } 317 }
318 318
319 private static class MetadataContentPipe extends ContentPipe { 319 private static class MetadataContentPipe extends ContentPipe {
320 320
321 private final Metadata metadata; 321 private final Metadata metadata;
322 322 private final Path fname; // need this only for error reporting
323 public MetadataContentPipe(ByteChannel sink, Metadata _metadata) { 323
324 public MetadataContentPipe(ByteChannel sink, Metadata _metadata, Path file) {
324 super(sink, 0); 325 super(sink, 0);
325 metadata = _metadata; 326 metadata = _metadata;
327 fname = file;
326 } 328 }
327 329
328 @Override 330 @Override
329 protected void prepare(int revisionNumber, DataAccess da) throws HgException, IOException { 331 protected void prepare(int revisionNumber, DataAccess da) throws HgException, IOException {
330 final int daLength = da.length(); 332 final int daLength = da.length();
380 } 382 }
381 } 383 }
382 _metadata.trimToSize(); 384 _metadata.trimToSize();
383 metadata.add(revisionNumber, lastEntryStart, _metadata); 385 metadata.add(revisionNumber, lastEntryStart, _metadata);
384 if (da.isEmpty() || !byteOne) { 386 if (da.isEmpty() || !byteOne) {
385 throw new HgDataStreamException(String.format("Metadata for revision %d is not closed properly", revisionNumber), null); 387 throw new HgDataStreamException(fname, String.format("Metadata for revision %d is not closed properly", revisionNumber), null);
386 } 388 }
387 // da is in prepared state (i.e. we consumed all bytes up to metadata end). 389 // da is in prepared state (i.e. we consumed all bytes up to metadata end).
388 } 390 }
389 } 391 }
390 } 392 }