annotate src/org/tmatesoft/hg/internal/FNCacheFile.java @ 647:c75297c17867

Location of repository files as enumeration, use file constants instead of plain names
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 25 Jun 2013 18:53:18 +0200
parents 507602cb4fb3
children 46b56864b483
rev   line source
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
647
c75297c17867 Location of repository files as enumeration, use file constants instead of plain names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
19 import static org.tmatesoft.hg.repo.HgRepositoryFiles.FNCache;
c75297c17867 Location of repository files as enumeration, use file constants instead of plain names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
20
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.File;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.FileOutputStream;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.io.IOException;
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
24 import java.nio.ByteBuffer;
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
25 import java.nio.CharBuffer;
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
26 import java.nio.channels.FileChannel;
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.nio.charset.Charset;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import java.util.ArrayList;
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
29 import java.util.List;
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 import org.tmatesoft.hg.util.Path;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 /**
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
34 * Append-only fncache support
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
35 *
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * <blockquote>
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 * The fncache file contains the paths of all filelog files in the store as encoded by mercurial.filelog.encodedir. The paths are separated by '\n' (LF).
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 * </blockquote>
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
40 *
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
41 *
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 * @author Artem Tikhomirov
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 * @author TMate Software Ltd.
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 */
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 public class FNCacheFile {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 private final Internals repo;
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
48 // private final List<Path> files;
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
49 private final List<Path> addedDotI;
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
50 private final List<Path> addedDotD;
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
51 private final FNCachePathHelper pathHelper;
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 public FNCacheFile(Internals internalRepo) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 repo = internalRepo;
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
55 // files = new ArrayList<Path>();
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
56 pathHelper = new FNCachePathHelper();
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
57 addedDotI = new ArrayList<Path>(5);
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
58 addedDotD = new ArrayList<Path>(5);
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
61 /*
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
62 * For append-only option, we don't care reading the original content
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 public void read(Path.Source pathFactory) throws IOException {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 File f = fncacheFile();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 files.clear();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 if (!f.exists()) {
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
67 return;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 ArrayList<String> entries = new ArrayList<String>();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 // names in fncache are in local encoding, shall translate to unicode
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 new LineReader(f, repo.getSessionContext().getLog(), repo.getFilenameEncoding()).read(new LineReader.SimpleLineCollector(), entries);
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 for (String e : entries) {
624
507602cb4fb3 FIXMEs and TODOs: pay some technical debt
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 616
diff changeset
73 // XXX plain wrong, need either to decode paths and strip off .i/.d or (if keep names as is) change write()
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 files.add(pathFactory.path(e));
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 }
559
6ca3d0c5b4bc Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 539
diff changeset
77 */
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 public void write() throws IOException {
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
80 if (addedDotI.isEmpty() && addedDotD.isEmpty()) {
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 return;
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
82 }
647
c75297c17867 Location of repository files as enumeration, use file constants instead of plain names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 624
diff changeset
83 File f = repo.getRepositoryFile(FNCache);
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
84 f.getParentFile().mkdirs();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 final Charset filenameEncoding = repo.getFilenameEncoding();
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
86 ArrayList<CharBuffer> added = new ArrayList<CharBuffer>();
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
87 for (Path p : addedDotI) {
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
88 added.add(CharBuffer.wrap(pathHelper.rewrite(p)));
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
89 }
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
90 for (Path p : addedDotD) {
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
91 // XXX FNCachePathHelper always return name of an index file, need to change it into a name of data file,
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
92 // although the approach (to replace last char) is depressingly awful
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
93 CharSequence cs = pathHelper.rewrite(p);
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
94 CharBuffer cb = CharBuffer.allocate(cs.length());
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
95 cb.append(cs);
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
96 cb.put(cs.length()-1, 'd');
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
97 cb.flip();
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
98 added.add(cb);
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
99 }
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
100 FileChannel fncacheFile = new FileOutputStream(f, true).getChannel();
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
101 ByteBuffer lf = ByteBuffer.wrap(new byte[] { 0x0A });
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
102 for (CharBuffer b : added) {
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
103 fncacheFile.write(filenameEncoding.encode(b));
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
104 fncacheFile.write(lf);
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
105 lf.rewind();
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 fncacheFile.close();
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109
616
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
110 public void addIndex(Path p) {
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
111 addedDotI.add(p);
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
112 }
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
113
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
114 public void addData(Path p) {
5e0313485eef encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 559
diff changeset
115 addedDotD.add(p);
539
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
9edfd5a223b8 Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 }