DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

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.

Oren Eini user avatar by
Oren Eini
·
Oct. 15, 18 · Analysis
Like (1)
Save
Tweet
Share
4.82K Views

Join the DZone community and get the full member experience.

Join For Free

The 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.

Popular on DZone

  • Java Development Trends 2023
  • Real-Time Stream Processing With Hazelcast and StreamNative
  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • Why Every Fintech Company Needs DevOps

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: