public void SetChunkCompressedBytes(Vector relativeChunkLocation, byte[] compressedNbtBytes) { // Sanity check if (locationTable is null || timestampTable is null) { return; } var tableIndex = GetChunkTableIndex(relativeChunkLocation); var(currentOffset, currentSize) = locationTable.GetOffsetSizeAtIndex(tableIndex); var newSize = compressedNbtBytes.Length; Memory <byte> memAllocation; if (newSize <= currentSize && currentSize > 0) { // New chunk will fit in place of the old one. memAllocation = GetAllocation(tableIndex); } else { memAllocation = GetNewAllocation(newSize, tableIndex); } var chunkAllocation = new ChunkAllocation(memAllocation); chunkAllocation.SetChunkBytes(compressedNbtBytes); var timestamp = DateTimeOffset.Now.ToUnixTimeSeconds(); timestampTable.SetTimestampAtIndex(tableIndex, timestamp); }
private Memory <byte> GetNewAllocation(int payloadSize, int tableIndex) { lock (this) { var allocationSize = ChunkAllocation.GetAllocationSize(payloadSize); var assignedOffset = nextAvailableOffset; Interlocked.Add(ref nextAvailableOffset, allocationSize); locationTable.SetOffsetSizeAtIndex(tableIndex, assignedOffset, allocationSize); return(fileCache.Memory.Slice(assignedOffset, allocationSize)); } }
public byte[] GetChunkCompressedBytes(Vector relativeChunkLocation) { // Sanity check if (locationTable is null || timestampTable is null) { return(null); } var chunkIndex = GetChunkTableIndex(relativeChunkLocation); var(offset, size) = locationTable.GetOffsetSizeAtIndex(chunkIndex); if (size == 0) { return(null); } Memory <byte> memAlloc = fileCache.Memory.Slice(offset, size); var chunkAllocation = new ChunkAllocation(memAlloc); return(chunkAllocation.GetChunkBytes()); }
public ReadOnlyMemory <byte> GetChunkCompressedBytes(Vector relativeChunkLocation) { if (locationTable is null || timestampTable is null) { return(ReadOnlyMemory <byte> .Empty); } int chunkIndex = GetChunkTableIndex(relativeChunkLocation); (int offset, int size) = locationTable.GetOffsetSizeAtIndex(chunkIndex); if (size == 0) { return(ReadOnlyMemory <byte> .Empty); } ReadOnlyMemory <byte> memory = fileCache.Memory.Slice(offset, size); return(ChunkAllocation.GetChunkBytes(memory)); }