Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
index_tutorial.cpp
Go to the documentation of this file.
2
3#include <iostream>
4
5/////////////////////////////////////////////////////////////////////////////
6int main()
7/////////////////////////////////////////////////////////////////////////////
8{
11
12 //
13 // Insert some cities
14 //
15 db.new_city("Paris");
16 db.new_city("Tokyo");
17 db.new_city("Chicago");
18
19 try
20 {
21 std::cout << "Trying to insert another Paris:\n";
22 db.new_city("Paris");
23 }
24 catch(const std::runtime_error &e)
25 {
26 std::cout << "Exception: " << e.what() << '\n';
27 }
28
29 //
30 // Finding a city by name
31 //
32 const auto Paris = db.find_city_by_name("Paris");
33 const auto Tokyo = db.find_city_by_name("Tokyo");
34 if (db.find_city_by_name("Monte Carlo").is_null())
35 std::cout << "\nMonte Carlo is not in the database\n";
36
37 //
38 // Inserting persons
39 //
40 db.new_person("John", "Smith", Paris);
41 db.new_person("John", "Smith", Tokyo);
42 db.new_person("Hiroshi", "Yamada", Tokyo);
43 db.new_person("René", "Dubois", Tokyo);
44 db.new_person("Hélène", "Dubois", db.null_city());
45 db.new_person("Daniel", "Dubois", db.null_city());
46 db.new_person("Laurent", "Dubois", db.null_city());
47 db.new_person("Albert", "Camus", Paris);
48
49 //
50 // Finding persons with the index
51 //
52 std::cout << "\nFinding all the John Smiths:\n";
53 for (const auto person: db.find_person_by_name("Smith", "John"))
54 {
55 std::cout << db.get_first_name(person) << ' ';
56 std::cout << db.get_last_name(person) << ", ";
57 std::cout << db.get_name(db.get_home(person)) << '\n';
58 }
59
60 //
61 // Using the index to sort persons
62 //
63 std::cout << "\nSorted list of persons:\n";
64 for (const auto &[name, person]: db.get_index_of_person_by_name())
65 {
66 const auto &[last, first] = name;
67 std::cout << last << ", " << first << '\n';
68 }
69
70 db.checkpoint();
71
72 return 0;
73}
const std::string & get_last_name(id_of_person record) const
Definition Database.h:755
const std::string & get_first_name(id_of_person record) const
Definition Database.h:749
range_of_person_by_name find_person_by_name(const std::string &field_value_of_last_name, const std::string &field_value_of_first_name) const
Definition Database.h:953
id_of_city find_city_by_name(const std::string &field_value_of_name) const
Definition Database.h:796
id_of_city get_home(id_of_person record) const
Definition Database.h:761
const std::string & get_name(id_of_city record) const
Definition Database.h:717
static id_of_city null_city()
Definition Database.h:712
const std::multimap< std::tuple< std::string, std::string >, id_of_person > & get_index_of_person_by_name()
Definition Database.h:772
A writable Database constructed from a writable joedb::Buffered_File.
constexpr bool is_null() const
Definition ids.h:33
int main()