Ejemplo n.º 1
0
        public void StorageRecoveryShouldWorkWhenThereAreCommitedAndUncommitedTransactions2()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.ForPath(DataDir)))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("atree");
                    tx.CreateTree("btree");

                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    for (var i = 0; i < 10000; i++)
                    {
                        tx.CreateTree("atree").Add("a" + i, new MemoryStream());
                        tx.CreateTree("btree").MultiAdd("a" + i, "a" + i);
                    }
                }
            }

            using (var env = new StorageEnvironment(StorageEnvironmentOptions.ForPath(DataDir)))
            {
            }
        }
Ejemplo n.º 2
0
        public void DataIsKeptAfterRestart_OnDisk()
        {
            using (var pager = StorageEnvironmentOptions.ForPath(DataDir))
            {
                pager.OwnsPagers = false;
                using (var env = new StorageEnvironment(pager))
                {
                    using (var tx = env.WriteTransaction())
                    {
                        var tree = tx.CreateTree("foo");
                        tree.Add("test/1", new MemoryStream());
                        tx.Commit();
                    }
                    using (var tx = env.WriteTransaction())
                    {
                        var tree = tx.CreateTree("foo");
                        tree.Add("test/2", new MemoryStream());
                        tx.Commit();
                    }
                }

                using (var env = new StorageEnvironment(pager))
                {
                    using (var tx = env.ReadTransaction())
                    {
                        var tree = tx.CreateTree("foo");
                        Assert.NotNull(tree.Read("test/1"));
                        Assert.NotNull(tree.Read("test/2"));
                        tx.Commit();
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public void IterationShouldNotFindAnyRecordsAndShouldNotThrowWhenNumberOfEntriesOnPageIs1AndKeyDoesNotMatch()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.CreateMemoryOnly()))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("tree");

                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.ReadTree("tree");
                    tree.Add(@"Raven\Database\1", StreamFor("123"));

                    tx.Commit();
                }

                using (var snapshot = env.ReadTransaction())
                    using (var iterator = snapshot.ReadTree("tree").Iterate(false))
                    {
                        Slice v;
                        Slice.From(snapshot.Allocator, @"Raven\Filesystem\", out v);
                        Assert.False(iterator.Seek(v));
                    }
            }
        }
        public void AllScratchPagesShouldBeReleased()
        {
            var options = StorageEnvironmentOptions.CreateMemoryOnly();

            options.ManualFlushing = true;
            using (var env = new StorageEnvironment(options))
            {
                using (var txw = env.WriteTransaction())
                {
                    txw.CreateTree("test");

                    txw.Commit();
                }

                using (var txw = env.WriteTransaction())
                {
                    var tree = txw.CreateTree("test");

                    tree.Add("key/1", new MemoryStream(new byte[100]));
                    tree.Add("key/1", new MemoryStream(new byte[200]));
                    txw.Commit();
                }

                env.FlushLogToDataFile(); // non read nor write transactions, so it should flush and release everything from scratch

                // we keep track of the pages in scratch for one additional transaction, to avoid race
                // condition with FlushLogToDataFile concurrently with new read transactions
                Assert.Equal(2, env.ScratchBufferPool.GetNumberOfAllocations(0));
            }
        }
Ejemplo n.º 5
0
        public void ShouldNotThrowChecksumMismatch()
        {
            var random = new Random(1);
            var buffer = new byte[100];

            random.NextBytes(buffer);

            for (int i = 0; i < 100; i++)
            {
                buffer[i] = 13;
            }

            var options = StorageEnvironmentOptions.ForPath(DataDir);

            using (var env = new StorageEnvironment(options))
            {
                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("foo");
                    for (int i = 0; i < 50; i++)
                    {
                        tree.Add("items/" + i, new MemoryStream(buffer));
                    }

                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("foo");
                    for (int i = 50; i < 100; i++)
                    {
                        tree.Add("items/" + i, new MemoryStream(buffer));
                    }

                    tx.Commit();
                }
            }

            options = StorageEnvironmentOptions.ForPath(DataDir);

            using (var env = new StorageEnvironment(options))
            {
                using (var tx = env.ReadTransaction())
                {
                    var tree = tx.CreateTree("foo");
                    for (int i = 0; i < 100; i++)
                    {
                        var readResult = tree.Read("items/" + i);
                        Assert.NotNull(readResult);
                        var memoryStream = new MemoryStream();
                        readResult.Reader.CopyTo(memoryStream);
                        Assert.Equal(memoryStream.ToArray(), buffer);
                    }
                }
            }
        }
