Created
August 21, 2019 00:11
-
-
Save pmarreck/7959a31172230dd86b693ebd2a9f519d to your computer and use it in GitHub Desktop.
Ecto compressed erlang term binary custom data type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Mpnetwork.Ecto.CompressedTerm do | |
@behaviour Ecto.Type | |
# The functionality below depends on the lz4_erl module. | |
# Note that pack/unpack stores the decompressed data length as the first 4 bytes | |
# or assumes those bytes are that value | |
defp compress(txt) when is_binary(txt) do | |
{:ok, comp} = :lz4.pack(txt, [:high]) | |
comp | |
end | |
defp decompress(bin) when is_binary(bin) do | |
{:ok, decomp} = :lz4.unpack(bin) | |
decomp | |
end | |
def type, do: :binary | |
def cast(term), do: {:ok, term} | |
def load(binary) when is_binary(binary), do: {:ok, :erlang.binary_to_term(decompress(binary))} | |
def dump(term), do: {:ok, compress(:erlang.term_to_binary(term))} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment