You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by is...@apache.org on 2019/04/05 15:29:08 UTC

[ignite] branch master updated: IGNITE-11588: Fixed C++ Query example, if run in cluster.

This is an automated email from the ASF dual-hosted git repository.

isapego pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new b3c59b1  IGNITE-11588: Fixed C++ Query example, if run in cluster.
b3c59b1 is described below

commit b3c59b113cdd62f71755f0f67fd6145f774ce076
Author: Pavel Kuznetsov <pa...@gmail.com>
AuthorDate: Fri Apr 5 18:21:22 2019 +0300

    IGNITE-11588: Fixed C++ Query example, if run in cluster.
---
 .../cpp/examples/include/ignite/examples/person.h  | 53 +++++++++++++++++
 .../query-example/config/query-example.xml         | 12 +++-
 .../examples/query-example/src/query_example.cpp   | 68 +++++++++++++---------
 .../project/vs/thin-client-put-get-example.vcxproj |  3 -
 .../vs/thin-client-put-get-example.vcxproj.filters |  8 ---
 5 files changed, 103 insertions(+), 41 deletions(-)

diff --git a/modules/platforms/cpp/examples/include/ignite/examples/person.h b/modules/platforms/cpp/examples/include/ignite/examples/person.h
index 9a87ef2..0631b96 100644
--- a/modules/platforms/cpp/examples/include/ignite/examples/person.h
+++ b/modules/platforms/cpp/examples/include/ignite/examples/person.h
@@ -65,6 +65,35 @@ namespace ignite
             std::string resume;
             double salary;
         };
+
+        // Person key with affinity Organization info. If we want to to collocate Persons with the same value of orgId
+        // we need to put struct as a cache key that contains fields: 1) id of the record (person id or serial id number)
+        // 2) collocation column info (orgId). This is required because of constraint : affinity key must be part of the
+        // key.
+        struct PersonKey {
+            PersonKey(int64_t _id, int64_t _orgId) : id(_id), orgIdAff(_id)
+            {
+                // No-op.
+            }
+
+            PersonKey() : id(0), orgIdAff(0)
+            {
+                // No-op.
+            }
+
+            std::string ToString() const
+            {
+                std::ostringstream oss;
+
+                oss << "PersonKey [id=" << id
+                    << ", orgIdAff=" << orgIdAff
+                    << "]";
+                return oss.str();
+            }
+
+            int64_t  id;
+            int64_t  orgIdAff;
+        };
     }
 }
 
@@ -101,6 +130,30 @@ namespace ignite
             }
 
         IGNITE_BINARY_TYPE_END
+
+        IGNITE_BINARY_TYPE_START(ignite::examples::PersonKey)
+
+            typedef ignite::examples::PersonKey PersonKey;
+
+            IGNITE_BINARY_GET_TYPE_ID_AS_HASH(PersonKey)
+            IGNITE_BINARY_GET_TYPE_NAME_AS_IS(PersonKey)
+            IGNITE_BINARY_GET_FIELD_ID_AS_HASH
+            IGNITE_BINARY_IS_NULL_FALSE(PersonKey)
+            IGNITE_BINARY_GET_NULL_DEFAULT_CTOR(PersonKey)
+
+            static void Write(BinaryWriter& writer, const ignite::examples::PersonKey& obj)
+            {
+                writer.WriteInt64("id", obj.id);
+                writer.WriteInt64("orgIdAff", obj.orgIdAff);
+            }
+
+            static void Read(BinaryReader& reader, ignite::examples::PersonKey& dst)
+            {
+                dst.id = reader.ReadInt64("id");
+                dst.orgIdAff = reader.ReadInt64("orgIdAff");
+            }
+
+        IGNITE_BINARY_TYPE_END
     }
 };
 
diff --git a/modules/platforms/cpp/examples/query-example/config/query-example.xml b/modules/platforms/cpp/examples/query-example/config/query-example.xml
index a7b63c2..758da99 100644
--- a/modules/platforms/cpp/examples/query-example/config/query-example.xml
+++ b/modules/platforms/cpp/examples/query-example/config/query-example.xml
@@ -29,6 +29,16 @@
         <!-- Set to true to enable distributed class loading for examples, default is false. -->
         <property name="peerClassLoadingEnabled" value="true"/>
 
+        <property name="cacheKeyConfiguration">
+            <list>
+                <bean class="org.apache.ignite.cache.CacheKeyConfiguration">
+                    <property name="typeName" value="PersonKey"/>
+
+                    <property name="affinityKeyFieldName" value="orgIdAff" />
+                </bean>
+            </list>
+        </property>
+
         <property name="cacheConfiguration">
             <list>
                 <bean class="org.apache.ignite.configuration.CacheConfiguration">
