Skip to content

Commit

Permalink
more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoczim committed Nov 12, 2020
1 parent 100a408 commit 9e844fd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,25 @@ void database_load(
char *file_buf;

trie_root_init(&database_out->trie_root);
/* Initializes movies to capacity 2003. */
movies_init(&database_out->movies, 2003, error);

if (error->code == error_none) {
/* Initializes users to capacity 2003. */
users_init(&database_out->users, 2003, error);
}

if (error->code == error_none) {
/* Initializes tags to capacity 2003. */
tags_init(&database_out->tags, 2003, error);
}

if (error->code == error_none) {
/* Allocates the buffer for file buffering. */
file_buf = moviedb_alloc(IO_BUF_SIZE, error);
}

/* Actually loads everything, if no error. */
if (error->code == error_none) {
load_movies(database_out, buf, file_buf, error);

Expand Down Expand Up @@ -96,22 +101,33 @@ static void load_movies(
input_file_setbuf(file, file_buf, IO_BUF_SIZE, error);

if (error->code == error_none) {
/* Initializes the CSV parser. */
movie_parser_init(&parser, file, buf, error);
}

has_data = error->code == error_none;
while (has_data) {
has_data = movie_row_parse(&parser, buf, &row, error);
if (has_data) {
/* Inserts into the trie. */
trie_insert(&database->trie_root, row.title, row.id, error);

if (error->code == error_dup_movie_title) {
/* Ignore duplicated movie title error. */
error_set_code(error, error_none);
}

if (error->code == error_none) {
/* Inserts into the movie table. */
movies_insert(&database->movies, &row, error);
}
if (error->code != error_none) {
/*
* Destroys the row memory if an error happens.
*
* Normally, movie table would get the ownership of the
* heap-allocated fields of the row.
*/
movie_row_destroy(&row);
}

Expand Down Expand Up @@ -147,6 +163,7 @@ static void load_ratings(
input_file_setbuf(file, file_buf, IO_BUF_SIZE, error);

if (error->code == error_none) {
/* Initializes the CSV parser. */
rating_parser_init(&parser, file, buf, error);
}

Expand All @@ -155,9 +172,11 @@ static void load_ratings(
has_data = rating_row_parse(&parser, buf, &row, error);

if (has_data) {
/* Inserts into the user table. */
users_insert_rating(&database->users, &row, error);

if (error->code == error_none) {
/* Adds this rating to the respective movie. */
movies_add_rating(
&database->movies,
row.movieid,
Expand Down Expand Up @@ -195,6 +214,7 @@ static void load_tags(
input_file_setbuf(file, file_buf, IO_BUF_SIZE, error);

if (error->code == error_none) {
/* Initializes the CSV parser. */
tag_parser_init(&parser, file, buf, error);
}

Expand All @@ -203,8 +223,10 @@ static void load_tags(
has_data = tag_row_parse(&parser, buf, &row, error);

if (has_data) {
/* Inserts into the tag table. */
tags_insert(&database->tags, &row, error);
if (error->code == error_dup_movie_id) {
/* Ignore duplicated movie ID error. */
error_set_code(error, error_none);
}
has_data = error->code == error_none;
Expand Down
5 changes: 5 additions & 0 deletions src/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ moviedb_hash_t moviedb_hash_str(char const *restrict string)

while (string[index] != 0) {
curr = (unsigned char) string[index];
/* Multiplication of previous state by large prime. */
hash *= 0xC3FB39E53E91D33F;
/* XOR with multiplication of current element by prime. */
hash ^= curr * 0x1A3C2A17EE36142B;
/* Marks the index. */
hash += index;
index++;
}

/* Marks the key length. */
hash ^= moviedb_hash_uint64(index);

return hash;
Expand Down
4 changes: 4 additions & 0 deletions src/id.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ moviedb_id_t moviedb_id_parse(

while (string[i] != 0 && error->code == error_none) {
if (id * 10 >= id && string[i] >= '0' && string[i] <= '9') {
/* Accounts the digit. */
id *= 10;
id += string[i] - '0';
i++;
} else {
if (error->code == error_none) {
error_string = moviedb_alloc(strlen(string) + 1, error);
if (error->code == error_none) {
/* Copies the input string to an error. */
strcpy(error_string, string);
error_set_code(error, error_id);
error->data.id.has_line = false;
Expand All @@ -29,6 +31,7 @@ moviedb_id_t moviedb_id_parse(
}
}

/* Empty string is an error. */
if (i == 0 && error->code == error_none) {
error_set_code(error, error_id);
error->data.id.has_line = false;
Expand All @@ -50,6 +53,7 @@ size_t moviedb_id_to_str(
start--;
buffer[start] = 0;

/* Write the number digits starting backwards. */
do {
if (start > 0) {
start--;
Expand Down

0 comments on commit 9e844fd

Please sign in to comment.