Ejemplo n.º 1
0
        public ushort this[BlockPos p]
        {
            get
            {
                if (p.Y < -200)
                {
                    return(1);
                }

                var chunk = PeekChunk((ChunkPos)p);
                if (chunk == null)
                {
                    return(0);
                }

                return(chunk[p.X & _chunkMask, p.Y & _chunkMask, p.Z & _chunkMask]);
            }

            set
            {
                var chunkPos = (ChunkPos)p;
                var chunk    = GetChunk(chunkPos);
                if (chunk.IsCreated)
                {
                    chunk[p.X & _chunkMask, p.Y & _chunkMask, p.Z & _chunkMask] = value;
                    Universe?.SaveFile?.Write(StorageKey.Get("ChunkData", chunkPos), StorageValue.Serialize(chunk.GetChunkData()));
                }
            }
        }
Ejemplo n.º 2
0
 void StoreHashUpdate(ByteBuffer key, StorageValue storageValue)
 {
     if (_hashIndexWriter == null)
     {
         StartNewHashIndexFile();
     }
     _hashIndexWriter.WriteVUInt32(storageValue.FileId);
     _hashIndexWriter.WriteVUInt32(storageValue.FileOfs);
     _hashIndexWriter.WriteVUInt32(storageValue.ContentLengthCompressedIsLeaf);
     _hashIndexWriter.WriteBlock(key);
 }
Ejemplo n.º 3
0
        public void Write(StorageKey key, StorageValue value)
        {
            if (key == null) throw new ArgumentNullException(nameof(key));
            if (value == null) throw new ArgumentNullException(nameof(value));

            lock (_mutex)
            {
                _queuedValues[key] = value;
            }

            _queuedKeys.Add(key);
        }
Ejemplo n.º 4
0
        public async Task AddStorage_WritesCorrectKeyValue()
        {
            var inputHash = UInt160.Parse(RandomInt().ToString("X40"));
            var inputKey  = new StorageKey {
                ScriptHash = inputHash, Key = new byte[0]
            };
            var inputValue = new StorageValue {
                Value = new byte[1]
            };
            var redisDbContextMock = AutoMockContainer.GetMock <IRedisDbJsonContext>();
            var testee             = AutoMockContainer.Create <RedisDbJsonRepository>();
            await testee.AddStorage(inputKey, inputValue);

            redisDbContextMock.Verify(m =>
                                      m.Set(It.Is <RedisKey>(b => b == inputKey.BuildStateStorageKey()), inputValue.Value));
        }
Ejemplo n.º 5
0
        private async void LoadOrGenerate(Chunk chunk)
        {
            var          savedValueTask = Universe?.SaveFile?.ReadAsync(StorageKey.Get("ChunkData", chunk.Pos));
            StorageValue savedValue     = savedValueTask == null ? null : await savedValueTask;

            if (savedValue != null)
            {
                var chunkData = savedValue.Deserialize <ChunkData>();
                if (chunkData != null)
                {
                    chunk.PasteChunkData(savedValue.Deserialize <ChunkData>());
                }
            }
            else if (Generator != null)
            {
                Generator.CreateChunk(chunk);
            }
        }
Ejemplo n.º 6
0
        void LoadHashKeyIndex(uint hashKeyIndexFileId)
        {
            var reader = _fileCollection.GetFile(hashKeyIndexFileId).GetExclusiveReader();

            _keyLen = (int)((IHashKeyIndex)_fileCollection.FileInfoByIdx(hashKeyIndexFileId)).KeyLen;
            HashKeyIndex.SkipHeader(reader);
            var keyBuf = ByteBuffer.NewSync(new byte[_keyLen]);

            while (!reader.Eof)
            {
                var value = new StorageValue();
                value.FileId  = reader.ReadVUInt32();
                value.FileOfs = reader.ReadVUInt32();
                value.ContentLengthCompressedIsLeaf = reader.ReadVUInt32();
                reader.ReadBlock(keyBuf);
                _dict20.TryAdd(new ByteStructs.Key20(keyBuf), value);
            }
        }
