This repository has been archived by the owner on Oct 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Compression
danielwertheim edited this page Dec 4, 2012
·
1 revision
It's quite easy to add compression or encryption of the JSON document. Just implement SisoDb.IDbPipe
and hook it in against a certain db.
Lets implement a simple deflate compression using System.IO.Compression
.
public class DeflatePipe : IDbPipe
{
public string Writing(IStructureSchema structureSchema, string data)
{
return Compress(data);
}
public string Reading(IStructureSchema structureSchema, string data)
{
return Decompress(data);
}
private static string Compress(string j)
{
using (var source = new MemoryStream(Encoding.UTF8.GetBytes(j)))
{
source.Position = 0;
using (var trg = new MemoryStream())
{
using (var c = new DeflateStream(trg, CompressionMode.Compress, false))
{
source.CopyTo(c);
}
return Convert.ToBase64String(trg.ToArray());
}
}
}
private static string Decompress(string j)
{
using (var source = new MemoryStream(Convert.FromBase64String(j)))
{
source.Position = 0;
using (var trg = new MemoryStream())
{
using (var c = new DeflateStream(source, CompressionMode.Decompress, false))
{
c.CopyTo(trg);
}
return Encoding.UTF8.GetString(trg.ToArray());
}
}
}
}
I don't make use of the IStructureSchema
but could easily use it to just use compression for a certain document by inspecting:
if(structureSchema.Name == "Customer") {}
or
if(structureSchema.Type.Type == typeof(Customer)) {}
Now lets hook the pipe in:
db.Pipe = new DeflatePipe();
The result:
---5000 items
-- Raw
--select sum(DATALENGTH(Json)) from dbo.CustomerStructure
--4883358 bytes
-- Deflate
--select sum(DATALENGTH(Json)) from dbo.CustomerStructure
--3678256 bytes
-- Diff
select
(4883358 - 3678256) TotalSaveDeflate, --1205102
(4883358 - 3678256) / 5000 PerItemDeflate, --241
You should really evaluate both algorithm and the document type to see if you save anything.