Пример #1
0
        /// <inheritdoc/>
        public void StoreStateReference <T>(
            string @namespace,
            IImmutableSet <Address> addresses,
            Block <T> block)
            where T : IAction, new()
        {
            int hashSize = HashDigest <SHA256> .Size;

            byte[] hashBytes  = block.Hash.ToByteArray();
            byte[] indexBytes = BitConverter.GetBytes(block.Index);

            foreach (Address address in addresses)
            {
                string addrHex = address.ToHex();
                var    fileId  = $"{StateRefIdPrefix}{@namespace}/{addrHex}";
                if (!_db.FileStorage.Exists(fileId))
                {
                    _db.FileStorage.Upload(
                        fileId,
                        addrHex,
                        new MemoryStream());
                }

                LiteFileInfo file = _db.FileStorage.FindById(fileId);
                using (var temp = new MemoryStream())
                {
                    file.CopyTo(temp);
                    temp.Seek(0, SeekOrigin.Begin);
                    byte[] prev = temp.ToArray();

                    using (LiteFileStream stream = file.OpenWrite())
                    {
                        stream.Write(prev, 0, prev.Length);
                        stream.Write(hashBytes, 0, hashSize);
                        stream.Write(indexBytes, 0, sizeof(long));
                    }
                }
            }
        }
        public override void FlushBuffer(byte[] b, int offset, int len)
        {
            var segment = new byte[len];

            Buffer.BlockCopy(b, offset, segment, 0, len);

            var fsinfo = _db.FileStorage.FindById(_name);

            if (fsinfo != null)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    //Console.WriteLine($"{fsinfo.Id} already exist with lenth of {fsinfo.GetContentFileDataLength}");
                    fsinfo.CopyTo(memoryStream);
                    //memoryStream.Position = memoryStream.GetContentFileDataLength;
                    memoryStream.Position = FilePointer - len;
                    //Console.WriteLine($"MemorySteam lenth: {memoryStream.GetContentFileDataLength} before writing");
                    memoryStream.Write(segment, 0, len);
                    //memoryStream.Flush();
                    //_db.FileStorage.Delete(_name);
                    //Console.WriteLine($"MemorySteam lenth: {memoryStream.GetContentFileDataLength} after writing and flush");
                    memoryStream.Position = 0;
                    fsinfo = _db.FileStorage.Upload(_name, _name, memoryStream);
                    //Console.WriteLine($"{_name} lenth {fsinfo.GetContentFileDataLength} after flush");
                }
            }
            else
            {
                using (LiteFileStream fileStream = _db.FileStorage.OpenWrite(_name, _name))
                {
                    //Console.WriteLine($"Opened a new file:{_name} to write.");
                    fileStream.Write(segment, 0, len);
                    fileStream.Flush();
                    //Console.WriteLine($"{_name} lenth {fileStream.GetContentFileDataLength} after flush");
                }
            }
            GC.Collect();
        }
Пример #3
0
        private void Write(byte[] buffer, int index, int len, bool isFirstWrite)
        {
            var fsinfo = _db.FileStorage.FindById(_name);

            if (fsinfo != null)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    //Console.WriteLine($"{fsinfo.Id} already exist with lenth of {fsinfo.Length}");
                    fsinfo.CopyTo(memoryStream);
                    //memoryStream.Position = memoryStream.Length;
                    memoryStream.Position = index;
                    //Console.WriteLine($"MemorySteam lenth: {memoryStream.Length} before writing");
                    memoryStream.Write(buffer, 0, len);
                    //memoryStream.Flush();
                    //_db.FileStorage.Delete(_name);
                    //Console.WriteLine($"MemorySteam lenth: {memoryStream.Length} after writing and flush");
                    memoryStream.Position = 0;
                    fsinfo = _db.FileStorage.Upload(_name, _name, memoryStream);
                    //Console.WriteLine($"{_name} lenth {fsinfo.Length} after flush");
                }
            }
            else
            {
                using (LiteFileStream fileStream = _db.FileStorage.OpenWrite(_name, _name))
                {
                    //Console.WriteLine($"Opened a new file:{_name} to write.");
                    fileStream.Write(buffer, 0, len);
                    fileStream.Flush();
                    //Console.WriteLine($"{_name} lenth {fileStream.Length} after flush");
                }
            }
            GC.Collect();


            //Console.WriteLine($"---------------------------------------");
        }
Пример #4
0
        /// <inheritdoc/>
        public void ForkStateReferences <T>(
            string srcNamespace,
            string destNamespace,
            Block <T> branchPoint,
            IImmutableSet <Address> addressesToStrip)
            where T : IAction, new()
        {
            long branchPointIndex     = branchPoint.Index;
            List <LiteFileInfo> files =
                _db.FileStorage
                .Find($"{StateRefIdPrefix}{srcNamespace}")
                .ToList();

            if (!files.Any() && addressesToStrip.Any())
            {
                throw new NamespaceNotFoundException(
                          srcNamespace,
                          "The source namespace to be forked does not exist.");
            }

            foreach (LiteFileInfo srcFile in files)
            {
                string destId =
                    $"{StateRefIdPrefix}{destNamespace}/{srcFile.Filename}";
                _db.FileStorage.Upload(
                    destId,
                    srcFile.Filename,
                    new MemoryStream());

                LiteFileInfo destFile = _db.FileStorage.FindById(destId);
                using (LiteFileStream srcStream = srcFile.OpenRead())
                    using (LiteFileStream destStream = destFile.OpenWrite())
                    {
                        while (srcStream.Position < srcStream.Length)
                        {
                            var hashBytes  = new byte[HashDigest <SHA256> .Size];
                            var indexBytes = new byte[sizeof(long)];

                            srcStream.Read(hashBytes, 0, hashBytes.Length);
                            srcStream.Read(indexBytes, 0, indexBytes.Length);

                            long currentIndex =
                                BitConverter.ToInt64(indexBytes, 0);

                            if (currentIndex <= branchPointIndex)
                            {
                                destStream.Write(hashBytes, 0, hashBytes.Length);
                                destStream.Write(indexBytes, 0, indexBytes.Length);
                            }
                            else
                            {
                                break;
                            }
                        }
                    }

                if (destFile.Length == 0)
                {
                    _db.FileStorage.Delete(destId);
                }
            }
        }