Ejemplo n.º 7
0
        StorageValue StoreContent(ByteBuffer content)
        {
            var result = new StorageValue();

            result.Compressed    = false;
            result.ContentLength = (uint)content.Length;
            if (_pureValueFile == null)
            {
                StartNewPureValueFile();
            }
            result.FileId  = _pureValueFile.Index;
            result.FileOfs = (uint)_pureValueFileWriter.GetCurrentPosition();
            _pureValueFileWriter.WriteBlock(content);
            if (_pureValueFileWriter.GetCurrentPosition() >= _maxFileSize)
            {
                _pureValueFileWriter.FlushBuffer();
                StartNewPureValueFile();
            }
            return(result);
        }
Ejemplo n.º 8
0
        StorageValue StoreContent(ByteBuffer content)
        {
            var result = new StorageValue();

            result.Compressed    = false;
            result.ContentLength = (uint)content.Length;
            if (_pureValueFile == null)
            {
                StartNewPureValueFile();
            }
            result.FileId  = _pureValueFile !.Index;
            result.FileOfs = (uint)_pureValueFileWriter !.GetCurrentPositionWithoutWriter();
            _pureValueFileWriter.WriteBlockWithoutWriter(ref MemoryMarshal.GetReference(content.AsSyncReadOnlySpan()), (uint)content.Length);
            _pureValueFile.Flush();
            if (_pureValueFileWriter.GetCurrentPositionWithoutWriter() >= _maxFileSize)
            {
                _pureValueFile.HardFlushTruncateSwitchToReadOnlyMode();
                StartNewPureValueFile();
            }
            return(result);
        }
Ejemplo n.º 9
0
        public async Task GetStorage_ValueFound_ReturnsStorageValue()
        {
            var input = new StorageKey
            {
                ScriptHash = UInt160.Parse(RandomInt().ToString("X40")),
                Key        = new byte[1]
            };
            var expectedBytes      = new byte[1];
            var expectedResult     = new StorageValue();
            var rocksDbContextMock = AutoMockContainer.GetMock <IRocksDbContext>();

            rocksDbContextMock
            .Setup(m => m.Get(It.Is <byte[]>(b => b.SequenceEqual(input.BuildStateStorageKey()))))
            .ReturnsAsync(expectedBytes);
            _serializerMock.Setup(m => m.Deserialize <StorageValue>(expectedBytes, null)).Returns(expectedResult);
            var testee = AutoMockContainer.Create <RocksDbRepository>();

            var result = await testee.GetStorage(input);

            result.Should().Be(expectedResult);
        }
Ejemplo n.º 10
0
 public void Write(StorageKey key, StorageValue value)
 {
 }
Ejemplo n.º 11
0
 public Task AddStorage(StorageKey key, StorageValue val)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 12
0
 void StoreHashUpdate(ByteBuffer key, StorageValue storageValue)
 {
     if (_hashIndexWriter == null)
     {
         StartNewHashIndexFile();
     }
     _hashIndexWriter.WriteVUInt32(storageValue.FileId);
     _hashIndexWriter.WriteVUInt32(storageValue.FileOfs);
     _hashIndexWriter.WriteVUInt32(storageValue.ContentLengthCompressedIsLeaf);
     _hashIndexWriter.WriteBlock(key);
 }
Ejemplo n.º 13
0
 bool TryLoadHashKeyIndex(uint hashKeyIndexFileId)
 {
     var reader = _fileCollection.GetFile(hashKeyIndexFileId).GetExclusiveReader();
     _keyLen = (int) ((IHashKeyIndex)_fileCollection.FileInfoByIdx(hashKeyIndexFileId)).KeyLen;
     HashKeyIndex.SkipHeader(reader);
     var keyBuf = ByteBuffer.NewSync(new byte[_keyLen]);
     while (!reader.Eof)
     {
         var value = new StorageValue();
         value.FileId = reader.ReadVUInt32();
         value.FileOfs = reader.ReadVUInt32();
         value.ContentLengthCompressedIsLeaf = reader.ReadVUInt32();
         reader.ReadBlock(keyBuf);
         _dict20.TryAdd(new ByteStructs.Key20(keyBuf), value);
     }
     return true;
 }
