Ejemplo n.º 1
0
 private void ReinitEngine()
 {
     if (StaticKeyValueDatabase.IsDisposed || _engine == null)
     {
         _engine = StaticKeyValueDatabase.GetDatabaseEngine(_dbPath);
     }
 }
Ejemplo n.º 2
0
        public void GetTrxAsyncWithTransactionReturnsExistingTransaction()
        {
            string dir   = CreateTestDir(this);
            var    trans = Network.Main.Consensus.ConsensusFactory.CreateTransaction();

            trans.Version = 125;

            using (var engine = new DBreezeEngine(dir))
            {
                var block = Network.Main.Consensus.ConsensusFactory.CreateBlock();
                block.Header.GetHash();
                block.Transactions.Add(trans);

                var transaction = engine.GetTransaction();
                transaction.Insert <byte[], byte[]>("Block", block.Header.GetHash().ToBytes(), block.ToBytes());
                transaction.Insert <byte[], byte[]>("Transaction", trans.GetHash().ToBytes(), block.Header.GetHash().ToBytes());
                transaction.Insert <byte[], byte[]>("Common", new byte[0], uint256.Zero.ToBytes());
                transaction.Insert <byte[], bool>("Common", new byte[1], true);
                transaction.Commit();
            }

            using (var repository = this.SetupRepository(Network.Main, dir))
            {
                var task = repository.GetTrxAsync(trans.GetHash());
                task.Wait();

                Assert.Equal((uint)125, task.Result.Version);
            }
        }
Ejemplo n.º 3
0
        public void SetTxIndexUpdatesTxIndex()
        {
            string dir = CreateTestDir(this);

            using (var engine = new DBreezeEngine(dir))
            {
                var trans = engine.GetTransaction();
                trans.Insert <byte[], bool>("Common", new byte[1], true);
                trans.Commit();
            }

            using (var repository = this.SetupRepository(Network.Main, dir))
            {
                var task = repository.SetTxIndexAsync(false);
                task.Wait();
            }

            using (var engine = new DBreezeEngine(dir))
            {
                var trans = engine.GetTransaction();

                var txIndexRow = trans.Select <byte[], bool>("Common", new byte[1]);
                Assert.False(txIndexRow.Value);
            }
        }
Ejemplo n.º 4
0
        private static uint256[] SetupTransactionData(string folder)
        {
            using (var engine = new DBreezeEngine(folder))
            {
                var data = new[]
                {
                    new uint256(3),
                    new uint256(2),
                    new uint256(5),
                    new uint256(10),
                };

                int i = 0;
                using (DBreeze.Transactions.Transaction tx = engine.GetTransaction())
                {
                    foreach (uint256 d in data)
                    {
                        tx.Insert <int, byte[]>("Table", i++, d.ToBytes(false));
                    }

                    tx.Commit();
                }

                return(data);
            }
        }
Ejemplo n.º 5
0
        public TextDeferredIndexer(DBreezeEngine engine)
        {
            this.DBreezeEngine = engine;
            LTrieSettings      = new TrieSettings()
            {
                InternalTable = true
            };
            Storage         = new StorageLayer(Path.Combine(engine.MainFolder, TableFileName), LTrieSettings, engine.Configuration);
            LTrie           = new LTrie(Storage);
            LTrie.TableName = "DBreeze.TextIndexer";

            if (LTrie.Storage.Length > 100000)  //Recreating file if its size more then 100KB and it is empty
            {
                if (LTrie.Count(true) == 0)
                {
                    LTrie.Storage.RecreateFiles();
                    LTrie.Dispose();

                    Storage         = new StorageLayer(Path.Combine(engine.MainFolder, TableFileName), LTrieSettings, engine.Configuration);
                    LTrie           = new LTrie(Storage);
                    LTrie.TableName = "DBreeze.TextIndexer";
                }
            }

            if (LTrie.Count(true) > 0)
            {
                this.StartDefferedIndexing();
            }
        }
