http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/User.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/User.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/User.java new file mode 100644 index 0000000..2ee6c38 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/User.java @@ -0,0 +1,151 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.hibernate; + +import org.hibernate.annotations.*; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.*; +import java.util.*; + +/** + * A user entity class. Represents a user of some public service, + * having a number of personal information fields as well as a + * number of posts written. + */ +@Entity +class User { + /** ID. */ + @Id + @GeneratedValue(strategy=GenerationType.AUTO) + private long id; + + /** Login. */ + @NaturalId + private String login; + + /** First name. */ + private String firstName; + + /** Last name. */ + private String lastName; + + /** Posts. */ + @OneToMany(mappedBy = "author", cascade = CascadeType.ALL) + private Set<Post> posts = new HashSet<>(); + + /** + * Default constructor (required by Hibernate). + */ + User() { + // No-op. + } + + /** + * Constructor. + * + * @param login Login. + * @param firstName First name. + * @param lastName Last name. + */ + User(String login, String firstName, String lastName) { + this.login = login; + this.firstName = firstName; + this.lastName = lastName; + } + + /** + * @return ID. + */ + public long getId() { + return id; + } + + /** + * @param id New ID. + */ + public void setId(long id) { + this.id = id; + } + + /** + * @return Login. + */ + public String getLogin() { + return login; + } + + /** + * @param login New login. + */ + public void setLogin(String login) { + this.login = login; + } + + /** + * @return First name. + */ + public String getFirstName() { + return firstName; + } + + /** + * @param firstName New first name. + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * @return Last name. + */ + public String getLastName() { + return lastName; + } + + /** + * @param lastName New last name. + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } + + /** + * @return Posts. + */ + public Set<Post> getPosts() { + return posts; + } + + /** + * @param posts New posts. + */ + public void setPosts(Set<Post> posts) { + this.posts = posts; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "User [id=" + id + + ", login=" + login + + ", firstName=" + firstName + + ", lastName=" + lastName + + ']'; + } +}
http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/package-info.java new file mode 100644 index 0000000..9458510 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/hibernate/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Hibernate example. + */ +package org.apache.ignite.examples.java7.datagrid.hibernate; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/package-info.java new file mode 100644 index 0000000..15a19ab --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Demonstrates data ignite cache usage. + */ +package org.apache.ignite.examples.java7.datagrid; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/CacheStarSchemaExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/CacheStarSchemaExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/CacheStarSchemaExample.java new file mode 100644 index 0000000..082d31f --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/CacheStarSchemaExample.java @@ -0,0 +1,244 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.starschema; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.query.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.java7.*; + +import javax.cache.*; +import java.util.*; +import java.util.concurrent.*; + +/** + * <a href="http://en.wikipedia.org/wiki/Snowflake_schema">Snowflake Schema</a> is a logical + * arrangement of data in which data is split into {@code dimensions} and {@code facts}. + * <i>Dimensions</i> can be referenced or joined by other <i>dimensions</i> or <i>facts</i>, + * however, <i>facts</i> are generally not referenced by other facts. You can view <i>dimensions</i> + * as your master or reference data, while <i>facts</i> are usually large data sets of events or + * other objects that continuously come into the system and may change frequently. In Ignite + * such architecture is supported via cross-cache queries. By storing <i>dimensions</i> in + * {@link CacheMode#REPLICATED REPLICATED} caches and <i>facts</i> in much larger + * {@link CacheMode#PARTITIONED PARTITIONED} caches you can freely execute distributed joins across + * your whole in-memory data ignite cluster, thus querying your in memory data without any limitations. + * <p> + * In this example we have two <i>dimensions</i>, {@link DimProduct} and {@link DimStore} and + * one <i>fact</i> - {@link FactPurchase}. Queries are executed by joining dimensions and facts + * in various ways. + * <p> + * Remote nodes should always be started with special configuration file which + * enables P2P class loading: {@code 'ignite.{sh|bat} examples/config/example-ignite.xml'}. + * <p> + * Alternatively you can run {@link ExampleNodeStartup} in another JVM which will + * start node with {@code examples/config/example-ignite.xml} configuration. + */ +public class CacheStarSchemaExample { + /** Partitioned cache name. */ + private static final String PARTITIONED_CACHE_NAME = CacheStarSchemaExample.class.getSimpleName() + "Partitioned"; + + /** Replicated cache name. */ + private static final String REPLICATED_CACHE_NAME = CacheStarSchemaExample.class.getSimpleName() + "Replicated"; + + /** ID generator. */ + private static int idGen = (int)System.currentTimeMillis(); + + /** DimStore data. */ + private static Map<Integer, DimStore> dataStore = new HashMap<>(); + + /** DimProduct data. */ + private static Map<Integer, DimProduct> dataProduct = new HashMap<>(); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + */ + public static void main(String[] args) { + try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) { + + System.out.println(); + System.out.println(">>> Cache star schema example started."); + + CacheConfiguration<Integer, FactPurchase> factCacheCfg = new CacheConfiguration<>(); + + factCacheCfg.setCacheMode(CacheMode.PARTITIONED); + factCacheCfg.setName(PARTITIONED_CACHE_NAME); + factCacheCfg.setIndexedTypes( + Integer.class, FactPurchase.class + ); + + CacheConfiguration<Integer, Object> dimCacheCfg = new CacheConfiguration<>(); + + dimCacheCfg.setCacheMode(CacheMode.REPLICATED); + dimCacheCfg.setName(REPLICATED_CACHE_NAME); + dimCacheCfg.setIndexedTypes( + Integer.class, DimStore.class, + Integer.class, DimProduct.class + ); + + try (IgniteCache<Integer, FactPurchase> factCache = ignite.createCache(factCacheCfg); + IgniteCache<Integer, Object> dimCache = ignite.createCache(dimCacheCfg)) { + populateDimensions(dimCache); + populateFacts(factCache); + + queryStorePurchases(); + queryProductPurchases(); + } + } + } + + /** + * Populate cache with {@code 'dimensions'} which in our case are + * {@link DimStore} and {@link DimProduct} instances. + * @param dimCache Cache to populate. + * + * @throws IgniteException If failed. + */ + private static void populateDimensions(Cache<Integer, Object> dimCache) throws IgniteException { + DimStore store1 = new DimStore(idGen++, "Store1", "12345", "321 Chilly Dr, NY"); + DimStore store2 = new DimStore(idGen++, "Store2", "54321", "123 Windy Dr, San Francisco"); + + // Populate stores. + dimCache.put(store1.getId(), store1); + dimCache.put(store2.getId(), store2); + + dataStore.put(store1.getId(), store1); + dataStore.put(store2.getId(), store2); + + // Populate products + for (int i = 0; i < 20; i++) { + int id = idGen++; + + DimProduct product = new DimProduct(id, "Product" + i, i + 1, (i + 1) * 10); + + dimCache.put(id, product); + + dataProduct.put(id, product); + } + } + + /** + * Populate cache with {@code 'facts'}, which in our case are {@link FactPurchase} objects. + * @param factCache Cache to populate. + * + * @throws IgniteException If failed. + */ + private static void populateFacts(Cache<Integer, FactPurchase> factCache) throws IgniteException { + for (int i = 0; i < 100; i++) { + int id = idGen++; + + DimStore store = rand(dataStore.values()); + DimProduct prod = rand(dataProduct.values()); + + factCache.put(id, new FactPurchase(id, prod.getId(), store.getId(), (i + 1))); + } + } + + /** + * Query all purchases made at a specific store. This query uses cross-cache joins + * between {@link DimStore} objects stored in {@code 'replicated'} cache and + * {@link FactPurchase} objects stored in {@code 'partitioned'} cache. + * + * @throws IgniteException If failed. + */ + private static void queryStorePurchases() { + IgniteCache<Integer, FactPurchase> factCache = Ignition.ignite().jcache(PARTITIONED_CACHE_NAME); + + // All purchases for store1. + // ======================== + + // Create cross cache query to get all purchases made at store1. + QueryCursor<Cache.Entry<Integer, FactPurchase>> storePurchases = factCache.query(new SqlQuery( + FactPurchase.class, + "from \"" + REPLICATED_CACHE_NAME + "\".DimStore, \"" + PARTITIONED_CACHE_NAME + "\".FactPurchase " + + "where DimStore.id=FactPurchase.storeId and DimStore.name=?").setArgs("Store1")); + + printQueryResults("All purchases made at store1:", storePurchases.getAll()); + } + + /** + * Query all purchases made at a specific store for 3 specific products. + * This query uses cross-cache joins between {@link DimStore}, {@link DimProduct} + * objects stored in {@code 'replicated'} cache and {@link FactPurchase} objects + * stored in {@code 'partitioned'} cache. + * + * @throws IgniteException If failed. + */ + private static void queryProductPurchases() { + IgniteCache<Integer, FactPurchase> factCache = Ignition.ignite().jcache(PARTITIONED_CACHE_NAME); + + // All purchases for certain product made at store2. + // ================================================= + + DimProduct p1 = rand(dataProduct.values()); + DimProduct p2 = rand(dataProduct.values()); + DimProduct p3 = rand(dataProduct.values()); + + System.out.println("IDs of products [p1=" + p1.getId() + ", p2=" + p2.getId() + ", p3=" + p3.getId() + ']'); + + // Create cross cache query to get all purchases made at store2 + // for specified products. + QueryCursor<Cache.Entry<Integer, FactPurchase>> prodPurchases = factCache.query(new SqlQuery( + FactPurchase.class, + "from \"" + REPLICATED_CACHE_NAME + "\".DimStore, \"" + REPLICATED_CACHE_NAME + "\".DimProduct, " + + "\"" + PARTITIONED_CACHE_NAME + "\".FactPurchase " + + "where DimStore.id=FactPurchase.storeId and DimProduct.id=FactPurchase.productId " + + "and DimStore.name=? and DimProduct.id in(?, ?, ?)") + .setArgs("Store2", p1.getId(), p2.getId(), p3.getId())); + + printQueryResults("All purchases made at store2 for 3 specific products:", prodPurchases.getAll()); + } + + /** + * Print query results. + * + * @param msg Initial message. + * @param res Results to print. + */ + private static <V> void printQueryResults(String msg, Iterable<Cache.Entry<Integer, V>> res) { + System.out.println(msg); + + for (Cache.Entry<?, ?> e : res) + System.out.println(" " + e.getValue().toString()); + } + + /** + * Gets random value from given collection. + * + * @param c Input collection (no {@code null} and not emtpy). + * @return Random value from the input collection. + */ + @SuppressWarnings("UnusedDeclaration") + private static <T> T rand(Collection<? extends T> c) { + if (c == null) + throw new IllegalArgumentException(); + + int n = ThreadLocalRandom.current().nextInt(c.size()); + + int i = 0; + + for (T t : c) { + if (i++ == n) + return t; + } + + throw new ConcurrentModificationException(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimProduct.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimProduct.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimProduct.java new file mode 100644 index 0000000..8c4c08c --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimProduct.java @@ -0,0 +1,101 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.starschema; + +import org.apache.ignite.cache.query.annotations.*; +import org.apache.ignite.cache.*; + +/** + * Represents a product available for purchase. In our {@code snowflake} schema a {@code product} + * is a {@code 'dimension'} and will be cached in {@link CacheMode#REPLICATED} + * cache. + */ +public class DimProduct { + /** Primary key. */ + @QuerySqlField(index = true) + private int id; + + /** Product name. */ + private String name; + + /** Product list price. */ + @QuerySqlField + private float price; + + /** Available product quantity. */ + private int qty; + + /** + * Constructs a product instance. + * + * @param id Product ID. + * @param name Product name. + * @param price Product list price. + * @param qty Available product quantity. + */ + public DimProduct(int id, String name, float price, int qty) { + this.id = id; + this.name = name; + this.price = price; + this.qty = qty; + } + + /** + * Gets product ID. + * + * @return Product ID. + */ + public int getId() { + return id; + } + + /** + * Gets product name. + * + * @return Product name. + */ + public String getName() { + return name; + } + + /** + * Gets product list price. + * + * @return Product list price. + */ + public float getPrice() { + return price; + } + + /** + * Gets available product quantity. + * + * @return Available product quantity. + */ + public int getQuantity() { + return qty; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "DimProduct [id=" + id + + ", name=" + name + + ", price=" + price + + ", qty=" + qty + ']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimStore.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimStore.java new file mode 100644 index 0000000..7b7f49a --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/DimStore.java @@ -0,0 +1,101 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.starschema; + +import org.apache.ignite.cache.query.annotations.*; +import org.apache.ignite.cache.*; + +/** + * Represents a physical store location. In our {@code snowflake} schema a {@code store} + * is a {@code 'dimension'} and will be cached in {@link CacheMode#REPLICATED} + * cache. + */ +public class DimStore { + /** Primary key. */ + @QuerySqlField(index = true) + private int id; + + /** Store name. */ + @QuerySqlField + private String name; + + /** Zip code. */ + private String zip; + + /** Address. */ + private String addr; + + /** + * Constructs a store instance. + * + * @param id Store ID. + * @param name Store name. + * @param zip Store zip code. + * @param addr Store address. + */ + public DimStore(int id, String name, String zip, String addr) { + this.id = id; + this.name = name; + this.zip = zip; + this.addr = addr; + } + + /** + * Gets store ID. + * + * @return Store ID. + */ + public int getId() { + return id; + } + + /** + * Gets store name. + * + * @return Store name. + */ + public String getName() { + return name; + } + + /** + * Gets store zip code. + * + * @return Store zip code. + */ + public String getZip() { + return zip; + } + + /** + * Gets store address. + * + * @return Store address. + */ + public String getAddress() { + return addr; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "DimStore [id=" + id + + ", name=" + name + + ", zip=" + zip + + ", addr=" + addr + ']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/FactPurchase.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/FactPurchase.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/FactPurchase.java new file mode 100644 index 0000000..78d92ac --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/FactPurchase.java @@ -0,0 +1,103 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.starschema; + +import org.apache.ignite.cache.query.annotations.*; +import org.apache.ignite.cache.*; + +/** + * Represents a purchase record. In our {@code snowflake} schema purchase + * is a {@code 'fact'} and will be cached in larger {@link CacheMode#PARTITIONED} + * cache. + */ +public class FactPurchase { + /** Primary key. */ + @QuerySqlField(index = true) + private int id; + + /** Foreign key to store at which purchase occurred. */ + @QuerySqlField + private int storeId; + + /** Foreign key to purchased product. */ + @QuerySqlField + private int productId; + + /** Purchase price. */ + @QuerySqlField + private float purchasePrice; + + /** + * Constructs a purchase record. + * + * @param id Purchase ID. + * @param productId Purchased product ID. + * @param storeId Store ID. + * @param purchasePrice Purchase price. + */ + public FactPurchase(int id, int productId, int storeId, float purchasePrice) { + this.id = id; + this.productId = productId; + this.storeId = storeId; + this.purchasePrice = purchasePrice; + } + + /** + * Gets purchase ID. + * + * @return Purchase ID. + */ + public int getId() { + return id; + } + + /** + * Gets purchased product ID. + * + * @return Product ID. + */ + public int getProductId() { + return productId; + } + + /** + * Gets ID of store at which purchase was made. + * + * @return Store ID. + */ + public int getStoreId() { + return storeId; + } + + /** + * Gets purchase price. + * + * @return Purchase price. + */ + public float getPurchasePrice() { + return purchasePrice; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "FactPurchase [id=" + id + + ", productId=" + productId + + ", storeId=" + storeId + + ", purchasePrice=" + purchasePrice + ']'; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/package-info.java new file mode 100644 index 0000000..fcd7c7a --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/starschema/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Demonstrates distributed SQL joins over ignite using Snowflake schema. + */ +package org.apache.ignite.examples.java7.datagrid.starschema; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheNodeWithStoreStartup.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheNodeWithStoreStartup.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheNodeWithStoreStartup.java new file mode 100644 index 0000000..3191611 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheNodeWithStoreStartup.java @@ -0,0 +1,159 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store; + +import org.apache.ignite.*; +import org.apache.ignite.cache.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.datagrid.store.dummy.*; +import org.apache.ignite.examples.datagrid.store.hibernate.*; +import org.apache.ignite.examples.datagrid.store.jdbc.*; +import org.apache.ignite.examples.datagrid.store.model.*; +import org.apache.ignite.examples.java7.datagrid.store.dummy.*; +import org.apache.ignite.examples.java7.datagrid.store.hibernate.*; +import org.apache.ignite.examples.java7.datagrid.store.jdbc.*; +import org.apache.ignite.examples.java7.datagrid.store.model.*; +import org.apache.ignite.internal.util.typedef.*; +import org.apache.ignite.spi.discovery.tcp.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.multicast.*; +import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.*; + +import javax.cache.configuration.*; +import java.sql.*; +import java.util.*; + +import static org.apache.ignite.cache.CacheAtomicityMode.*; + +/** + * Starts up an empty node with example cache and store configuration. + */ +public class CacheNodeWithStoreStartup { + /** Use org.apache.ignite.examples.datagrid.store.dummy.CacheDummyPersonStore to run example. */ + public static final String DUMMY = "DUMMY"; + + /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPersonStore to run example. */ + public static final String SIMPLE_JDBC = "SIMPLE_JDBC"; + + /** Use org.apache.ignite.examples.datagrid.store.hibernate.CacheHibernatePersonStore to run example. */ + public static final String HIBERNATE = "HIBERNATE"; + + /** Use org.apache.ignite.examples.datagrid.store.jdbc.CacheJdbcPojoPersonStore to run example. */ + public static final String AUTO = "AUTO"; + + /** Store to use. */ + public static final String STORE = DUMMY; + + /** + * Start up an empty node with specified cache configuration. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + Ignition.start(configure()); + } + + /** + * Configure ignite. + * + * @return Ignite configuration. + * @throws IgniteException If failed. + */ + public static IgniteConfiguration configure() throws IgniteException { + IgniteConfiguration cfg = new IgniteConfiguration(); + + cfg.setLocalHost("127.0.0.1"); + + // Discovery SPI. + TcpDiscoverySpi discoSpi = new TcpDiscoverySpi(); + + TcpDiscoveryVmIpFinder ipFinder = new TcpDiscoveryMulticastIpFinder(); + + ipFinder.setAddresses(Arrays.asList("127.0.0.1:47500..47509")); + + discoSpi.setIpFinder(ipFinder); + + CacheConfiguration<Long, Person> cacheCfg = new CacheConfiguration<>(); + + // Set atomicity as transaction, since we are showing transactions in example. + cacheCfg.setAtomicityMode(TRANSACTIONAL); + + cacheCfg.setCacheStoreFactory(new Factory<CacheStore<? super Long, ? super Person>>() { + @Override public CacheStore<? super Long, ? super Person> create() { + CacheStore<Long, Person> store; + + switch (STORE) { + case DUMMY: + store = new CacheDummyPersonStore(); + break; + + case SIMPLE_JDBC: + store = new CacheJdbcPersonStore(); + break; + + case HIBERNATE: + store = new CacheHibernatePersonStore(); + break; + + default: + if (!STORE.equals(AUTO)) + throw new IllegalStateException("Unexpected store configured: " + STORE); + + store = new CacheJdbcPojoPersonStore(); + break; + } + + return store; + } + }); + + if (STORE.equals(AUTO)) + cacheCfg.setTypeMetadata(typeMetadata()); + + cacheCfg.setReadThrough(true); + cacheCfg.setWriteThrough(true); + + cfg.setDiscoverySpi(discoSpi); + cfg.setCacheConfiguration(cacheCfg); + + return cfg; + } + + /** + * @return Type mapping description. + */ + private static Collection<CacheTypeMetadata> typeMetadata() { + CacheTypeMetadata tm = new CacheTypeMetadata(); + + tm.setDatabaseTable("PERSON"); + + tm.setKeyType("java.lang.Long"); + tm.setValueType("org.apache.ignite.examples.datagrid.store.model.Person"); + + tm.setKeyFields(F.asList(new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", Long.class))); + + tm.setValueFields(F.asList( + new CacheTypeFieldMetadata("ID", Types.BIGINT, "id", long.class), + new CacheTypeFieldMetadata("FIRST_NAME", Types.VARCHAR, "firstName", String.class), + new CacheTypeFieldMetadata("LAST_NAME", Types.VARCHAR, "lastName", String.class) + )); + + return F.asList(tm); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreExample.java new file mode 100644 index 0000000..bcf43a1 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreExample.java @@ -0,0 +1,108 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store; + +import org.apache.ignite.*; +import org.apache.ignite.configuration.*; +import org.apache.ignite.examples.datagrid.store.model.*; +import org.apache.ignite.examples.java7.datagrid.store.model.*; +import org.apache.ignite.transactions.*; + +import java.util.*; + +import static org.apache.ignite.examples.java7.datagrid.store.CacheNodeWithStoreStartup.*; + +/** + * Demonstrates usage of cache with underlying persistent store configured. + * <p> + * Remote nodes should always be started using {@link CacheNodeWithStoreStartup}. + * Also you can change type of underlying store modifying configuration in the + * {@link CacheNodeWithStoreStartup#configure()} method. + */ +public class CacheStoreExample { + /** Global person ID to use across entire example. */ + private static final Long id = Math.abs(UUID.randomUUID().getLeastSignificantBits()); + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + IgniteConfiguration cfg = CacheNodeWithStoreStartup.configure(); + + // To start ignite with desired configuration uncomment the appropriate line. + try (Ignite ignite = Ignition.start(cfg)) { + System.out.println(); + System.out.println(">>> Cache store example started."); + System.out.println(">>> Store: " + STORE); + + IgniteCache<Long, Person> cache = ignite.jcache(null); + + // Clean up caches on all nodes before run. + cache.clear(); + + try (Transaction tx = ignite.transactions().txStart()) { + Person val = cache.get(id); + + System.out.println("Read value: " + val); + + val = cache.getAndPut(id, person(id, "Isaac", "Newton")); + + System.out.println("Overwrote old value: " + val); + + val = cache.get(id); + + System.out.println("Read value: " + val); + + tx.commit(); + } + + System.out.println("Read value after commit: " + cache.get(id)); + + // If example run with CacheJdbcPojoStore. + // Example of CacheJdbcPojoStore special features. + if (STORE.equals(AUTO)) { + System.out.println(">>> Example of CacheJdbcPojoStore special feature: load from DB with custom SQL."); + + cache.clear(); + + System.out.println("Cache size: " + cache.size()); + + // Load values from DB into store with custom SQL. + cache.loadCache(null, "java.lang.Long", "select * from PERSON where id = 2"); + + System.out.println("Cache size: " + cache.size()); + System.out.println("Person: " + cache.get(2L)); + } + } + } + + /** + * Creates person. + * + * @param id ID. + * @param firstName First name. + * @param lastName Last name. + * @return Newly created person. + */ + private static Person person(long id, String firstName, String lastName) { + return new Person(id, firstName, lastName); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreLoadDataExample.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreLoadDataExample.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreLoadDataExample.java new file mode 100644 index 0000000..e1b0165 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/CacheStoreLoadDataExample.java @@ -0,0 +1,67 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store; + +import org.apache.ignite.*; +import org.apache.ignite.examples.java7.*; +import org.apache.ignite.lang.*; + +/** + * Loads data on all cache nodes from persistent store at cache startup by calling + * {@link IgniteCache#loadCache(IgniteBiPredicate, Object...)} method. + * <p> + * Remote nodes should always be started using {@link CacheNodeWithStoreStartup}. + * Also you can change type of underlying store modifying configuration in the + * {@link CacheNodeWithStoreStartup#configure()} method. + */ +public class CacheStoreLoadDataExample { + /** Heap size required to run this example. */ + public static final int MIN_MEMORY = 1024 * 1024 * 1024; + + /** Number of entries to load. */ + private static final int ENTRY_COUNT = 100_000; + + /** + * Executes example. + * + * @param args Command line arguments, none required. + * @throws IgniteException If example execution failed. + */ + public static void main(String[] args) throws IgniteException { + ExamplesUtils.checkMinMemory(MIN_MEMORY); + + try (Ignite ignite = Ignition.start(CacheNodeWithStoreStartup.configure())) { + System.out.println(); + System.out.println(">>> Cache store load data example started."); + + final IgniteCache<String, Integer> cache = ignite.jcache(null); + + // Clean up caches on all nodes before run. + cache.clear(); + + long start = System.currentTimeMillis(); + + // Start loading cache from persistent store on all caching nodes. + cache.loadCache(null, ENTRY_COUNT); + + long end = System.currentTimeMillis(); + + System.out.println(">>> Loaded " + cache.size() + " keys with backups in " + (end - start) + "ms."); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/CacheDummyPersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/CacheDummyPersonStore.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/CacheDummyPersonStore.java new file mode 100644 index 0000000..ff1be52 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/CacheDummyPersonStore.java @@ -0,0 +1,121 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store.dummy; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.examples.datagrid.store.model.*; +import org.apache.ignite.examples.java7.datagrid.store.model.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; +import org.apache.ignite.transactions.*; +import org.jetbrains.annotations.*; + +import java.util.*; +import java.util.concurrent.*; + +/** + * Dummy cache store implementation. + */ +public class CacheDummyPersonStore extends CacheStoreAdapter<Long, Person> { + /** Auto-inject ignite instance. */ + @IgniteInstanceResource + private Ignite ignite; + + /** Auto-inject cache name. */ + @CacheNameResource + private String cacheName; + + /** */ + @CacheStoreSessionResource + private CacheStoreSession ses; + + /** Dummy database. */ + private Map<Long, Person> dummyDB = new ConcurrentHashMap<>(); + + /** {@inheritDoc} */ + @Override public Person load(Long key) { + Transaction tx = transaction(); + + System.out.println(">>> Store load [key=" + key + ", xid=" + (tx == null ? null : tx.xid()) + ']'); + + return dummyDB.get(key); + } + + /** {@inheritDoc} */ + @Override public void write(javax.cache.Cache.Entry<? extends Long, ? extends Person> entry) { + Transaction tx = transaction(); + + Long key = entry.getKey(); + Person val = entry.getValue(); + + System.out.println(">>> Store put [key=" + key + ", val=" + val + ", xid=" + (tx == null ? null : tx.xid()) + ']'); + + dummyDB.put(key, val); + } + + /** {@inheritDoc} */ + @Override public void delete(Object key) { + Transaction tx = transaction(); + + System.out.println(">>> Store remove [key=" + key + ", xid=" + (tx == null ? null : tx.xid()) + ']'); + + dummyDB.remove(key); + } + + /** {@inheritDoc} */ + @Override public void loadCache(IgniteBiInClosure<Long, Person> clo, Object... args) { + int cnt = (Integer)args[0]; + + System.out.println(">>> Store loadCache for entry count: " + cnt); + + for (int i = 0; i < cnt; i++) { + // Generate dummy person on the fly. + Person p = new Person(i, "first-" + i, "last-" + 1); + + // Ignite will automatically discard entries that don't belong on this node, + // but we check if local node is primary or backup anyway just to demonstrate that we can. + // Ideally, partition ID of a key would be stored in the database and only keys + // for partitions that belong on this node would be loaded from database. + if (ignite.affinity(cacheName).isPrimaryOrBackup(ignite.cluster().localNode(), p.getId())) { + // Update dummy database. + // In real life data would be loaded from database. + dummyDB.put(p.getId(), p); + + // Pass data to cache. + clo.apply(p.getId(), p); + } + } + } + + /** + * @return Current transaction. + */ + @Nullable private Transaction transaction() { + CacheStoreSession ses = session(); + + return ses != null ? ses.transaction() : null; + } + + /** + * @return Store session. + */ + private CacheStoreSession session() { + return ses; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/package-info.java new file mode 100644 index 0000000..10870ea --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/dummy/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Contains dummy cache store implementation. + */ +package org.apache.ignite.examples.java7.datagrid.store.dummy; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/CacheHibernatePersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/CacheHibernatePersonStore.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/CacheHibernatePersonStore.java new file mode 100644 index 0000000..16c199e --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/CacheHibernatePersonStore.java @@ -0,0 +1,292 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store.hibernate; + +import org.apache.ignite.cache.store.*; +import org.apache.ignite.examples.datagrid.store.model.*; +import org.apache.ignite.examples.java7.datagrid.store.model.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; +import org.apache.ignite.transactions.Transaction; +import org.hibernate.*; +import org.hibernate.cfg.*; +import org.jetbrains.annotations.*; + +import javax.cache.integration.*; +import java.util.*; + +/** + * Example of {@link CacheStore} implementation that uses Hibernate + * and deals with maps {@link UUID} to {@link Person}. + */ +public class CacheHibernatePersonStore extends CacheStoreAdapter<Long, Person> { + /** Default hibernate configuration resource path. */ + private static final String DFLT_HIBERNATE_CFG = "/org/apache/ignite/examples/java7/datagrid/store/hibernate" + + "/hibernate.cfg.xml"; + + /** Session attribute name. */ + private static final String ATTR_SES = "HIBERNATE_STORE_SESSION"; + + /** Session factory. */ + private SessionFactory sesFactory; + + /** Auto-injected store session. */ + @CacheStoreSessionResource + private CacheStoreSession ses; + + /** + * Default constructor. + */ + public CacheHibernatePersonStore() { + sesFactory = new Configuration().configure(DFLT_HIBERNATE_CFG).buildSessionFactory(); + } + + /** {@inheritDoc} */ + @Override public Person load(Long key) { + Transaction tx = transaction(); + + System.out.println(">>> Store load [key=" + key + ", xid=" + (tx == null ? null : tx.xid()) + ']'); + + Session ses = session(tx); + + try { + return (Person) ses.get(Person.class, key); + } + catch (HibernateException e) { + rollback(ses, tx); + + throw new CacheLoaderException("Failed to load value from cache store with key: " + key, e); + } + finally { + end(ses, tx); + } + } + + /** {@inheritDoc} */ + @Override public void write(javax.cache.Cache.Entry<? extends Long, ? extends Person> entry) { + Transaction tx = transaction(); + + Long key = entry.getKey(); + + Person val = entry.getValue(); + + System.out.println(">>> Store put [key=" + key + ", val=" + val + ", xid=" + (tx == null ? null : tx.xid()) + ']'); + + if (val == null) { + delete(key); + + return; + } + + Session ses = session(tx); + + try { + ses.saveOrUpdate(val); + } + catch (HibernateException e) { + rollback(ses, tx); + + throw new CacheWriterException("Failed to put value to cache store [key=" + key + ", val" + val + "]", e); + } + finally { + end(ses, tx); + } + } + + /** {@inheritDoc} */ + @SuppressWarnings({"JpaQueryApiInspection"}) + @Override public void delete(Object key) { + Transaction tx = transaction(); + + System.out.println(">>> Store remove [key=" + key + ", xid=" + (tx == null ? null : tx.xid()) + ']'); + + Session ses = session(tx); + + try { + ses.createQuery("delete " + Person.class.getSimpleName() + " where key = :key") + .setParameter("key", key).setFlushMode(FlushMode.ALWAYS).executeUpdate(); + } + catch (HibernateException e) { + rollback(ses, tx); + + throw new CacheWriterException("Failed to remove value from cache store with key: " + key, e); + } + finally { + end(ses, tx); + } + } + + /** {@inheritDoc} */ + @Override public void loadCache(IgniteBiInClosure<Long, Person> clo, Object... args) { + if (args == null || args.length == 0 || args[0] == null) + throw new CacheLoaderException("Expected entry count parameter is not provided."); + + final int entryCnt = (Integer)args[0]; + + Session ses = session(null); + + try { + int cnt = 0; + + List res = ses.createCriteria(Person.class).list(); + + if (res != null) { + Iterator iter = res.iterator(); + + while (cnt < entryCnt && iter.hasNext()) { + Person person = (Person)iter.next(); + + clo.apply(person.getId(), person); + + cnt++; + } + } + + System.out.println(">>> Loaded " + cnt + " values into cache."); + } + catch (HibernateException e) { + throw new CacheLoaderException("Failed to load values from cache store.", e); + } + finally { + end(ses, null); + } + } + + /** + * Rolls back hibernate session. + * + * @param ses Hibernate session. + * @param tx Cache ongoing transaction. + */ + private void rollback(Session ses, Transaction tx) { + // Rollback only if there is no cache transaction, + // otherwise txEnd() will do all required work. + if (tx == null) { + org.hibernate.Transaction hTx = ses.getTransaction(); + + if (hTx != null && hTx.isActive()) + hTx.rollback(); + } + } + + /** + * Ends hibernate session. + * + * @param ses Hibernate session. + * @param tx Cache ongoing transaction. + */ + private void end(Session ses, @Nullable Transaction tx) { + // Commit only if there is no cache transaction, + // otherwise txEnd() will do all required work. + if (tx == null) { + org.hibernate.Transaction hTx = ses.getTransaction(); + + if (hTx != null && hTx.isActive()) + hTx.commit(); + + ses.close(); + } + } + + /** {@inheritDoc} */ + @Override public void txEnd(boolean commit) { + CacheStoreSession storeSes = session(); + + Transaction tx = storeSes.transaction(); + + Map<String, Session> props = storeSes.properties(); + + Session ses = props.remove(ATTR_SES); + + if (ses != null) { + org.hibernate.Transaction hTx = ses.getTransaction(); + + if (hTx != null) { + try { + if (commit) { + ses.flush(); + + hTx.commit(); + } + else + hTx.rollback(); + + System.out.println("Transaction ended [xid=" + tx.xid() + ", commit=" + commit + ']'); + } + catch (HibernateException e) { + throw new CacheWriterException("Failed to end transaction [xid=" + tx.xid() + + ", commit=" + commit + ']', e); + } + finally { + ses.close(); + } + } + } + } + + /** + * Gets Hibernate session. + * + * @param tx Cache transaction. + * @return Session. + */ + private Session session(@Nullable Transaction tx) { + Session ses; + + if (tx != null) { + Map<String, Session> props = session().properties(); + + ses = props.get(ATTR_SES); + + if (ses == null) { + ses = sesFactory.openSession(); + + ses.beginTransaction(); + + // Store session in session properties, so it can be accessed + // for other operations on the same transaction. + props.put(ATTR_SES, ses); + + System.out.println("Hibernate session open [ses=" + ses + ", tx=" + tx.xid() + "]"); + } + } + else { + ses = sesFactory.openSession(); + + ses.beginTransaction(); + } + + return ses; + } + + /** + * @return Current transaction. + */ + @Nullable private Transaction transaction() { + CacheStoreSession ses = session(); + + return ses != null ? ses.transaction() : null; + } + + /** + * @return Store session. + */ + private CacheStoreSession session() { + return ses; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/Person.hbm.xml ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/Person.hbm.xml b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/Person.hbm.xml new file mode 100644 index 0000000..ea9b640 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/Person.hbm.xml @@ -0,0 +1,34 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + 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. +--> + + +<!DOCTYPE hibernate-mapping PUBLIC + "-//Hibernate/Hibernate Mapping DTD 3.0//EN" + "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> + +<hibernate-mapping default-access="field"> + <class name="org.apache.ignite.examples.java7.datagrid.store.model.Person" table="PERSONS"> + <!-- ID. --> + <id name="id"/> + + <!-- We only map data we are interested in. --> + <property name="firstName"/> + <property name="lastName"/> + </class> +</hibernate-mapping> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/hibernate.cfg.xml ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/hibernate.cfg.xml b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/hibernate.cfg.xml new file mode 100644 index 0000000..80a43e7 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/hibernate.cfg.xml @@ -0,0 +1,41 @@ +<?xml version='1.0' encoding='utf-8'?> + +<!-- + 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. +--> + +<!DOCTYPE hibernate-configuration PUBLIC + "-//Hibernate/Hibernate Configuration DTD 3.0//EN" + "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> + +<!-- + Hibernate configuration. +--> +<hibernate-configuration> + <session-factory> + <!-- Database connection settings (private in-memory database). --> + <property name="connection.url">jdbc:h2:mem:example;DB_CLOSE_DELAY=-1</property> + + <!-- Only validate the database schema on startup in production mode. --> + <property name="hbm2ddl.auto">update</property> + + <!-- Do not output SQL. --> + <property name="show_sql">false</property> + + <!-- Mappings. --> + <mapping resource="org/apache/ignite/examples/datagrid/store/hibernate/Person.hbm.xml"/> + </session-factory> +</hibernate-configuration> http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/package-info.java new file mode 100644 index 0000000..7f95e02 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/hibernate/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Contains Hibernate-based cache store implementation. + */ +package org.apache.ignite.examples.java7.datagrid.store.hibernate; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPersonStore.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPersonStore.java new file mode 100644 index 0000000..daf1674 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPersonStore.java @@ -0,0 +1,276 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store.jdbc; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.*; +import org.apache.ignite.examples.datagrid.store.model.*; +import org.apache.ignite.examples.java7.datagrid.store.model.*; +import org.apache.ignite.lang.*; +import org.apache.ignite.resources.*; +import org.jetbrains.annotations.*; + +import javax.cache.*; +import javax.cache.integration.*; +import java.sql.*; +import java.util.*; + +/** + * Example of {@link CacheStore} implementation that uses JDBC + * transaction with cache transactions and maps {@link Long} to {@link Person}. + * + */ +public class CacheJdbcPersonStore extends CacheStoreAdapter<Long, Person> { + /** Transaction metadata attribute name. */ + private static final String ATTR_NAME = "SIMPLE_STORE_CONNECTION"; + + /** Auto-injected store session. */ + @CacheStoreSessionResource + private CacheStoreSession ses; + + /** + * Constructor. + * + * @throws IgniteException If failed. + */ + public CacheJdbcPersonStore() throws IgniteException { + prepareDb(); + } + + /** + * Prepares database for example execution. This method will create a + * table called "PERSONS" so it can be used by store implementation. + * + * @throws IgniteException If failed. + */ + private void prepareDb() throws IgniteException { + try (Connection conn = openConnection(false); Statement st = conn.createStatement()) { + st.execute("create table if not exists PERSONS (id number unique, firstName varchar(255), " + + "lastName varchar(255))"); + + conn.commit(); + } + catch (SQLException e) { + throw new IgniteException("Failed to create database table.", e); + } + } + + /** {@inheritDoc} */ + @Override public void txEnd(boolean commit) { + Map<String, Connection> props = ses.properties(); + + try (Connection conn = props.remove(ATTR_NAME)) { + if (conn != null) { + if (commit) + conn.commit(); + else + conn.rollback(); + } + + System.out.println(">>> Transaction ended [commit=" + commit + ']'); + } + catch (SQLException e) { + throw new CacheWriterException("Failed to end transaction: " + ses.transaction(), e); + } + } + + /** {@inheritDoc} */ + @Override public Person load(Long key) { + System.out.println(">>> Loading key: " + key); + + Connection conn = null; + + try { + conn = connection(); + + try (PreparedStatement st = conn.prepareStatement("select * from PERSONS where id=?")) { + st.setString(1, key.toString()); + + ResultSet rs = st.executeQuery(); + + if (rs.next()) + return new Person(rs.getLong(1), rs.getString(2), rs.getString(3)); + } + } + catch (SQLException e) { + throw new CacheLoaderException("Failed to load object: " + key, e); + } + finally { + end(conn); + } + + return null; + } + + /** {@inheritDoc} */ + @Override public void write(Cache.Entry<? extends Long, ? extends Person> entry) { + Long key = entry.getKey(); + + Person val = entry.getValue(); + + System.out.println(">>> Putting [key=" + key + ", val=" + val + ']'); + + Connection conn = null; + + try { + conn = connection(); + + int updated; + + // Try update first. If it does not work, then try insert. + // Some databases would allow these to be done in one 'upsert' operation. + try (PreparedStatement st = conn.prepareStatement( + "update PERSONS set firstName=?, lastName=? where id=?")) { + st.setString(1, val.getFirstName()); + st.setString(2, val.getLastName()); + st.setLong(3, val.getId()); + + updated = st.executeUpdate(); + } + + // If update failed, try to insert. + if (updated == 0) { + try (PreparedStatement st = conn.prepareStatement( + "insert into PERSONS (id, firstName, lastName) values(?, ?, ?)")) { + st.setLong(1, val.getId()); + st.setString(2, val.getFirstName()); + st.setString(3, val.getLastName()); + + st.executeUpdate(); + } + } + } + catch (SQLException e) { + throw new CacheLoaderException("Failed to put object [key=" + key + ", val=" + val + ']', e); + } + finally { + end(conn); + } + } + + /** {@inheritDoc} */ + @Override public void delete(Object key) { + System.out.println(">>> Removing key: " + key); + + Connection conn = null; + + try { + conn = connection(); + + try (PreparedStatement st = conn.prepareStatement("delete from PERSONS where id=?")) { + st.setLong(1, (Long)key); + + st.executeUpdate(); + } + } + catch (SQLException e) { + throw new CacheWriterException("Failed to remove object: " + key, e); + } + finally { + end(conn); + } + } + + /** {@inheritDoc} */ + @Override public void loadCache(IgniteBiInClosure<Long, Person> clo, Object... args) { + if (args == null || args.length == 0 || args[0] == null) + throw new CacheLoaderException("Expected entry count parameter is not provided."); + + final int entryCnt = (Integer)args[0]; + + try (Connection conn = connection()) { + try (PreparedStatement st = conn.prepareStatement("select * from PERSONS")) { + try (ResultSet rs = st.executeQuery()) { + int cnt = 0; + + while (cnt < entryCnt && rs.next()) { + Person person = new Person(rs.getLong(1), rs.getString(2), rs.getString(3)); + + clo.apply(person.getId(), person); + + cnt++; + } + + System.out.println(">>> Loaded " + cnt + " values into cache."); + } + } + } + catch (SQLException e) { + throw new CacheLoaderException("Failed to load values from cache store.", e); + } + } + + /** + * @return Connection. + * @throws SQLException In case of error. + */ + private Connection connection() throws SQLException { + // If there is an ongoing transaction, + // we must reuse the same connection. + if (ses.isWithinTransaction()) { + Map<Object, Object> props = ses.properties(); + + Connection conn = (Connection)props.get(ATTR_NAME); + + if (conn == null) { + conn = openConnection(false); + + // Store connection in session properties, so it can be accessed + // for other operations on the same transaction. + props.put(ATTR_NAME, conn); + } + + return conn; + } + // Transaction can be null in case of simple load or put operation. + else + return openConnection(true); + } + + /** + * Closes allocated resources depending on transaction status. + * + * @param conn Allocated connection. + */ + private void end(@Nullable Connection conn) { + if (!ses.isWithinTransaction() && conn != null) { + // Close connection right away if there is no transaction. + try { + conn.close(); + } + catch (SQLException ignored) { + // No-op. + } + } + } + + /** + * Gets connection from a pool. + * + * @param autocommit {@code true} If connection should use autocommit mode. + * @return Pooled connection. + * @throws SQLException In case of error. + */ + private Connection openConnection(boolean autocommit) throws SQLException { + Connection conn = DriverManager.getConnection("jdbc:h2:mem:example;DB_CLOSE_DELAY=-1"); + + conn.setAutoCommit(autocommit); + + return conn; + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java new file mode 100644 index 0000000..87efd33 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/CacheJdbcPojoPersonStore.java @@ -0,0 +1,83 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store.jdbc; + +import org.apache.ignite.*; +import org.apache.ignite.cache.store.jdbc.*; +import org.apache.ignite.examples.datagrid.store.model.*; +import org.apache.ignite.examples.java7.datagrid.store.model.*; +import org.apache.ignite.internal.util.typedef.internal.*; +import org.h2.tools.*; + +import javax.cache.*; +import java.io.*; +import java.sql.*; + +/** + * Example of {@link CacheJdbcPojoStore} implementation that uses JDBC + * transaction with cache transactions and maps {@link Long} to {@link Person}. + */ +public class CacheJdbcPojoPersonStore extends CacheJdbcPojoStore<Long, Person> { + /** + * Constructor. + * + * @throws IgniteException If failed. + */ + public CacheJdbcPojoPersonStore() throws IgniteException { + try { + // Try to connect to database server. + dataSrc = org.h2.jdbcx.JdbcConnectionPool.create("jdbc:h2:tcp://localhost/mem:ExampleDb", "sa", ""); + + resolveDialect(); + } + catch (CacheException ignore) { + // Construct example database in memory. + dataSrc = org.h2.jdbcx.JdbcConnectionPool.create("jdbc:h2:mem:ExampleDb;DB_CLOSE_DELAY=-1", "sa", ""); + + prepareDb(); + } + } + + /** + * Prepares database for example execution. This method will create a table called "PERSONS" + * so it can be used by store implementation. + * + * @throws IgniteException If failed. + */ + private void prepareDb() throws IgniteException { + File script = U.resolveIgnitePath("examples/config/store/example-database.script"); + + if (script == null) + throw new IgniteException("Failed to find example database script: " + + "examples/config/store/example-database.script"); + + try { + // Start H2 database TCP server in order to access sample in-memory database from other processes. + Server.createTcpServer("-tcpDaemon").start(); + + // Load sample data into database. + RunScript.execute(dataSrc.getConnection(), new FileReader(script)); + } + catch (SQLException e) { + throw new IgniteException("Failed to initialize database", e); + } + catch (FileNotFoundException e) { + throw new IgniteException("Failed to find example database script: " + script.getPath(), e); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/package-info.java new file mode 100644 index 0000000..fcb21ae --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/jdbc/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Contains JDBC-based cache store implementation. + */ +package org.apache.ignite.examples.java7.datagrid.store.jdbc; http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/model/Person.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/model/Person.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/model/Person.java new file mode 100644 index 0000000..071baf6 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/model/Person.java @@ -0,0 +1,155 @@ +/* + * 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. + */ + +package org.apache.ignite.examples.java7.datagrid.store.model; + +import java.io.*; + +/** + * Person definition. + * + * Code generated by Apache Ignite Schema Import utility: 02/24/2015. + */ +public class Person implements Serializable { + /** */ + private static final long serialVersionUID = 0L; + + /** Value for id. */ + private long id; + + /** Value for first name. */ + private String firstName; + + /** Value for last name. */ + private String lastName; + + /** + * Empty constructor. + */ + public Person() { + // No-op. + } + + /** + * Full constructor. + */ + public Person( + long id, + String firstName, + String lastName + ) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + /** + * Gets id. + * + * @return Value for id. + */ + public long getId() { + return id; + } + + /** + * Sets id. + * + * @param id New value for id. + */ + public void setId(long id) { + this.id = id; + } + + /** + * Gets first name. + * + * @return Value for first name. + */ + public String getFirstName() { + return firstName; + } + + /** + * Sets first name. + * + * @param firstName New value for first name. + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * Gets last name. + * + * @return Value for last name. + */ + public String getLastName() { + return lastName; + } + + /** + * Sets last name. + * + * @param lastName New value for last name. + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) + return true; + + if (!(o instanceof Person)) + return false; + + Person that = (Person)o; + + if (id != that.id) + return false; + + if (firstName != null ? !firstName.equals(that.firstName) : that.firstName != null) + return false; + + if (lastName != null ? !lastName.equals(that.lastName) : that.lastName != null) + return false; + + return true; + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + int res = (int)(id ^ (id >>> 32)); + + res = 31 * res + (firstName != null ? firstName.hashCode() : 0); + + res = 31 * res + (lastName != null ? lastName.hashCode() : 0); + + return res; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return "Person [id=" + id + + ", firstName=" + firstName + + ", lastName=" + lastName + + "]"; + } +} + http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/be0e755b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/package-info.java ---------------------------------------------------------------------- diff --git a/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/package-info.java b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/package-info.java new file mode 100644 index 0000000..15127f8 --- /dev/null +++ b/examples/src/main/java/org/apache/ignite/examples/java7/datagrid/store/package-info.java @@ -0,0 +1,22 @@ +/* + * 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. + */ + +/** + * <!-- Package description. --> + * Demonstrates using of cache store. + */ +package org.apache.ignite.examples.java7.datagrid.store;