Ejemplo n.º 6
0
        public void StorageRecoveryShouldWorkWhenThereAreMultipleCommitedTransactions2()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.ForPath(DataDir)))
            {
                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("atree");

                    for (var i = 0; i < 1000; i++)
                    {
                        tree.Add("key" + i, new MemoryStream());
                    }

                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("btree");

                    for (var i = 0; i < 5; i++)
                    {
                        tree.Add("key" + i, new MemoryStream());
                    }

                    tx.Commit();
                }
            }

            using (var env = new StorageEnvironment(StorageEnvironmentOptions.ForPath(DataDir)))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("atree");
                    tx.CreateTree("btree");

                    tx.Commit();
                }

                using (var tx = env.ReadTransaction())
                {
                    var aTree = tx.CreateTree("atree");
                    var bTree = tx.CreateTree("btree");

                    for (var i = 0; i < 1000; i++)
                    {
                        Assert.NotNull(aTree.Read("key" + i));
                    }

                    for (var i = 0; i < 5; i++)
                    {
                        Assert.NotNull(bTree.Read("key" + i));
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public void Initialize()
        {
            var transactionPersistentContext = new TransactionPersistentContext();

            using (var tx = _environment.WriteTransaction(transactionPersistentContext))
            {
                tx.CreateTree(SubscriptionSchema.IdsTree);
                _subscriptionsSchema.Create(tx, SubscriptionSchema.SubsTree, 16);

                tx.Commit();
            }
        }
Ejemplo n.º 8
0
        public void ShouldBeAbleToWriteValuesGreaterThanLogAndRecoverThem()
        {
            var random = new Random(1234);
            var buffer = new byte[1024 * 512];

            random.NextBytes(buffer);

            var options = StorageEnvironmentOptions.ForPath(DataDir);

            options.MaxLogFileSize = 10 * Constants.Storage.PageSize;

            using (var env = new StorageEnvironment(options))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("tree");
                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("tree").Add("key1", new MemoryStream(buffer));
                    tx.Commit();
                }
            }

            options = StorageEnvironmentOptions.ForPath(DataDir);
            options.MaxLogFileSize = 10 * Constants.Storage.PageSize;

            using (var env = new StorageEnvironment(options))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("tree");
                    tx.Commit();
                }

                using (var tx = env.ReadTransaction())
                {
                    var read = tx.CreateTree("tree").Read("key1");
                    Assert.NotNull(read);

                    {
                        Assert.Equal(buffer.Length, read.Reader.Length);
                        var bytes = read.Reader.ReadBytes(read.Reader.Length);
                        Assert.Equal(buffer, bytes.Array.Skip(bytes.Offset).Take(bytes.Count).ToArray());
                    }
                }
            }
        }
Ejemplo n.º 9
0
        public void ShouldOccupyLessSpace(int seed)
        {
            var r = new Random(seed);
            var storageEnvironmentOptions = StorageEnvironmentOptions.ForPath(DataDir);

            storageEnvironmentOptions.ManualFlushing = true;
            using (var env = new StorageEnvironment(storageEnvironmentOptions))
            {
                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("records");

                    for (int i = 0; i < 100; i++)
                    {
                        var bytes = new byte[r.Next(10, 2 * 1024 * 1024)];
                        r.NextBytes(bytes);

                        tree.Add("record/" + i, bytes);
                    }

                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("records");

                    for (int i = 0; i < 50; i++)
                    {
                        tree.Delete("record/" + r.Next(0, 100));
                    }

                    tx.Commit();
                }
                env.FlushLogToDataFile();
            }

            var oldSize = GetDirSize(new DirectoryInfo(DataDir));

            storageEnvironmentOptions = StorageEnvironmentOptions.ForPath(DataDir);
            storageEnvironmentOptions.ManualFlushing = true;
            var compactedData = Path.Combine(DataDir, "Compacted");

            StorageCompaction.Execute(storageEnvironmentOptions,
                                      (StorageEnvironmentOptions.DirectoryStorageEnvironmentOptions)StorageEnvironmentOptions.ForPath(compactedData));

            var newSize = GetDirSize(new DirectoryInfo(compactedData));

            Assert.True(newSize < oldSize, string.Format("Old size: {0:#,#;;0} MB, new size {1:#,#;;0} MB", oldSize / 1024 / 1024, newSize / 1024 / 1024));
        }
