objectbox / objectbox-c

C and C++ database for objects and structs
https://objectbox.io/
Apache License 2.0
211 stars 37 forks source link

Better docs #25

Open paulocoutinhox opened 2 years ago

paulocoutinhox commented 2 years ago

Hi,

I think that you can improve the docs with these topics:

  1. More complete and complex examples
  2. Relation one-to-many
  3. Relation one-to-one
  4. Storing date/time
  5. Storing float/double
  6. More complex queries (the examples in docs are too simple)
  7. About indexes (if exists)
  8. Full text-search
  9. Queries with date/time
  10. Date/time are stored as UTC timestamp?
  11. How to work with Multi-thread?
  12. Recovery after corruption file (it exists?)

Thanks.

rmszc81 commented 2 years ago

hello @paulocoutinhox

did you figured it out how to use any of the items above? I just discovered object-box so, I'm trying to find it out yet the best practices. for C# and .NET, I'm using LiteDB, from @mbdavid

(btw, também sou BR).

paulocoutinhox commented 2 years ago

Hi,

Not yet :(

No answer until now.

BLACKHXTM0NER0 commented 1 year ago
paulocoutinhox commented 4 months ago

Hi @rmszc81,

I asked chatgpt 4.5 about it and it generate one example for each item. Only need someone to check the information if is correct or try compile it:

ObjectBox C++ Examples

More complete and complex examples

1. Relation One-to-Many

#include "objectbox.hpp"
#include <vector>

class Author {
public:
    int64_t id;
    std::string name;
    OBX_ENTITY_ID(Author)

    OBX_RELATION_ONE_TO_MANY(Book, books)
};

class Book {
public:
    int64_t id;
    std::string title;
    OBX_ENTITY_ID(Book)

    OBX_RELATION_ONE_TO_MANY(Author, author)
};

void createAuthorWithBooks() {
    OBX_store* store = createStore();

    Author author;
    author.name = "J.K. Rowling";

    Book book1;
    book1.title = "Harry Potter and the Philosopher's Stone";

    Book book2;
    book2.title = "Harry Potter and the Chamber of Secrets";

    author.books.push_back(book1);
    author.books.push_back(book2);

    auto authorBox = obx::Box<Author>(store);
    authorBox.put(author);

    closeStore(store);
}

2. Relation One-to-One

#include "objectbox.hpp"

class Address {
public:
    int64_t id;
    std::string city;
    OBX_ENTITY_ID(Address)
};

class Person {
public:
    int64_t id;
    std::string name;
    OBX_ENTITY_ID(Person)

    OBX_RELATION_ONE_TO_ONE(Address, address)
};

void createPersonWithAddress() {
    OBX_store* store = createStore();

    Address address;
    address.city = "New York";

    Person person;
    person.name = "John Doe";
    person.address = address;

    auto personBox = obx::Box<Person>(store);
    personBox.put(person);

    closeStore(store);
}

3. Storing Date/Time

#include "objectbox.hpp"
#include <chrono>

class Event {
public:
    int64_t id;
    std::string name;
    std::chrono::system_clock::time_point date;
    OBX_ENTITY_ID(Event)
};

void createEvent() {
    OBX_store* store = createStore();

    Event event;
    event.name = "Conference";
    event.date = std::chrono::system_clock::now();

    auto eventBox = obx::Box<Event>(store);
    eventBox.put(event);

    closeStore(store);
}

4. Storing Float/Double

#include "objectbox.hpp"

class Measurement {
public:
    int64_t id;
    float temperature;
    double pressure;
    OBX_ENTITY_ID(Measurement)
};

void createMeasurement() {
    OBX_store* store = createStore();

    Measurement measurement;
    measurement.temperature = 23.5f;
    measurement.pressure = 1013.25;

    auto measurementBox = obx::Box<Measurement>(store);
    measurementBox.put(measurement);

    closeStore(store);
}

5. More Complex Queries

#include "objectbox.hpp"

class Product {
public:
    int64_t id;
    std::string name;
    float price;
    OBX_ENTITY_ID(Product)
};

void queryProducts() {
    OBX_store* store = createStore();
    auto productBox = obx::Box<Product>(store);

    // Find products with price between 10 and 50
    auto products = productBox.query().between(Product_::price, 10.0f, 50.0f).build().find();

    for (const auto& product : products) {
        std::cout << "Product: " << product.name << ", Price: " << product.price << std::endl;
    }

    closeStore(store);
}

6. Indexes

#include "objectbox.hpp"

class Customer {
public:
    int64_t id;
    std::string email;
    OBX_ENTITY_ID(Customer)
    OBX_PROPERTY(email, IndexType::value)
};

void createCustomer() {
    OBX_store* store = createStore();

    Customer customer;
    customer.email = "customer@example.com";

    auto customerBox = obx::Box<Customer>(store);
    customerBox.put(customer);

    closeStore(store);
}

7. Full-Text Search

#include "objectbox.hpp"

class Article {
public:
    int64_t id;
    std::string content;
    OBX_ENTITY_ID(Article)
    OBX_PROPERTY(content, IndexType::value, OBX_PROPERTY_FLAG_FULLTEXT)
};

void searchArticles() {
    OBX_store* store = createStore();
    auto articleBox = obx::Box<Article>(store);

    // Full-text search for "objectbox"
    auto articles = articleBox.query().contains(Article_::content, "objectbox").build().find();

    for (const auto& article : articles) {
        std::cout << "Article: " << article.content << std::endl;
    }

    closeStore(store);
}

8. Queries with Date/Time

#include "objectbox.hpp"
#include <chrono>

class LogEntry {
public:
    int64_t id;
    std::string message;
    std::chrono::system_clock::time_point timestamp;
    OBX_ENTITY_ID(LogEntry)
};

void queryLogEntries() {
    OBX_store* store = createStore();
    auto logBox = obx::Box<LogEntry>(store);

    // Find logs within the last 24 hours
    auto now = std::chrono::system_clock::now();
    auto one_day_ago = now - std::chrono::hours(24);

    auto logs = logBox.query().greater(LogEntry_::timestamp, one_day_ago).build().find();

    for (const auto& log : logs) {
        std::cout << "Log: " << log.message << std::endl;
    }

    closeStore(store);
}

9. Date/Time are Stored as UTC Timestamp?

Yes, ObjectBox stores date/time as UTC timestamps.

10. Working with Multi-thread

#include "objectbox.hpp"
#include <thread>

void writeData(obx::Box<MyEntity>& box) {
    MyEntity entity;
    entity.name = "Test";
    box.put(entity);
}

void multiThreadExample() {
    OBX_store* store = createStore();
    auto box = obx::Box<MyEntity>(store);

    std::thread t1(writeData, std::ref(box));
    std::thread t2(writeData, std::ref(box));

    t1.join();
    t2.join();

    closeStore(store);
}

11. Recovery After File Corruption (if it exists)

ObjectBox does not have built-in recovery for corrupted files, but you can implement periodic backups and use those for recovery.

#include "objectbox.hpp"
#include <filesystem>
#include <iostream>

void backupDatabase() {
    std::filesystem::copy("data.mdb", "data_backup.mdb", std::filesystem::copy_options::overwrite_existing);
}

void restoreDatabase() {
    std::filesystem::copy("data_backup.mdb", "data.mdb", std::filesystem::copy_options::overwrite_existing);
}

void exampleUsage() {
    try {
        OBX_store* store = createStore();
        // Perform operations
        closeStore(store);
    } catch (...) {
        std::cerr << "Database error occurred. Restoring from backup." << std::endl;
        restoreDatabase();
    }
}

Can you check?