diff --git a/src/database.c b/src/database.c index ced3cbe..cc9bc7f 100644 --- a/src/database.c +++ b/src/database.c @@ -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); @@ -96,6 +101,7 @@ 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); } @@ -103,15 +109,25 @@ static void load_movies( 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); } @@ -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); } @@ -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, @@ -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); } @@ -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; diff --git a/src/hash.c b/src/hash.c index bc5b43d..88b6a9d 100644 --- a/src/hash.c +++ b/src/hash.c @@ -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; diff --git a/src/id.c b/src/id.c index 4fa9c27..50bd67b 100644 --- a/src/id.c +++ b/src/id.c @@ -12,6 +12,7 @@ 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++; @@ -19,6 +20,7 @@ moviedb_id_t moviedb_id_parse( 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; @@ -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; @@ -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--;