Ejemplo n.º 10
0
        private void CreateAndPopulateTree(bool startWithBigTx)
        {
            using (var env = new StorageEnvironment(ModifyOptions(StorageEnvironmentOptions.ForPath(DataDir), manualFlushing: true)))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("items");
                    tx.Commit();
                }

                if (startWithBigTx)
                {
                    // tx with size bigger than MaxLogFileSize
                    using (var tx = env.WriteTransaction())
                    {
                        var bytes = new byte[9 * 1024 * 1024];
                        new Random().NextBytes(bytes);

                        var tree = tx.ReadTree("items");
                        tree.Add("items/" + 0, bytes);

                        tx.Commit();
                    }
                }

                using (var tx = env.WriteTransaction())
                {
                    var bytes = new byte[1024];
                    new Random().NextBytes(bytes);

                    var tree = tx.ReadTree("items");
                    tree.Add("items/" + 1, bytes);

                    tx.Commit();
                }

                // tx with size bigger than MaxLogFileSize
                using (var tx = env.WriteTransaction())
                {
                    var bytes = new byte[9 * 1024 * 1024];
                    new Random().NextBytes(bytes);

                    var tree = tx.ReadTree("items");
                    tree.Add("items/" + 322, bytes);

                    tx.Commit();
                }
            }
        }
Ejemplo n.º 11
0
        public unsafe void ShouldNotHappen()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.CreateMemoryOnly()))
            {
                using (var tx = env.WriteTransaction())
                {
                    tx.CreateTree("Fields");
                    tx.CreateTree("Options");
                    _entriesSchema.Create(tx, "IndexEntries", 16);
                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    var options = tx.CreateTree("Options");
                    var entries = tx.OpenTable(_entriesSchema, "IndexEntries");
                    for (int i = 0; i < 10; i++)
                    {
                        var assembly = typeof(DuplicatePageUsage).GetTypeInfo().Assembly;

                        fixed(byte *buffer = new byte[1024])
                        using (var fs = assembly.GetManifestResourceStream("SlowTests.Data.places.txt"))
                            using (var reader = new StreamReader(fs))
                            {
                                string readLine;
                                while ((readLine = reader.ReadLine()) != null)
                                {
                                    var strings = readLine.Split(',');
                                    var id      = long.Parse(strings[0]);
                                    var size    = int.Parse(strings[1]);
                                    entries.Set(new TableValueBuilder
                                    {
                                        { (byte *)&id, sizeof(int) },
                                        { buffer, size }
                                    });
                                    //Console.WriteLine($"{id} {readLine.Length + 55}");
                                }
                            }
                    }

                    var   val = long.MaxValue / 2;
                    Slice key, v;
                    Slice.From(tx.Allocator, "LastEntryId", out key);
                    Slice.From(tx.Allocator, (byte *)&val, sizeof(long), out v);
                    options.Add(key, v);
                    tx.Commit();
                }
            }
        }
Ejemplo n.º 12
0
        public void PageSplitterShouldCalculateSeparatorKeyCorrectly2()
        {
            var ids = ReadIds("data2.txt");

            StorageEnvironmentOptions storageEnvironmentOptions = StorageEnvironmentOptions.CreateMemoryOnly();

            storageEnvironmentOptions.MaxScratchBufferSize *= 2;
            using (var env = new StorageEnvironment(storageEnvironmentOptions))
            {
                var rand       = new Random();
                var testBuffer = new byte[69];
                rand.NextBytes(testBuffer);

                var trees = CreateTrees(env, 1, "tree");

                foreach (var id in ids)
                {
                    using (var tx = env.WriteTransaction())
                    {
                        foreach (var treeName in trees)
                        {
                            var tree = tx.CreateTree(treeName);
                            tree.Add(id, new MemoryStream(testBuffer));
                        }

                        tx.Commit();
                    }
                }

                ValidateRecords(env, trees, ids);
            }
        }
