/// <exception cref="System.IO.IOException"></exception> private void OnOpenPack() { PackIndex idx = Idx(); byte[] buf = new byte[20]; fd.Seek(0); fd.ReadFully(buf, 0, 12); if (RawParseUtils.Match(buf, 0, Constants.PACK_SIGNATURE) != 4) { throw new IOException(JGitText.Get().notAPACKFile); } long vers = NB.DecodeUInt32(buf, 4); long packCnt = NB.DecodeUInt32(buf, 8); if (vers != 2 && vers != 3) { throw new IOException(MessageFormat.Format(JGitText.Get().unsupportedPackVersion, vers)); } if (packCnt != idx.GetObjectCount()) { throw new PackMismatchException(MessageFormat.Format(JGitText.Get().packObjectCountMismatch , packCnt, idx.GetObjectCount(), GetPackFile())); } fd.Seek(length - 20); fd.ReadFully(buf, 0, 20); if (!Arrays.Equals(buf, packChecksum)) { throw new PackMismatchException(MessageFormat.Format(JGitText.Get().packObjectCountMismatch , ObjectId.FromRaw(buf).Name, ObjectId.FromRaw(idx.packChecksum).Name, GetPackFile ())); } }
private void AssertContent(PackIndex pi, IList <ObjectId> expected) { NUnit.Framework.Assert.AreEqual(expected.Count, pi.GetObjectCount(), "Pack index has wrong size." ); for (int i = 0; i < pi.GetObjectCount(); i++) { NUnit.Framework.Assert.IsTrue(expected.Contains(pi.GetObjectId(i)), "Pack index didn't contain the expected id " + pi.GetObjectId(i)); } }
public virtual void TestFindNextOffset() { long offset = FindFirstOffset(); NUnit.Framework.Assert.IsTrue(offset > 0); for (int i = 0; i < idx.GetObjectCount(); i++) { long newOffset = reverseIdx.FindNextOffset(offset, long.MaxValue); NUnit.Framework.Assert.IsTrue(newOffset > offset); if (i == idx.GetObjectCount() - 1) { NUnit.Framework.Assert.AreEqual(newOffset, long.MaxValue); } else { NUnit.Framework.Assert.AreEqual(newOffset, idx.FindOffset(reverseIdx.FindObject(newOffset ))); } offset = newOffset; } }
public virtual void TestWriteIndex() { config.SetIndexVersion(2); WriteVerifyPack4(false); FilePath packFile = pack.GetPackFile(); string name = packFile.GetName(); string @base = Sharpen.Runtime.Substring(name, 0, name.LastIndexOf('.')); FilePath indexFile = new FilePath(packFile.GetParentFile(), @base + ".idx"); // Validate that IndexPack came up with the right CRC32 value. PackIndex idx1 = PackIndex.Open(indexFile); NUnit.Framework.Assert.IsTrue(idx1 is PackIndexV2); NUnit.Framework.Assert.AreEqual(unchecked ((long)(0x4743F1E4L)), idx1.FindCRC32(ObjectId .FromString("82c6b885ff600be425b4ea96dee75dca255b69e7"))); // Validate that an index written by PackWriter is the same. FilePath idx2File = new FilePath(indexFile.GetAbsolutePath() + ".2"); FileOutputStream @is = new FileOutputStream(idx2File); try { writer.WriteIndex(@is); } finally { @is.Close(); } PackIndex idx2 = PackIndex.Open(idx2File); NUnit.Framework.Assert.IsTrue(idx2 is PackIndexV2); NUnit.Framework.Assert.AreEqual(idx1.GetObjectCount(), idx2.GetObjectCount()); NUnit.Framework.Assert.AreEqual(idx1.GetOffset64Count(), idx2.GetOffset64Count()); for (int i = 0; i < idx1.GetObjectCount(); i++) { ObjectId id = idx1.GetObjectId(i); NUnit.Framework.Assert.AreEqual(id, idx2.GetObjectId(i)); NUnit.Framework.Assert.AreEqual(idx1.FindOffset(id), idx2.FindOffset(id)); NUnit.Framework.Assert.AreEqual(idx1.FindCRC32(id), idx2.FindCRC32(id)); } }
public virtual void TestKeepFiles() { BranchBuilder bb = tr.Branch("refs/heads/master"); bb.Commit().Add("A", "A").Add("B", "B").Create(); stats = gc.GetStatistics(); NUnit.Framework.Assert.AreEqual(4, stats.numberOfLooseObjects); NUnit.Framework.Assert.AreEqual(0, stats.numberOfPackedObjects); NUnit.Framework.Assert.AreEqual(0, stats.numberOfPackFiles); gc.Gc(); stats = gc.GetStatistics(); NUnit.Framework.Assert.AreEqual(0, stats.numberOfLooseObjects); NUnit.Framework.Assert.AreEqual(4, stats.numberOfPackedObjects); NUnit.Framework.Assert.AreEqual(1, stats.numberOfPackFiles); Iterator <PackFile> packIt = ((ObjectDirectory)repo.ObjectDatabase).GetPacks().Iterator (); PackFile singlePack = packIt.Next(); NUnit.Framework.Assert.IsFalse(packIt.HasNext()); FilePath keepFile = new FilePath(singlePack.GetPackFile().GetPath() + ".keep"); NUnit.Framework.Assert.IsFalse(keepFile.Exists()); NUnit.Framework.Assert.IsTrue(keepFile.CreateNewFile()); bb.Commit().Add("A", "A2").Add("B", "B2").Create(); stats = gc.GetStatistics(); NUnit.Framework.Assert.AreEqual(4, stats.numberOfLooseObjects); NUnit.Framework.Assert.AreEqual(4, stats.numberOfPackedObjects); NUnit.Framework.Assert.AreEqual(1, stats.numberOfPackFiles); gc.Gc(); stats = gc.GetStatistics(); NUnit.Framework.Assert.AreEqual(0, stats.numberOfLooseObjects); NUnit.Framework.Assert.AreEqual(8, stats.numberOfPackedObjects); NUnit.Framework.Assert.AreEqual(2, stats.numberOfPackFiles); // check that no object is packed twice Iterator <PackFile> packs = ((ObjectDirectory)repo.ObjectDatabase).GetPacks().Iterator (); PackIndex ind1 = packs.Next().GetIndex(); NUnit.Framework.Assert.AreEqual(4, ind1.GetObjectCount()); PackIndex ind2 = packs.Next().GetIndex(); NUnit.Framework.Assert.AreEqual(4, ind2.GetObjectCount()); foreach (PackIndex.MutableEntry e in ind1) { if (ind2.HasObject(e.ToObjectId())) { NUnit.Framework.Assert.IsFalse(ind2.HasObject(e.ToObjectId()), "the following object is in both packfiles: " + e.ToObjectId()); } } }
/// <summary> /// Create reverse index from straight/forward pack index, by indexing all /// its entries. /// </summary> /// <remarks> /// Create reverse index from straight/forward pack index, by indexing all /// its entries. /// </remarks> /// <param name="packIndex">forward index - entries to (reverse) index.</param> public PackReverseIndex(PackIndex packIndex) { index = packIndex; long cnt = index.GetObjectCount(); long n64 = index.GetOffset64Count(); long n32 = cnt - n64; if (n32 > int.MaxValue || n64 > int.MaxValue || cnt > unchecked ((long)(0xffffffffL ))) { throw new ArgumentException(JGitText.Get().hugeIndexesAreNotSupportedByJgitYet); } offsets32 = new int[(int)n32]; offsets64 = new long[(int)n64]; nth32 = new int[offsets32.Length]; nth64 = new int[offsets64.Length]; int i32 = 0; int i64 = 0; foreach (PackIndex.MutableEntry me in index) { long o = me.GetOffset(); if (o < int.MaxValue) { offsets32[i32++] = (int)o; } else { offsets64[i64++] = o; } } Arrays.Sort(offsets32); Arrays.Sort(offsets64); int nth = 0; foreach (PackIndex.MutableEntry me_1 in index) { long o = me_1.GetOffset(); if (o < int.MaxValue) { nth32[System.Array.BinarySearch(offsets32, (int)o)] = nth++; } else { nth64[System.Array.BinarySearch(offsets64, o)] = nth++; } } }