Mercurial > hg4j
comparison src/org/tmatesoft/hg/internal/RevlogStreamFactory.java @ 591:e447384f3771
CommitFacility as internal class; refactored infrastructure around internals (access to RevlogStream)
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 30 Apr 2013 18:55:42 +0200 |
parents | |
children | e1b29756f901 |
comparison
equal
deleted
inserted
replaced
590:8cbc2a883d95 | 591:e447384f3771 |
---|---|
1 /* | |
2 * Copyright (c) 2013 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@hg4j.com | |
16 */ | |
17 package org.tmatesoft.hg.internal; | |
18 | |
19 import java.io.File; | |
20 import java.io.IOException; | |
21 import java.lang.ref.SoftReference; | |
22 import java.util.HashMap; | |
23 | |
24 import org.tmatesoft.hg.repo.HgInvalidControlFileException; | |
25 import org.tmatesoft.hg.util.Path; | |
26 | |
27 /** | |
28 * Factory to create {@link RevlogStream RevlogStreams}, cache-capable. | |
29 * | |
30 * @author Artem Tikhomirov | |
31 * @author TMate Software Ltd. | |
32 */ | |
33 public final class RevlogStreamFactory { | |
34 | |
35 private final Internals repo; | |
36 private final HashMap<Path, SoftReference<RevlogStream>> streamsCache; | |
37 | |
38 | |
39 public RevlogStreamFactory(Internals hgRepo, boolean shallCacheRevlogs) { | |
40 repo = hgRepo; | |
41 if (shallCacheRevlogs) { | |
42 streamsCache = new HashMap<Path, SoftReference<RevlogStream>>(); | |
43 } else { | |
44 streamsCache = null; | |
45 } | |
46 } | |
47 | |
48 /** | |
49 * Creates a stream for specified file, doesn't cache stream | |
50 */ | |
51 /*package-local*/ RevlogStream create(File f) { | |
52 return new RevlogStream(repo.getDataAccess(), f); | |
53 } | |
54 | |
55 /** | |
56 * Perhaps, should be separate interface, like ContentLookup | |
57 * @param path - normalized file name | |
58 * @return <code>null</code> if path doesn't resolve to a existing file | |
59 */ | |
60 /*package-local*/ RevlogStream resolveStoreFile(Path path) { | |
61 final SoftReference<RevlogStream> ref = shallCacheRevlogs() ? streamsCache.get(path) : null; | |
62 RevlogStream cached = ref == null ? null : ref.get(); | |
63 if (cached != null) { | |
64 return cached; | |
65 } | |
66 File f = repo.getFileFromDataDir(path); | |
67 if (f.exists()) { | |
68 RevlogStream s = create(f); | |
69 if (shallCacheRevlogs()) { | |
70 streamsCache.put(path, new SoftReference<RevlogStream>(s)); | |
71 } | |
72 return s; | |
73 } | |
74 return null; | |
75 } | |
76 | |
77 /*package-local*/ RevlogStream createStoreFile(Path path) throws HgInvalidControlFileException { | |
78 File f = repo.getFileFromDataDir(path); | |
79 try { | |
80 if (!f.exists()) { | |
81 f.getParentFile().mkdirs(); | |
82 f.createNewFile(); | |
83 } | |
84 RevlogStream s = create(f); | |
85 if (shallCacheRevlogs()) { | |
86 streamsCache.put(path, new SoftReference<RevlogStream>(s)); | |
87 } | |
88 return s; | |
89 } catch (IOException ex) { | |
90 throw new HgInvalidControlFileException("Can't create a file in the storage", ex, f); | |
91 } | |
92 } | |
93 | |
94 private boolean shallCacheRevlogs() { | |
95 return streamsCache != null; | |
96 } | |
97 } |