public async Task Update(DocumentModel model)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(nameof(model));
            }

            var entry = await _index.Get(model.Id);

            var oldModel = _documentSerializer.Deserialize(_records.Find(entry.Item2));

            _records.Update(entry.Item2, _documentSerializer.Serialize(model));

            var oldDict = _indexingOperations.CreateIndexes(oldModel.Value, oldModel).Result;

            foreach (var(k, v) in oldDict)
            {
                await _secondaryIndex.Delete(k, v);
            }

            var dict = await _indexingOperations.CreateIndexes(model.Value, model);

            foreach (var(k, v) in dict)
            {
                await _secondaryIndex.Insert(k, v);
            }
        }
示例#2
0
        public void TestDeletion()
        {
            var d1 = GenerateRandomData(1029);
            var d2 = GenerateRandomData(14 * 1024 * 4);
            var d3 = GenerateRandomData(3591);

            var d4 = GenerateRandomData(4444);
            var d5 = GenerateRandomData(5555);
            var d6 = GenerateRandomData(6666);

            using (var ms = new MemoryStream())
            {
                var recordStorage = new RecordStorage(new BlockStorage(ms));
                var r1            = recordStorage.Create(d1);
                var r2            = recordStorage.Create(d2);
                var r3            = recordStorage.Create(d3);

                Assert.AreEqual(1, r1);
                Assert.AreEqual(2, r2);
                Assert.AreEqual(4, r3);

                Assert.True(recordStorage.Find(r1).SequenceEqual(d1));
                Assert.True(recordStorage.Find(r2).SequenceEqual(d2));
                Assert.True(recordStorage.Find(r3).SequenceEqual(d3));

                // Delete off 2, free 2 records
                recordStorage.Delete(r2);

                Assert.True(recordStorage.Find(r2) == null);

                // INsert 2 new records, should take id of 2,3,4
                var r4 = recordStorage.Create(d4);
                var r5 = recordStorage.Create(d5);
                var r6 = recordStorage.Create(d6);
                Assert.AreEqual(3, r4);
                Assert.AreEqual(2, r5);
                Assert.AreEqual(5, r6);

                // Check that data is not being corrupted if we use the reusable block
                Assert.True(recordStorage.Find(r4).SequenceEqual(d4));
                Assert.True(recordStorage.Find(r5).SequenceEqual(d5));
                Assert.True(recordStorage.Find(r6).SequenceEqual(d6));

                // Test persistance
                var recordStorage2 = new RecordStorage(new BlockStorage(ms));
                Assert.True(recordStorage2.Find(r1).SequenceEqual(d1));
                Assert.True(recordStorage2.Find(r3).SequenceEqual(d3));
                Assert.True(recordStorage2.Find(r4).SequenceEqual(d4));
                Assert.True(recordStorage2.Find(r5).SequenceEqual(d5));
                Assert.True(recordStorage2.Find(r6).SequenceEqual(d6));
            }
        }
示例#3
0
        public void TestCreateNewPersist()
        {
            var customData = new byte[4096 * 16 + 27];
            var rnd        = new Random();

            for (var i = 0; i < customData.Length; i++)
            {
                customData[i] = (byte)rnd.Next(0, 256);
            }

            using (var ms = new MemoryStream())
            {
                var recordStorage = new RecordStorage(new BlockStorage(ms));
                var recordId      = recordStorage.Create(customData);

                // First record, shoud has id of 1..
                Assert.AreEqual(1, recordId);

                // Test read back the data
                Assert.True(customData.SequenceEqual(recordStorage.Find(1)));

                // Now test persistant
                var recordStorage2 = new RecordStorage(new BlockStorage(ms));
                Assert.True(customData.SequenceEqual(recordStorage2.Find(1)));
            }
        }
示例#4
0
        public void TestUpdateBlockToMuchBiggerSize()
        {
            var recordStorage = new RecordStorage(new BlockStorage(new MemoryStream(), 8192, 48));
            var x1            = UnitTestHelper.RandomData(2491);
            var x2            = UnitTestHelper.RandomData(9182);
            var x3            = UnitTestHelper.RandomData(5182);

            recordStorage.Create(x1);                            // Use 1 block
            recordStorage.Create(x2);                            // Use 2 blocks
            recordStorage.Create(x3);                            // Use 1 block

            var x2u = UnitTestHelper.RandomData(8192 * 11 + 19); // Use 12 blocks

            recordStorage.Update(2, x2u);

            Assert.IsTrue(recordStorage.Find(1).SequenceEqual(x1));
            Assert.IsTrue(recordStorage.Find(2).SequenceEqual(x2u));
            Assert.IsTrue(recordStorage.Find(4).SequenceEqual(x3));
        }
示例#5
0
        public void TestUpdateBlockToSmallerSize()
        {
            var recordStorage = new RecordStorage(new BlockStorage(new MemoryStream(), 8192, 48));
            var x1            = UnitTestHelper.RandomData(2491);
            var x2            = UnitTestHelper.RandomData(9182);
            var x3            = UnitTestHelper.RandomData(5182);

            recordStorage.Create(x1);                  // Use 1 block
            recordStorage.Create(x2);                  // Use 2 blocks
            recordStorage.Create(x3);                  // Use 1 block

            var x2u = UnitTestHelper.RandomData(1177); // Use 1 block, so this record should be truncated

            recordStorage.Update(2, x2u);

            Assert.IsTrue(recordStorage.Find(1).SequenceEqual(x1));
            Assert.IsTrue(recordStorage.Find(2).SequenceEqual(x2u));
            Assert.IsTrue(recordStorage.Find(4).SequenceEqual(x3));

            Assert.IsTrue(recordStorage.Create(UnitTestHelper.RandomData(10)) == 3); // Check if block #3 being reused
        }