You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2015/12/11 09:00:12 UTC

[10/16] ignite git commit: IGNITE-2118 Updated cache store examples to produce stable output.

IGNITE-2118 Updated cache store examples to produce stable output.


Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/478657b4
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/478657b4
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/478657b4

Branch: refs/heads/ignite-1537
Commit: 478657b4d08d38583ad6698390308f11ab8d29ff
Parents: 7fc6d81
Author: Alexey Kuznetsov <ak...@apache.org>
Authored: Fri Dec 11 11:21:07 2015 +0700
Committer: Alexey Kuznetsov <ak...@apache.org>
Committed: Fri Dec 11 11:21:07 2015 +0700

----------------------------------------------------------------------
 .../store/auto/CacheBinaryAutoStoreExample.java | 38 ++++++++++++--------
 .../store/auto/CacheAutoStoreExample.java       | 32 +++++++++++------
 .../apache/ignite/examples/model/Person.java    | 36 ++++++++++++++++---
 .../ignite/examples/util/DbH2ServerStartup.java | 31 ++++++++++++----
 4 files changed, 101 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/478657b4/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/store/auto/CacheBinaryAutoStoreExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/store/auto/CacheBinaryAutoStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/store/auto/CacheBinaryAutoStoreExample.java
index 63d947c..aa228f1 100644
--- a/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/store/auto/CacheBinaryAutoStoreExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/binary/datagrid/store/auto/CacheBinaryAutoStoreExample.java
@@ -56,7 +56,7 @@ import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
  */
 public class CacheBinaryAutoStoreExample {
     /** Global person ID to use across entire example. */
-    private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits());
+    private static final Long id = 25121642L;
 
     /** Cache name. */
     public static final String CACHE_NAME = CacheBinaryAutoStoreExample.class.getSimpleName();
