Skip to content

Commit

Permalink
feat: use dbname instead of catalog and schema
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed May 5, 2023
1 parent e3c5bf3 commit 05bb76b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ A proper list contains:
* `auth`: authentication options, `{auth, {basic, #{username => <<"greptime_user">>, password => <<"greptime_pwd">>}}}` for example.

### Write and datatypes
The metric name can be a string or binary. If you want to set the database, the metric name can be set in the form of `{dbname, metric}`. The data will be written into `greptime-public` by default.

Write each row by `greptimedb:write/3` function. Every row contains:

Expand Down
23 changes: 7 additions & 16 deletions src/greptimedb.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,20 @@ start_client(Options0) ->
{error, Reason}
end.

write(#{protocol := Protocol} = Client, Metric, Points) ->
write(Client, Metric, Points) ->
try
case Protocol of
http ->
Request = greptimedb_encoder:insert_request(Client, Metric, Points),
rpc_call(Client, Request)
end
Request = greptimedb_encoder:insert_request(Client, Metric, Points),
rpc_call(Client, Request)
catch
E:R:S ->
logger:error("[GreptimeDB] write ~0p failed: ~0p ~0p ~0p ~p",
[Metric, Points, E, R, S]),
{error, R}
end.

write_stream(#{protocol := Protocol} = Client) ->
write_stream(Client) ->
try
case Protocol of
http ->
rpc_write_stream(Client)
end
rpc_write_stream(Client)
catch
E:R:S ->
logger:error("[GreptimeDB] create write stream failed: ~0p ~0p ~p", [E, R, S]),
Expand All @@ -67,11 +61,8 @@ ddl(_Client) ->
todo.

-spec stop_client(Client :: map()) -> ok | term().
stop_client(#{pool := Pool, protocol := Protocol}) ->
case Protocol of
http ->
ecpool:stop_sup_pool(Pool)
end.
stop_client(#{pool := Pool}) ->
ecpool:stop_sup_pool(Pool).

%%%===================================================================
%%% Internal functions
Expand Down
12 changes: 5 additions & 7 deletions src/greptimedb_encoder.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,19 @@
-export([insert_request/3]).

-define(TS_COLUMN, <<"greptime_timestamp">>).
-define(DEFAULT_CATALOG, "greptime").
-define(DEFAULT_SCHEMA, "public").
-define(DEFAULT_DBNAME, "greptime-public").

insert_request(#{cli_opts := Options} = _Client, {Catalog, Schema, Table}, Points) ->
insert_request(#{cli_opts := Options} = _Client, {DbName, Table}, Points) ->
RowCount = length(Points),
Columns =
lists:map(fun(Column) -> pad_null_mask(Column, RowCount) end, collect_columns(Points)),
AuthHeader = proplists:get_value(auth, Options, {}),
Header =
case AuthHeader of
{} ->
#{catalog => Catalog, schema => Schema};
#{dbname => DbName};
Scheme ->
#{catalog => Catalog,
schema => Schema,
#{dbname => DbName,
authorization => #{auth_scheme => Scheme}}
end,

Expand All @@ -42,7 +40,7 @@ insert_request(#{cli_opts := Options} = _Client, {Catalog, Schema, Table}, Point
row_count => RowCount}},
#{header => Header, request => Request};
insert_request(Client, Table, Points) ->
insert_request(Client, {?DEFAULT_CATALOG, ?DEFAULT_SCHEMA, Table}, Points).
insert_request(Client, {?DEFAULT_DBNAME, Table}, Points).

%%%===================================================================
%%% Internal functions
Expand Down
6 changes: 2 additions & 4 deletions test/greptimedb_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ t_collect_columns(_) ->
Request = greptimedb_encoder:insert_request(Client, Metric, Points),
case Request of
#{header :=
#{catalog := Catalog,
schema := Schema,
#{dbname := DbName,
authorization := Auth},
request := {insert, #{columns := Columns}}} ->
?assertEqual(Catalog, "greptime"),
?assertEqual(Schema, "public"),
?assertEqual(DbName, "greptime-public"),
?assertEqual(8, length(Columns)),
?assertEqual(Auth, #{auth_scheme => AuthInfo}),

Expand Down

0 comments on commit 05bb76b

Please sign in to comment.