示例#1
0
        /// <summary>
        /// Allocates a new value and produces reference to it.
        /// </summary>
        /// <param name="value">Value to allocate</param>
        /// <returns>Reference to allocated value</returns>
        public DbItemReference AllocateNew(TValue value)
        {
            var valueBytes = _valueSerializer.Serialize(value);

            return(IsVersioningEnabled
                ? VersionedRecord.CreateNew(valueBytes, DataTankerTransaction.Current.Id, _memoryManager)
                : _memoryManager.Allocate(valueBytes));
        }
        public void RecordIsVisibleToItsCreator()
        {
            var memoryManager = new MemoryManager();

            var content = new byte[] { 0, 1 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new int[] { });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            Assert.IsTrue(record.HasVisibleVersionTo(0));
        }
示例#3
0
        private DbItemReference GetVersionReferenceByRecordReference(DbItemReference reference)
        {
            if (IsVersioningEnabled)
            {
                var record = new VersionedRecord(_memoryManager.Get(reference).RawData, _memoryManager, SnapshotData);

                if (record.HasVisibleVersionTo(DataTankerTransaction.Current.Id))
                {
                    reference = record.GetMatchingVersionReference(DataTankerTransaction.Current.Id);
                }
            }

            return(reference);
        }
        public void VersionCreatedByCommitedTransactionIsVisible()
        {
            var memoryManager = new MemoryManager();

            var content = new byte[] { 0, 1 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new[] { 0 });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            Assert.IsTrue(record.HasVisibleVersionTo(1));
        }
        public void ExpireOfUpdatedRecordProduceWriteConflict()
        {
            var memoryManager = new MemoryManager();

            var content = new byte[] { 0, 1 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new int[] { });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            record.Update(new byte[] { 2, 3 }, 0);                                                   // zero transaction updates the record
            Assert.That(() => record.Expire(1), Throws.Exception.TypeOf <WriteConflictException>()); // first transaction trying to expire it
        }
示例#6
0
        /// <summary>
        /// Fetches value by its reference.
        /// </summary>
        /// <param name="reference">Reference to the value</param>
        /// <returns>The instance of value</returns>
        public TValue Fetch(DbItemReference reference)
        {
            var item = _memoryManager.Get(reference);

            if (IsVersioningEnabled)
            {
                var record = new VersionedRecord(item.RawData, _memoryManager, SnapshotData);

                if (record.HasVisibleVersionTo(DataTankerTransaction.Current.Id))
                {
                    item = record.GetMatchingVersion(DataTankerTransaction.Current.Id);
                }
            }

            return(_valueSerializer.Deserialize(item.RawData));
        }
        public void ExpiredVersionIsInvisible()
        {
            var memoryManager = new MemoryManager();

            var content = new byte[] { 0, 1 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new int[] { });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            record.Expire(0);

            Assert.IsFalse(record.HasVisibleVersionTo(0));
        }
        public void UpdateRecordCreatedByCommitedTransaction()
        {
            var memoryManager = new MemoryManager();

            var content    = new byte[] { 0, 1 };
            var newContent = new byte[] { 2, 3 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new[] { 0 }); // zero transaction is commited

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            record.Update(newContent, 1);
            Assert.IsTrue(newContent.SequenceEqual(record.GetMatchingVersion(1).RawData));
        }
        public void UpdateProduceNewVersion()
        {
            var memoryManager = new MemoryManager();

            var oldContent = new byte[] { 0, 1 };
            var newContent = new byte[] { 2, 3 };

            var recordReference = VersionedRecord.CreateNew(oldContent, 0, memoryManager);

            var snapshotData = new SnapshotData(new int[] { });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            record.Update(newContent, 0);

            Assert.IsTrue(record.HasVisibleVersionTo(0));
            Assert.IsTrue(newContent.SequenceEqual(record.GetMatchingVersion(0).RawData));
        }
示例#10
0
        public void ConcurrentlyRunningTransactionsViewTheirOwnVersions()
        {
            var memoryManager = new MemoryManager();

            var content    = new byte[] { 0, 1 };
            var newContent = new byte[] { 2, 3 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new[] { 0 });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            var newReference = record.InsertOrUpdate(newContent, 1);

            record = new VersionedRecord(memoryManager.Get(newReference).RawData, memoryManager, snapshotData);

            Assert.IsTrue(newContent.SequenceEqual(record.GetMatchingVersion(1).RawData));
            Assert.IsTrue(content.SequenceEqual(record.GetMatchingVersion(2).RawData));
        }
示例#11
0
        public void UpdateRecordExpiredByRolledBackTransaction()
        {
            var memoryManager = new MemoryManager();

            var content    = new byte[] { 0, 1 };
            var newContent = new byte[] { 2, 3 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new[] { 0 });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            var newReference = record.Expire(1);

            snapshotData = new SnapshotData(new[] { 0 }, new[] { 1 });

            record = new VersionedRecord(memoryManager.Get(newReference).RawData, memoryManager, snapshotData);

            record.Update(newContent, 2);
            Assert.IsTrue(newContent.SequenceEqual(record.GetMatchingVersion(2).RawData));
        }
示例#12
0
        public void ResurrectRecordExpiredByCommitedTransaction()
        {
            var memoryManager = new MemoryManager();

            var content    = new byte[] { 0, 1 };
            var newContent = new byte[] { 2, 3 };

            var recordReference = VersionedRecord.CreateNew(content, 0, memoryManager);

            var snapshotData = new SnapshotData(new int[] { });

            var record = new VersionedRecord(memoryManager.Get(recordReference).RawData, memoryManager, snapshotData);

            var newReference = record.Expire(0);

            snapshotData = new SnapshotData(new[] { 0 });

            record = new VersionedRecord(memoryManager.Get(newReference).RawData, memoryManager, snapshotData);

            record.Insert(newContent, 1);
            Assert.IsTrue(newContent.SequenceEqual(record.GetMatchingVersion(1).RawData));
        }