C++ Upgrade Example

<!– Copyright © VMware, Inc. 2022. All rights reserved. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. –>

UpgradeSample.cpp shows usage of the V10 API, with examples of the V9 API provided for comparison.

#include <iostream>
#include <thread>

#include <geode/CacheFactory.hpp>
#include <geode/PoolManager.hpp>
#include <geode/RegionFactory.hpp>
#include <geode/RegionShortcut.hpp>

using namespace apache::geode::client;

int main(int argc, char** argv) {
    try {

        // *** Create the cache *********************************************************

        // OLD:
        //   CacheFactoryPtr cacheFactory = CacheFactory::createCacheFactory();
        //   CachePtr cachePtr = cacheFactory->create();

        // NEW:
        auto cacheFactory = CacheFactory();
        auto cache = cacheFactory.create();

        // *** Create the region ********************************************************

        // OLD:
        //  RegionFactoryPtr regionFactory = cachePtr->createRegionFactory(CACHING_PROXY);
        //  RegionPtr regionPtr = regionFactory->create("exampleRegion");

        // NEW: (pool required)
        auto poolFactory = cache.getPoolManager().createFactory();
        auto pool = poolFactory.addLocator("localhost", 10334).create("pool");
        auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY);
        auto region = regionFactory.setPoolName("pool").create("exampleRegion");

        // *** Put constant data into the region ****************************************

        // OLD:
        //regionPtr->put("blue", "The color of the sky");

        // NEW:
        region->put("blue", "The color of the sky");

        // *** Put variable data into the region ****************************************

        // OLD:
        // CacheableKeyPtr keyPtr = CacheableString::create("green");
        // CacheablePtr valuePtr = CacheableString::create("The color of life");
        // regionPtr->put(keyPtr, valuePtr);

        // NEW:
        auto key = CacheableString::create("green");
        auto value = CacheableString::create("The color of life");
        region->put(key, value);

        // *** Get data from the region *************************************************

        // OLD:
        //CacheablePtr result1Ptr = regionPtr->get("Key1");
        //CacheablePtr result2Ptr = regionPtr->get(keyPtr);

        // NEW:
        auto result1 = region->get("Key1");
        auto result2 = region->get(key);

        // *** Invalidate an Entry in the Region ****************************************

        // OLD:
        //regionPtr->invalidate("Key1");
        //regionPtr->invalidate(keyPtr);

        // NEW:
        region->invalidate("Key1");
        region->invalidate(key);

        // *** Destroy an Entry in the Region *******************************************

        // OLD:
        //regionPtr->destroy("Key1");
        //regionPtr->destroy(keyPtr);

        // NEW:
        region->destroy("Key1");
        region->destroy(key);

        // *** Close the cache **********************************************************

        // OLD:
        //cachePtr->close();

        // NEW:
        cache.close();
    }
    catch (Exception ex) {}
}