fix line protocol
This commit is contained in:
parent
c16563a7b5
commit
686c415dc8
@ -16,67 +16,53 @@
|
|||||||
-spec parse(Metric :: binary()) -> map().
|
-spec parse(Metric :: binary()) -> map().
|
||||||
parse(Metric) when is_binary(Metric) ->
|
parse(Metric) when is_binary(Metric) ->
|
||||||
case lexer(Metric) of
|
case lexer(Metric) of
|
||||||
[Measurement, Fields, Timestamp] ->
|
[[Measurement|Tags], Fields, [Timestamp|_]] ->
|
||||||
Map = case binary:split(Measurement, <<",">>) of
|
{ok, #{
|
||||||
[Measurement] ->
|
<<"measurement">> => Measurement,
|
||||||
#{<<"measurement">> => Measurement, <<"tags">> => #{}};
|
<<"tags">> => parser_keys(Tags),
|
||||||
[Measurement0, Tags0] ->
|
<<"fields">> => parser_keys(Fields),
|
||||||
Tags = maps:from_list(parser_keys(Tags0)),
|
<<"timestamp">> => Timestamp
|
||||||
#{<<"measurement">> => Measurement0, <<"tags">> => Tags}
|
}};
|
||||||
end,
|
|
||||||
FieldsMap = maps:from_list(parser_keys(Fields)),
|
|
||||||
{ok, Map#{<<"fields">> => FieldsMap, <<"timestamp">> => binary_to_integer(Timestamp)}};
|
|
||||||
_ ->
|
_ ->
|
||||||
error
|
error
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec lexer(Input :: binary()) -> [Token :: binary()].
|
%% 词法分析:将输入文本转换为标记序列
|
||||||
lexer(Input) when is_binary(Input) ->
|
lexer(Input) ->
|
||||||
lexer(Input, false, [], []).
|
lexer(Input, false, [], [], []).
|
||||||
lexer(<<>>, _Quotes, Current, Acc) ->
|
lexer(<<>>, _Quotes, Current, Acc, Groups) ->
|
||||||
Last = list_to_binary(lists:reverse(Current)),
|
Last = list_to_binary(lists:reverse(Current)),
|
||||||
lists:reverse([Last|Acc]);
|
NAcc = lists:reverse([Last|Acc]),
|
||||||
lexer(<<$\s, Rest/binary>>, Quotes, Current, Acc) ->
|
lists:reverse([NAcc|Groups]);
|
||||||
|
%% 遇到逗号
|
||||||
|
lexer(<<$,, Rest/binary>>, Quotes, Current, Acc, Groups) ->
|
||||||
case Quotes of
|
case Quotes of
|
||||||
true ->
|
true ->
|
||||||
lexer(Rest, Quotes, [" "|Current], Acc);
|
lexer(Rest, Quotes, [","|Current], Acc, Groups);
|
||||||
false ->
|
false ->
|
||||||
Part = list_to_binary(lists:reverse(Current)),
|
Part = list_to_binary(lists:reverse(Current)),
|
||||||
lexer(Rest, Quotes, [], [Part|Acc])
|
lexer(Rest, Quotes, [], [Part|Acc], Groups)
|
||||||
end;
|
end;
|
||||||
lexer(<<$\", Rest/binary>>, Quotes, Current, Acc) ->
|
lexer(<<$\s, Rest/binary>>, Quotes, Current, Acc, Groups) ->
|
||||||
lexer(Rest, not Quotes, [$\"|Current], Acc);
|
case Quotes of
|
||||||
lexer(<<Char/utf8, Rest/binary>>, Quotes, Current, Acc) ->
|
true ->
|
||||||
lexer(Rest, Quotes, [Char|Current], Acc).
|
lexer(Rest, Quotes, [$\s|Current], Acc, Groups);
|
||||||
|
false ->
|
||||||
|
Part = list_to_binary(lists:reverse(Current)),
|
||||||
|
NAcc = lists:reverse([Part|Acc]),
|
||||||
|
lexer(Rest, Quotes, [], [], [NAcc|Groups])
|
||||||
|
end;
|
||||||
|
lexer(<<$\", Rest/binary>>, Quotes, Current, Acc, Groups) ->
|
||||||
|
lexer(Rest, not Quotes, [$\"|Current], Acc, Groups);
|
||||||
|
lexer(<<Char/utf8, Rest/binary>>, Quotes, Current, Acc, Groups) ->
|
||||||
|
lexer(Rest, Quotes, [Char|Current], Acc, Groups).
|
||||||
|
|
||||||
-spec parser_keys(Input :: binary()) -> [{Key :: binary(), Val :: binary()}].
|
parser_keys(Tokens) ->
|
||||||
parser_keys(Input) when is_binary(Input) ->
|
maps:from_list(lists:flatmap(fun(Item) ->
|
||||||
Parts = split_keys(Input),
|
|
||||||
lists:flatmap(fun(Item) ->
|
|
||||||
case binary:split(Item, <<"=">>) of
|
case binary:split(Item, <<"=">>) of
|
||||||
[Key, Val] ->
|
[Key, Val] ->
|
||||||
[{Key, Val}];
|
[{Key, Val}];
|
||||||
_ ->
|
_ ->
|
||||||
[]
|
[]
|
||||||
end
|
end
|
||||||
end, Parts).
|
end, Tokens)).
|
||||||
|
|
||||||
%% 将 "key=val,key1=val1"的格式转换成 ["key=val", "key1=val1"]
|
|
||||||
-spec split_keys(Input :: binary()) -> [binary()].
|
|
||||||
split_keys(Input) when is_binary(Input) ->
|
|
||||||
split_keys(Input, false, [], []).
|
|
||||||
split_keys(<<>>, _Quotes, Current, Acc) ->
|
|
||||||
Last = list_to_binary(lists:reverse(Current)),
|
|
||||||
lists:reverse([Last|Acc]);
|
|
||||||
split_keys(<<$,, Rest/binary>>, Quotes, Current, Acc) ->
|
|
||||||
case Quotes of
|
|
||||||
true ->
|
|
||||||
split_keys(Rest, Quotes, [$,|Current], Acc);
|
|
||||||
false ->
|
|
||||||
Part = list_to_binary(lists:reverse(Current)),
|
|
||||||
split_keys(Rest, Quotes, [], [Part|Acc])
|
|
||||||
end;
|
|
||||||
split_keys(<<$\", Rest/binary>>, Quotes, Current, Acc) ->
|
|
||||||
split_keys(Rest, not Quotes, [$\"|Current], Acc);
|
|
||||||
split_keys(<<Char/utf8, Rest/binary>>, Quotes, Current, Acc) ->
|
|
||||||
split_keys(Rest, Quotes, [Char|Current], Acc).
|
|
||||||
Loading…
x
Reference in New Issue
Block a user