Exemple #1
0
        public bool GetIdByHash(string nodeHash, out ulong id)
        {
            id = 0;
            if (nodeHash.Equals(EmptyHash))
            {
                return(true);
            }
            if (_idCache.TryGetValue(nodeHash, out id))
            {
                return(true);
            }
            var idByte = _dbContext.Get(EntryPrefix.VersionByHash.BuildPrefix(HexUtils.HexToBytes(nodeHash)));

            if (!(idByte is null))
            {
                id = UInt64Utils.FromBytes(idByte);
                return(true);
            }
            id = _versionFactory.NewVersion();
            _idCache[nodeHash] = id;
            if (_idCache.Count >= _idCacheCapacity)
            {
                CommitIds();
            }
            return(false);
        }
Exemple #2
0
        public IHashTrieNode?TryGetNode(byte[] nodeHash, out List <byte[]> childrenHash)
        {
            childrenHash = new List <byte[]>();
            var prefix = EntryPrefix.VersionByHash.BuildPrefix(nodeHash);
            var idByte = _rocksDbContext.Get(prefix);

            if (idByte == null)
            {
                return(null);
            }
            ulong         id   = UInt64Utils.FromBytes(idByte);
            IHashTrieNode?node = TryGetNode(id);

            if (node == null)
            {
                return(null);
            }

            if (node.Type == NodeType.Internal)
            {
                foreach (var childId in node.Children)
                {
                    var child = TryGetNode(childId);
                    if (child == null)
                    {
                        return(null);
                    }
                    childrenHash.Add(child.Hash);
                }
            }
            return(node);
        }
Exemple #3
0
        public ulong GetOldestSnapshotInDb()
        {
            var prefix = EntryPrefix.OldestSnapshotInDb.BuildPrefix();
            var block  = Get(prefix);

            if (block is null)
            {
                return(0);
            }
            return(UInt64Utils.FromBytes(block));
        }
Exemple #4
0
        public ulong?GetDbShrinkDepth()
        {
            var prefix = EntryPrefix.DbShrinkDepth.BuildPrefix();
            var depth  = Get(prefix);

            if (depth is null)
            {
                return(null);
            }
            return(UInt64Utils.FromBytes(depth));
        }
Exemple #5
0
        public void CommitNodes()
        {
            RocksDbAtomicWrite tx = new RocksDbAtomicWrite(_dbContext);

            foreach (var item in _nodeCache)
            {
                tx.Put(EntryPrefix.PersistentHashMap.BuildPrefix(item.Key), NodeSerializer.ToBytes(item.Value));
                Console.WriteLine("Adding node to DB : " + item.Key);
            }
            ulong nodesCnt = UInt64Utils.FromBytes(_dbContext.Get(EntryPrefix.NodesDownloadedTillNow.BuildPrefix()));

            nodesCnt += (ulong)_nodeCache.Count;
            tx.Put(EntryPrefix.NodesDownloadedTillNow.BuildPrefix(), UInt64Utils.ToBytes(nodesCnt));
            tx.Commit();
            _nodeCache.Clear();
        }
Exemple #6
0
 public ulong GetDownloadedNodeCount()
 {
     return(UInt64Utils.FromBytes(_rocksDbContext.Get(EntryPrefix.NodesDownloadedTillNow.BuildPrefix())));
 }