annotate src/org/tmatesoft/hg/internal/WorkingDirFileWriter.java @ 574:88afffd39899

Improve memory consumption of HgManifest#getFileRevision(): avoid extra byte[] instances
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 16 Apr 2013 14:44:57 +0200
parents becd2a1310a2
children bd5926e24aa3
rev   line source
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2012-2013 TMate Software Ltd
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import java.io.File;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20 import java.io.FileOutputStream;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.IOException;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.nio.ByteBuffer;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.nio.channels.FileChannel;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import org.tmatesoft.hg.repo.HgDataFile;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import org.tmatesoft.hg.util.ByteChannel;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import org.tmatesoft.hg.util.CancelledException;
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
28 import org.tmatesoft.hg.util.LogFacility.Severity;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import org.tmatesoft.hg.util.Path;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 /**
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 *
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 * @author Artem Tikhomirov
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 * @author TMate Software Ltd.
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 */
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 public class WorkingDirFileWriter implements ByteChannel {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
39 private final Internals hgRepo;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private File dest;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private FileChannel destChannel;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 private int totalBytesWritten;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
44 public WorkingDirFileWriter(Internals internalRepo) {
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
45 hgRepo = internalRepo;
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47
572
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
48 /**
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
49 * Information purposes only, to find out trouble location if {@link #processFile(HgDataFile, int)} fails
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
50 */
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
51 public File getDestinationFile() {
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
52 return dest;
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
53 }
becd2a1310a2 Report file object in case of error to be helpful as much as possible
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 526
diff changeset
54
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 public void processFile(HgDataFile df, int fileRevIndex) throws IOException {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 try {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 prepare(df.getPath());
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 df.contentWithFilters(fileRevIndex, this);
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 } catch (CancelledException ex) {
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
60 hgRepo.getSessionContext().getLog().dump(getClass(), Severity.Error, ex, "Our impl doesn't throw cancellation");
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 finish();
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 public void prepare(Path fname) throws IOException {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 String fpath = fname.toString();
526
2f9ed6bcefa2 Initial support for Revert command with accompanying minor refactoring
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 525
diff changeset
67 dest = new File(hgRepo.getRepo().getWorkingDir(), fpath);
525
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 if (fpath.indexOf('/') != -1) {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 dest.getParentFile().mkdirs();
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 destChannel = new FileOutputStream(dest).getChannel();
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 totalBytesWritten = 0;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 public int write(ByteBuffer buffer) throws IOException, CancelledException {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 int written = destChannel.write(buffer);
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 totalBytesWritten += written;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 return written;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 public void finish() throws IOException {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 destChannel.close();
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
83 dest = null;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 public int bytesWritten() {
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87 return totalBytesWritten;
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 }
0be5be8d57e9 Repository checkout support, first iteration
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 }