Ejemplo n.º 6
0
        public void GetAsyncWithExistingBlocksReturnsBlocks()
        {
            string dir    = CreateTestDir(this);
            var    blocks = new Block[10];

            blocks[0] = this.Network.Consensus.ConsensusFactory.CreateBlock();
            for (int i = 1; i < blocks.Length; i++)
            {
                blocks[i] = this.Network.Consensus.ConsensusFactory.CreateBlock();
                blocks[i].Header.HashPrevBlock = blocks[i - 1].Header.GetHash();
            }

            using (var engine = new DBreezeEngine(dir))
            {
                DBreeze.Transactions.Transaction transaction = engine.GetTransaction();
                for (int i = 0; i < blocks.Length; i++)
                {
                    transaction.Insert <byte[], byte[]>("Block", blocks[i].GetHash().ToBytes(), blocks[i].ToBytes());
                }
                transaction.Commit();
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                List <Block> result = repository.GetBlocks(blocks.Select(b => b.GetHash()).ToList());

                Assert.Equal(blocks.Length, result.Count);
                for (int i = 0; i < 10; i++)
                {
                    Assert.Equal(blocks[i].GetHash(), result[i].GetHash());
                }
            }
        }
        public void SetTxIndexUpdatesTxIndex()
        {
            var dir = AssureEmptyDir("TestData/BlockRepository/SetTxIndexUpdatesTxIndex");

            using (var engine = new DBreezeEngine(dir))
            {
                var trans = engine.GetTransaction();
                trans.Insert <byte[], bool>("Common", new byte[1], true);
                trans.Commit();
            }

            using (var repository = SetupRepository(Network.Main, dir))
            {
                var task = repository.SetTxIndex(false);
                task.Wait();
            }

            using (var engine = new DBreezeEngine(dir))
            {
                var trans = engine.GetTransaction();

                var txIndexRow = trans.Select <byte[], bool>("Common", new byte[1]);
                Assert.False(txIndexRow.Value);
            }
        }
Ejemplo n.º 8
0
        public void GetChainReturnsConcurrentChainFromDisk()
        {
            string dir   = CreateTestDir(this);
            var    chain = new ConcurrentChain(Network.StratisRegTest);
            var    tip   = this.AppendBlock(chain);

            using (var engine = new DBreezeEngine(dir))
            {
                using (DBreeze.Transactions.Transaction transaction = engine.GetTransaction())
                {
                    ChainedBlock        toSave = tip;
                    List <ChainedBlock> blocks = new List <ChainedBlock>();
                    while (toSave != null)
                    {
                        blocks.Insert(0, toSave);
                        toSave = toSave.Previous;
                    }

                    foreach (var block in blocks)
                    {
                        transaction.Insert <int, BlockHeader>("Chain", block.Height, block.Header);
                    }

                    transaction.Commit();
                }
            }
            using (var repo = new ChainRepository(dir))
            {
                var testChain = new ConcurrentChain(Network.StratisRegTest);
                repo.LoadAsync(testChain).GetAwaiter().GetResult();
                Assert.Equal(tip, testChain.Tip);
            }
        }
