load_table

Function: load_table

The load_table function is designed to load all records stored in a specific table file into memory. This allows subsequent operations to work on the table's data effectively without directly accessing the .table file repeatedly.


Declaration

operation load_table(std::string table);

Parameters

  • table (std::string) The name of the table to be loaded. The function looks for a file named <table>.table in the database directory.

Return Value

  • operation An object containing:

    • stat: Indicates whether the operation was SUCCESS or FAILED.

    • error: A message describing the error in case the operation fails.

Functionality

  1. Checks if the Database is Open The function validates whether the database is currently open. If not, it returns a failure status with the error message "Database not open".

  2. Validates Table Existence It checks if the specified table file (<table>.table) exists in the database directory. If not, the operation fails with the error message "Table does not exist".

  3. Parses the Table's Schema The function retrieves the schema (column names and their data types) of the table by reading its header using get_table_content_header.

  4. Reads and Loads Records The function processes the table file to identify and parse each record. Records are enclosed within square brackets ([ ... ]), and individual fields are extracted based on the schema.

  5. Decompresses Strings For columns of type STRING, the function decompresses the stored values to retrieve their original form.

  6. Stores Records in Memory Each parsed record is added to the in-memory representation of the table (std::vector<std::unordered_map<std::string, std::any>>).

Example

Here’s how to use the load_table function:

#include "qic.h"
#include <iostream>
#include <unordered_map>

int main() {
  data_base db;
  db.open_database("mydatabase.db");
  db.load_table("COMPANY"); // loads the table in memory

  auto table =
      db.get_values_from_table("COMPANY"); // if we run this function without
                                           // the load table function before it
                                           // wont have all the tables in memory
  table->clear(); // by clearing this list and saving the db we can clear the
                  // table

  db.save();
  std::cin >> new char;
  db.close();
  return 0;
}

Last updated