Ejemplo n.º 13
0
        public void Cleanup()
        {
            if (_recycleArea.Count == 0 && _scratchBuffers.Count == 1)
            {
                return;
            }

            long txIdAllowingToReleaseOldScratches = -1;

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var scratchBufferItem in _scratchBuffers)
            {
                if (scratchBufferItem.Value == _current)
                {
                    continue;
                }

                txIdAllowingToReleaseOldScratches = Math.Max(txIdAllowingToReleaseOldScratches,
                                                             scratchBufferItem.Value.File.TxIdAfterWhichLatestFreePagesBecomeAvailable);
            }

            while (_env.CurrentReadTransactionId <= txIdAllowingToReleaseOldScratches)
            {
                // we've just flushed and had no more writes after that, let us bump id of next read transactions to ensure
                // that nobody will attempt to read old scratches so we will be able to release more files

                try
                {
                    using (var tx = _env.NewLowLevelTransaction(new TransactionPersistentContext(),
                                                                TransactionFlags.ReadWrite, timeout: TimeSpan.FromMilliseconds(500)))
                    {
                        tx.ModifyPage(0);
                        tx.Commit();
                    }
                }
                catch (TimeoutException)
                {
                    break;
                }
                catch (DiskFullException)
                {
                    break;
                }
            }

            // we need to ensure that no access to _recycleArea and _scratchBuffers will take place in the same time
            // and only methods that access this are used within write transaction
            try
            {
                using (_env.WriteTransaction())
                {
                    RemoveInactiveScratches(_current);

                    RemoveInactiveRecycledScratches();
                }
            }
            catch (TimeoutException)
            {
            }
        }
Ejemplo n.º 14
0
        public void Initialize(StorageEnvironment environment)
        {
            if (_initialized)
            {
                throw new InvalidOperationException();
            }

            TempFileCache = new TempFileCache(environment.Options);

            environment.NewTransactionCreated += SetStreamCacheInTx;

            using (var tx = environment.WriteTransaction())
            {
                InitializeMainIndexStorage(tx, environment);
                InitializeSuggestionsIndexStorage(tx, environment);
                BuildStreamCacheAfterTx(tx);

                // force tx commit so it will bump tx counter and just created searcher holder will have valid tx id
                tx.LowLevelTransaction.ModifyPage(0);

                tx.Commit();
            }

            _initialized = true;
        }
Ejemplo n.º 15
0
        public void Initialize(StorageEnvironment environment)
        {
            if (_initialized)
            {
                throw new InvalidOperationException();
            }

            _directory = new LuceneVoronDirectory(environment);

            using (var tx = environment.WriteTransaction())
            {
                using (_directory.SetTransaction(tx))
                {
                    CreateIndexStructure();
                    RecreateSearcher(tx);

                    // force tx commit so it will bump tx counter and just created searcher holder will have valid tx id
                    tx.LowLevelTransaction.ModifyPage(0);
                }

                tx.Commit();
            }

            _initialized = true;
        }
Ejemplo n.º 16
0
        public void CannotCompactStorageIfIncrementalBackupEnabled()
        {
            var envOptions = StorageEnvironmentOptions.ForPath(DataDir);

            envOptions.IncrementalBackupEnabled = true;
            using (var env = new StorageEnvironment(envOptions))
            {
                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("records");

                    tree.Add("record/1", new byte[9]);
                    tree.Add("record/2", new byte[9]);

                    tx.Commit();
                }
            }

            var srcOptions = StorageEnvironmentOptions.ForPath(DataDir);

            srcOptions.IncrementalBackupEnabled = true;

            var invalidOperationException = Assert.Throws <InvalidOperationException>(() => StorageCompaction.Execute(srcOptions,
                                                                                                                      (StorageEnvironmentOptions.DirectoryStorageEnvironmentOptions)StorageEnvironmentOptions.ForPath(Path.Combine(DataDir, "Compacted"))));

            Assert.Equal(StorageCompaction.CannotCompactBecauseOfIncrementalBackup, invalidOperationException.Message);
        }
