ignite-1794
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/be40f937 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/be40f937 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/be40f937 Branch: refs/heads/ignite-1794 Commit: be40f937784883a6de3ba4479e9f5f9f4e6656c1 Parents: 979d872 Author: sboikov <[email protected]> Authored: Thu Apr 20 11:58:19 2017 +0300 Committer: sboikov <[email protected]> Committed: Thu Apr 20 11:58:19 2017 +0300 ---------------------------------------------------------------------- assembly/dependencies-fabric.xml | 3 +- modules/hibernate-5.1/README.txt | 48 + modules/hibernate-5.1/licenses/apache-2.0.txt | 202 ++ modules/hibernate-5.1/pom.xml | 152 ++ .../HibernateAbstractRegionAccessStrategy.java | 103 + .../hibernate/HibernateCollectionRegion.java | 114 + .../cache/hibernate/HibernateEntityRegion.java | 128 ++ .../hibernate/HibernateGeneralDataRegion.java | 79 + .../cache/hibernate/HibernateKeyWrapper.java | 109 + .../hibernate/HibernateNaturalIdRegion.java | 113 + .../hibernate/HibernateQueryResultsRegion.java | 70 + .../ignite/cache/hibernate/HibernateRegion.java | 99 + .../cache/hibernate/HibernateRegionFactory.java | 168 ++ .../hibernate/HibernateTimestampsRegion.java | 39 + .../HibernateTransactionalDataRegion.java | 84 + .../ignite/cache/hibernate/package-info.java | 24 + .../hibernate/CacheHibernateBlobStore.java | 543 +++++ .../CacheHibernateBlobStoreEntry.hbm.xml | 31 + .../hibernate/CacheHibernateBlobStoreEntry.java | 89 + .../CacheHibernateBlobStoreFactory.java | 235 +++ .../CacheHibernateStoreSessionListener.java | 224 ++ .../cache/store/hibernate/package-info.java | 22 + .../src/test/config/factory-cache.xml | 59 + .../src/test/config/factory-cache1.xml | 61 + .../config/factory-incorrect-store-cache.xml | 56 + .../HibernateL2CacheConfigurationSelfTest.java | 407 ++++ .../hibernate/HibernateL2CacheSelfTest.java | 1946 ++++++++++++++++++ .../HibernateL2CacheTransactionalSelfTest.java | 154 ++ ...nateL2CacheTransactionalUseSyncSelfTest.java | 31 + .../CacheHibernateBlobStoreNodeRestartTest.java | 46 + .../CacheHibernateBlobStoreSelfTest.java | 114 + .../CacheHibernateStoreFactorySelfTest.java | 253 +++ ...heHibernateStoreSessionListenerSelfTest.java | 242 +++ .../cache/store/hibernate/hibernate.cfg.xml | 42 + .../cache/store/hibernate/package-info.java | 22 + .../IgniteBinaryHibernate5TestSuite.java | 37 + .../testsuites/IgniteHibernate5TestSuite.java | 57 + modules/hibernate5/README.txt | 48 - modules/hibernate5/licenses/apache-2.0.txt | 202 -- modules/hibernate5/pom.xml | 152 -- .../HibernateAbstractRegionAccessStrategy.java | 103 - .../hibernate/HibernateCollectionRegion.java | 114 - .../cache/hibernate/HibernateEntityRegion.java | 129 -- .../hibernate/HibernateGeneralDataRegion.java | 79 - .../cache/hibernate/HibernateKeyWrapper.java | 109 - .../hibernate/HibernateNaturalIdRegion.java | 113 - .../hibernate/HibernateQueryResultsRegion.java | 70 - .../ignite/cache/hibernate/HibernateRegion.java | 99 - .../cache/hibernate/HibernateRegionFactory.java | 168 -- .../hibernate/HibernateTimestampsRegion.java | 39 - .../HibernateTransactionalDataRegion.java | 84 - .../ignite/cache/hibernate/package-info.java | 24 - .../hibernate/CacheHibernateBlobStore.java | 542 ----- .../CacheHibernateBlobStoreEntry.hbm.xml | 31 - .../hibernate/CacheHibernateBlobStoreEntry.java | 89 - .../CacheHibernateBlobStoreFactory.java | 235 --- .../CacheHibernateStoreSessionListener.java | 223 -- .../cache/store/hibernate/package-info.java | 22 - .../src/test/config/factory-cache.xml | 59 - .../src/test/config/factory-cache1.xml | 61 - .../config/factory-incorrect-store-cache.xml | 56 - .../HibernateL2CacheConfigurationSelfTest.java | 407 ---- .../hibernate/HibernateL2CacheSelfTest.java | 1946 ------------------ .../HibernateL2CacheTransactionalSelfTest.java | 154 -- ...nateL2CacheTransactionalUseSyncSelfTest.java | 31 - .../CacheHibernateBlobStoreNodeRestartTest.java | 46 - .../CacheHibernateBlobStoreSelfTest.java | 113 - .../CacheHibernateStoreFactorySelfTest.java | 326 --- ...heHibernateStoreSessionListenerSelfTest.java | 241 --- .../cache/store/hibernate/hibernate.cfg.xml | 42 - .../cache/store/hibernate/package-info.java | 22 - .../IgniteBinaryHibernate5TestSuite.java | 37 - .../testsuites/IgniteHibernate5TestSuite.java | 57 - pom.xml | 6 +- 74 files changed, 6209 insertions(+), 6276 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/assembly/dependencies-fabric.xml ---------------------------------------------------------------------- diff --git a/assembly/dependencies-fabric.xml b/assembly/dependencies-fabric.xml index 23a1731..342c6f2 100644 --- a/assembly/dependencies-fabric.xml +++ b/assembly/dependencies-fabric.xml @@ -128,7 +128,8 @@ <exclude>org.apache.ignite:ignite-codegen</exclude> <exclude>org.apache.ignite:ignite-apache-license-gen</exclude> <exclude>org.apache.ignite:ignite-hibernate</exclude> - <exclude>org.apache.ignite:ignite-hibernate5</exclude> + <exclude>org.apache.ignite:ignite-hibernate-core</exclude> + <exclude>org.apache.ignite:ignite-hibernate_5.1</exclude> <exclude>org.apache.ignite:ignite-schedule</exclude> <exclude>org.apache.ignite:ignite-geospatial</exclude> <exclude>org.apache.ignite:ignite-appserver-test</exclude> http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/README.txt ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/README.txt b/modules/hibernate-5.1/README.txt new file mode 100644 index 0000000..370258b --- /dev/null +++ b/modules/hibernate-5.1/README.txt @@ -0,0 +1,48 @@ +Apache Ignite Hibernate Module +------------------------------ + +Apache Ignite Hibernate module provides Hibernate second-level cache (L2 cache) implementation based +on Apache Ignite In-Memory Data Grid. + +To enable Hibernate module when starting a standalone node, move 'optional/ignite-hibernate5' folder to +'libs' folder before running 'ignite.{sh|bat}' script. The content of the module folder will +be added to classpath in this case. + +Importing Hibernate Module In Maven Project +------------------------------------------- + +If you are using Maven to manage dependencies of your project, you can add Hibernate module +dependency like this (replace '${ignite.version}' with actual Ignite version you are +interested in): + +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + ... + <dependencies> + ... + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-hibernate5</artifactId> + <version>${ignite.version}</version> + </dependency> + ... + </dependencies> + ... +</project> + + +LGPL dependencies +----------------- + +Ignite includes the following optional LGPL dependencies: + - Hibernate L2 Cache Integration, http://hibernate.org/orm/ + - JTS Topology Suite for Geospatial indexing, http://tsusiatsoftware.net/jts/main.html + - cron4j for cron-based task scheduling, http://www.sauronsoftware.it/projects/cron4j + +Apache binary releases cannot include LGPL dependencies. If you would like include +optional LGPL dependencies into your release, you should download the source release +from Ignite website and do the build with the following maven command: + +mvn clean package -DskipTests -Prelease,lgpl http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/licenses/apache-2.0.txt ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/licenses/apache-2.0.txt b/modules/hibernate-5.1/licenses/apache-2.0.txt new file mode 100644 index 0000000..d645695 --- /dev/null +++ b/modules/hibernate-5.1/licenses/apache-2.0.txt @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed 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. http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/pom.xml ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/pom.xml b/modules/hibernate-5.1/pom.xml new file mode 100644 index 0000000..aa86486 --- /dev/null +++ b/modules/hibernate-5.1/pom.xml @@ -0,0 +1,152 @@ +<?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. +--> + +<!-- + POM file. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-parent</artifactId> + <version>1</version> + <relativePath>../../parent</relativePath> + </parent> + + <artifactId>ignite-hibernate_5.1</artifactId> + <version>2.0.0-SNAPSHOT</version> + <url>http://ignite.apache.org</url> + + <dependencies> + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-core</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-hibernate-core</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> + <groupId>org.hibernate</groupId> + <artifactId>hibernate-core</artifactId> + <version>5.1.5.Final</version> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-jta</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.ow2.jotm</groupId> + <artifactId>jotm-core</artifactId> + <version>2.1.9</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>commons-dbcp</groupId> + <artifactId>commons-dbcp</artifactId> + <version>1.4</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>com.h2database</groupId> + <artifactId>h2</artifactId> + <version>${h2.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>javax.resource</groupId> + <artifactId>connector-api</artifactId> + <version>1.5</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-core</artifactId> + <version>${project.version}</version> + <type>test-jar</type> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-spring</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.apache.ignite</groupId> + <artifactId>ignite-log4j</artifactId> + <version>${project.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-beans</artifactId> + <version>${spring.version}</version> + <scope>test</scope> + </dependency> + + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-context</artifactId> + <version>${spring.version}</version> + <scope>test</scope> + </dependency> + </dependencies> + + <build> + <testResources> + <testResource> + <directory>src/main/java</directory> + <excludes> + <exclude>**/*.java</exclude> + </excludes> + </testResource> + <testResource> + <directory>src/test/java</directory> + <excludes> + <exclude>**/*.java</exclude> + </excludes> + </testResource> + </testResources> + + <plugins> + <!-- Generate the OSGi MANIFEST.MF for this bundle. --> + <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-bundle-plugin</artifactId> + </plugin> + </plugins> + </build> +</project> http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateAbstractRegionAccessStrategy.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateAbstractRegionAccessStrategy.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateAbstractRegionAccessStrategy.java new file mode 100644 index 0000000..c5829bf --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateAbstractRegionAccessStrategy.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.cache.hibernate; + +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.access.RegionAccessStrategy; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.jetbrains.annotations.Nullable; + +/** + * Implementation of L2 cache access strategy delegating to {@link HibernateAccessStrategyAdapter}. + */ +public abstract class HibernateAbstractRegionAccessStrategy implements RegionAccessStrategy { + /** */ + protected final HibernateAccessStrategyAdapter stgy; + + /** + * @param stgy Access strategy implementation. + */ + protected HibernateAbstractRegionAccessStrategy(HibernateAccessStrategyAdapter stgy) { + this.stgy = stgy; + } + + /** {@inheritDoc} */ + @Nullable @Override public Object get(SessionImplementor ses, Object key, long txTs) throws CacheException { + return stgy.get(key); + } + + /** {@inheritDoc} */ + @Override public boolean putFromLoad(SessionImplementor ses, Object key, Object val, long txTs, Object ver) throws CacheException { + stgy.putFromLoad(key, val); + + return true; + } + + /** {@inheritDoc} */ + @Override public boolean putFromLoad(SessionImplementor ses, Object key, Object val, long txTs, Object ver, boolean minimalPutOverride) + throws CacheException { + stgy.putFromLoad(key, val, minimalPutOverride); + + return true; + } + + /** {@inheritDoc} */ + @Nullable @Override public SoftLock lockItem(SessionImplementor ses, Object key, Object ver) throws CacheException { + stgy.lock(key); + + return null; + } + + /** {@inheritDoc} */ + @Nullable @Override public SoftLock lockRegion() throws CacheException { + stgy.lockRegion(); + + return null; + } + + /** {@inheritDoc} */ + @Override public void unlockRegion(SoftLock lock) throws CacheException { + stgy.unlockRegion(); + } + + /** {@inheritDoc} */ + @Override public void unlockItem(SessionImplementor ses, Object key, SoftLock lock) throws CacheException { + stgy.unlock(key); + } + + /** {@inheritDoc} */ + @Override public void remove(SessionImplementor ses, Object key) throws CacheException { + stgy.remove(key); + } + + /** {@inheritDoc} */ + @Override public void removeAll() throws CacheException { + stgy.removeAll(); + } + + /** {@inheritDoc} */ + @Override public void evict(Object key) throws CacheException { + stgy.evict(key); + } + + /** {@inheritDoc} */ + @Override public void evictAll() throws CacheException { + stgy.evictAll(); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateCollectionRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateCollectionRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateCollectionRegion.java new file mode 100644 index 0000000..be99e98 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateCollectionRegion.java @@ -0,0 +1,114 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cache.spi.access.CollectionRegionAccessStrategy; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; + +/** + * Implementation of {@link CollectionRegion}. This region is used to store collection data. + * <p> + * L2 cache for collection can be enabled in the Hibernate configuration file: + * <pre name="code" class="xml"> + * <hibernate-configuration> + * <!-- Enable L2 cache. --> + * <property name="cache.use_second_level_cache">true</property> + * + * <!-- Use Ignite as L2 cache provider. --> + * <property name="cache.region.factory_class">org.apache.ignite.cache.hibernate.HibernateRegionFactory</property> + * + * <!-- Specify entities. --> + * <mapping class="com.example.Entity"/> + * <mapping class="com.example.ChildEntity"/> + * + * <!-- Enable L2 cache with nonstrict-read-write access strategy for entities and collection. --> + * <collection-cache collection="com.example.Entity" usage="nonstrict-read-write"/> + * <collection-cache collection="com.example.ChildEntity" usage="nonstrict-read-write"/> + * <collection-cache collection="com.example.Entity.children" usage="nonstrict-read-write"/> + * </hibernate-configuration> + * </pre> + * Also cache for collection can be enabled using annotations: + * <pre name="code" class="java"> + * @javax.persistence.Entity + * public class Entity { + * ... + * + * @javax.persistence.OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER) + * @javax.persistence.JoinColumn(name="PARENT_ID") + * @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) + * public List<ChildEntity> getChildren() {...} + * } + * </pre> + * Note: the collection cache does not cache the state of the actual entities in the cache, it caches only identifier + * values. For this reason, the collection cache should always be used in conjunction with + * the second-level cache for those entities expected to be cached as part of a collection cache. + */ +public class HibernateCollectionRegion extends HibernateTransactionalDataRegion implements CollectionRegion { + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache. + * @param dataDesc Region data description. + */ + public HibernateCollectionRegion(HibernateRegionFactory factory, String name, + Ignite ignite, HibernateCacheProxy cache, CacheDataDescription dataDesc) { + super(factory, name, ignite, cache, dataDesc); + } + + /** {@inheritDoc} */ + @Override public CollectionRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException { + return new AccessStrategy(createAccessStrategy(accessType)); + } + + /** + * Collection region access strategy. + */ + private class AccessStrategy extends HibernateAbstractRegionAccessStrategy + implements CollectionRegionAccessStrategy { + /** + * @param stgy Access strategy implementation. + */ + private AccessStrategy(HibernateAccessStrategyAdapter stgy) { + super(stgy); + } + + /** {@inheritDoc} */ + @Override public Object generateCacheKey(Object id, + CollectionPersister persister, + SessionFactoryImplementor factory, String tenantIdentifier) { + return HibernateKeyWrapper.staticCreateCollectionKey(id, persister, tenantIdentifier); + } + + /** {@inheritDoc} */ + @Override public Object getCacheKeyId(Object cacheKey) { + return ((HibernateKeyWrapper)cacheKey).id(); + } + + /** {@inheritDoc} */ + @Override public CollectionRegion getRegion() { + return HibernateCollectionRegion.this; + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateEntityRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateEntityRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateEntityRegion.java new file mode 100644 index 0000000..c669400 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateEntityRegion.java @@ -0,0 +1,128 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cache.spi.access.EntityRegionAccessStrategy; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; + +/** + * Implementation of {@link EntityRegion}. This region is used to store entity data. + * <p> + * L2 cache for entity can be enabled in the Hibernate configuration file: + * <pre name="code" class="xml"> + * <hibernate-configuration> + * <!-- Enable L2 cache. --> + * <property name="cache.use_second_level_cache">true</property> + * + * <!-- Use Ignite as L2 cache provider. --> + * <property name="cache.region.factory_class">org.apache.ignite.cache.hibernate.HibernateRegionFactory</property> + * + * <!-- Specify entity. --> + * <mapping class="com.example.Entity"/> + * + * <!-- Enable L2 cache with nonstrict-read-write access strategy for entity. --> + * <class-cache class="com.example.Entity" usage="nonstrict-read-write"/> + * </hibernate-configuration> + * </pre> + * Also cache for entity can be enabled using annotations: + * <pre name="code" class="java"> + * @javax.persistence.Entity + * @javax.persistence.Cacheable + * @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) + * public class Entity { ... } + * </pre> + */ +public class HibernateEntityRegion extends HibernateTransactionalDataRegion implements EntityRegion { + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache, + * @param dataDesc Region data description. + */ + public HibernateEntityRegion(HibernateRegionFactory factory, String name, Ignite ignite, + HibernateCacheProxy cache, CacheDataDescription dataDesc) { + super(factory, name, ignite, cache, dataDesc); + } + + /** {@inheritDoc} */ + @Override public EntityRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException { + return new AccessStrategy(createAccessStrategy(accessType)); + } + + /** + * Entity region access strategy. + */ + private class AccessStrategy extends HibernateAbstractRegionAccessStrategy + implements EntityRegionAccessStrategy { + /** + * @param stgy Access strategy implementation. + */ + private AccessStrategy(HibernateAccessStrategyAdapter stgy) { + super(stgy); + } + + /** {@inheritDoc} */ + @Override public Object generateCacheKey(Object id, + EntityPersister persister, + SessionFactoryImplementor factory, + String tenantIdentifier) { + return HibernateKeyWrapper.staticCreateEntityKey(id, persister, tenantIdentifier); + } + + /** {@inheritDoc} */ + @Override public Object getCacheKeyId(Object cacheKey) { + return ((HibernateKeyWrapper)cacheKey).id(); + } + + /** {@inheritDoc} */ + @Override public EntityRegion getRegion() { + return HibernateEntityRegion.this; + } + + /** {@inheritDoc} */ + @Override public boolean insert(SessionImplementor ses, Object key, Object val, Object ver) throws CacheException { + return stgy.insert(key, val); + } + + /** {@inheritDoc} */ + @Override public boolean afterInsert(SessionImplementor ses, Object key, Object val, Object ver) throws CacheException { + return stgy.afterInsert(key, val); + } + + /** {@inheritDoc} */ + @Override public boolean update(SessionImplementor ses, Object key, Object val, Object currVer, Object previousVer) + throws CacheException { + return stgy.update(key, val); + } + + /** {@inheritDoc} */ + @Override public boolean afterUpdate(SessionImplementor ses, Object key, Object val, Object currVer, Object previousVer, SoftLock lock) + throws CacheException { + return stgy.afterUpdate(key, val); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateGeneralDataRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateGeneralDataRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateGeneralDataRegion.java new file mode 100644 index 0000000..9365157 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateGeneralDataRegion.java @@ -0,0 +1,79 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCheckedException; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.GeneralDataRegion; +import org.hibernate.cache.spi.QueryResultsRegion; +import org.hibernate.cache.spi.TimestampsRegion; +import org.hibernate.engine.spi.SessionImplementor; +import org.jetbrains.annotations.Nullable; + +/** + * Implementation of {@link GeneralDataRegion}. This interface defines common contract for {@link QueryResultsRegion} + * and {@link TimestampsRegion}. + */ +public class HibernateGeneralDataRegion extends HibernateRegion implements GeneralDataRegion { + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache. + */ + HibernateGeneralDataRegion(HibernateRegionFactory factory, String name, + Ignite ignite, HibernateCacheProxy cache) { + super(factory, name, ignite, cache); + } + + /** {@inheritDoc} */ + @Nullable @Override public Object get(SessionImplementor ses, Object key) throws CacheException { + try { + return cache.get(key); + } + catch (IgniteCheckedException e) { + throw new CacheException(e); + } + } + + /** {@inheritDoc} */ + @Override public void put(SessionImplementor ses, Object key, Object val) throws CacheException { + try { + cache.put(key, val); + } + catch (IgniteCheckedException e) { + throw new CacheException(e); + } + } + + /** {@inheritDoc} */ + @Override public void evict(Object key) throws CacheException { + HibernateAccessStrategyAdapter.evict(ignite, cache, key); + } + + /** {@inheritDoc} */ + @Override public void evictAll() throws CacheException { + try { + HibernateAccessStrategyAdapter.evictAll(cache); + } + catch (IgniteCheckedException e) { + throw HibernateRegionFactory.EXCEPTION_CONVERTER.convert(e); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateKeyWrapper.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateKeyWrapper.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateKeyWrapper.java new file mode 100644 index 0000000..45d00e4 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateKeyWrapper.java @@ -0,0 +1,109 @@ +/* + * 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.cache.hibernate; + +import java.io.Serializable; +import org.apache.ignite.internal.util.typedef.internal.S; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; +import org.hibernate.engine.spi.SessionFactoryImplementor; +import org.hibernate.persister.collection.CollectionPersister; +import org.hibernate.persister.entity.EntityPersister; + +/** + * Hibernate cache key wrapper. + */ +public class HibernateKeyWrapper implements Serializable { + /** Key. */ + private final Object key; + + /** Entry. */ + private final String entry; + + /** */ + private final String tenantId; + + /** + * @param key Key. + * @param entry Entry. + * @param tenantId Tenant ID. + */ + HibernateKeyWrapper(Object key, String entry, String tenantId) { + this.key = key; + this.entry = entry; + this.tenantId = tenantId; + } + + /** + * @return ID. + */ + Object id() { + return key; + } + + /** + * @param id ID. + * @param persister Persister. + * @param tenantIdentifier Tenant ID. + * @return Cache key. + * @see DefaultCacheKeysFactory#staticCreateCollectionKey(Object, CollectionPersister, SessionFactoryImplementor, String) + */ + static Object staticCreateCollectionKey(Object id, + CollectionPersister persister, + String tenantIdentifier) { + return new HibernateKeyWrapper(id, persister.getRole(), tenantIdentifier); + } + + /** + * @param id ID. + * @param persister Persister. + * @param tenantIdentifier Tenant ID. + * @return Cache key. + * @see DefaultCacheKeysFactory#staticCreateEntityKey(Object, EntityPersister, SessionFactoryImplementor, String) + */ + public static Object staticCreateEntityKey(Object id, EntityPersister persister, String tenantIdentifier) { + return new HibernateKeyWrapper(id, persister.getRootEntityName(), tenantIdentifier); + } + + + /** {@inheritDoc} */ + @Override public boolean equals(Object o) { + if (this == o) return true; + + if (o == null || getClass() != o.getClass()) + return false; + + HibernateKeyWrapper that = (HibernateKeyWrapper) o; + + return (key != null ? key.equals(that.key) : that.key == null) && + (entry != null ? entry.equals(that.entry) : that.entry == null) && + (tenantId != null ? tenantId.equals(that.tenantId) : that.tenantId == null); + } + + /** {@inheritDoc} */ + @Override public int hashCode() { + int res = key != null ? key.hashCode() : 0; + res = 31 * res + (entry != null ? entry.hashCode() : 0); + res = 31 * res + (tenantId != null ? tenantId.hashCode() : 0); + return res; + } + + /** {@inheritDoc} */ + @Override public String toString() { + return S.toString(HibernateKeyWrapper.class, this); + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateNaturalIdRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateNaturalIdRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateNaturalIdRegion.java new file mode 100644 index 0000000..68816de --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateNaturalIdRegion.java @@ -0,0 +1,113 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.internal.DefaultCacheKeysFactory; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.NaturalIdRegion; +import org.hibernate.cache.spi.access.AccessType; +import org.hibernate.cache.spi.access.NaturalIdRegionAccessStrategy; +import org.hibernate.cache.spi.access.SoftLock; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.persister.entity.EntityPersister; + +/** + * Implementation of {@link NaturalIdRegion}. This region is used to store naturalId data. + * <p> + * L2 cache for entity naturalId and target cache region can be set using annotations: + * <pre name="code" class="java"> + * @javax.persistence.Entity + * @javax.persistence.Cacheable + * @org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) + * @org.hibernate.annotations.NaturalIdCache + * public class Entity { + * @org.hibernate.annotations.NaturalId + * private String entityCode; + * + * ... + * } + * </pre> + */ +public class HibernateNaturalIdRegion extends HibernateTransactionalDataRegion implements NaturalIdRegion { + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache, + * @param dataDesc Region data description. + */ + public HibernateNaturalIdRegion(HibernateRegionFactory factory, String name, + Ignite ignite, HibernateCacheProxy cache, CacheDataDescription dataDesc) { + super(factory, name, ignite, cache, dataDesc); + } + + /** {@inheritDoc} */ + @Override public NaturalIdRegionAccessStrategy buildAccessStrategy(AccessType accessType) throws CacheException { + return new AccessStrategy(createAccessStrategy(accessType)); + } + + /** + * NaturalId region access strategy. + */ + private class AccessStrategy extends HibernateAbstractRegionAccessStrategy implements + NaturalIdRegionAccessStrategy { + /** + * @param stgy Access strategy implementation. + */ + private AccessStrategy(HibernateAccessStrategyAdapter stgy) { + super(stgy); + } + + /** {@inheritDoc} */ + @Override public Object generateCacheKey(Object[] naturalIdValues, EntityPersister persister, SessionImplementor ses) { + return DefaultCacheKeysFactory.staticCreateNaturalIdKey(naturalIdValues, persister, ses); + } + + /** {@inheritDoc} */ + @Override public Object[] getNaturalIdValues(Object cacheKey) { + return DefaultCacheKeysFactory.staticGetNaturalIdValues(cacheKey); + } + + /** {@inheritDoc} */ + @Override public NaturalIdRegion getRegion() { + return HibernateNaturalIdRegion.this; + } + + /** {@inheritDoc} */ + @Override public boolean insert(SessionImplementor ses, Object key, Object val) throws CacheException { + return stgy.insert(key, val); + } + + /** {@inheritDoc} */ + @Override public boolean afterInsert(SessionImplementor ses, Object key, Object val) throws CacheException { + return stgy.afterInsert(key, val); + } + + /** {@inheritDoc} */ + @Override public boolean update(SessionImplementor ses, Object key, Object val) throws CacheException { + return stgy.update(key, val); + } + + /** {@inheritDoc} */ + @Override public boolean afterUpdate(SessionImplementor ses, Object key, Object val, SoftLock lock) throws CacheException { + return stgy.afterUpdate(key, val); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateQueryResultsRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateQueryResultsRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateQueryResultsRegion.java new file mode 100644 index 0000000..0b9a43d --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateQueryResultsRegion.java @@ -0,0 +1,70 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.hibernate.Query; +import org.hibernate.cache.spi.QueryResultsRegion; + +/** + * Implementation of {@link QueryResultsRegion}. This region is used to store query results. + * <p> + * Query results caching can be enabled in the Hibernate configuration file: + * <pre name="code" class="xml"> + * <hibernate-configuration> + * <!-- Enable L2 cache. --> + * <property name="cache.use_second_level_cache">true</property> + * + * <!-- Enable query cache. --> + * <property name="cache.use_second_level_cache">true</property> + + * <!-- Use Ignite as L2 cache provider. --> + * <property name="cache.region.factory_class">org.apache.ignite.cache.hibernate.HibernateRegionFactory</property> + * + * <!-- Specify entity. --> + * <mapping class="com.example.Entity"/> + * + * <!-- Enable L2 cache with nonstrict-read-write access strategy for entity. --> + * <class-cache class="com.example.Entity" usage="nonstrict-read-write"/> + * </hibernate-configuration> + * </pre> + * By default queries are not cached even after enabling query caching, to enable results caching for a particular + * query, call {@link Query#setCacheable(boolean)}: + * <pre name="code" class="java"> + * Session ses = getSession(); + * + * Query qry = ses.createQuery("..."); + * + * qry.setCacheable(true); // Enable L2 cache for query. + * </pre> + * Note: the query cache does not cache the state of the actual entities in the cache, it caches only identifier + * values. For this reason, the query cache should always be used in conjunction with + * the second-level cache for those entities expected to be cached as part of a query result cache + */ +public class HibernateQueryResultsRegion extends HibernateGeneralDataRegion implements QueryResultsRegion { + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache. + */ + public HibernateQueryResultsRegion(HibernateRegionFactory factory, String name, + Ignite ignite, HibernateCacheProxy cache) { + super(factory, name, ignite, cache); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegion.java new file mode 100644 index 0000000..11a96d0 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegion.java @@ -0,0 +1,99 @@ +/* + * 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.cache.hibernate; + +import java.util.Collections; +import java.util.Map; +import org.apache.ignite.Ignite; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.Region; + +/** + * Implementation of {@link Region}. This interface defines base contract for all L2 cache regions. + */ +public class HibernateRegion implements Region { + /** */ + protected final HibernateRegionFactory factory; + + /** */ + private final String name; + + /** Cache instance. */ + protected final HibernateCacheProxy cache; + + /** Grid instance. */ + protected Ignite ignite; + + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache. + */ + public HibernateRegion(HibernateRegionFactory factory, String name, Ignite ignite, HibernateCacheProxy cache) { + this.factory = factory; + this.name = name; + this.ignite = ignite; + this.cache = cache; + } + + /** {@inheritDoc} */ + @Override public String getName() { + return name; + } + + /** {@inheritDoc} */ + @Override public void destroy() throws CacheException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public boolean contains(Object key) { + return cache.containsKey(key); + } + + /** {@inheritDoc} */ + @Override public long getSizeInMemory() { + return -1; + } + + /** {@inheritDoc} */ + @Override public long getElementCountInMemory() { + return cache.size(); + } + + /** {@inheritDoc} */ + @Override public long getElementCountOnDisk() { + return -1; + } + + /** {@inheritDoc} */ + @Override public Map toMap() { + return Collections.emptyMap(); + } + + /** {@inheritDoc} */ + @Override public long nextTimestamp() { + return System.currentTimeMillis(); + } + + /** {@inheritDoc} */ + @Override public int getTimeout() { + return 0; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java new file mode 100644 index 0000000..bf7d7e9 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateRegionFactory.java @@ -0,0 +1,168 @@ +/* + * 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.cache.hibernate; + +import java.util.Properties; +import org.apache.ignite.internal.processors.cache.IgniteInternalCache; +import org.hibernate.boot.spi.SessionFactoryOptions; +import org.hibernate.cache.CacheException; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.cache.spi.NaturalIdRegion; +import org.hibernate.cache.spi.QueryResultsRegion; +import org.hibernate.cache.spi.RegionFactory; +import org.hibernate.cache.spi.TimestampsRegion; +import org.hibernate.cache.spi.access.AccessType; + +import static org.apache.ignite.cache.hibernate.HibernateAccessStrategyFactory.DFLT_ACCESS_TYPE_PROPERTY; +import static org.hibernate.cache.spi.access.AccessType.NONSTRICT_READ_WRITE; + +/** + * Hibernate L2 cache region factory. + * <p> + * Following Hibernate settings should be specified to enable second level cache and to use this + * region factory for caching: + * <pre name="code" class="brush: xml; gutter: false;"> + * hibernate.cache.use_second_level_cache=true + * hibernate.cache.region.factory_class=org.apache.ignite.cache.hibernate.HibernateRegionFactory + * </pre> + * Note that before region factory is started you need to start properly configured Ignite node in the same JVM. + * For example to start Ignite node one of loader provided in {@code org.apache.ignite.grid.startup} package can be used. + * <p> + * Name of Ignite instance to be used for region factory must be specified as following Hibernate property: + * <pre name="code" class="brush: xml; gutter: false;"> + * org.apache.ignite.hibernate.ignite_instance_name=<Ignite instance name> + * </pre> + * Each Hibernate cache region must be associated with some {@link IgniteInternalCache}, by default it is assumed that + * for each cache region there is a {@link IgniteInternalCache} with the same name. Also it is possible to define + * region to cache mapping using properties with prefix {@code org.apache.ignite.hibernate.region_cache}. + * For example if for region with name "region1" cache with name "cache1" should be used then following + * Hibernate property should be specified: + * <pre name="code" class="brush: xml; gutter: false;"> + * org.apache.ignite.hibernate.region_cache.region1=cache1 + * </pre> + */ +public class HibernateRegionFactory implements RegionFactory { + /** */ + private static final long serialVersionUID = 0L; + + /** */ + static final HibernateExceptionConverter EXCEPTION_CONVERTER = new HibernateExceptionConverter() { + @Override public RuntimeException convert(Exception e) { + return new CacheException(e); + } + }; + + /** Default region access type. */ + private AccessType dfltAccessType; + + /** Key transformer. */ + private final HibernateKeyTransformer hibernate4transformer = new HibernateKeyTransformer() { + @Override public Object transform(Object key) { + return key; + } + }; + + /** */ + private final HibernateAccessStrategyFactory accessStgyFactory = + new HibernateAccessStrategyFactory(hibernate4transformer, EXCEPTION_CONVERTER); + + /** {@inheritDoc} */ + @Override public void start(SessionFactoryOptions settings, Properties props) throws CacheException { + String accessType = props.getProperty(DFLT_ACCESS_TYPE_PROPERTY, NONSTRICT_READ_WRITE.name()); + + dfltAccessType = AccessType.valueOf(accessType); + + accessStgyFactory.start(props); + } + + /** + * @return Access strategy factory. + */ + HibernateAccessStrategyFactory accessStrategyFactory() { + return accessStgyFactory; + } + + /** {@inheritDoc} */ + @Override public void stop() { + // No-op. + } + + /** {@inheritDoc} */ + @Override public boolean isMinimalPutsEnabledByDefault() { + return false; + } + + /** {@inheritDoc} */ + @Override public AccessType getDefaultAccessType() { + return dfltAccessType; + } + + /** {@inheritDoc} */ + @Override public long nextTimestamp() { + return System.currentTimeMillis(); + } + + /** {@inheritDoc} */ + @Override public EntityRegion buildEntityRegion(String regionName, Properties props, CacheDataDescription metadata) + throws CacheException { + return new HibernateEntityRegion(this, + regionName, + accessStgyFactory.node(), + accessStgyFactory.regionCache(regionName), + metadata); + } + + /** {@inheritDoc} */ + @Override public NaturalIdRegion buildNaturalIdRegion(String regionName, Properties props, + CacheDataDescription metadata) throws CacheException { + return new HibernateNaturalIdRegion(this, + regionName, + accessStgyFactory.node(), + accessStgyFactory.regionCache(regionName), + metadata); + } + + /** {@inheritDoc} */ + @Override public CollectionRegion buildCollectionRegion(String regionName, Properties props, + CacheDataDescription metadata) throws CacheException { + return new HibernateCollectionRegion(this, + regionName, + accessStgyFactory.node(), + accessStgyFactory.regionCache(regionName), + metadata); + } + + /** {@inheritDoc} */ + @Override public QueryResultsRegion buildQueryResultsRegion(String regionName, Properties props) + throws CacheException { + return new HibernateQueryResultsRegion(this, + regionName, + accessStgyFactory.node(), + accessStgyFactory.regionCache(regionName)); + } + + /** {@inheritDoc} */ + @Override public TimestampsRegion buildTimestampsRegion(String regionName, Properties props) throws CacheException { + return new HibernateTimestampsRegion(this, + regionName, + accessStgyFactory.node(), + accessStgyFactory.regionCache(regionName)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTimestampsRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTimestampsRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTimestampsRegion.java new file mode 100644 index 0000000..8b4c243 --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTimestampsRegion.java @@ -0,0 +1,39 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.hibernate.cache.spi.TimestampsRegion; + +/** + * Implementation of {@link TimestampsRegion}. This region is automatically created when query + * caching is enabled and it holds most recent updates timestamps to queryable tables. + * Name of timestamps region is {@code "org.hibernate.cache.spi.UpdateTimestampsCache"}. + */ +public class HibernateTimestampsRegion extends HibernateGeneralDataRegion implements TimestampsRegion { + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache. + */ + public HibernateTimestampsRegion(HibernateRegionFactory factory, String name, + Ignite ignite, HibernateCacheProxy cache) { + super(factory, name, ignite, cache); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTransactionalDataRegion.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTransactionalDataRegion.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTransactionalDataRegion.java new file mode 100644 index 0000000..275ea9e --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/HibernateTransactionalDataRegion.java @@ -0,0 +1,84 @@ +/* + * 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.cache.hibernate; + +import org.apache.ignite.Ignite; +import org.hibernate.cache.spi.CacheDataDescription; +import org.hibernate.cache.spi.CollectionRegion; +import org.hibernate.cache.spi.EntityRegion; +import org.hibernate.cache.spi.NaturalIdRegion; +import org.hibernate.cache.spi.TransactionalDataRegion; +import org.hibernate.cache.spi.access.AccessType; + +/** + * Implementation of {@link TransactionalDataRegion} (transactional means that + * data in the region is updated in connection with database transaction). + * This interface defines base contract for {@link EntityRegion}, {@link CollectionRegion} + * and {@link NaturalIdRegion}. + */ +public class HibernateTransactionalDataRegion extends HibernateRegion implements TransactionalDataRegion { + /** */ + private final CacheDataDescription dataDesc; + + /** + * @param factory Region factory. + * @param name Region name. + * @param ignite Grid. + * @param cache Region cache. + * @param dataDesc Region data description. + */ + HibernateTransactionalDataRegion(HibernateRegionFactory factory, String name, + Ignite ignite, HibernateCacheProxy cache, CacheDataDescription dataDesc) { + super(factory, name, ignite, cache); + + this.dataDesc = dataDesc; + } + + /** {@inheritDoc} */ + @Override public boolean isTransactionAware() { + return false; // This method is not used by Hibernate. + } + + /** {@inheritDoc} */ + @Override public CacheDataDescription getCacheDataDescription() { + return dataDesc; + } + + /** + * @param accessType Hibernate L2 cache access type. + * @return Access strategy for given access type. + */ + HibernateAccessStrategyAdapter createAccessStrategy(AccessType accessType) { + switch (accessType) { + case READ_ONLY: + return factory.accessStrategyFactory().createReadOnlyStrategy(cache); + + case NONSTRICT_READ_WRITE: + return factory.accessStrategyFactory().createNonStrictReadWriteStrategy(cache); + + case READ_WRITE: + return factory.accessStrategyFactory().createReadWriteStrategy(cache); + + case TRANSACTIONAL: + return factory.accessStrategyFactory().createTransactionalStrategy(cache); + + default: + throw new IllegalArgumentException("Unknown Hibernate access type: " + accessType); + } + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/be40f937/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/package-info.java ---------------------------------------------------------------------- diff --git a/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/package-info.java b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/package-info.java new file mode 100644 index 0000000..1179aec --- /dev/null +++ b/modules/hibernate-5.1/src/main/java/org/apache/ignite/cache/hibernate/package-info.java @@ -0,0 +1,24 @@ +/* + * 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 implementation of Hibernate L2 cache. Refer to + * <i>org.apache.ignite.examples.datagrid.hibernate.HibernateL2CacheExample</i> for more information on how to + * configure and use Ignite with Hibernate. + */ +package org.apache.ignite.cache.hibernate; \ No newline at end of file
