]> source.dussan.org Git - jgit.git/commitdiff
Add numberOfObjectsSinceBitmap to RepoStatistics 98/1203398/8
authorJacek Centkowski <geminica.programs@gmail.com>
Thu, 31 Oct 2024 17:30:02 +0000 (18:30 +0100)
committerMatthias Sohn <matthias.sohn@sap.com>
Wed, 20 Nov 2024 10:31:31 +0000 (10:31 +0000)
Introduce a numberOfObjectsSinceBitmap that contains the number of
objects stored in pack files and as loose objects created since the
latest bitmap generation.

Note that the existing
GcNumberOfPackFilesAfterBitmapStatisticsTest.java was renamed to
GcSinceBitmapStatisticsTest.java and extended to cover also this
statistic.

Change-Id: I8ae1db142ddfcd42a5a1d6da01bc67f695562e0e

org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcNumberOfPackFilesSinceBitmapStatisticsTest.java [deleted file]
org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcSinceBitmapStatisticsTest.java [new file with mode: 0644]
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/GC.java

diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcNumberOfPackFilesSinceBitmapStatisticsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcNumberOfPackFilesSinceBitmapStatisticsTest.java
deleted file mode 100644 (file)
index fa13bb5..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*
- * Copyright (c) 2024 Jacek Centkowski <geminica.programs@gmail.com> and others.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Distribution License v. 1.0 which is available at
- * https://www.eclipse.org/org/documents/edl-v10.php.
- *
- * SPDX-License-Identifier: BSD-3-Clause
- */
-
-package org.eclipse.jgit.internal.storage.file;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.IOException;
-import java.nio.file.Files;
-import java.util.stream.StreamSupport;
-
-import org.eclipse.jgit.lib.PersonIdent;
-import org.eclipse.jgit.revwalk.RevCommit;
-import org.junit.Test;
-
-public class GcNumberOfPackFilesSinceBitmapStatisticsTest extends GcTestCase {
-       @Test
-       public void testShouldReportZeroObjectsForInitializedRepo()
-                       throws IOException {
-               assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
-       }
-
-       @Test
-       public void testShouldReportAllPackFilesWhenNoGcWasPerformed()
-                       throws Exception {
-               tr.packAndPrune();
-               long result = gc.getStatistics().numberOfPackFilesSinceBitmap;
-
-               assertEquals(repo.getObjectDatabase().getPacks().size(), result);
-       }
-
-       @Test
-       public void testShouldReportNoObjectsDirectlyAfterGc() throws Exception {
-               // given
-               addCommit(null);
-               gc.gc().get();
-               assertEquals(1L, repositoryBitmapFiles());
-               assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
-       }
-
-       @Test
-       public void testShouldReportNewObjectsSinceGcWhenRepositoryProgresses()
-                       throws Exception {
-               // commit & gc
-               RevCommit parent = addCommit(null);
-               gc.gc().get();
-               assertEquals(1L, repositoryBitmapFiles());
-
-               // progress & pack
-               addCommit(parent);
-               tr.packAndPrune();
-
-               assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
-       }
-
-       @Test
-       public void testShouldReportNewObjectsFromTheLatestBitmapWhenRepositoryProgresses()
-                       throws Exception {
-               // commit & gc
-               RevCommit parent = addCommit(null);
-               gc.gc().get();
-               assertEquals(1L, repositoryBitmapFiles());
-
-               // progress & gc
-               parent = addCommit(parent);
-               gc.gc().get();
-               assertEquals(2L, repositoryBitmapFiles());
-
-               // progress & pack
-               addCommit(parent);
-               tr.packAndPrune();
-
-               assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
-       }
-
-       private RevCommit addCommit(RevCommit parent) throws Exception {
-               return tr.branch("master").commit()
-                               .author(new PersonIdent("repo-metrics", "repo@metrics.com"))
-                               .parent(parent).create();
-       }
-
-       private long repositoryBitmapFiles() throws IOException {
-               return StreamSupport
-                               .stream(Files
-                                               .newDirectoryStream(repo.getObjectDatabase()
-                                                               .getPackDirectory().toPath(), "pack-*.bitmap")
-                                               .spliterator(), false)
-                               .count();
-       }
-}
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcSinceBitmapStatisticsTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/internal/storage/file/GcSinceBitmapStatisticsTest.java
new file mode 100644 (file)
index 0000000..3cd766c
--- /dev/null
@@ -0,0 +1,167 @@
+/*
+ * Copyright (c) 2024 Jacek Centkowski <geminica.programs@gmail.com> and others.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Distribution License v. 1.0 which is available at
+ * https://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+package org.eclipse.jgit.internal.storage.file;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Collection;
+import java.util.stream.StreamSupport;
+
+import org.eclipse.jgit.internal.storage.file.GC.RepoStatistics;
+import org.eclipse.jgit.lib.PersonIdent;
+import org.eclipse.jgit.revwalk.RevCommit;
+import org.junit.Test;
+
+public class GcSinceBitmapStatisticsTest extends GcTestCase {
+       @Test
+       public void testShouldReportZeroPacksAndObjectsForInitializedRepo()
+                       throws IOException {
+               RepoStatistics s = gc.getStatistics();
+               assertEquals(0L, s.numberOfPackFilesSinceBitmap);
+               assertEquals(0L, s.numberOfObjectsSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportAllPackFilesWhenNoGcWasPerformed()
+                       throws Exception {
+               tr.packAndPrune();
+               long result = gc.getStatistics().numberOfPackFilesSinceBitmap;
+
+               assertEquals(repo.getObjectDatabase().getPacks().size(), result);
+       }
+
+       @Test
+       public void testShouldReportAllObjectsWhenNoGcWasPerformed()
+                       throws Exception {
+               tr.packAndPrune();
+
+               assertEquals(
+                               getNumberOfObjectsInPacks(repo.getObjectDatabase().getPacks()),
+                               gc.getStatistics().numberOfObjectsSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportNoPacksDirectlyAfterGc() throws Exception {
+               // given
+               addCommit(null);
+               gc.gc().get();
+               assertEquals(1L, repositoryBitmapFiles());
+               assertEquals(0L, gc.getStatistics().numberOfPackFilesSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportNoObjectsDirectlyAfterGc() throws Exception {
+               // given
+               addCommit(null);
+               assertEquals(2L, gc.getStatistics().numberOfObjectsSinceBitmap);
+
+               gc.gc().get();
+               assertEquals(0L, gc.getStatistics().numberOfObjectsSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportNewPacksSinceGcWhenRepositoryProgresses()
+                       throws Exception {
+               // commit & gc
+               RevCommit parent = addCommit(null);
+               gc.gc().get();
+               assertEquals(1L, repositoryBitmapFiles());
+
+               // progress & pack
+               addCommit(parent);
+               tr.packAndPrune();
+
+               assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportNewObjectsSinceGcWhenRepositoryProgresses()
+                       throws Exception {
+               // commit & gc
+               RevCommit parent = addCommit(null);
+               gc.gc().get();
+               assertEquals(0L, gc.getStatistics().numberOfObjectsSinceBitmap);
+
+               // progress & pack
+               addCommit(parent);
+               assertEquals(1L, gc.getStatistics().numberOfObjectsSinceBitmap);
+
+               tr.packAndPrune();
+               assertEquals(3L, gc.getStatistics().numberOfObjectsSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportNewPacksFromTheLatestBitmapWhenRepositoryProgresses()
+                       throws Exception {
+               // commit & gc
+               RevCommit parent = addCommit(null);
+               gc.gc().get();
+               assertEquals(1L, repositoryBitmapFiles());
+
+               // progress & gc
+               parent = addCommit(parent);
+               gc.gc().get();
+               assertEquals(2L, repositoryBitmapFiles());
+
+               // progress & pack
+               addCommit(parent);
+               tr.packAndPrune();
+
+               assertEquals(1L, gc.getStatistics().numberOfPackFilesSinceBitmap);
+       }
+
+       @Test
+       public void testShouldReportNewObjectsFromTheLatestBitmapWhenRepositoryProgresses()
+                       throws Exception {
+               // commit & gc
+               RevCommit parent = addCommit(null);
+               gc.gc().get();
+
+               // progress & gc
+               parent = addCommit(parent);
+               gc.gc().get();
+               assertEquals(0L, gc.getStatistics().numberOfObjectsSinceBitmap);
+
+               // progress & pack
+               addCommit(parent);
+               assertEquals(1L, gc.getStatistics().numberOfObjectsSinceBitmap);
+
+               tr.packAndPrune();
+               assertEquals(4L, gc.getStatistics().numberOfObjectsSinceBitmap);
+       }
+
+       private RevCommit addCommit(RevCommit parent) throws Exception {
+               return tr.branch("master").commit()
+                               .author(new PersonIdent("repo-metrics", "repo@metrics.com"))
+                               .parent(parent).create();
+       }
+
+       private long repositoryBitmapFiles() throws IOException {
+               return StreamSupport
+                               .stream(Files
+                                               .newDirectoryStream(repo.getObjectDatabase()
+                                                               .getPackDirectory().toPath(), "pack-*.bitmap")
+                                               .spliterator(), false)
+                               .count();
+       }
+
+       private long getNumberOfObjectsInPacks(Collection<Pack> packs) {
+               return packs.stream().mapToLong(pack -> {
+                       try {
+                               return pack.getObjectCount();
+                       } catch (IOException e) {
+                               throw new RuntimeException(e);
+                       }
+               }).sum();
+       }
+}
index 9494057a608090eece56ff8b3969c4be0eafdfe8..b33afed1312fe892a90b168d91f9786fdd0fdda0 100644 (file)
@@ -1514,6 +1514,12 @@ public class GC {
                 */
                public long numberOfPackFilesSinceBitmap;
 
