2.8. Blobs

Blobs are strings that are not automatically loaded into memory. It is a convenient feature to store large pieces of data, such as photos or other media. For each blob, only the position of the data in the joedb file is loaded into memory. The content of a blob can be loaded on demand with the read_blob_data method of the file object.

Here is an example database schema:

create_table person
add_field person name blob
add_field person city string

And the unit test that demonstrates how to use a blob:

#include "db/blob.h"
#include "joedb/journal/Memory_File.h"

#include "gtest/gtest.h"

/////////////////////////////////////////////////////////////////////////////
TEST(Compiler, blob)
/////////////////////////////////////////////////////////////////////////////
{
 joedb::Memory_File file;

 //
 // A blob is created with the write_blob_data function
 //
 {
  blob::Generic_File_Database db(file);
  const auto person = db.new_person();
  const joedb::Blob name_blob = db.write_blob_data("Jacques");
  db.set_name(person, name_blob);
  EXPECT_EQ("Jacques", db.read_blob_data(name_blob));
  db.set_city(person, "Paris");
  db.checkpoint();
 }

 //
 // The content of a blob is not kept in RAM. In order to access it,
 // it must be explicitly read from the file.
 //
 {
  blob::Readonly_Database db(file);

  const auto person = db.get_person_table().first();
  EXPECT_EQ("Paris", db.get_city(person));
  const joedb::Blob name_blob = db.get_name(person);
  EXPECT_EQ("Jacques", file.read_blob_data(name_blob));
 }
}

In order to get better performance when manipulating a large amount of blob data, blobs can be stored in a separate file. This way, non-blob data will be stored contiguously, and can be read much faster.