]> source.dussan.org Git - jgit.git/commitdiff
PackIndex: Add protected setters to populate the MutableEntry 30/1201530/6
authorjackdt@google.com <jackdt@google.com>
Thu, 19 Sep 2024 20:28:27 +0000 (13:28 -0700)
committerIvan Frade <ifrade@google.com>
Thu, 19 Sep 2024 22:32:39 +0000 (22:32 +0000)
Implementations of the iterator out of package receive a MutableEntry but they don't have a way to set data into it.

Add setters to the MutableEntry via protected methods in the iterator. This way, only implementors of the Iterator can modify the entry (cannot be modified e.g. by callers).

Change-Id: Id50c69d8be230ebdb8108acc47df13abcad0af0a

org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndex.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndexV1.java
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackIndexV2.java

index 95050d472eda6fd36d5ae9c7e2908469f6ed390d..dfea5c1c80f0a0b04f8bb5c6f89c2beef5b3785b 100644 (file)
@@ -359,10 +359,11 @@ public interface PackIndex
                private long returnedNumber = 0;
 
                /**
-               * Default constructor.
-               *
-               * @param objectCount the number of objects in the PackFile.
-               */
+                * Construct an iterator that can move objectCount times forward.
+                *
+                * @param objectCount
+                *            the number of objects in the PackFile.
+                */
                protected EntriesIterator(long objectCount) {
                        this.objectCount = objectCount;
                }
@@ -379,17 +380,54 @@ public interface PackIndex
                 */
                @Override
                public MutableEntry next() {
-                       readNext(entry);
+                       readNext();
                        returnedNumber++;
                        return entry;
                }
 
                /**
                 * Used by subclasses to load the next entry into the MutableEntry.
+                * <p>
+                * Subclasses are expected to populate the entry with
+                * {@link #setIdBuffer} and {@link #setOffset}.
+                */
+               protected abstract void readNext();
+
+
+               /**
+                * Copies to the entry an {@link ObjectId} from the int buffer and
+                * position idx
+                *
+                * @param raw
+                *            the raw data
+                * @param idx
+                *            the index into {@code raw}
+                */
+               protected void setIdBuffer(int[] raw, int idx) {
+                       entry.idBuffer.fromRaw(raw, idx);
+               }
+
+               /**
+                * Copies to the entry an {@link ObjectId} from the byte array at
+                * position idx.
                 *
-                * @param entry the container of the next Iterator entry.
-                */
-               protected abstract void readNext(MutableEntry entry);
+                * @param raw
+                *            the raw data
+                * @param idx
+                *            the index into {@code raw}
+                */
+               protected void setIdBuffer(byte[] raw, int idx) {
+                       entry.idBuffer.fromRaw(raw, idx);
+               }
+
+               /**
+                * Sets the {@code offset} to the entry
+                *
+                * @param offset the offset in the pack file
+                */
+               protected void setOffset(long offset) {
+                       entry.offset = offset;
+               }
 
                @Override
                public void remove() {
index 99f331581118a89284d0852b0033b7aeb6015c70..be48358a0da9c78258499c7505be330e358c1ff1 100644 (file)
@@ -259,15 +259,15 @@ class PackIndexV1 implements PackIndex {
                }
 
                @Override
-               protected void readNext(MutableEntry entry) {
+               protected void readNext() {
                        for (; levelOne < packIndex.idxdata.length; levelOne++) {
                                if (packIndex.idxdata[levelOne] == null)
                                        continue;
                                if (levelTwo < packIndex.idxdata[levelOne].length) {
-                                       entry.offset = NB.decodeUInt32(packIndex.idxdata[levelOne],
-                                                       levelTwo);
+                                       super.setOffset(NB.decodeUInt32(packIndex.idxdata[levelOne],
+                                                       levelTwo));
                                        this.levelTwo += Constants.OBJECT_ID_LENGTH + 4;
-                                       entry.idBuffer.fromRaw(packIndex.idxdata[levelOne],
+                                       super.setIdBuffer(packIndex.idxdata[levelOne],
                                                        levelTwo - Constants.OBJECT_ID_LENGTH);
                                        return;
                                }
index f23380dd6bf3607b15c429bbb3c50a1446898284..36e54fcd62175ca7af3268c7bd7e58d8131b81e8 100644 (file)
@@ -302,7 +302,7 @@ class PackIndexV2 implements PackIndex {
                }
 
                @Override
-               protected void readNext(MutableEntry entry) {
+               protected void readNext() {
                        for (; levelOne < packIndex.names.length; levelOne++) {
                                if (levelTwo < packIndex.names[levelOne].length) {
                                        int idx = levelTwo / (Constants.OBJECT_ID_LENGTH / 4) * 4;
@@ -312,9 +312,9 @@ class PackIndexV2 implements PackIndex {
                                                idx = (8 * (int) (offset & ~IS_O64));
                                                offset = NB.decodeUInt64(packIndex.offset64, idx);
                                        }
-                                       entry.offset = offset;
+                                       super.setOffset(offset);
                                        this.levelTwo += Constants.OBJECT_ID_LENGTH / 4;
-                                       entry.idBuffer.fromRaw(packIndex.names[levelOne],
+                                       super.setIdBuffer(packIndex.names[levelOne],
                                                        levelTwo - Constants.OBJECT_ID_LENGTH / 4);
                                        return;
                                }