<commit_before>-module(agner_mnesia).

-export([start/0, add_movie/3, get_random_movie/0]).

-include("agner_playlist.hrl").

start() ->
  mnesia:create_schema([node()]),
  mnesia:start(),

  ensure_mnesia_running(),
  ensure_mnesia_dir(),

  try
    mnesia:table_info(song, type)
  catch
    exit: _ ->
      {atomic, ok} = mnesia:create_table(song, [{attributes, record_info(fields, song)},
        {disc_copies, [node()]}])
  end.

ensure_mnesia_running() ->
  case mnesia:system_info(is_running) of
    yes ->
      ok;
    starting ->
      timer:sleep(1000),
      ensure_mnesia_running();
    Reason when Reason =:= no; Reason =:= stopping ->
      throw({error, mnesia_not_running})
  end.

ensure_mnesia_dir() ->
  MnesiaDir = mnesia:system_info(directory) ++ "/",
  case filelib:ensure_dir(MnesiaDir) of
    {error, Reason} ->
      throw({error, {cannot_create_mnesia_dir, MnesiaDir, Reason}});
    ok ->
      ok
  end.

add_movie(MovieId, Title, AuthorId) ->
  mnesia:wait_for_tables([song], 1000),

  Fun = fun() ->
    mnesia:write(#song{movie_id = MovieId, title = Title, author_id = AuthorId})
        end,
  {atomic, ok} = mnesia:transaction(Fun).

get_random_movie() ->
  Keys = mnesia:dirty_all_keys(song),
  get_random_movie(Keys).

%%
%%todo do it in efficient way
%%
get_random_movie(Keys) ->
  Key = lists:nth(rand:uniform(length(Keys)), Keys),
  [#song{movie_id = MovieId}] = mnesia:dirty_read({song, Key}),
  {ok, MovieId};
get_random_movie([]) ->
  {error, no_songs}.<commit_msg>Bugfix - wrong pattern matching get_random_movie<commit_after>-module(agner_mnesia).

-export([start/0, add_movie/3, get_random_movie/0]).

-include("agner_playlist.hrl").

start() ->
  mnesia:create_schema([node()]),
  mnesia:start(),

  ensure_mnesia_running(),
  ensure_mnesia_dir(),

  try
    mnesia:table_info(song, type)
  catch
    exit: _ ->
      {atomic, ok} = mnesia:create_table(song, [{attributes, record_info(fields, song)},
        {disc_copies, [node()]}])
  end.

ensure_mnesia_running() ->
  case mnesia:system_info(is_running) of
    yes ->
      ok;
    starting ->
      timer:sleep(1000),
      ensure_mnesia_running();
    Reason when Reason =:= no; Reason =:= stopping ->
      throw({error, mnesia_not_running})
  end.

ensure_mnesia_dir() ->
  MnesiaDir = mnesia:system_info(directory) ++ "/",
  case filelib:ensure_dir(MnesiaDir) of
    {error, Reason} ->
      throw({error, {cannot_create_mnesia_dir, MnesiaDir, Reason}});
    ok ->
      ok
  end.

add_movie(MovieId, Title, AuthorId) ->
  mnesia:wait_for_tables([song], 1000),

  Fun = fun() ->
    mnesia:write(#song{movie_id = MovieId, title = Title, author_id = AuthorId})
        end,
  {atomic, ok} = mnesia:transaction(Fun).

get_random_movie() ->
  Keys = mnesia:dirty_all_keys(song),
  get_random_movie(Keys).

%%
%%todo do it in efficient way
%%
get_random_movie([]) ->
  {error, no_songs};
get_random_movie(Keys) ->
  Key = lists:nth(rand:uniform(length(Keys)), Keys),
  [#song{movie_id = MovieId}] = mnesia:dirty_read({song, Key}),
  {ok, MovieId}.

{'fraction_non_alphanumeric': 0.1624015748031496, 'fraction_numerical': 0.007217847769028871, 'mean_word_length': 3.9820261437908497, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2184748', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1145, 'n_words': 197}