@@ -41,7 +51,7 @@
                     <property name="queryEntities">
                         <list>
                             <bean class="org.apache.ignite.cache.QueryEntity">
-                                <property name="keyType" value="java.lang.Long"/>
+                                <property name="keyType" value="PersonKey"/>
                                 <property name="valueType" value="Person"/>
 
                                 <property name="fields">
diff --git a/modules/platforms/cpp/examples/query-example/src/query_example.cpp b/modules/platforms/cpp/examples/query-example/src/query_example.cpp
index bc4417a..bf8095f 100644
--- a/modules/platforms/cpp/examples/query-example/src/query_example.cpp
+++ b/modules/platforms/cpp/examples/query-example/src/query_example.cpp
@@ -48,9 +48,9 @@ const char* PERSON_TYPE = "Person";
  */
 void DoSqlQueryWithDistributedJoin()
 {
-    typedef std::vector< CacheEntry<int64_t, Person> > ResVector;
+    typedef std::vector< CacheEntry<PersonKey, Person> > ResVector;
 
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     // SQL clause query which joins on 2 types to select people for a specific organization.
     std::string joinSql(
@@ -73,7 +73,7 @@ void DoSqlQueryWithDistributedJoin()
     std::cout << "Following people are 'ApacheIgnite' employees (distributed join): " << std::endl;
 
     for (ResVector::const_iterator i = igniters.begin(); i != igniters.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 
@@ -88,7 +88,7 @@ void DoSqlQueryWithDistributedJoin()
     std::cout << "Following people are 'Other' employees (distributed join): " << std::endl;
 
     for (ResVector::const_iterator i = others.begin(); i != others.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 }
@@ -102,7 +102,7 @@ void DoSqlQueryWithDistributedJoin()
  */
 void DoSqlFieldsQueryWithJoin()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     // Execute query to get names of all employees.
     std::string sql(
@@ -137,7 +137,7 @@ void DoSqlFieldsQueryWithJoin()
  */
 void DoSqlFieldsQuery()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     // Execute query to get names of all employees.
     QueryFieldsCursor cursor = cache.Query(SqlFieldsQuery(
@@ -161,7 +161,7 @@ void DoSqlFieldsQuery()
  */
 void DoSqlQueryWithAggregation()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     // Calculate average of salary of all persons in ApacheIgnite.
     // Note that we also join on Organization cache as well.
@@ -194,9 +194,9 @@ void DoSqlQueryWithAggregation()
  */
 void DoTextQuery()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
-    typedef std::vector< CacheEntry<int64_t, Person> > ResVector;
+    typedef std::vector< CacheEntry<PersonKey, Person> > ResVector;
 
     //  Query for all people with "Master" in their resumes.
     ResVector masters;
@@ -210,7 +210,7 @@ void DoTextQuery()
 
     // Printing first result set.
     for (ResVector::const_iterator i = masters.begin(); i != masters.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 
@@ -218,7 +218,7 @@ void DoTextQuery()
 
     // Printing second result set.
     for (ResVector::const_iterator i = bachelors.begin(); i != bachelors.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 }
@@ -231,9 +231,9 @@ void DoTextQuery()
  */
 void DoSqlQueryWithJoin()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
-    typedef std::vector< CacheEntry<int64_t, Person> > ResVector;
+    typedef std::vector< CacheEntry<PersonKey, Person> > ResVector;
 
     // SQL clause query which joins on 2 types to select people for a specific organization.
     std::string sql(
@@ -252,7 +252,7 @@ void DoSqlQueryWithJoin()
     cache.Query(qry).GetAll(res);
 
     for (ResVector::const_iterator i = res.begin(); i != res.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 
@@ -266,7 +266,7 @@ void DoSqlQueryWithJoin()
     cache.Query(qry).GetAll(res);
 
     for (ResVector::const_iterator i = res.begin(); i != res.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 }
@@ -279,14 +279,14 @@ void DoSqlQueryWithJoin()
  */
 void DoSqlQuery()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     // SQL clause which selects salaries based on range.
     std::string sql("salary > ? and salary <= ?");
 
     SqlQuery qry(PERSON_TYPE, sql);
 
-    typedef std::vector< CacheEntry<int64_t, Person> > ResVector;
+    typedef std::vector< CacheEntry<PersonKey, Person> > ResVector;
 
     // Execute queries for salary range 0 - 1000.
     std::cout << "People with salaries between 0 and 1000 (queried with SQL query): " << std::endl;
@@ -298,7 +298,7 @@ void DoSqlQuery()
     cache.Query(qry).GetAll(res);
 
     for (ResVector::const_iterator i = res.begin(); i != res.end(); ++i)
-            std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+            std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 
@@ -315,7 +315,7 @@ void DoSqlQuery()
     cache.Query(qry).GetAll(res);
 
     for (ResVector::const_iterator i = res.begin(); i != res.end(); ++i)
-        std::cout << i->GetKey() << " : " << i->GetValue().ToString() << std::endl;
+        std::cout << i->GetKey().ToString() << " : " << i->GetValue().ToString() << std::endl;
 
     std::cout << std::endl;
 }
@@ -325,11 +325,11 @@ void DoSqlQuery()
  */
 void DoScanQuery()
 {
-    Cache<int64_t, Person> cache = Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> cache = Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     ScanQuery scan;
 
-    typedef std::vector< CacheEntry<int64_t, Person> > ResVector;
+    typedef std::vector< CacheEntry<PersonKey, Person> > ResVector;
 
     ResVector res;
     cache.Query(scan).GetAll(res);
@@ -342,7 +342,7 @@ void DoScanQuery()
         Person person(i->GetValue());
 
         if (person.salary <= 1000)
-            std::cout << i->GetKey() << " : " << person.ToString() << std::endl;
+            std::cout << i->GetKey().ToString() << " : " << person.ToString() << std::endl;
     }
 
     std::cout << std::endl;
@@ -369,24 +369,34 @@ void Initialize()
     orgCache.Put(org1Id, org1);
     orgCache.Put(org2Id, org2);
 
-    Cache<int64_t, Person> personCache =
-        Ignition::Get().GetCache<int64_t, Person>(PERSON_CACHE);
+    Cache<PersonKey, Person> personCache =
+        Ignition::Get().GetCache<PersonKey, Person>(PERSON_CACHE);
 
     // Clear cache before running the example.
     personCache.Clear();
 
     // People.
+
+    // Collocated by 1st organisation:
     Person p1(org1Id, "John", "Doe", "John Doe has Master Degree.", 2000);
     Person p2(org1Id, "Jane", "Doe", "Jane Doe has Bachelor Degree.", 1000);
+
+    PersonKey pKey1 (1, org1Id);
+    PersonKey pKey2 (2, org1Id);
+
+    // Collocated by second organisation:
     Person p3(org2Id, "John", "Smith", "John Smith has Bachelor Degree.", 1000);
     Person p4(org2Id, "Jane", "Smith", "Jane Smith has Master Degree.", 2000);
 
+    PersonKey pKey3 (3, org2Id);
+    PersonKey pKey4 (4, org2Id);
+
     // Note that in this example we use custom affinity key for Person objects
     // to ensure that all persons are collocated with their organizations.
-    personCache.Put(1, p1);
-    personCache.Put(2, p2);
-    personCache.Put(3, p3);
-    personCache.Put(4, p4);
+    personCache.Put(pKey1, p1);
+    personCache.Put(pKey2, p2);
+    personCache.Put(pKey3, p3);
+    personCache.Put(pKey4, p4);
 }
 
 int main()
@@ -408,7 +418,7 @@ int main()
         Cache<int64_t, Organization> orgCache = ignite.GetCache<int64_t, Organization>(ORG_CACHE);
 
         // Get person cache instance.
-        Cache<int64_t, Person> personCache = ignite.GetCache<int64_t, Person>(PERSON_CACHE);
+        Cache<PersonKey, Person> personCache = ignite.GetCache<PersonKey, Person>(PERSON_CACHE);
 
         // Populate cache.
         Initialize();
diff --git a/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj b/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj
index 12c8a98..26bc473 100644
--- a/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj
+++ b/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj
@@ -98,9 +98,6 @@ copy "$(ProjectDir)..\..\..\..\project\vs\$(Platform)\$(Configuration)\ignite.th
     <ClInclude Include="..\..\..\include\ignite\examples\organization.h" />
     <ClInclude Include="..\..\..\include\ignite\examples\person.h" />
   </ItemGroup>
-  <ItemGroup>
-    <None Include="..\..\config\thin_client_put_get_example.xml" />
-  </ItemGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
   <ImportGroup Label="ExtensionTargets">
   </ImportGroup>
diff --git a/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj.filters b/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj.filters
index 41d4998..477f7bd 100644
--- a/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj.filters
+++ b/modules/platforms/cpp/examples/thin-client-put-get-example/project/vs/thin-client-put-get-example.vcxproj.filters
@@ -18,18 +18,10 @@
     <Filter Include="Header Files">
       <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
     </Filter>
-    <Filter Include="Config">
-      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
-    </Filter>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="..\..\src\thin_client_put_get_example.cpp">
       <Filter>Source Files</Filter>
     </ClCompile>
   </ItemGroup>
-  <ItemGroup>
-    <None Include="..\..\config\thin_client_put_get_example.xml">
-      <Filter>Config</Filter>
-    </None>
-  </ItemGroup>
 </Project>
\ No newline at end of file