<commit_before>-module(rinamo_api).

-export([create_table/1, put_item/1, get_item/1, query/1]).

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

create_table(DynamoRequest) ->
  % Parse Request
  [ {_, Table}, {_, Fields}, {_, KeySchema}, {_, LSI},
    {_, ProvisionedThroughput}, {_, RawSchema} ] = rinamo_codec:decode_create_table(DynamoRequest),
   
  % Creation Time
  {MegaSecs, Secs, MicroSecs} = now(),
  CreationTime = (MegaSecs * 1000000 + Secs) + MicroSecs / 1000000,

  % Put things into Riak
  _ = rinamo_rj:create_table(Table, Fields, KeySchema, LSI, ProvisionedThroughput, RawSchema),

  % Enrich Response as needed
  Response = [{ <<"TableDescription">>, [
    {<<"TableName">>, Table},
    {<<"CreationTime">>, CreationTime},
    {<<"AttributeDefinitions">>, [Fields]},
    {<<"KeySchema">>, [KeySchema]}
  ]}],

  % JSONify the Response
  rinamo_codec:encode_create_table_response(Response).

put_item(DynamoRequest) ->
  Request = rinamo_codec:decode_put_itme(DynamoRequest),
  Response = rinamo_rj:create_table(Request),
  rinamo_codec:encode_put_item_response(Response).

get_item(DynamoRequest) ->
  Request = rinamo_codec:decode_get_item(DynamoRequest),
  Response = rinamo_rj:cput_item(Request),
  rinamo_codec:encode_get_item_response(Response).

query(DynamoRequest) ->
  Request = rinamo_codec:decode_get_item(DynamoRequest),
  Response = rinamo_rj:query_item(Request),
  rinamo_codec:encode_query_response(Response).

-ifdef(TEST).

create_table_test() -><commit_msg>add more to rinamo create table response<commit_after>-module(rinamo_api).

-export([create_table/1, put_item/1, get_item/1, query/1]).

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

create_table(DynamoRequest) ->
  % Parse Request
  [ {_, Table}, {_, Fields}, {_, KeySchema}, {_, LSI},
    {_, ProvisionedThroughput}, {_, RawSchema} ] = rinamo_codec:decode_create_table(DynamoRequest),
   
  % Creation Time
  {MegaSecs, Secs, MicroSecs} = now(),
  CreationTime = (MegaSecs * 1000000 + Secs) + MicroSecs / 1000000,

  % Put things into Riak
  _ = rinamo_rj:create_table(Table, Fields, KeySchema, LSI, ProvisionedThroughput, RawSchema),

  % Enrich Response as needed
  Response = [{ <<"TableDescription">>, [
    {<<"TableName">>, Table},
    {<<"AttributeDefinitions">>, [Fields]},
    {<<"KeySchema">>, [KeySchema]},
    {<<"ProvisionedThroughput">>, ProvisionedThroughput},
    {<<"LocalSecondaryIndexes">>, [{}]},
    {<<"GlobalSecondaryIndexes">>, [{}]},
    {<<"TableSizeBytes">>, 0},
    {<<"TableStatus">>, <<"'CREATING'">>},
    {<<"CreationDateTime">>, CreationTime}
  ]}],

  % JSONify the Response
  rinamo_codec:encode_create_table_response(Response).

put_item(DynamoRequest) ->
  Request = rinamo_codec:decode_put_itme(DynamoRequest),
  Response = rinamo_rj:create_table(Request),
  rinamo_codec:encode_put_item_response(Response).

get_item(DynamoRequest) ->
  Request = rinamo_codec:decode_get_item(DynamoRequest),
  Response = rinamo_rj:cput_item(Request),
  rinamo_codec:encode_get_item_response(Response).

query(DynamoRequest) ->
  Request = rinamo_codec:decode_get_item(DynamoRequest),
  Response = rinamo_rj:query_item(Request),
  rinamo_codec:encode_query_response(Response).

-ifdef(TEST).

create_table_test() ->
{'fraction_non_alphanumeric': 0.15728274173806608, 'fraction_numerical': 0.011321909424724603, 'mean_word_length': 5.853249475890985, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 45, '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': '4967266', 'n_tokens_mistral': 1246, 'n_tokens_neox': 1171, 'n_words': 170}