<commit_before>%% @author mmartin
%% @doc Handle CDMI container objects.

-module(nebula2_containers).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-include("nebula.hrl").

%% ====================================================================
%% API functions
%% ====================================================================
-export([
            new_container/2,
            update_container/3
        ]).

%% @doc Create a new CDMI container
-spec nebula2_containers:new_container(cowboy_req:req(), cdmi_state()) -> {boolean(), Req, cdmi_state()}
        when Req::cowboy_req:req().
new_container(Req, State) ->
    lager:debug("Entry"),
    ObjectType = ?CONTENT_TYPE_CDMI_CONTAINER,
    Response = case nebula2_utils:create_object(Req, State, ObjectType) of
                   {true, Req2, Data} ->
                       {true, cowboy_req:set_resp_body(jsx:encode(maps:to_list(Data)), Req2), State};
                   false ->
                       {false, Req, State}
               end,
    Response.

%% @doc Update a CDMI container
-spec nebula2_containers:update_container(cowboy_req:req(), pid(), object_oid()) -> {ok, json_value()}.
update_container(Req, State, Oid) ->
    lager:debug("Entry"),
    {Pid, _} = State,
    {ok, Body, Req2} = cowboy_req:body(Req),
    NewData = jsx:decode(Body, [return_maps]),
    {ok, OldData} = nebula2_db:read(Pid, Oid),
    OldMetaData = maps:get(<<"metadata">>, OldData, maps:new()),
    NewMetaData = maps:get(<<"metadata">>, NewData, maps:new()),
    MetaData = maps:merge(OldMetaData, NewMetaData),
    Data = maps:merge(OldData, NewData),
    Data2 = maps:put(<<"metadata">>, MetaData, Data),
    Response = case nebula2_db:update(Pid, Oid, Data2) of<commit_msg>Update mtime on container update.<commit_after>%% @author mmartin
%% @doc Handle CDMI container objects.

-module(nebula2_containers).

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.

-include("nebula.hrl").

%% ====================================================================
%% API functions
%% ====================================================================
-export([
            new_container/2,
            update_container/3
        ]).

%% @doc Create a new CDMI container
-spec nebula2_containers:new_container(cowboy_req:req(), cdmi_state()) -> {boolean(), Req, cdmi_state()}
        when Req::cowboy_req:req().
new_container(Req, State) ->
    lager:debug("Entry"),
    ObjectType = ?CONTENT_TYPE_CDMI_CONTAINER,
    Response = case nebula2_utils:create_object(Req, State, ObjectType) of
                   {true, Req2, Data} ->
                       {true, cowboy_req:set_resp_body(jsx:encode(maps:to_list(Data)), Req2), State};
                   false ->
                       {false, Req, State}
               end,
    Response.

%% @doc Update a CDMI container
-spec nebula2_containers:update_container(cowboy_req:req(), pid(), object_oid()) -> {ok, json_value()}.
update_container(Req, State, Oid) ->
    lager:debug("Entry"),
    {Pid, _} = State,
    {ok, Body, Req2} = cowboy_req:body(Req),
    NewData = jsx:decode(Body, [return_maps]),
    {ok, OldData} = nebula2_db:read(Pid, Oid),
    OldMetaData = maps:get(<<"metadata">>, OldData, maps:new()),
    NewMetaData = maps:get(<<"metadata">>, NewData, maps:new()),
    MetaData = maps:merge(OldMetaData, NewMetaData),
    Data = maps:merge(OldData, NewData),
    Data2 = maps:put(<<"metadata">>, MetaData, Data),
    CList = [<<"cdmi_atime">>,
             <<"cdmi_mtime">>],
    Data3 = nebula2_utils:update_data_system_metadata(CList, Data2, State),
    Response = case nebula2_db:update(Pid, Oid, Data3) of
{'fraction_non_alphanumeric': 0.2231042978373939, 'fraction_numerical': 0.007938680536545305, 'mean_word_length': 3.7087628865979383, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3071529', 'n_tokens_mistral': 1211, 'n_tokens_neox': 1183, 'n_words': 226}