Put/Get/Remove Example

The native client release contains an example written for C++ showing how a client application can establish a connection to a cluster and then use that connection to perform basic operations on a remote server. The examples are located in examples/cpp/putgetremove.

The example performs a sequence of operations, displaying simple log entries as they run.

  • To run the example, follow the instructions in the README.md file in the example directory.

  • Review the source code in the example directory to see exactly how it operates.

  • Begin by running a script that sets up the server-side environment by invoking gfsh commands to create a region, simply called “example_userinfo.”

  • Run the example client application, which performs the following steps:

    • Connects to the server
    • Performs region put operations using key/value pairs
    • Uses region get to retrieve the values
    • Uses region remove to remove the values

Put/Get/Remove Example Code

This section contains code snippets showing highlights of the C++ put/get/remove example. They are not intended for cut-and-paste execution. For the complete source, see the example source directory.

The C++ example creates a cache, then uses it to create a connection pool and a region object (of class Region).

  auto cacheFactory = CacheFactory();
  cacheFactory.set("log-level", "none");
  auto cache = cacheFactory.create();
  auto poolFactory = cache.getPoolManager().createFactory();

  poolFactory.addLocator("localhost", 10334);
  auto pool = poolFactory.create("pool");
  auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY);
  auto region = regionFactory.setPoolName("pool").create("example_userinfo");

The client then populates the data store with two key/value pairs.

  region->put("rtimmons", "Robert Timmons");
  region->put("scharles", "Sylvia Charles");

Next, the application retrieves the stored values using Get operations.

  auto user1 = region->get("rtimmons");
  auto user2 = region->get("scharles");

Finally, the application deletes one of the stored values using the Remove method.

  if (region->existsValue("rtimmons")) {
    std::cout << "rtimmons's info not deleted" << std::endl;
  } else {
    std::cout << "rtimmons's info successfully deleted" << std::endl;
  }