Ejemplo n.º 17
0
        public void ShouldNotReuseRecycledJournalIfItExceedMaxLogFileSizeOnBigTxSize()
        {
            CreateAndPopulateTree(startWithBigTx: true);

            // restart
            using (var env = new StorageEnvironment(ModifyOptions(StorageEnvironmentOptions.ForPath(DataDir), manualFlushing: false)))
            {
                var journalPath   = env.Options.JournalPath.FullPath;
                var journalsCount = new DirectoryInfo(journalPath).GetFiles().Length;
                Assert.True(SpinWait.SpinUntil(() => new DirectoryInfo(journalPath).GetFiles($"{StorageEnvironmentOptions.RecyclableJournalFileNamePrefix}*").Length == journalsCount,
                                               TimeSpan.FromSeconds(30)));

                using (var tx = env.WriteTransaction())
                {
                    var bytes = new byte[8 * 1024 * 1024];
                    new Random().NextBytes(bytes);

                    var tree = tx.ReadTree("items");
                    tree.Add("items/" + 2, bytes);

                    tx.Commit();
                }

                Assert.True(SpinWait.SpinUntil(() => new DirectoryInfo(journalPath).GetFiles($"{StorageEnvironmentOptions.RecyclableJournalFileNamePrefix}*").Length == 2,
                                               TimeSpan.FromSeconds(30)));

                Assert.Equal(1, new DirectoryInfo(journalPath).GetFiles("000000000000000000*.journal").Length);
                Assert.Equal(3, new DirectoryInfo(journalPath).GetFiles().Length);
            }
        }
Ejemplo n.º 18
0
        public void ShouldWork()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.CreateMemoryOnly()))
            {
                using (var tx = env.WriteTransaction())
                {
                    var s    = new string('0', 500);
                    var tree = tx.CreateTree("data");
                    for (int i = 0; i < 10; i++)
                    {
                        tree.Add("users-" + i + "-" + s, new byte[0]);
                    }
                    tx.Commit();
                }

                using (var tx = env.ReadTransaction())
                {
                    var tree = tx.ReadTree("data");
                    using (var it = tree.Iterate(false))
                    {
                        Slice key;
                        Slice.From(tx.Allocator, "users-7", out key);
                        Assert.True(it.Seek(key));

                        for (int i = 0; i < 10; i++)
                        {
                            Slice.From(tx.Allocator, "users-" + i, out key);
                            Assert.True(it.Seek(key), i.ToString());
                        }
                    }
                }
            }
        }
Ejemplo n.º 19
0
        private static long CopyFixedSizeTrees(StorageEnvironment compactedEnv, Action <StorageCompactionProgress> progressReport, Transaction txr,
                                               TreeIterator rootIterator, string treeName, long copiedTrees, long totalTreesCount, TransactionPersistentContext context, CancellationToken token)
        {
            var treeNameSlice = rootIterator.CurrentKey.Clone(txr.Allocator);

            var header = (FixedSizeTreeHeader.Embedded *)txr.LowLevelTransaction.RootObjects.DirectRead(treeNameSlice);

            var fst = txr.FixedTreeFor(treeNameSlice, header->ValueSize);

            Report(copiedTrees, totalTreesCount, 0, fst.NumberOfEntries, progressReport, $"Copying fixed size tree '{treeName}'. Progress: 0/{fst.NumberOfEntries} entries.", treeName);

            using (var it = fst.Iterate())
            {
                var copiedEntries = 0L;
                if (it.Seek(Int64.MinValue) == false)
                {
                    return(copiedTrees);
                }

                do
                {
                    token.ThrowIfCancellationRequested();
                    using (var txw = compactedEnv.WriteTransaction(context))
                    {
                        var snd             = txw.FixedTreeFor(treeNameSlice, header->ValueSize);
                        var transactionSize = 0L;

                        do
                        {
                            token.ThrowIfCancellationRequested();

                            Slice val;
                            using (it.Value(out val))
                                snd.Add(it.CurrentKey, val);
                            transactionSize += fst.ValueSize + sizeof(long);
                            copiedEntries++;

                            var reportRate = fst.NumberOfEntries / 33 + 1;
                            if (copiedEntries % reportRate == 0)
                            {
                                Report(copiedTrees, totalTreesCount, copiedEntries, fst.NumberOfEntries, progressReport, $"Copying fixed size tree '{treeName}'. Progress: {copiedEntries}/{fst.NumberOfEntries} entries.", treeName);
                            }
                        } while (transactionSize < compactedEnv.Options.MaxScratchBufferSize / 2 && it.MoveNext());

                        txw.Commit();
                    }

                    if (fst.NumberOfEntries == copiedEntries)
                    {
                        copiedTrees++;
                        Report(copiedTrees, totalTreesCount, copiedEntries, fst.NumberOfEntries, progressReport, $"Finished copying fixed size tree '{treeName}'. Progress: {copiedEntries}/{fst.NumberOfEntries} entries.", treeName);
                    }

                    compactedEnv.FlushLogToDataFile();
                } while (it.MoveNext());
            }
            return(copiedTrees);
        }
