From 3ca84639c392d6966ffd72275c4ea588126e7a3d Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Thu, 19 Mar 2020 23:08:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/arangoApi/agDbMgr.erl | 56 +++++++++++++++++++++++---------------- src/httpCli/agHttpCli.erl | 32 +++++++++++----------- src/httpCli/test.erl | 19 ++++++++++++- 3 files changed, 67 insertions(+), 40 deletions(-) diff --git a/src/arangoApi/agDbMgr.erl b/src/arangoApi/agDbMgr.erl index 27397d3..75aaf6c 100644 --- a/src/arangoApi/agDbMgr.erl +++ b/src/arangoApi/agDbMgr.erl @@ -3,40 +3,50 @@ -compile([export_all, nowarn_export_all]). -%% 请注意,所有数据库管理操作只能通过默认数据库(_system)访问,而不能通过其他数据库访问。 +%% doc_address:https://www.arangodb.com/docs/stable/http/database-database-management.html -%% 检索有关当前数据库的信息 +%% 检索有关当前数据库的信息(别名 /_api/database/properties) %% GET /_api/database/current -curDbInfo(PoolName) -> - agHttpCli:callAgency(PoolName, ?Get, <<"/_api/database/current">>, [], undefined). +curDbInfo(PoolNameOrSocket) -> + agHttpCli:callAgency(PoolNameOrSocket, ?Get, <<"/_api/database/current">>, [], undefined). %% 检索当前用户可以访问的所有数据库的列表 %% GET /_api/database/user -userVisitDbs(PoolName) -> - agHttpCli:callAgency(PoolName, ?Get, <<"/_api/database/user">>, [], undefined, infinity, true). +curVisitDbs(PoolNameOrSocket) -> + agHttpCli:callAgency(PoolNameOrSocket, ?Get, <<"/_api/database/user">>, [], undefined). -%% 创建一个新的数据库 注意:仅可以在_system数据库中创建新数据库。 +%% 检索所有现有数据库的列表 +%% GET /_api/database +curDbList(PoolNameOrSocket) -> + agHttpCli:callAgency(PoolNameOrSocket, ?Get, <<"/_api/database">>, [], undefined). + + +%% 创建一个新的数据库 %% POST /_api/database % 具有以下属性的JSON对象是必需的: % name:必须包含一个有效的数据库名称。 -% users:必须是最初为新数据库创建的用户对象数组。对于已经存在的用户,不会更改用户信息。如果未指定用户或不包含任何用户,则将使用空字符串密码创建默认用户 root。这确保了新数据库在创建后将可访问。每个用户对象可以包含以下属性: -% username:要创建的用户的登录名 -% passwd:用户密码(字符串)。如果未指定,则默认为空字符串。 -% active:一个标志,指示是否应该激活用户帐户。默认值为true。如果设置为false,则用户将无法登录数据库。 -% extra:带有额外用户信息的JSON对象。Extra中包含的数据 将为用户存储,但ArangoDB不会进一步解释 - -newDb(PoolName, Name) -> - NameStr = jiffy:encode(Name), - agHttpCli:callAgency(PoolName, ?Post, <<"/_api/database">>, [], [<<"{\"name\":">>, NameStr, <<"}">>], infinity, true). - -newDb(PoolName, Name, Users) -> - - BodyStr = jiffy:encode(#{<<"name">> => Name, <<"users">> => Users}), - agHttpCli:callAgency(PoolName, ?Post, <<"/_api/database">>, [], BodyStr, infinity, true). +% options:可选对象,可以包含以下属性: +% sharding:用于此数据库中新集合的分片方法。有效值为:“”,“ flexible”或“ single”。前两个是等效的。(仅集群) +% ReplicationFactor:在此数据库中创建的新集合的默认复制因子。 +% 特殊值包括“ satellite”和将其禁用复制的“ satellite”(将集合复制到每个数据库服务器)和“ 1”。(仅集群) +% writeConcern:在此数据库中创建的新集合的默认写关注。 +% 它确定在不同的DBServer上同步每个分片需要多少个副本。如果集群中的副本数量很少,那么分片将拒绝写入。 +% 但是,具有足够最新副本的分片写入将同时成功。writeConcern的值 不能大于ReplicationFactor。(仅集群) +% users:必须是最初为新数据库创建的用户对象数组。对于已经存在的用户,不会更改用户信息。 +% 如果未指定users或不包含任何用户,则将使用空字符串密码创建默认用户 root。这确保了新数据库在创建后将可访问。 +% 每个用户对象可以包含以下属性: +% username:要创建的用户的登录名 +% passwd:用户密码(字符串)。如果未指定,则默认为空字符串。 +% active:一个标志,指示是否应该激活用户帐户。默认值为true。如果设置为false,则用户将无法登录数据库。 +% extra:带有额外用户信息的JSON对象。Extra中包含的数据 将为用户存储,但ArangoDB不会进一步解释。 + +newDb(PoolNameOrSocket, Args) -> + BodyStr = jiffy:encode(Args), + agHttpCli:callAgency(PoolNameOrSocket, ?Post, <<"/_api/database">>, [], BodyStr, true). %% 删除现有数据库 %% DELETE /_api/database/{database-name} -delDb(PoolName, Name) -> +delDb(PoolNameOrSocket, Name) -> Path = <<"/_api/database/", Name/binary>>, - agHttpCli:callAgency(PoolName, ?Delete, Path, [], undefined, infinity, true). + agHttpCli:callAgency(PoolNameOrSocket, ?Delete, Path, [], undefined, true). diff --git a/src/httpCli/agHttpCli.erl b/src/httpCli/agHttpCli.erl index c063cbd..1052553 100644 --- a/src/httpCli/agHttpCli.erl +++ b/src/httpCli/agHttpCli.erl @@ -30,15 +30,15 @@ -spec callAgency(poolNameOrSocket(), method(), path(), headers(), body()) -> term() | {error, term()}. callAgency(PoolNameOrSocket, Method, Path, Headers, Body) -> - callAgency(PoolNameOrSocket, Method, Path, Headers, Body, ?DEFAULT_TIMEOUT, false). + callAgency(PoolNameOrSocket, Method, Path, Headers, Body, false, ?DEFAULT_TIMEOUT). --spec callAgency(poolNameOrSocket(), method(), path(), headers(), body(), timeout()) -> term() | {error, atom()}. -callAgency(PoolNameOrSocket, Method, Path, Headers, Body, TimeOut) -> - callAgency(PoolNameOrSocket, Method, Path, Headers, Body, TimeOut, false). +-spec callAgency(poolNameOrSocket(), method(), path(), headers(), body(), boolean()) -> term() | {error, atom()}. +callAgency(PoolNameOrSocket, Method, Path, Headers, Body, IsSystem) -> + callAgency(PoolNameOrSocket, Method, Path, Headers, Body, IsSystem, ?DEFAULT_TIMEOUT). --spec callAgency(poolNameOrSocket(), method(), path(), headers(), body(), timeout(), boolean()) -> term() | {error, atom()}. -callAgency(PoolNameOrSocket, Method, Path, Headers, Body, Timeout, IsSystem) -> - case castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), Timeout, IsSystem) of +-spec callAgency(poolNameOrSocket(), method(), path(), headers(), body(), boolean(), timeout()) -> term() | {error, atom()}. +callAgency(PoolNameOrSocket, Method, Path, Headers, Body, IsSystem, Timeout) -> + case castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), IsSystem, Timeout) of {ok, RequestId} -> receiveResponse(RequestId); {error, _Reason} = Err -> @@ -49,18 +49,18 @@ callAgency(PoolNameOrSocket, Method, Path, Headers, Body, Timeout, IsSystem) -> -spec castAgency(poolNameOrSocket(), method(), path(), headers(), body()) -> {ok, requestId()} | {error, atom()}. castAgency(PoolNameOrSocket, Method, Path, Headers, Body) -> - castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), ?DEFAULT_TIMEOUT, false). + castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), false, ?DEFAULT_TIMEOUT). --spec castAgency(poolNameOrSocket(), method(), path(), headers(), body(), timeout()) -> {ok, requestId()} | {error, atom()}. -castAgency(PoolNameOrSocket, Method, Path, Headers, Body, Timeout) -> - castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), Timeout, false). +-spec castAgency(poolNameOrSocket(), method(), path(), headers(), body(), boolean()) -> {ok, requestId()} | {error, atom()}. +castAgency(PoolNameOrSocket, Method, Path, Headers, Body, IsSystem) -> + castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), IsSystem, ?DEFAULT_TIMEOUT). --spec castAgency(poolNameOrSocket(), method(), path(), headers(), body(), timeout(), boolean()) -> {ok, requestId()} | {error, atom()}. -castAgency(PoolNameOrSocket, Method, Path, Headers, Body, Timeout, IsSystem) -> - castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), Timeout, IsSystem). +-spec castAgency(poolNameOrSocket(), method(), path(), headers(), body(), boolean(), timeout()) -> {ok, requestId()} | {error, atom()}. +castAgency(PoolNameOrSocket, Method, Path, Headers, Body, IsSystem, Timeout) -> + castAgency(PoolNameOrSocket, Method, Path, Headers, Body, self(), IsSystem, Timeout). --spec castAgency(poolNameOrSocket(), method(), path(), headers(), body(), pid(), timeout(), boolean()) -> {ok, requestId()} | {error, atom()}. -castAgency(PoolNameOrSocket, Method, Path, Headers, Body, Pid, Timeout, IsSystem) -> +-spec castAgency(poolNameOrSocket(), method(), path(), headers(), body(), pid(), boolean(), timeout()) -> {ok, requestId()} | {error, atom()}. +castAgency(PoolNameOrSocket, Method, Path, Headers, Body, Pid, IsSystem, Timeout) -> OverTime = case Timeout of infinity -> infinity; diff --git a/src/httpCli/test.erl b/src/httpCli/test.erl index 25d7a54..24fc92f 100644 --- a/src/httpCli/test.erl +++ b/src/httpCli/test.erl @@ -44,4 +44,21 @@ test(N, Request) -> %% test(N, Request) -> %% erlang:put(cnt, N), %% agHttpCli:callAgency(tt, Request, 5000), -%% test(N - 1, Request). \ No newline at end of file +%% test(N - 1, Request). + + +tcjf(0, Args1) -> + Args = #{name => ffd, tet => "fdsff", <<"dfdf">> => 131245435346}, + jiffy:encode(Args); +tcjf(N, Args1) -> + Args = #{name => ffd, tet => "fdsff", <<"dfdf">> => 131245435346}, + jiffy:encode(Args), + tcjf(N - 1, Args1). + +tcjx(0, Args1) -> + Args = {[{name, ffd}, {tet, "fdsff"}, {<<"dfdf">>, 131245435346}]}, + jiffy:encode(Args); +tcjx(N, Args1) -> + Args = {[{name, ffd}, {tet, "fdsff"}, {<<"dfdf">>, 131245435346}]}, + jiffy:encode(Args), + tcjx(N - 1, Args1).