public void CanAllocateEnoughToFillEntireSection() { long pageNumber; using (var tx = Env.WriteTransaction()) { var section = ActiveRawDataSmallSection.Create(tx.LowLevelTransaction, "test", (byte)TableType.None); pageNumber = section.PageNumber; tx.Commit(); } using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); section.DataMoved += (previousId, newId, data, size) => { }; int allocationSize = 1020; long id; var list = new List <long>(); while (section.TryAllocate(allocationSize, out id)) { list.Add(id); } Assert.False(section.TryAllocate(allocationSize, out id)); var idToFree = list[list.Count / 2]; section.Free(idToFree); Assert.True(section.TryAllocate(allocationSize, out id)); } }
public void ShouldReturnValidIdsOfEntriesInSectionThatAreReadable() { long pageNumber; using (var tx = Env.WriteTransaction()) { var section = ActiveRawDataSmallSection.Create(tx.LowLevelTransaction, "test", (byte)TableType.None); pageNumber = section.PageNumber; tx.Commit(); } long idWhichIsGoingToBeDeleted1; long idWhichIsGoingToBeDeleted2; long existingId; using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); Assert.True(section.TryAllocate(2000, out idWhichIsGoingToBeDeleted1)); WriteValue(section, idWhichIsGoingToBeDeleted1, 1.ToString("0000000000000")); Assert.True(section.TryAllocate(2000, out idWhichIsGoingToBeDeleted2)); WriteValue(section, idWhichIsGoingToBeDeleted2, 2.ToString("0000000000000")); Assert.True(section.TryAllocate(2000, out existingId)); WriteValue(section, existingId, 3.ToString("0000000000000")); tx.Commit(); } using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); section.Free(idWhichIsGoingToBeDeleted1); section.Free(idWhichIsGoingToBeDeleted2); tx.Commit(); } using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); var ids = section.GetAllIdsInSectionContaining(existingId); Assert.Equal(1, ids.Count); Assert.Equal(existingId, ids[0]); AssertValueMatches(section, existingId, 3.ToString("0000000000000")); } }
public void ShouldNotReturnMoreIdsThanTotalNumberOfEntriesInSection() { long pageNumber; using (var tx = Env.WriteTransaction()) { var section = ActiveRawDataSmallSection.Create(tx.LowLevelTransaction, "test", (byte)TableType.None); pageNumber = section.PageNumber; tx.Commit(); } long newId; using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); Assert.True(section.TryAllocate(16, out newId)); WriteValue(section, newId, 1.ToString("0000000000000")); tx.Commit(); } using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); var ids = section.GetAllIdsInSectionContaining(newId); Assert.Equal(section.NumberOfEntries, ids.Count); Assert.Equal(1, ids.Count); Assert.Equal(newId, ids[0]); AssertValueMatches(section, newId, 1.ToString("0000000000000")); } }
public void CanReadAndWriteFromSection() { long pageNumber; using (var tx = Env.WriteTransaction()) { var section = ActiveRawDataSmallSection.Create(tx.LowLevelTransaction, "test", (byte)TableType.None); pageNumber = section.PageNumber; tx.Commit(); } long id; using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); Assert.True(section.TryAllocate(15, out id)); WriteValue(section, id, "Hello There"); tx.Commit(); } using (var tx = Env.ReadTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); AssertValueMatches(section, id, "Hello There"); } }
private long AllocateFromSmallActiveSection(int size) { long id; if (ActiveDataSmallSection.TryAllocate(size, out id) == false) { InactiveSections.Add(_activeDataSmallSection.PageNumber); using (var it = ActiveCandidateSection.Iterate()) { if (it.Seek(long.MinValue)) { do { var sectionPageNumber = it.CurrentKey; _activeDataSmallSection = new ActiveRawDataSmallSection(_tx.LowLevelTransaction, sectionPageNumber); _activeDataSmallSection.DataMoved += OnDataMoved; if (_activeDataSmallSection.TryAllocate(size, out id)) { ActiveCandidateSection.Delete(sectionPageNumber); return(id); } } while (it.MoveNext()); } } var newNumberOfPages = Math.Max((ushort)(ActiveDataSmallSection.NumberOfPages * 2), ushort.MaxValue); _activeDataSmallSection = ActiveRawDataSmallSection.Create(_tx.LowLevelTransaction, Name, newNumberOfPages); _activeDataSmallSection.DataMoved += OnDataMoved; Slice pageNumber; var val = _activeDataSmallSection.PageNumber; using (Slice.External(_tx.Allocator, (byte *)&val, sizeof(long), out pageNumber)) { _tableTree.Add(TableSchema.ActiveSectionSlice, pageNumber); } var allocationResult = _activeDataSmallSection.TryAllocate(size, out id); Debug.Assert(allocationResult); } return(id); }
public void CanAllocateMultipleValues(int seed) { var random = new Random(seed); long pageNumber; using (var tx = Env.WriteTransaction()) { var section = ActiveRawDataSmallSection.Create(tx.LowLevelTransaction, "test", (byte)TableType.None); pageNumber = section.PageNumber; tx.Commit(); } var dic = new Dictionary <long, int>(); for (int i = 0; i < 100; i++) { long id; using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); Assert.True(section.TryAllocate(random.Next(16, 256), out id)); WriteValue(section, id, i.ToString("0000000000000")); dic[id] = i; tx.Commit(); } using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); AssertValueMatches(section, id, i.ToString("0000000000000")); } } foreach (var kvp in dic) { using (var tx = Env.WriteTransaction()) { var section = new ActiveRawDataSmallSection(tx.LowLevelTransaction, pageNumber); AssertValueMatches(section, kvp.Key, kvp.Value.ToString("0000000000000")); } } }
private long AllocateFromSmallActiveSection(int size) { long id; if (ActiveDataSmallSection.TryAllocate(size, out id) == false) { InactiveSections.Add(_activeDataSmallSection.PageNumber); using (var it = ActiveCandidateSection.Iterate()) { if (it.Seek(long.MinValue)) { do { var sectionPageNumber = it.CurrentKey; _activeDataSmallSection = new ActiveRawDataSmallSection(_tx.LowLevelTransaction, sectionPageNumber); if (_activeDataSmallSection.TryAllocate(size, out id)) { ActiveCandidateSection.Delete(sectionPageNumber); return(id); } } while (it.MoveNext()); } } _activeDataSmallSection = ActiveRawDataSmallSection.Create(_tx.LowLevelTransaction, Name); var pageNumber = Slice.From(_tx.Allocator, EndianBitConverter.Little.GetBytes(_activeDataSmallSection.PageNumber), ByteStringType.Immutable); _tableTree.Add(TableSchema.ActiveSection, pageNumber); var allocationResult = _activeDataSmallSection.TryAllocate(size, out id); Debug.Assert(allocationResult); } return(id); }