Ejemplo n.º 9
0
 private async void InitDB()
 {
     if (engine == null)
     {
         engine = new DBreezeEngine(@"C:\temp\DBR1");
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// LoadsAllData on intiation
        /// </summary>
        static DbAccess()
        {
            DBreeze.Utils.CustomSerializator.Serializator   = JsonConvert.SerializeObject;
            DBreeze.Utils.CustomSerializator.Deserializator = JsonConvert.DeserializeObject;
            _dbEngine = new DBreeze.DBreezeEngine(@"./DBreeze");


            //LoadAllValues
            using (var tran = _dbEngine.GetTransaction())
            {
                foreach (var row in tran.SelectForward <Guid, DbCustomSerializer <Repository.DataGroup> >(TableType.DataGroup))
                {
                    _DataGroupsSet.Add(row.Key, row.Value.Get);
                }
                foreach (var row in tran.SelectForward <DateTime, DbCustomSerializer <Repository.Annotation> >(TableType.Annotation))
                {
                    _AnnotationsSet.Add(row.Key, row.Value.Get);
                }


                /*
                 * foreach(var row in tran.SelectForward<string, DbCustomSerializer<Repository.Sensor>>(TableType.SensorsAndValues))
                 * {
                 *  Sensors.Add(row.Key, row.Value.Get);
                 * }*/
            }
        }
Ejemplo n.º 11
0
        public void SaveWritesChainToDisk()
        {
            string dir   = CreateTestDir(this);
            var    chain = new ConcurrentChain(Network.StratisRegTest);

            this.AppendBlock(chain);

            using (var repo = new ChainRepository(dir))
            {
                repo.SaveAsync(chain).GetAwaiter().GetResult();
            }

            using (var engine = new DBreezeEngine(dir))
            {
                ChainedBlock tip = null;
                foreach (var row in engine.GetTransaction().SelectForward <int, BlockHeader>("Chain"))
                {
                    if (tip != null && row.Value.HashPrevBlock != tip.HashBlock)
                    {
                        break;
                    }
                    tip = new ChainedBlock(row.Value, row.Value.GetHash(), tip);
                }
                Assert.Equal(tip, chain.Tip);
            }
        }
Ejemplo n.º 12
0
 public InvoiceRepository(ApplicationDbContextFactory contextFactory, string dbreezePath, Network network)
 {
     _Engine         = new DBreezeEngine(dbreezePath);
     _IndexerThread  = new CustomThreadPool(1, "Invoice Indexer");
     _Network        = network;
     _ContextFactory = contextFactory;
 }
Ejemplo n.º 13
0
        public MetaDataStorage(DBreezeEngine engine, IPathMatcher matcher, bool fullValidation, bool disableInitialValidation = false)
        {
            if (engine == null)
            {
                throw new ArgumentNullException("engine");
            }

            if (matcher == null)
            {
                throw new ArgumentNullException("matcher");
            }

            this.engine  = engine;
            this.matcher = matcher;
            this.fullValidationOnEachManipulation = fullValidation;

            if (!disableInitialValidation)
            {
                try {
                    this.ValidateObjectStructure();
                } catch (InvalidDataException e) {
                    Logger.Fatal("Database object structure is invalid", e);
                }
            }
        }
Ejemplo n.º 14
0
 public void CreateDb(string dbPath)
 {
     _dbPath = dbPath;
     _engine = StaticKeyValueDatabase.GetDatabaseEngine(dbPath);
     CustomSerializator.ByteArraySerializator   = o => JsonConvert.SerializeObject(o).To_UTF8Bytes();
     CustomSerializator.ByteArrayDeSerializator = (bt, t) => JsonConvert.DeserializeObject(bt.UTF8_GetString(), t);
 }
Ejemplo n.º 15
0
        public void DoesNotOverwriteExistingBlockAndTxIndexOnFirstLoad()
        {
            string dir = CreateTestDir(this);

            using (var engine = new DBreezeEngine(dir))
            {
                var transaction = engine.GetTransaction();

                transaction.Insert <byte[], byte[]>("Common", new byte[0], new uint256(56).ToBytes());
                transaction.Insert <byte[], bool>("Common", new byte[1], true);
                transaction.Commit();
            }

            using (var repository = this.SetupRepository(Network.Main, dir))
            {
            }

            using (var engine = new DBreezeEngine(dir))
            {
                var transaction = engine.GetTransaction();

                var blockRow   = transaction.Select <byte[], uint256>("Common", new byte[0]);
                var txIndexRow = transaction.Select <byte[], bool>("Common", new byte[1]);

                Assert.Equal(new uint256(56), blockRow.Value);
                Assert.True(txIndexRow.Value);
            }
        }
        /// <summary>
        /// Disposes the object via a special disposing task that is executed with the exclusive scheduler.
        /// </summary>
        public void Dispose()
        {
            this.disposed = true;
            if (this.singleThread == null)
            {
                return;
            }

            ManualResetEventSlim cleaned = new ManualResetEventSlim();

            new Task(() =>
            {
                if (this.Transaction != null)
                {
                    this.transaction.Dispose();
                    this.transaction = null;
                }

                if (this.engine != null)
                {
                    this.engine.Dispose();
                    this.engine = null;
                }

                this.singleThread.Dispose();
                this.singleThread = null;

                cleaned.Set();
            }).Start(this.singleThread);

            cleaned.Wait();
        }
Ejemplo n.º 17
0
        public void GetTrxAsyncWithTransactionReturnsExistingTransaction()
        {
            string      dir   = CreateTestDir(this);
            Transaction trans = this.Network.CreateTransaction();

            trans.Version = 125;

            using (var engine = new DBreezeEngine(dir))
            {
                Block block = this.Network.CreateBlock();
                block.Header.GetHash();
                block.Transactions.Add(trans);

                DBreeze.Transactions.Transaction transaction = engine.GetTransaction();
                transaction.Insert <byte[], byte[]>("Block", block.Header.GetHash().ToBytes(), block.ToBytes());
                transaction.Insert <byte[], byte[]>("Transaction", trans.GetHash().ToBytes(), block.Header.GetHash().ToBytes());
                transaction.Insert <byte[], byte[]>("Common", new byte[0], this.DBreezeSerializer.Serialize(new HashHeightPair(uint256.Zero, 1)));
                transaction.Insert <byte[], bool>("Common", new byte[1], true);
                transaction.Commit();
            }

            using (IBlockRepository repository = this.SetupRepository(this.Network, dir))
            {
                Assert.Equal((uint)125, repository.GetTransactionById(trans.GetHash()).Version);
            }
        }
Ejemplo n.º 18
0
        public void SetBlockHashUpdatesBlockHash()
        {
            string dir = CreateTestDir(this);

            using (var engine = new DBreezeEngine(dir))
            {
                var trans = engine.GetTransaction();
                trans.Insert <byte[], byte[]>("Common", new byte[0], new uint256(41).ToBytes());
                trans.Commit();
            }

            using (var repository = this.SetupRepository(Network.Main, dir))
            {
                var task = repository.SetBlockHashAsync(new uint256(56));
                task.Wait();
            }

            using (var engine = new DBreezeEngine(dir))
            {
                var trans = engine.GetTransaction();

                var blockHashKeyRow = trans.Select <byte[], byte[]>("Common", new byte[0]);
                Assert.Equal(new uint256(56), new uint256(blockHashKeyRow.Value));
            }
        }
Ejemplo n.º 19
0
        public TextDeferredIndexer(DBreezeEngine engine)
        {
            this.DBreezeEngine = engine;
            LTrieSettings = new TrieSettings()
            {
                InternalTable = true
            };
            Storage = new StorageLayer(Path.Combine(engine.MainFolder, TableFileName), LTrieSettings, engine.Configuration);
            LTrie = new LTrie(Storage);
            LTrie.TableName = "DBreeze.TextIndexer";

            if (LTrie.Storage.Length > 100000)  //Recreating file if its size more then 100KB and it is empty
            {
                if (LTrie.Count(true) == 0)
                {
                    LTrie.Storage.RecreateFiles();
                    LTrie.Dispose();

                    Storage = new StorageLayer(Path.Combine(engine.MainFolder, TableFileName), LTrieSettings, engine.Configuration);
                    LTrie = new LTrie(Storage);
                    LTrie.TableName = "DBreeze.TextIndexer";
                }
            }

            if (LTrie.Count(true) > 0)
                this.StartDefferedIndexing();
        }
        public void GetTrxAsyncWithTransactionReturnsExistingTransaction_IX()
        {
            var dir   = AssureEmptyDir("TestData/IndexRepository/GetTrxAsyncWithTransaction");
            var trans = new Transaction {
                Version = 125
            };

            using (var engine = new DBreezeEngine(dir))
            {
                var block = new Block();
                block.Header.GetHash();
                block.Transactions.Add(trans);

                var transaction = engine.GetTransaction();
                transaction.Insert <byte[], byte[]>("Block", block.Header.GetHash().ToBytes(), block.ToBytes());
                transaction.Insert <byte[], byte[]>("Transaction", trans.GetHash().ToBytes(), block.Header.GetHash().ToBytes());
                transaction.Insert <byte[], byte[]>("Common", new byte[0], uint256.Zero.ToBytes());
                transaction.Insert <byte[], bool>("Common", new byte[1], true);
                transaction.Commit();
            }

            using (var repository = SetupRepository(Network.Main, dir))
            {
                var task = repository.GetTrxAsync(trans.GetHash());
                task.Wait();

                Assert.Equal((uint)125, task.Result.Version);
            }
        }
Ejemplo n.º 21
0
 public void Dispose()
 {
     if (_engine != null)
     {
         _engine.Dispose();
         _engine = null;
     }
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Secured by RaftNode
 /// </summary>
 public void Dispose()
 {
     if (db != null)
     {
         db.Dispose();
         db = null;
     }
 }
 public static void PutDbEntity <T>(this DBreezeEngine engine, string table, string key, T entity)
 {
     using (var tran = engine.GetTransaction())
     {
         tran.Insert(table, key, new DbCustomSerializer <T>(entity));
         tran.Commit();
     }
 }
Ejemplo n.º 24
0
        public DBreezeCatalogScanStoreTest()
        {
            Mock <IDBreezeConfig> config = new Mock <IDBreezeConfig>(MockBehavior.Loose);

            config.SetupGet(c => c.StorageBackend).Returns(eStorage.MEMORY);
            config.SetupGet(c => c.RootCacheDirectory).Returns("dummy");
            db = new DBreezeCatalogScanStore(engine = new DBreezeEngine(config.Object));
        }
Ejemplo n.º 25
0
 private LocalDbStore(Dictionary <string, int> participants, InmemStore inMemStore, DBreezeEngine db, string path, ILogger logger)
 {
     this.participants = participants;
     InMemStore        = inMemStore;
     this.db           = db;
     Path        = path;
     this.logger = logger;
 }
 public static void PurgeDbTable(this DBreezeEngine engine, string table)
 {
     using (var tran = engine.GetTransaction())
     {
         tran.RemoveAllKeys(table, false);
         tran.Commit();
     }
 }
        public void CreateDB(string dbPath = null, bool createNew = true)
        {
            DbPath = dbPath;
            engine = StaticKeyValueDatabase.GetDatabaseEngine(dbPath);
            DBreeze.Utils.CustomSerializator.ByteArraySerializator = (object o) => { return(JsonConvert.SerializeObject(o).To_UTF8Bytes()); };

            DBreeze.Utils.CustomSerializator.ByteArrayDeSerializator = (byte[] bt, Type t) => { return(JsonConvert.DeserializeObject(bt.UTF8_GetString(), t)); };
        }
Ejemplo n.º 28
0
 public void Dispose()
 {
     if (this.engine != null)
     {
         this.engine.Dispose();
         this.engine = null;
     }
 }
 public void Dispose()
 {
     if (this.storageEngine != null)
     {
         this.storageEngine.Dispose();
         this.storageEngine = null;
     }
 }
Ejemplo n.º 30
0
 public Benchmark(string dbreezeFolder)
 {
     _folder = dbreezeFolder;
     if (engine == null)
     {
         engine = new DBreezeEngine(dbreezeFolder);
     }
 }
Ejemplo n.º 31
0
        public Index(IIndexRepository repository)
            : base(false, null)
        {
            Guard.NotNull(repository, nameof(repository));

            this.repository = repository;
            this.dbreeze    = repository.GetDbreezeEngine();
        }