Ejemplo n.º 20
0
        public void OldestActiveTransactionShouldBeCalculatedProperly()
        {
            using (var options = StorageEnvironmentOptions.CreateMemoryOnly())
            {
                options.ManualFlushing = true;
                using (var env = new StorageEnvironment(options))
                {
                    var trees        = CreateTrees(env, 1, "tree");
                    var transactions = new List <Transaction>();

                    for (int a = 0; a < 100; a++)
                    {
                        var random = new Random(1337);
                        var buffer = new byte[random.Next(100, 1000)];
                        random.NextBytes(buffer);

                        using (var tx = env.WriteTransaction())
                        {
                            for (int i = 0; i < 100; i++)
                            {
                                foreach (var tree in trees)
                                {
                                    tx.CreateTree(tree).Add(string.Format("key/{0}/{1}", a, i), new MemoryStream(buffer));
                                }
                            }

                            var txr = env.ReadTransaction();
                            transactions.Add(txr);
                            tx.Commit();
                        }
                        env.FlushLogToDataFile();
                    }

                    Assert.Equal(transactions.OrderBy(x => x.LowLevelTransaction.Id).First().LowLevelTransaction.Id, env.ActiveTransactions.OldestTransaction);

                    foreach (var tx in transactions)
                    {
                        foreach (var tree in trees)
                        {
                            using (var iterator = tx.CreateTree(tree).Iterate(false))
                            {
                                if (!iterator.Seek(Slices.BeforeAllKeys))
                                {
                                    continue;
                                }

                                do
                                {
                                    Assert.Contains("key/", iterator.CurrentKey.ToString());
                                } while (iterator.MoveNext());
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 21
0
        public void MultiAdds_And_MultiDeletes_After_Causing_PageSplit_DoNot_Fail(int size)
        {
            using (var Env = new StorageEnvironment(StorageEnvironmentOptions.CreateMemoryOnly()))
            {
                var inputData = new List <byte[]>();
                for (int i = 0; i < size; i++)
                {
                    inputData.Add(Encoding.UTF8.GetBytes(RandomString(1024)));
                }

                using (var tx = Env.WriteTransaction())
                {
                    tx.CreateTree("foo");
                    tx.Commit();
                }

                using (var tx = Env.WriteTransaction())
                {
                    var tree = tx.CreateTree("foo");
                    foreach (var buffer in inputData)
                    {
                        Slice key;
                        Slice.From(tx.Allocator, buffer, out key);
                        tree.MultiAdd("ChildTreeKey", key);
                    }
                    tx.Commit();
                }

                using (var tx = Env.WriteTransaction())
                {
                    var tree = tx.CreateTree("foo");
                    for (int i = 0; i < inputData.Count; i++)
                    {
                        var   buffer = inputData[i];
                        Slice key;
                        Slice.From(tx.Allocator, buffer, out key);
                        tree.MultiDelete("ChildTreeKey", key);
                    }

                    tx.Commit();
                }
            }
        }
Ejemplo n.º 22
0
        public void MultipleTxPagesCanPointToOnePageNumberWhichShouldNotBeCausingIssuesDuringFlushing()
        {
            var options = StorageEnvironmentOptions.CreateMemoryOnly();

            options.ManualFlushing = true;
            using (var env = new StorageEnvironment(options))
            {
                var trees = CreateTrees(env, 2, "tree");
                var tree1 = trees[0];
                var tree2 = trees[1];

                using (var tx = env.WriteTransaction())
                {
                    var t1 = tx.ReadTree(tree1);

                    t1.MultiAdd("key", "value/1");
                    t1.MultiAdd("key", "value/2");

                    tx.Commit();
                }

                using (var tx = env.WriteTransaction())
                {
                    var t1 = tx.ReadTree(tree1);
                    var t2 = tx.ReadTree(tree2);

                    var buffer = new byte[1000];

                    t1.MultiDelete("key", "value/1");
                    t1.MultiDelete("key", "value/2");

                    t2.Add("key/1", new MemoryStream(buffer));
                    t2.Add("key/2", new MemoryStream(buffer));
                    t2.Add("key/3", new MemoryStream(buffer));
                    t2.Add("key/4", new MemoryStream(buffer));
                    t2.Add("key/5", new MemoryStream(buffer));

                    tx.Commit();
                }

                env.FlushLogToDataFile();
            }
        }
Ejemplo n.º 23
0
 public LuceneVoronDirectory(StorageEnvironment environment)
 {
     _environment = environment;
     base.SetLockFactory(NoLockFactory.Instance);
     using (var tx = _environment.WriteTransaction())
     {
         tx.CreateTree("Files");
         tx.Commit();
     }
 }
Ejemplo n.º 24
0
        public void SurviveRestart()
        {
            using (var options = StorageEnvironmentOptions.CreateMemoryOnly())
            {
                options.OwnsPagers = false;
                using (var env = new StorageEnvironment(options))
                {
                    using (var tx = env.WriteTransaction())
                    {
                        tx.CreateTree("events");

                        tx.Commit();
                    }

                    using (var tx = env.WriteTransaction())
                    {
                        tx.CreateTree("events").Add("test", new MemoryStream(0));

                        tx.Commit();
                    }
                }

                using (var env = new StorageEnvironment(options))
                {
                    using (var tx = env.WriteTransaction())
                    {
                        tx.CreateTree("events");

                        tx.Commit();
                    }

                    using (var tx = env.WriteTransaction())
                    {
                        var tree       = tx.CreateTree("events");
                        var readResult = tree.Read("test");
                        Assert.NotNull(readResult);

                        tx.Commit();
                    }
                }
            }
        }
Ejemplo n.º 25
0
        private static string Write(StorageEnvironment env, int i)
        {
            using (var txw = env.WriteTransaction())
            {
                var key = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + i.ToString("D2");

                txw.ReadTree("tree0").MultiAdd("key/1", key);
                txw.Commit();

                return(key);
            }
        }
Ejemplo n.º 26
0
        public void ShouldReportProgress()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.ForPath(DataDir)))
            {
                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("fruits");

                    tree.Add("apple", new byte[123]);
                    tree.Add("orange", new byte[99]);

                    var tree2 = tx.CreateTree("vegetables");

                    tree2.Add("carrot", new byte[123]);
                    tree2.Add("potato", new byte[99]);

                    var tree3 = tx.CreateTree("multi");

                    tree3.MultiAdd("fruits", "apple");
                    tree3.MultiAdd("fruits", "orange");


                    tree3.MultiAdd("vegetables", "carrot");
                    tree3.MultiAdd("vegetables", "carrot");

                    tx.Commit();
                }
            }

            var progressReport = new List <string>();

            StorageCompaction.Execute(StorageEnvironmentOptions.ForPath(DataDir),
                                      (StorageEnvironmentOptions.DirectoryStorageEnvironmentOptions)StorageEnvironmentOptions.ForPath(Path.Combine(DataDir, "Compacted")),
                                      x => progressReport.Add($"Copied {x.ObjectProgress} of {x.ObjectTotal} records in '{x.ObjectName}' tree. Copied {x.GlobalProgress} of {x.GlobalTotal} trees."));

            Assert.NotEmpty(progressReport);
            var lines = new[]
            {
                "Copied 0 of 2 records in '$Database-Metadata' tree. Copied 0 of 4 trees.",
                "Copied 2 of 2 records in '$Database-Metadata' tree. Copied 1 of 4 trees.",
                "Copied 0 of 2 records in 'fruits' tree. Copied 1 of 4 trees.",
                "Copied 2 of 2 records in 'fruits' tree. Copied 2 of 4 trees.",
                "Copied 0 of 2 records in 'multi' tree. Copied 2 of 4 trees.",
                "Copied 2 of 2 records in 'multi' tree. Copied 3 of 4 trees.",
                "Copied 0 of 2 records in 'vegetables' tree. Copied 3 of 4 trees.",
                "Copied 2 of 2 records in 'vegetables' tree. Copied 4 of 4 trees."
            };

            foreach (var line in lines)
            {
                Assert.Contains(line, lines);
            }
        }
Ejemplo n.º 27
0
        public void ShouldReportProgress()
        {
            using (var env = new StorageEnvironment(StorageEnvironmentOptions.ForPath(DataDir)))
            {
                using (var tx = env.WriteTransaction())
                {
                    var tree = tx.CreateTree("fruits");

                    tree.Add("apple", new byte[123]);
                    tree.Add("orange", new byte[99]);

                    var tree2 = tx.CreateTree("vegetables");

                    tree2.Add("carrot", new byte[123]);
                    tree2.Add("potato", new byte[99]);

                    var tree3 = tx.CreateTree("multi");

                    tree3.MultiAdd("fruits", "apple");
                    tree3.MultiAdd("fruits", "orange");


                    tree3.MultiAdd("vegetables", "carrot");
                    tree3.MultiAdd("vegetables", "carrot");

                    tx.Commit();
                }
            }

            var progressReport = new List <string>();

            StorageCompaction.Execute(StorageEnvironmentOptions.ForPath(DataDir),
                                      (StorageEnvironmentOptions.DirectoryStorageEnvironmentOptions)StorageEnvironmentOptions.ForPath(Path.Combine(DataDir, "Compacted")),
                                      x => progressReport.Add($"{x.Message} ({x.TreeName} - {x.TreeProgress}/{x.TreeTotal}). Copied {x.GlobalProgress} of {x.GlobalTotal} trees."));

            Assert.NotEmpty(progressReport);
            var lines = new[]
            {
                "Copying variable size tree ($Database-Metadata - 0/2). Copied 0 of 4 trees.",
                "Copied variable size tree ($Database-Metadata - 2/2). Copied 1 of 4 trees.",
                "Copying variable size tree (fruits - 0/2). Copied 1 of 4 trees.",
                "Copied variable size tree (fruits - 2/2). Copied 2 of 4 trees.",
                "Copying variable size tree (multi - 0/2). Copied 2 of 4 trees.",
                "Copied variable size tree (multi - 2/2). Copied 3 of 4 trees.",
                "Copying variable size tree (vegetables - 0/2). Copied 3 of 4 trees.",
                "Copied variable size tree (vegetables - 2/2). Copied 4 of 4 trees."
            };

            foreach (var line in lines)
            {
                Assert.Contains(line, progressReport);
            }
        }
Ejemplo n.º 28
0
        public void Initialize(StorageEnvironment environment, TransactionContextPool contextPool)
        {
            _environment = environment;
            _contextPool = contextPool;

            using (_contextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var tx = _environment.WriteTransaction(context.PersistentContext))
                {
                    tx.CreateTree(OperationsTree);
                    tx.Commit();
                }
        }
Ejemplo n.º 29
0
        public void DataIsKeptAfterRestartForSubTrees()
        {
            using (var pureMemoryPager = StorageEnvironmentOptions.CreateMemoryOnly())
            {
                pureMemoryPager.OwnsPagers = false;
                using (var env = new StorageEnvironment(pureMemoryPager))
                {
                    using (var tx = env.WriteTransaction())
                    {
                        tx.CreateTree("test");
                        tx.Commit();
                    }
                    using (var tx = env.WriteTransaction())
                    {
                        var tree = tx.CreateTree("test");
                        tree.Add("test", Stream.Null);
                        tx.Commit();

                        Assert.NotNull(tree.Read("test"));
                    }
                }

                using (var env = new StorageEnvironment(pureMemoryPager))
                {
                    using (var tx = env.WriteTransaction())
                    {
                        var tree = tx.CreateTree("test");
                        tx.Commit();
                    }

                    using (var tx = env.ReadTransaction())
                    {
                        var tree = tx.CreateTree("test");
                        Assert.NotNull(tree.Read("test"));
                        tx.Commit();
                    }
                }
            }
        }
Ejemplo n.º 30
0
        public void Initialize(StorageEnvironment environment, TransactionContextPool contextPool)
        {
            _environment = environment;
            _contextPool = contextPool;

            using (contextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (var tx = _environment.WriteTransaction(context.PersistentContext))
                {
                    _databaseInfoSchema.Create(tx, DatabaseInfoSchema.DatabaseInfoTree, 16);

                    tx.Commit();
                }
        }