+-spec to_bin(t()) ->
+ binary().
+to_bin(
+ ?T
+ { path = Path
+ , value = Value
+ , timestamp = Timestamp
+ }
+) ->
+ PathBin = path_to_bin(Path),
+ ValueBin = integer_to_binary(Value),
+ TimestampInt = timestamp_to_integer(Timestamp),
+ TimestampBin = integer_to_binary(TimestampInt),
+ <<PathBin/binary, " ", ValueBin/binary, " ", TimestampBin/binary>>.
+
+-spec add_path_prefix(t(), binary()) ->
+ t().
+add_path_prefix(?T{path=Path}=T, <<Prefix/binary>>) ->
+ T?T{path = [Prefix | Path]}.
+
+-spec path_to_bin([binary()]) ->
+ binary().
+path_to_bin(Path) ->
+ bin_join(Path, <<".">>).
+
+-spec node_id_to_bin(node()) ->
+ binary().
+node_id_to_bin(NodeID) ->
+ NodeIDBin = atom_to_binary(NodeID, utf8),
+ re:replace(NodeIDBin, "[\@\.]", "_", [global, {return, binary}]).
+
+%% ============================================================================
+%% Helpers
+%% ============================================================================
+
+-spec bin_join([binary()], binary()) ->
+ binary().
+bin_join([] , <<_/binary>> ) -> <<>>;
+bin_join([<<B/binary>> | []] , <<_/binary>> ) -> B;
+bin_join([<<B/binary>> | [_|_]=Bins], <<Sep/binary>>) ->
+ BinsBin = bin_join(Bins, Sep),
+ <<B/binary, Sep/binary, BinsBin/binary>>.
+
+-spec timestamp_to_integer(erlang:timestamp()) ->
+ non_neg_integer().
+timestamp_to_integer({Megaseconds, Seconds, _}) ->
+ Megaseconds * 1000000 + Seconds.
+