You are currently browsing legacy 3.0 version of documentation. Click here to switch to the newest 4.2 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Plugins: Codecs

The AbstractDocumentCodec and AbstractIndexCodec classes have been introduced as an entry point to custom compression methods.

public abstract class AbstractDocumentCodec
{
	public DocumentDatabase Database { get; set; }

	public virtual void Initialize(DocumentDatabase database) { }

	public virtual void Initialize() { }

	public virtual void SecondStageInit() { }

	public abstract Stream Encode(string key, RavenJObject data, RavenJObject metadata, Stream dataStream);

	public abstract Stream Decode(string key, RavenJObject metadata, Stream dataStream);
}

public abstract class AbstractIndexCodec
{
	public virtual void Initialize(DocumentDatabase database) { }

	public virtual void SecondStageInit() { }

	public abstract Stream Encode(string key, Stream dataStream);

	public abstract Stream Decode(string key, Stream dataStream);
}

where:
* Encode is executed when the given document/index is written.
* Decode is executed when the provided document/index is read.
* Initialize and SecondStageInit are used to trigger initialization process.

Example - Compression

public class SimpleCompressionCodec : AbstractDocumentCodec
{
	private readonly SimpleCompressor compressor = new SimpleCompressor();

	public override Stream Encode(string key, RavenJObject data, RavenJObject metadata, Stream dataStream)
	{
		return compressor.Compress(key, data, metadata, dataStream);
	}

	public override Stream Decode(string key, RavenJObject metadata, Stream dataStream)
	{
		return compressor.Decompress(key, metadata, dataStream);
	}
}

Example - Encryption

public class SimpleEncryptionCodec : AbstractDocumentCodec
{
	private readonly SimpleEncryptor encryptor = new SimpleEncryptor();

	public override Stream Encode(string key, RavenJObject data, RavenJObject metadata, Stream dataStream)
	{
		return encryptor.Encrypt(key, data, metadata, dataStream);
	}

	public override Stream Decode(string key, RavenJObject metadata, Stream dataStream)
	{
		return encryptor.Decrypt(key, metadata, dataStream);
	}
}