Ejemplo n.º 14
0
 public async Task AddStorage(StorageKey key, StorageValue val)
 {
     await _redisDbContext.Set(key.BuildStateStorageKey(), val.Value);
 }
Ejemplo n.º 15
0
 StorageValue StoreContent(ByteBuffer content)
 {
     var result = new StorageValue();
     result.Compressed = false;
     result.ContentLength = (uint)content.Length;
     if (_pureValueFile == null)
         StartNewPureValueFile();
     result.FileId = _pureValueFile.Index;
     result.FileOfs = (uint)_pureValueFileWriter.GetCurrentPosition();
     _pureValueFileWriter.WriteBlock(content);
     if (_pureValueFileWriter.GetCurrentPosition() >= _maxFileSize)
     {
         _pureValueFileWriter.FlushBuffer();
         StartNewPureValueFile();
     }
     return result;
 }
Ejemplo n.º 16
0
        /// <summary>
        /// Process the request (once). Get the response (page content, cookies, ...).
        /// </summary>
        private void ProcessRequest()
        {
            if (RequestProcessed)
            {
                return; // test, unlocked
            }
            lock (this)
            {
                if (RequestProcessed)
                {
                    return;                   // double checked lock, test again
                }
                RequestProcessed = true;

                StorageValue data = (StorageValue)DataCache.GetItem(StorageKey,
                                                                    delegate(out DateTime expiration)
                {
                    WebRequest webreq = WebRequest.Create(ContextUri);

                    string RespContent           = null;
                    CookieCollection RespCookies = null;

                    if (webreq is HttpWebRequest)
                    {
                        HttpWebRequest req = (HttpWebRequest)webreq;

                        req.Referer           = (RefererContext != null && RefererContext.ContextUri != null) ? RefererContext.ContextUri.AbsoluteUri : null;
                        req.UserAgent         = DefaultUserAgent;
                        req.Timeout           = 30000;
                        req.AllowAutoRedirect = false;
                        req.KeepAlive         = false;
                        var cookieJar         = req.CookieContainer = new CookieContainer();
                        if (RefererContext != null && RefererContext.Cookies != null)
                        {       // TODO: filter cookies by domain and path
                            req.CookieContainer.Add(RefererContext.Cookies);
                        }
                        //req.Headers.Add("Accept-Language", "en,cs");

                        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

                        // page content
                        Stream RespStream = resp.GetResponseStream();
                        RespContent       = new StreamReader(RespStream /*, Encoding.GetEncoding(resp.ContentEncoding)*/).ReadToEnd();
                        RespStream.Close();

                        // cookies
                        foreach (Cookie c in cookieJar.GetCookies(req.RequestUri))
                        {
                            if (RespCookies == null)
                            {
                                RespCookies = new CookieCollection();
                            }

                            RespCookies.Add(c);
                        }

                        // TODO: headers (language, cache expire, content type, encoding, Response URI, ...)

                        // close the response
                        resp.Close();
                    }
                    else if (webreq is FileWebRequest)
                    {
                        FileWebRequest req   = (FileWebRequest)webreq;
                        FileWebResponse resp = (FileWebResponse)req.GetResponse();

                        // page content
                        Stream RespStream = resp.GetResponseStream();
                        RespContent       = new StreamReader(RespStream /*, Encoding.GetEncoding(resp.ContentEncoding)*/).ReadToEnd();
                        RespStream.Close();

                        // close the response
                        resp.Close();
                    }

                    expiration = DateTime.Now.AddHours(1.0);        // TODO: time based on HTML header or HtmlContext parameters

                    return(new StorageValue()
                    {
                        Content = RespContent, Cookies = RespCookies
                    });
                }
                                                                    );

                _Content = data.Content;
                _Cookies = data.Cookies;
            }
        }
Ejemplo n.º 17
0
 private void Save()
 {
     _saveFile?.Write(_storageKey, StorageValue.Serialize(GetChunkData()));
 }