@@ -81,9 +81,9 @@ public class CacheBinaryAutoStoreExample {
 
         jdbcType.setValueType("org.apache.ignite.examples.model.Person");
         jdbcType.setValueFields(
-                new JdbcTypeField(Types.BIGINT, "ID", Long.class, "id"),
-                new JdbcTypeField(Types.VARCHAR, "FIRST_NAME", String.class, "firstName"),
-                new JdbcTypeField(Types.VARCHAR, "LAST_NAME", String.class, "lastName")
+            new JdbcTypeField(Types.BIGINT, "ID", Long.class, "id"),
+            new JdbcTypeField(Types.VARCHAR, "FIRST_NAME", String.class, "firstName"),
+            new JdbcTypeField(Types.VARCHAR, "LAST_NAME", String.class, "lastName")
         );
 
         storeFactory.setTypes(jdbcType);
@@ -108,32 +108,42 @@ public class CacheBinaryAutoStoreExample {
      * Executes example.
      *
      * @param args Command line arguments, none required.
-     * @throws IgniteException If example execution failed.
+     * @throws Exception If example execution failed.
      */
-    public static void main(String[] args) throws IgniteException {
+    public static void main(String[] args) throws Exception {
         // To start ignite with desired configuration uncomment the appropriate line.
         try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
             System.out.println();
-            System.out.println(">>> Cache auto store example started.");
+            System.out.println(">>> Populate database with data...");
+            DbH2ServerStartup.populateDatabase();
+
+            System.out.println();
+            System.out.println(">>> Cache auto store example started...");
 
             try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheConfiguration())) {
                 try (Transaction tx = ignite.transactions().txStart()) {
                     Person val = cache.get(id);
 
-                    System.out.println("Read value: " + val);
+                    System.out.println(">>> Read value: " + val);
 
-                    val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
+                    val = cache.getAndPut(id, new Person(id, 1L, "Isaac", "Newton", 100.10, "English physicist and mathematician"));
 
-                    System.out.println("Overwrote old value: " + val);
+                    System.out.println(">>> Overwrote old value: " + val);
 
                     val = cache.get(id);
 
-                    System.out.println("Read value: " + val);
+                    System.out.println(">>> Read value: " + val);
+
+                    System.out.println(">>> Update salary in transaction...");
+
+                    val.salary *= 2;
+
+                    cache.put(id, val);
 
                     tx.commit();
                 }
 
-                System.out.println("Read value after commit: " + cache.get(id));
+                System.out.println(">>> Read value after commit: " + cache.get(id));
 
                 cache.clear();
 
@@ -143,7 +153,7 @@ public class CacheBinaryAutoStoreExample {
                 // Load cache on all data nodes with custom SQL statement.
                 cache.loadCache(null, "java.lang.Long", "select * from PERSON where id <= 3");
 
-                System.out.println("Loaded cache entries: " + cache.size());
+                System.out.println(">>> Loaded cache entries: " + cache.size());
 
                 cache.clear();
 
@@ -151,7 +161,7 @@ public class CacheBinaryAutoStoreExample {
                 System.out.println(">>> Load ALL data to cache from DB...");
                 cache.loadCache(null);
 
-                System.out.println("Loaded cache entries: " + cache.size());
+                System.out.println(">>> Loaded cache entries: " + cache.size());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/478657b4/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java
index 7d21fce..a61752d 100644
--- a/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java
+++ b/examples/src/main/java/org/apache/ignite/examples/datagrid/store/auto/CacheAutoStoreExample.java
@@ -54,7 +54,7 @@ import static org.apache.ignite.cache.CacheAtomicityMode.TRANSACTIONAL;
  */
 public class CacheAutoStoreExample {
     /** Global person ID to use across entire example. */
-    private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits());
+    private static final Long id = 25121642L;
 
     /** Cache name. */
     public static final String CACHE_NAME = CacheAutoStoreExample.class.getSimpleName();
@@ -114,32 +114,42 @@ public class CacheAutoStoreExample {
      * Executes example.
      *
      * @param args Command line arguments, none required.
-     * @throws IgniteException If example execution failed.
+     * @throws Exception If example execution failed.
      */
-    public static void main(String[] args) throws IgniteException {
+    public static void main(String[] args) throws Exception {
         // To start ignite with desired configuration uncomment the appropriate line.
         try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
             System.out.println();
-            System.out.println(">>> Cache auto store example started.");
+            System.out.println(">>> Populate database with data...");
+            DbH2ServerStartup.populateDatabase();
+
+            System.out.println();
+            System.out.println(">>> Cache auto store example started...");
 
             try (IgniteCache<Long, Person> cache = ignite.getOrCreateCache(cacheConfiguration())) {
                 try (Transaction tx = ignite.transactions().txStart()) {
                     Person val = cache.get(id);
 
-                    System.out.println("Read value: " + val);
+                    System.out.println(">>> Read value: " + val);
 
-                    val = cache.getAndPut(id, new Person(id, "Isaac", "Newton"));
+                    val = cache.getAndPut(id, new Person(id, 1L, "Isaac", "Newton", 100.10, "English physicist and mathematician"));
 
-                    System.out.println("Overwrote old value: " + val);
+                    System.out.println(">>> Overwrote old value: " + val);
 
                     val = cache.get(id);
 
-                    System.out.println("Read value: " + val);
+                    System.out.println(">>> Read value: " + val);
+
+                    System.out.println(">>> Update salary in transaction...");
+
+                    val.salary *= 2;
+
+                    cache.put(id, val);
 
                     tx.commit();
                 }
 
-                System.out.println("Read value after commit: " + cache.get(id));
+                System.out.println(">>> Read value after commit: " + cache.get(id));
 
                 cache.clear();
 
@@ -149,7 +159,7 @@ public class CacheAutoStoreExample {
                 // Load cache on all data nodes with custom SQL statement.
                 cache.loadCache(null, "java.lang.Long", "select * from PERSON where id <= 3");
 
-                System.out.println("Loaded cache entries: " + cache.size());
+                System.out.println(">>> Loaded cache entries: " + cache.size());
 
                 cache.clear();
 
@@ -157,7 +167,7 @@ public class CacheAutoStoreExample {
                 System.out.println(">>> Load ALL data to cache from DB...");
                 cache.loadCache(null);
 
-                System.out.println("Loaded cache entries: " + cache.size());
+                System.out.println(">>> Loaded cache entries: " + cache.size());
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/478657b4/examples/src/main/java/org/apache/ignite/examples/model/Person.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/model/Person.java b/examples/src/main/java/org/apache/ignite/examples/model/Person.java
index 5f7deae..618fa5c 100644
--- a/examples/src/main/java/org/apache/ignite/examples/model/Person.java
+++ b/examples/src/main/java/org/apache/ignite/examples/model/Person.java
@@ -58,6 +58,13 @@ public class Person implements Serializable {
     private transient AffinityKey<Long> key;
 
     /**
+     * Default constructor.
+     */
+    public Person() {
+        // No-op.
+    }
+
+    /**
      * Constructs person record.
      *
      * @param org       Organization.
@@ -74,8 +81,27 @@ public class Person implements Serializable {
 
         this.firstName = firstName;
         this.lastName = lastName;
+        this.salary = salary;
         this.resume = resume;
+    }
+
+    /**
+     * Constructs person record.
+     *
+     * @param id Person ID.
+     * @param orgId Organization ID.
+     * @param firstName First name.
+     * @param lastName Last name.
+     * @param salary    Salary.
+     * @param resume    Resume text.
+     */
+    public Person(Long id, Long orgId, String firstName, String lastName, double salary, String resume) {
+        this.id = id;
+        this.orgId = orgId;
+        this.firstName = firstName;
+        this.lastName = lastName;
         this.salary = salary;
+        this.resume = resume;
     }
 
     /**
@@ -109,11 +135,11 @@ public class Person implements Serializable {
      * {@inheritDoc}
      */
     @Override public String toString() {
-        return "Person [firstName=" + firstName +
-                ", lastName=" + lastName +
-                ", id=" + id +
+        return "Person [id=" + id +
                 ", orgId=" + orgId +
-                ", resume=" + resume +
-                ", salary=" + salary + ']';
+                ", lastName=" + lastName +
+                ", firstName=" + firstName +
+                ", salary=" + salary +
+                ", resume=" + resume + ']';
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/478657b4/examples/src/main/java/org/apache/ignite/examples/util/DbH2ServerStartup.java
----------------------------------------------------------------------
diff --git a/examples/src/main/java/org/apache/ignite/examples/util/DbH2ServerStartup.java b/examples/src/main/java/org/apache/ignite/examples/util/DbH2ServerStartup.java
index 01717d0..f3da07d 100644
--- a/examples/src/main/java/org/apache/ignite/examples/util/DbH2ServerStartup.java
+++ b/examples/src/main/java/org/apache/ignite/examples/util/DbH2ServerStartup.java
@@ -30,17 +30,34 @@ import org.h2.tools.Server;
  */
 public class DbH2ServerStartup {
     /** Create table script. */
-    private static final String CREATE_TABLE =
-        "create table PERSON(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id));";
+    private static final String CREATE_PERSON_TABLE =
+        "create table if not exists PERSON(id bigint not null, first_name varchar(50), last_name varchar(50), PRIMARY KEY(id));";
 
     /** Sample data script. */
-    private static final String POPULATE_TABLE =
+    private static final String POPULATE_PERSON_TABLE =
+        "delete from PERSON;\n" +
         "insert into PERSON(id, first_name, last_name) values(1, 'Johannes', 'Kepler');\n" +
         "insert into PERSON(id, first_name, last_name) values(2, 'Galileo', 'Galilei');\n" +
         "insert into PERSON(id, first_name, last_name) values(3, 'Henry', 'More');\n" +
         "insert into PERSON(id, first_name, last_name) values(4, 'Polish', 'Brethren');\n" +
         "insert into PERSON(id, first_name, last_name) values(5, 'Robert', 'Boyle');\n" +
-        "insert into PERSON(id, first_name, last_name) values(6, 'Isaac', 'Newton');";
+        "insert into PERSON(id, first_name, last_name) values(6, 'Wilhelm', 'Leibniz');";
+
+    /**
+     * Populate sample database.
+     *
+     * @throws SQLException if
+     */
+    public static void populateDatabase() throws SQLException {
+        // Try to connect to database TCP server.
+        JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", "");
+
+        // Create Person table in database.
+        RunScript.execute(dataSrc.getConnection(), new StringReader(CREATE_PERSON_TABLE));
+
+        // Populates Person table with sample data in database.
+        RunScript.execute(dataSrc.getConnection(), new StringReader(POPULATE_PERSON_TABLE));
+    }
 
     /**
      * Start H2 database TCP server.
@@ -53,14 +70,16 @@ public class DbH2ServerStartup {
             // Start H2 database TCP server in order to access sample in-memory database from other processes.
             Server.createTcpServer("-tcpDaemon").start();
 
+            populateDatabase();
+
             // Try to connect to database TCP server.
             JdbcConnectionPool dataSrc = JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", "");
 
             // Create Person table in database.
-            RunScript.execute(dataSrc.getConnection(), new StringReader(CREATE_TABLE));
+            RunScript.execute(dataSrc.getConnection(), new StringReader(CREATE_PERSON_TABLE));
 
             // Populates Person table with sample data in database.
-            RunScript.execute(dataSrc.getConnection(), new StringReader(POPULATE_TABLE));
+            RunScript.execute(dataSrc.getConnection(), new StringReader(POPULATE_PERSON_TABLE));
         }
         catch (SQLException e) {
             throw new IgniteException("Failed to start database TCP server", e);