2 * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
3 * Copyright (C) 2009, Google Inc.
7 * Redistribution and use in source and binary forms, with or
8 * without modification, are permitted provided that the following
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
19 * - Neither the name of the Git Development Community nor the
20 * names of its contributors may be used to endorse or promote
21 * products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
25 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
26 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 package org
.spearce
.jgit
.lib
;
42 import java
.io
.FileOutputStream
;
43 import java
.io
.IOException
;
44 import java
.util
.Arrays
;
46 import org
.spearce
.jgit
.errors
.IncorrectObjectTypeException
;
47 import org
.spearce
.jgit
.errors
.MissingObjectException
;
48 import org
.spearce
.jgit
.revwalk
.RevObject
;
49 import org
.spearce
.jgit
.revwalk
.RevWalk
;
51 public class ConcurrentRepackTest
extends RepositoryTestCase
{
52 public void setUp() throws Exception
{
53 WindowCacheConfig windowCacheConfig
= new WindowCacheConfig();
54 windowCacheConfig
.setPackedGitOpenFiles(1);
55 WindowCache
.reconfigure(windowCacheConfig
);
59 protected void tearDown() throws Exception
{
61 WindowCacheConfig windowCacheConfig
= new WindowCacheConfig();
62 WindowCache
.reconfigure(windowCacheConfig
);
65 public void testObjectInNewPack() throws IncorrectObjectTypeException
,
67 // Create a new object in a new pack, and test that it is present.
69 final Repository eden
= createNewEmptyRepo();
70 final RevObject o1
= writeBlob(eden
, "o1");
72 assertEquals(o1
.name(), parse(o1
).name());
75 public void testObjectMovedToNewPack1()
76 throws IncorrectObjectTypeException
, IOException
{
77 // Create an object and pack it. Then remove that pack and put the
78 // object into a different pack file, with some other object. We
79 // still should be able to access the objects.
81 final Repository eden
= createNewEmptyRepo();
82 final RevObject o1
= writeBlob(eden
, "o1");
83 final File
[] out1
= pack(eden
, o1
);
84 assertEquals(o1
.name(), parse(o1
).name());
86 final RevObject o2
= writeBlob(eden
, "o2");
89 // Force close, and then delete, the old pack.
94 // Now here is the interesting thing. Will git figure the new
95 // object exists in the new pack, and not the old one.
97 assertEquals(o2
.name(), parse(o2
).name());
98 assertEquals(o1
.name(), parse(o1
).name());
101 public void testObjectMovedWithinPack()
102 throws IncorrectObjectTypeException
, IOException
{
103 // Create an object and pack it.
105 final Repository eden
= createNewEmptyRepo();
106 final RevObject o1
= writeBlob(eden
, "o1");
107 final File
[] out1
= pack(eden
, o1
);
108 assertEquals(o1
.name(), parse(o1
).name());
110 // Force close the old pack.
114 // Now overwrite the old pack in place. This method of creating a
115 // different pack under the same file name is partially broken. We
116 // should also have a different file name because the list of objects
117 // within the pack has been modified.
119 final RevObject o2
= writeBlob(eden
, "o2");
120 final PackWriter pw
= new PackWriter(eden
, NullProgressMonitor
.INSTANCE
);
125 // Try the old name, then the new name. The old name should cause the
126 // pack to reload when it opens and the index and pack mismatch.
128 assertEquals(o1
.name(), parse(o1
).name());
129 assertEquals(o2
.name(), parse(o2
).name());
132 public void testObjectMovedToNewPack2()
133 throws IncorrectObjectTypeException
, IOException
{
134 // Create an object and pack it. Then remove that pack and put the
135 // object into a different pack file, with some other object. We
136 // still should be able to access the objects.
138 final Repository eden
= createNewEmptyRepo();
139 final RevObject o1
= writeBlob(eden
, "o1");
140 final File
[] out1
= pack(eden
, o1
);
141 assertEquals(o1
.name(), parse(o1
).name());
143 final ObjectLoader load1
= db
.openBlob(o1
);
144 assertNotNull(load1
);
146 final RevObject o2
= writeBlob(eden
, "o2");
149 // Force close, and then delete, the old pack.
154 // Now here is the interesting thing... can the loader we made
155 // earlier still resolve the object, even though its underlying
156 // pack is gone, but the object still exists.
158 final ObjectLoader load2
= db
.openBlob(o1
);
159 assertNotNull(load2
);
160 assertNotSame(load1
, load2
);
162 final byte[] data2
= load2
.getCachedBytes();
163 final byte[] data1
= load1
.getCachedBytes();
164 assertNotNull(data2
);
165 assertNotNull(data1
);
166 assertNotSame(data1
, data2
); // cache should be per-pack, not per object
167 assertTrue(Arrays
.equals(data1
, data2
));
168 assertEquals(load2
.getType(), load1
.getType());
171 private static void whackCache() {
172 final WindowCacheConfig config
= new WindowCacheConfig();
173 config
.setPackedGitOpenFiles(1);
174 WindowCache
.reconfigure(config
);
177 private RevObject
parse(final AnyObjectId id
)
178 throws MissingObjectException
, IOException
{
179 return new RevWalk(db
).parseAny(id
);
182 private File
[] pack(final Repository src
, final RevObject
... list
)
184 final PackWriter pw
= new PackWriter(src
, NullProgressMonitor
.INSTANCE
);
185 for (final RevObject o
: list
) {
189 final ObjectId name
= pw
.computeName();
190 final File packFile
= fullPackFileName(name
, ".pack");
191 final File idxFile
= fullPackFileName(name
, ".idx");
192 final File
[] files
= new File
[] { packFile
, idxFile
};
197 private static void write(final File
[] files
, final PackWriter pw
)
199 final long begin
= files
[0].getParentFile().lastModified();
200 FileOutputStream out
;
202 out
= new FileOutputStream(files
[0]);
209 out
= new FileOutputStream(files
[1]);
216 touch(begin
, files
[0].getParentFile());
219 private static void delete(final File
[] list
) {
220 final long begin
= list
[0].getParentFile().lastModified();
221 for (final File f
: list
) {
223 assertFalse(f
+ " was removed", f
.exists());
225 touch(begin
, list
[0].getParentFile());
228 private static void touch(final long begin
, final File dir
) {
229 while (begin
>= dir
.lastModified()) {
232 } catch (InterruptedException ie
) {
235 dir
.setLastModified(System
.currentTimeMillis());
239 private File
fullPackFileName(final ObjectId name
, final String suffix
) {
240 final File packdir
= new File(db
.getObjectsDirectory(), "pack");
241 return new File(packdir
, "pack-" + name
.name() + suffix
);
244 private RevObject
writeBlob(final Repository repo
, final String data
)
246 final RevWalk revWalk
= new RevWalk(repo
);
247 final byte[] bytes
= data
.getBytes(Constants
.CHARACTER_ENCODING
);
248 final ObjectWriter ow
= new ObjectWriter(repo
);
249 final ObjectId id
= ow
.writeBlob(bytes
);
252 fail("Object " + id
.name() + " should not exist in test repository");
253 } catch (MissingObjectException e
) {
256 return revWalk
.lookupBlob(id
);