<commit_before>%%======================================================================
%%
%% Leo Object Storage
%%
%% Copyright (c) 2012 Rakuten, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% ---------------------------------------------------------------------
%% Leo Object Storage - API
%% @doc
%% @end
%%======================================================================
-module(leo_object_storage_api).

-author('<NAME>').

-include("leo_object_storage.hrl").

-export([start/2,
         put/2, get/1, get/3, delete/2, head/1,
         fetch_by_addr_id/2, fetch_by_key/2,
         compact/0, stats/0,
         add_container/1, remove_container/1
        ]).


-define(ETS_CONTAINERS_TABLE, 'leo_object_storage_containers').
-define(ETS_INFO_TABLE,       'leo_object_storage_info').
-define(SERVER_MODULE,        'leo_object_storage_server').


%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
%% @doc Create object-storage processes
%%
-spec(start(list(), string()) ->
             ok | {error, any()}).
start([], []) ->
    {error, badarg};
start(_, []) ->
    {error, badarg};
start([], _) ->
    {error, badarg};

start(NumOfContainers, Path0) ->
    start_app(),

    Path1     = get_path(Path0),
    Storage1  = get_object_storage_mod(),
    Metadata1 = get_metadata_db(),
    true = ets:insert(?ETS_INFO_TABLE, {?MODULE, [{num_of_containers, NumOfContainers},
                                                  {path, Path1},
                                                  {storage_mod, Storage1},
                                                  {metadata_db, Metadata1}]}),

    %% Generate process of object-storage-containers.
    %%
    lists:foreach(fun(I) ->
                          add_container(I-1)
                  end, lists:seq(1, NumOfContainers)),

    %% Launch a supervisor.
    %%
    case whereis(leo_object_storage_sup) of
        undefined ->
            {error, "NOT started supervisor"};
        SupRef ->
            case supervisor:count_children(SupRef) of
                [{specs, _},{active, Active},
                 {supervisors, _},{workers, Workers}] when Active == Workers  ->
                    ok;
                _ ->
                    {error, "Could NOT launch worker processes"}
            end
    end.


%% @doc Insert an object into the object-storage
%% @param Key = {$VNODE_ID, $OBJ_KEY}<commit_msg>Fix a bug in api<commit_after>%%======================================================================
%%
%% Leo Object Storage
%%
%% Copyright (c) 2012 Rakuten, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License.  You may obtain
%% a copy of the License at
%%
%%   http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied.  See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% ---------------------------------------------------------------------
%% Leo Object Storage - API
%% @doc
%% @end
%%======================================================================
-module(leo_object_storage_api).

-author('<NAME>').

-include("leo_object_storage.hrl").

-export([start/2,
         put/2, get/1, get/3, delete/2, head/1,
         fetch_by_addr_id/2, fetch_by_key/2,
         compact/0, stats/0,
         add_container/1, remove_container/1
        ]).


-define(ETS_CONTAINERS_TABLE, 'leo_object_storage_containers').
-define(ETS_INFO_TABLE,       'leo_object_storage_info').
-define(SERVER_MODULE,        'leo_object_storage_server').


%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
%% @doc Create object-storage processes
%%
-spec(start(list(), string()) ->
             ok | {error, any()}).
start([], []) ->
    {error, badarg};
start(_, []) ->
    {error, badarg};
start([], _) ->
    {error, badarg};

start(NumOfContainers, Path0) ->
    Res = start_app(),
    start(Res, NumOfContainers, Path0).

start(ok, NumOfContainers, Path0) ->
    Path1     = get_path(Path0),
    Storage1  = get_object_storage_mod(),
    Metadata1 = get_metadata_db(),
    true = ets:insert(?ETS_INFO_TABLE, {?MODULE, [{num_of_containers, NumOfContainers},
                                                  {path, Path1},
                                                  {storage_mod, Storage1},
                                                  {metadata_db, Metadata1}]}),

    %% Generate process of object-storage-containers.
    %%
    lists:foreach(fun(I) ->
                          add_container(I-1)
                  end, lists:seq(1, NumOfContainers)),

    %% Launch a supervisor.
    %%
    case whereis(leo_object_storage_sup) of
        undefined ->
            {error, "NOT started supervisor"};
        SupRef ->
            case supervisor:count_children(SupRef) of
                [{specs, _},{active, Active},
                 {supervisors, _},{workers, Workers}] when Active == Workers  ->
                    ok;
                _ ->
                    {error, "Could NOT launch worker processes"}
            end
    end;

start({error, Cause},_NumOfContainers,_Path) ->
    {error, Cause}.




%% @doc Insert an object into the object-storage
%% @param Key = {$VNODE_ID, $OBJ_KEY}
{'fraction_non_alphanumeric': 0.22960090483115203, 'fraction_numerical': 0.010017773469058007, 'mean_word_length': 1.9773929773929775, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '753264', 'n_tokens_mistral': 1705, 'n_tokens_neox': 1612, 'n_words': 436}