+               /**
+                * The number of objects stored in pack files and as loose object
+                * created after the last bitmap generation.
+                */
+               public long numberOfObjectsSinceBitmap;
+
                /**
                 * The number of objects stored as loose objects.
                 */
@@ -1551,6 +1557,8 @@ public class GC {
                        b.append(", numberOfPackFiles=").append(numberOfPackFiles); //$NON-NLS-1$
                        b.append(", numberOfPackFilesSinceBitmap=") //$NON-NLS-1$
                                        .append(numberOfPackFilesSinceBitmap);
+                       b.append(", numberOfObjectsSinceBitmap=") //$NON-NLS-1$
+                                       .append(numberOfObjectsSinceBitmap);
                        b.append(", numberOfLooseObjects=").append(numberOfLooseObjects); //$NON-NLS-1$
                        b.append(", numberOfLooseRefs=").append(numberOfLooseRefs); //$NON-NLS-1$
                        b.append(", numberOfPackedRefs=").append(numberOfPackedRefs); //$NON-NLS-1$
@@ -1571,14 +1579,19 @@ public class GC {
        public RepoStatistics getStatistics() throws IOException {
                RepoStatistics ret = new RepoStatistics();
                Collection<Pack> packs = repo.getObjectDatabase().getPacks();
+               long latestBitmapTime = Long.MIN_VALUE;
                for (Pack p : packs) {
-                       ret.numberOfPackedObjects += p.getIndex().getObjectCount();
+                       long packedObjects = p.getIndex().getObjectCount();
+                       ret.numberOfPackedObjects += packedObjects;
                        ret.numberOfPackFiles++;
                        ret.sizeOfPackedObjects += p.getPackFile().length();
                        if (p.getBitmapIndex() != null) {
                                ret.numberOfBitmaps += p.getBitmapIndex().getBitmapCount();
+                               latestBitmapTime = p.getFileSnapshot().lastModifiedInstant()
+                                               .toEpochMilli();
                        } else {
                                ret.numberOfPackFilesSinceBitmap++;
+                               ret.numberOfObjectsSinceBitmap += packedObjects;
                        }
                }
                File objDir = repo.getObjectsDirectory();
@@ -1595,6 +1608,9 @@ public class GC {
                                                continue;
                                        ret.numberOfLooseObjects++;
                                        ret.sizeOfLooseObjects += f.length();
+                                       if (f.lastModified() > latestBitmapTime) {
+                                               ret.numberOfObjectsSinceBitmap ++;
+                                       }
                                }
                        }
                }