public void CompressTest() { byte[] someBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; MemoryStream ms = new MemoryStream(someBytes); MemoryStream result = MemoryStreamCompression.Decompress(MemoryStreamCompression.Compress(ms)); Assert.True(result.ToArray().SequenceEqual(someBytes), "Bytes are not the same after compression/decompression cycle"); }
/// <summary> /// Supports writing a named data stream to the persistent store via the grid cache. /// </summary> public FileSystemErrorStatus WriteStreamToPersistentStore(Guid dataModelId, string streamName, FileSystemStreamType streamType, MemoryStream mutableStream, object source) { try { var cacheKey = ComputeNamedStreamCacheKey(dataModelId, streamName); using (var compressedStream = MemoryStreamCompression.Compress(mutableStream)) { if (_log.IsTraceEnabled()) { _log.LogInformation($"Putting key:{cacheKey} in {NonSpatialCache(streamType).Name}, size:{mutableStream.Length} -> {compressedStream.Length}, ratio:{(compressedStream.Length / (1.0 * mutableStream.Length)) * 100}%"); } NonSpatialCache(streamType).Put(cacheKey, new SerialisedByteArrayWrapper(compressedStream.ToArray())); } try { // Create the immutable stream from the source data if (Mutability == StorageMutability.Mutable && ImmutableProxy != null) { if (!PerformNonSpatialImmutabilityConversion(mutableStream, ImmutableProxy.NonSpatialCache(streamType), cacheKey, streamType, source)) { _log.LogError("Unable to project an immutable stream"); return(FileSystemErrorStatus.MutableToImmutableConversionError); } } } catch (Exception e) { _log.LogError(e, $"Exception performing mutability conversion in {nameof(WriteStreamToPersistentStore)}"); return(FileSystemErrorStatus.MutableToImmutableConversionError); } return(FileSystemErrorStatus.OK); } catch (Exception e) { _log.LogError(e, $"Exception writing stream {streamName} to persistent store"); return(FileSystemErrorStatus.UnknownErrorWritingToFS); } }
/// <summary> /// Supports writing a spatial data stream to the persistent store via the grid cache. /// </summary> public FileSystemErrorStatus WriteSpatialStreamToPersistentStore(Guid dataModelId, string streamName, int subGridX, int subGridY, long segmentStartDateTicks, long segmentEndDateTicks, long version, FileSystemStreamType streamType, MemoryStream mutableStream, object source) { try { var cacheKey = new SubGridSpatialAffinityKey(version, dataModelId, subGridX, subGridY, segmentStartDateTicks, segmentEndDateTicks); using (var compressedStream = MemoryStreamCompression.Compress(mutableStream)) { var spatialCache = SpatialCache(streamType); if (_log.IsTraceEnabled()) { _log.LogInformation($"Putting key:{cacheKey} in {spatialCache.Name}, size:{mutableStream.Length} -> {compressedStream.Length}, ratio:{(compressedStream.Length / (1.0 * mutableStream.Length)) * 100}%"); } spatialCache.Put(cacheKey, new SerialisedByteArrayWrapper(compressedStream.ToArray())); } // Convert the stream to the immutable form and write it to the immutable storage proxy try { if (Mutability == StorageMutability.Mutable && ImmutableProxy != null) { PerformSpatialImmutabilityConversion(mutableStream, ImmutableProxy.SpatialCache(streamType), cacheKey, streamType, source); } } catch (Exception e) { _log.LogError(e, $"Exception performing mutability conversion in {nameof(WriteSpatialStreamToPersistentStore)}"); return(FileSystemErrorStatus.MutableToImmutableConversionError); } return(FileSystemErrorStatus.OK); } catch (Exception e) { _log.LogError(e, $"Exception writing spatial stream {streamName} to persistent store"); return(FileSystemErrorStatus.UnknownErrorWritingToFS); } }
public void CompressTest_Null() { Assert.Null(MemoryStreamCompression.Compress(null)); }