]> source.dussan.org Git - jgit.git/commitdiff
dfs: optionally store blockSize in DfsPackDescription 42/101742/2
authorShawn Pearce <spearce@spearce.org>
Fri, 21 Jul 2017 14:54:00 +0000 (07:54 -0700)
committerShawn Pearce <spearce@spearce.org>
Fri, 21 Jul 2017 15:33:17 +0000 (08:33 -0700)
Allow a DFS implementation to report blockSize to DfsPackFile,
bypassing alignment errors and corrections in the DfsBlockCache when
the blockSize of a specific file differs from the cache's configured
blockSize.

Change-Id: Ic376314d4a86a0bd528c033e169d93eef035b233

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsGarbageCollector.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsInserter.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackCompactor.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackDescription.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackFile.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/dfs/DfsPackParser.java

index c7567b58c2f6f1eece65541da597a5cde565e919..ce2b05382a7d47be4e9f9dbc9b53f43e3af9a469 100644 (file)
@@ -563,22 +563,25 @@ public class DfsGarbageCollector {
                try (DfsOutputStream out = objdb.writeFile(pack, PACK)) {
                        pw.writePack(pm, pm, out);
                        pack.addFileExt(PACK);
+                       pack.setBlockSize(PACK, out.blockSize());
                }
 
-               try (CountingOutputStream cnt =
-                               new CountingOutputStream(objdb.writeFile(pack, INDEX))) {
+               try (DfsOutputStream out = objdb.writeFile(pack, INDEX)) {
+                       CountingOutputStream cnt = new CountingOutputStream(out);
                        pw.writeIndex(cnt);
                        pack.addFileExt(INDEX);
                        pack.setFileSize(INDEX, cnt.getCount());
+                       pack.setBlockSize(INDEX, out.blockSize());
                        pack.setIndexVersion(pw.getIndexVersion());
                }
 
                if (pw.prepareBitmapIndex(pm)) {
-                       try (CountingOutputStream cnt = new CountingOutputStream(
-                                       objdb.writeFile(pack, BITMAP_INDEX))) {
+                       try (DfsOutputStream out = objdb.writeFile(pack, BITMAP_INDEX)) {
+                               CountingOutputStream cnt = new CountingOutputStream(out);
                                pw.writeBitmapIndex(cnt);
                                pack.addFileExt(BITMAP_INDEX);
                                pack.setFileSize(BITMAP_INDEX, cnt.getCount());
+                               pack.setBlockSize(BITMAP_INDEX, out.blockSize());
                        }
                }
 
index 3a62c9394c6f3c02d1e7ea86a23402292d360304..01654d45dbece489645235ba5ac86a69a12ad3f2 100644 (file)
@@ -281,7 +281,9 @@ public class DfsInserter extends ObjectInserter {
 
                rollback = true;
                packDsc = db.newPack(DfsObjDatabase.PackSource.INSERT);
-               packOut = new PackStream(db.writeFile(packDsc, PACK));
+               DfsOutputStream dfsOut = db.writeFile(packDsc, PACK);
+               packDsc.setBlockSize(PACK, dfsOut.blockSize());
+               packOut = new PackStream(dfsOut);
                packKey = packDsc.getStreamKey(PACK);
 
                // Write the header as though it were a single object pack.
@@ -312,13 +314,14 @@ public class DfsInserter extends ObjectInserter {
                        packIndex = PackIndex.read(buf.openInputStream());
                }
 
-               DfsOutputStream os = db.writeFile(pack, INDEX);
-               try (CountingOutputStream cnt = new CountingOutputStream(os)) {
+               try (DfsOutputStream os = db.writeFile(pack, INDEX)) {
+                       CountingOutputStream cnt = new CountingOutputStream(os);
                        if (buf != null)
                                buf.writeTo(cnt, null);
                        else
                                index(cnt, packHash, list);
                        pack.addFileExt(INDEX);
+                       pack.setBlockSize(INDEX, os.blockSize());
                        pack.setFileSize(INDEX, cnt.getCount());
                } finally {
                        if (buf != null) {
index f7c87a4e7914169933e273005b15161c48597f7f..ac14c0bc3251b1b3efe070247cf163560233a7e9 100644 (file)
@@ -370,27 +370,23 @@ public class DfsPackCompactor {
        private static void writePack(DfsObjDatabase objdb,
                        DfsPackDescription pack,
                        PackWriter pw, ProgressMonitor pm) throws IOException {
-               DfsOutputStream out = objdb.writeFile(pack, PACK);
-               try {
+               try (DfsOutputStream out = objdb.writeFile(pack, PACK)) {
                        pw.writePack(pm, pm, out);
                        pack.addFileExt(PACK);
-               } finally {
-                       out.close();
+                       pack.setBlockSize(PACK, out.blockSize());
                }
        }
 
        private static void writeIndex(DfsObjDatabase objdb,
                        DfsPackDescription pack,
                        PackWriter pw) throws IOException {
-               DfsOutputStream out = objdb.writeFile(pack, INDEX);
-               try {
+               try (DfsOutputStream out = objdb.writeFile(pack, INDEX)) {
                        CountingOutputStream cnt = new CountingOutputStream(out);
                        pw.writeIndex(cnt);
                        pack.addFileExt(INDEX);
                        pack.setFileSize(INDEX, cnt.getCount());
+                       pack.setBlockSize(INDEX, out.blockSize());
                        pack.setIndexVersion(pw.getIndexVersion());
-               } finally {
-                       out.close();
                }
        }
 
index 0e2ed3b8944555eaa76b2a2bfa17cc7d748f0871..58a006e45ba1156e4118fc93b2ee95c38ec13788 100644 (file)
@@ -65,6 +65,7 @@ public class DfsPackDescription implements Comparable<DfsPackDescription> {
        private PackSource packSource;
        private long lastModified;
        private long[] sizeMap;
+       private int[] blockSizeMap;
        private long objectCount;
        private long deltaCount;
        private PackStatistics stats;
@@ -91,7 +92,10 @@ public class DfsPackDescription implements Comparable<DfsPackDescription> {
                this.repoDesc = repoDesc;
                int dot = name.lastIndexOf('.');
                this.packName = (dot < 0) ? name : name.substring(0, dot);
-               this.sizeMap = new long[PackExt.values().length];
+
+               int extCnt = PackExt.values().length;
+               sizeMap = new long[extCnt];
+               blockSizeMap = new int[extCnt];
        }
 
        /** @return description of the repository. */
@@ -193,6 +197,34 @@ public class DfsPackDescription implements Comparable<DfsPackDescription> {
                return i < sizeMap.length ? sizeMap[i] : 0;
        }
 
+       /**
+        * @param ext
+        *            the file extension.
+        * @return blockSize of the file, in bytes. If 0 the blockSize size is not
+        *         yet known and may be discovered when opening the file.
+        */
+       public int getBlockSize(PackExt ext) {
+               int i = ext.getPosition();
+               return i < blockSizeMap.length ? blockSizeMap[i] : 0;
+       }
+
+       /**
+        * @param ext
+        *            the file extension.
+        * @param blockSize
+        *            blockSize of the file, in bytes. If 0 the blockSize is not
+        *            known and will be determined on first read.
+        * @return {@code this}
+        */
+       public DfsPackDescription setBlockSize(PackExt ext, int blockSize) {
+               int i = ext.getPosition();
+               if (i >= blockSizeMap.length) {
+                       blockSizeMap = Arrays.copyOf(blockSizeMap, i + 1);
+               }
+               blockSizeMap[i] = Math.max(0, blockSize);
+               return this;
+       }
+
        /**
         * @param estimatedPackSize
         *            estimated size of the .pack file in bytes. If 0 the pack file
index b58016fd5e1c7752ddb58fdfc1dd121efd7c6a0f..2326219fd861e67452f4302c05bbda2aab0e0497 100644 (file)
@@ -123,9 +123,14 @@ public final class DfsPackFile extends BlockBasedFile {
         */
        DfsPackFile(DfsBlockCache cache, DfsPackDescription desc) {
                super(cache, desc, PACK);
-               length = desc.getFileSize(PACK);
-               if (length <= 0)
-                       length = -1;
+
+               int bs = desc.getBlockSize(PACK);
+               if (bs > 0) {
+                       setBlockSize(bs);
+               }
+
+               long sz = desc.getFileSize(PACK);
+               length = sz > 0 ? sz : -1;
        }
 
        /** @return description that was originally used to configure this pack file. */
index 07f51edf1577a0168ae89dd7777fb768384208b6..fd99db1e2814056a17b80cac118d6f217d9a2b62 100644 (file)
@@ -150,6 +150,7 @@ public class DfsPackParser extends PackParser {
                        readBlock = null;
                        packDsc.addFileExt(PACK);
                        packDsc.setFileSize(PACK, packEnd);
+                       packDsc.setBlockSize(PACK, blockSize);
 
                        writePackIndex();
                        objdb.commitPack(Collections.singletonList(packDsc), null);