RavenDB C++ Client: Laying the Ground Work
Let's look at the philosophy behind RavenDB's C++ client, the challenges working with C++ brings, and how the RavenDB team is working around them.
Join the DZone community and get the full member experience.
Join For FreeThe core concept underlying the RavenDB client API is the notion of Units of Work. This provides core features such as change tracking and identity map. In all our previous clients, that was pretty easy to deal with because the GC solved memory ownership and reflection gave us a lot of stuff basically for free.
Right now, what I want to achieve is the following:
struct User {
std::string name;
int age;
User(std::string n): name(n) {}
};
int main() {
auto user = std::make_shared <User> ("oren");
Session session;
session.store(user);
user->age = 2;
session.save_changes();
}
It seems pretty simple, right? Because both the session and the caller code are going to share ownership on the passed User. Notice that we modify the user after we call store() but before save_changes(). We expect to see the modification in the document that is being generated.
The memory ownership is handled here by using shared_ptr as the underlying mode in which we accept and return data to the session. Now, let’s see how we actually deal with serialization, shall we? I have chosen to use nlohmann’s json for the project, which means that the provided API is quite nice. As a consumer, you’ll need to write your JSON serialization code, but it is fairly obvious how to do so, check this out:
struct Address {
std::string line1, city, state;
};
struct User {
std::string name;
int age;
Address address;
User(std::string n): name(n) {}
};
void from_json(const json& j, Address& p) {
j.at("line1").get_to(p.line1);
j.at("city").get_to(p.city);
j.at("state").get_to(p.state);
}
void from_json(const json & j, User & p) {
j.at("name").get_to(p.name);
j.at("age").get_to(p.age);
j.at("address").get_to(p.address);
}
void to_json(json& j,
const Address& p) {
j = json {
{
"line1",
p.line1
}, {
"city",
p.city
}, {
"state",
p.state
}
};
}
void to_json(json& j,
const User& p) {
j = json {
{
"name",
p.name
}, {
"age",
p.age
}, {
"address",
p.address
}
};
}
Given that C++ doesn’t have reflection, I think that this represents a really nice handling of the issue. Now, how does this play with everything else? Here is what the skeleton of the session looks like:
class Session {
struct IEntityDetails {
virtual json serialize() = 0;
};
template <typename T>
struct EntityDetails: IEntityDetails {
std::shared_ptr<T> entity;
EntityDetails(std::shared_ptr<T> e): entity(e) {}
json serialize() {
return *entity;
}
};
std::vector <std::shared_ptr<IEntityDetails>> items;
public:
template <typename T>
void store(std::shared_ptr<T> entity) {
auto ed = std::make_shared <EntityDetails<T>> (entity);
items.push_back(ed);
}
void save_changes() {
for (auto it: items) {
auto json = it -> serialize();
std::cout << json << std::endl;
}
}
};
There is a whole bunch of stuff that is going on here that we need to deal with.
First, we have the IEntityDetails interface, which is non-generic. It is implemented by the generic class EntityDetails, which has the actual type that we are using and can then use the json serialization we defined to convert the entity to JSON. The rest are just details: We need to use a vector of shared_ptr instead of the abstract class because the abstract class has no defined size.
The generic store() method just captures the generic type and store it, and the rest of the code can work with the non-generic interface.
I’m not sure how idiomatic this code is, or how performant, but at least as a proof of concept, it works to show that we can get a really good interface to our users in C++.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments