├── .github
├── dependabot.yml
├── workflows
│ ├── drafter.yml
│ └── build.yml
├── ISSUE_TEMPLATE
│ ├── feature_request.md
│ ├── config.yml
│ └── bug_report.md
└── release-drafter.yml
├── template.mf
├── checkstyle
├── ClassHeader.txt
├── suppressions.xml
└── checkstyle.xml
├── src
├── main
│ └── java
│ │ └── org
│ │ └── springframework
│ │ └── data
│ │ └── hazelcast
│ │ ├── repository
│ │ ├── config
│ │ │ ├── package-info.java
│ │ │ ├── HazelcastRepositoriesRegistrar.java
│ │ │ ├── HazelcastRepositoryConfigurationExtension.java
│ │ │ └── EnableHazelcastRepositories.java
│ │ ├── query
│ │ │ ├── package-info.java
│ │ │ ├── Query.java
│ │ │ ├── HazelcastCriteriaAccessor.java
│ │ │ ├── HazelcastSortAccessor.java
│ │ │ ├── HazelcastPropertyComparator.java
│ │ │ └── GeoPredicate.java
│ │ ├── package-info.java
│ │ ├── support
│ │ │ ├── package-info.java
│ │ │ ├── SimpleHazelcastRepository.java
│ │ │ ├── HazelcastEntityInformation.java
│ │ │ ├── HazelcastQueryMethod.java
│ │ │ ├── StringBasedHazelcastRepositoryQuery.java
│ │ │ ├── HazelcastRepositoryFactoryBean.java
│ │ │ ├── HazelcastRepositoryFactory.java
│ │ │ └── HazelcastQueryLookupStrategy.java
│ │ └── HazelcastRepository.java
│ │ ├── package-info.java
│ │ ├── HazelcastKeyValueAdapter.java
│ │ └── HazelcastQueryEngine.java
└── test
│ ├── java
│ ├── test
│ │ └── utils
│ │ │ ├── domain
│ │ │ ├── NoIdEntity.java
│ │ │ ├── Song.java
│ │ │ ├── Movie.java
│ │ │ ├── City.java
│ │ │ ├── Makeup.java
│ │ │ ├── MyTitle.java
│ │ │ └── Person.java
│ │ │ ├── repository
│ │ │ ├── custom
│ │ │ │ ├── SongRepository.java
│ │ │ │ ├── MovieRepository.java
│ │ │ │ ├── MyTitleRepository.java
│ │ │ │ ├── MyTitleRepositoryImpl.java
│ │ │ │ ├── MyTitleRepositoryFactoryBean.java
│ │ │ │ └── MyTitleRepositoryFactory.java
│ │ │ └── standard
│ │ │ │ ├── CityRepository.java
│ │ │ │ └── PersonRepository.java
│ │ │ ├── TestConstants.java
│ │ │ ├── TestDataHelper.java
│ │ │ └── InstanceHelper.java
│ └── org
│ │ └── springframework
│ │ └── data
│ │ └── hazelcast
│ │ ├── topology
│ │ ├── ClusterIT.java
│ │ ├── ClientServerIT.java
│ │ └── AbstractTopologyIT.java
│ │ ├── repository
│ │ ├── KeyValueIT.java
│ │ ├── support
│ │ │ ├── HazelcastEntityInformationTest.java
│ │ │ └── SimpleHazelcastRepositoryIT.java
│ │ ├── config
│ │ │ └── EnableHazelcastRepositoriesIT.java
│ │ ├── query
│ │ │ ├── HazelcastPropertyComparatorTest.java
│ │ │ └── HazelcastSortAccessorTest.java
│ │ ├── custom
│ │ │ └── CustomRepoIT.java
│ │ └── PagingSortingIT.java
│ │ └── HazelcastUtils.java
│ └── resources
│ └── logback-test.xml
├── README.md
├── .gitignore
└── LICENSE
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: "maven"
4 | directory: "/"
5 | schedule:
6 | interval: "daily"
7 | time: "02:00"
8 | - package-ecosystem: "github-actions"
9 | directory: "/"
10 | schedule:
11 | interval: "daily"
12 | time: "02:00"
13 |
--------------------------------------------------------------------------------
/.github/workflows/drafter.yml:
--------------------------------------------------------------------------------
1 | name: Release Drafter
2 |
3 | on:
4 | push:
5 | branches:
6 | - master
7 |
8 | jobs:
9 | update_release_draft:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: release-drafter/release-drafter@v5.17.5
13 | env:
14 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: "Feature request"
3 | about: Suggest an idea for this project
4 | labels: 'Type: Enhancement'
5 | ---
6 |
7 |
12 |
13 | **Please describe the problem you are trying to solve**
14 | ...
15 |
16 | **Please describe the desired behavior**
17 | ...
18 |
19 | **Describe alternatives you've considered**
20 | ...
21 |
--------------------------------------------------------------------------------
/template.mf:
--------------------------------------------------------------------------------
1 | Bundle-SymbolicName: org.springframework.data.hazelcast
2 | Bundle-Name: ${project.name}
3 | Bundle-Vendor: Hazelcast, Inc., Pivotal Software, Inc.
4 | Bundle-Version: ${project.version}
5 | Bundle-ManifestVersion: 2
6 | Bundle-RequiredExecutionEnvironment: JavaSE-1.8
7 | Import-Template:
8 | com.hazelcast.*;version="${hazelcast:[=.=.=,+1.0.0)}";resolution:=optional,
9 | org.springframework.*;version="${spring:[=.=.=,+1.0.0)}",
10 | org.springframework.data.*;version="${springdata.keyvalue:[=.=.=,+1.0.0)}"
11 | DynamicImport-Package: *
12 |
--------------------------------------------------------------------------------
/.github/release-drafter.yml:
--------------------------------------------------------------------------------
1 | name-template: $NEXT_PATCH_VERSION
2 | tag-template: $NEXT_PATCH_VERSION
3 | template: |
4 | ## What’s Changed
5 |
6 | $CHANGES
7 |
8 | ## Contributors
9 | $CONTRIBUTORS
10 |
11 | Special thanks to all contributors! 👏
12 |
13 | categories:
14 | - title: Features & Enhancements
15 | labels:
16 | - 'feature'
17 | - 'enhancement'
18 | - title: Fixes
19 | labels:
20 | - 'fix'
21 | - 'bugfix'
22 | - 'bug'
23 | - title: Dependencies
24 | label: dependencies
25 | - title: Documentation
26 | label: documentation
27 |
28 | exclude-labels:
29 | - 'internal'
30 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/config.yml:
--------------------------------------------------------------------------------
1 | blank_issues_enabled: true
2 | contact_links:
3 | - name: Hazelcast Google Group
4 | url: https://groups.google.com/forum/#!forum/hazelcast
5 | about: We're not actively answering questions on GitHub, so feel free to ask one on our Google Group!
6 | - name: Stack Overflow
7 | url: https://stackoverflow.com/questions/tagged/hazelcast
8 | about: We're not actively answering questions on GitHub, so feel free to ask one on Stack Overflow!
9 | - name: Hazelcast community Slack
10 | url: https://slack.hazelcast.com/
11 | about: We're not actively answering questions on Github, so feel free to ask one on Slack!
12 |
--------------------------------------------------------------------------------
/checkstyle/ClassHeader.txt:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/config/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | *
18 | * Configuration defaults for using Hazelcast as a key-value store.
19 | *
20 | */
21 | package org.springframework.data.hazelcast.repository.config;
22 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/query/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | *
18 | * Implementation of querying so that Hazelcast repositories can be queried in the standard Spring query method style.
19 | *
20 | */
21 | package org.springframework.data.hazelcast.repository.query;
22 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | *
18 | * Contains the {@link org.springframework.stereotype.Repository @Repository} bean interface available for Hazelcast
19 | * applications.
20 | *
21 | */
22 | package org.springframework.data.hazelcast.repository;
23 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | *
18 | * Mainly defines {@link org.springframework.data.hazelcast.HazelcastKeyValueAdapter} providing Hazelcast as an
19 | * implementation of a key-value store.
20 | *
21 | */
22 | package org.springframework.data.hazelcast;
23 |
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: build
2 | on:
3 | push:
4 | paths-ignore:
5 | - '**.md'
6 |
7 | jobs:
8 | build:
9 | runs-on: ubuntu-latest
10 | strategy:
11 | matrix:
12 | java: [ '8' ]
13 | hazelcast: [ '4.0.3', '4.1' ]
14 | architecture: [ 'x64' ]
15 | name: Build with JDK ${{ matrix.java }} and Hazelcast ${{ matrix.hazelcast }}
16 | steps:
17 | - uses: actions/checkout@v6
18 | - name: Setup JDK
19 | uses: actions/setup-java@v5
20 | with:
21 | distribution: 'zulu'
22 | java-version: ${{ matrix.java }}
23 | architecture: ${{ matrix.architecture }}
24 |
25 | - uses: actions/cache@v4
26 | with:
27 | path: ~/.m2/repository
28 | key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
29 | restore-keys: ${{ runner.os }}-maven-
30 |
31 | - name: Build with Maven
32 | run: mvn -Dhazelcast.version=${{ matrix.hazelcast }} package
33 |
34 |
--------------------------------------------------------------------------------
/checkstyle/suppressions.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
18 |
19 |
22 |
23 |
24 |
27 |
28 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/domain/NoIdEntity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.domain;
18 |
19 | import org.springframework.data.annotation.Id;
20 | import org.springframework.data.keyvalue.annotation.KeySpace;
21 |
22 | import java.io.Serializable;
23 |
24 | /**
25 | * An entity with no {@link Id} annotation.
26 | *
27 | * @author Gokhan Oner
28 | */
29 | @KeySpace
30 | public class NoIdEntity
31 | implements Serializable {
32 | private static final long serialVersionUID = 1L;
33 |
34 | private String name;
35 | }
36 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/query/Query.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.query;
17 |
18 | import java.lang.annotation.ElementType;
19 | import java.lang.annotation.Retention;
20 | import java.lang.annotation.RetentionPolicy;
21 | import java.lang.annotation.Target;
22 |
23 | /**
24 | * Query Annotation do define Hazelcast SqlPredicates
25 | */
26 | @Retention(RetentionPolicy.RUNTIME)
27 | @Target(ElementType.METHOD)
28 | public @interface Query {
29 |
30 | String value() default "";
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/support/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | /**
17 | *
18 | * {@link HazelcastPartTreeQuery} is a required modification to that provided by Spring-Data-Keyvalue (class
19 | * {@link KeyValuePartTreeQuery}) to implement queries correctly for Hazelcast.
20 | *
21 | *
22 | * As this is embedded, need to provide {@link HazelcastRepositoryFactoryBean} to create
23 | * {@link HazelcastRepositoryFactory} which provide a {@link HazelcastQueryLookupStrategy}. There largely extend their
24 | * Spring-Data-KeyValue counterparts, copying where necessary, and overriding where possible.
25 | *
26 | */
27 | package org.springframework.data.hazelcast.repository.support;
28 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/topology/ClusterIT.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.springframework.data.hazelcast.topology;
18 |
19 | import org.springframework.test.context.ActiveProfiles;
20 | import test.utils.TestConstants;
21 |
22 | /**
23 | *
24 | * Run the {@link AbstractTopologyIT} tests with the server-only profile, that defines a cluster with multiple server
25 | * nodes.
26 | *
27 | *
28 | * Spring Data Hazelcast selects one of server nodes to connect to, so the tests compare expected outcome against
29 | * another server node.
30 | *
31 | *
32 | * @author Neil Stevenson
33 | */
34 | @ActiveProfiles(TestConstants.SPRING_TEST_PROFILE_CLUSTER)
35 | public class ClusterIT
36 | extends AbstractTopologyIT {
37 | }
38 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug
3 | about: Create a report to help us improve
4 | labels: 'Type: Defect'
5 | ---
6 |
7 |
10 |
11 | **Describe the bug**
12 | A clear and concise description of what the bug is.
13 |
14 | **Expected behavior**
15 | A clear and concise description of what you expected to happen.
16 |
17 | **To Reproduce**
18 |
19 | Steps to reproduce the behavior:
20 | 1. Go to '...'
21 | 2. Click on '....'
22 | 3. ...
23 |
24 | **Additional context**
25 |
26 |
39 |
--------------------------------------------------------------------------------
/src/test/resources/logback-test.xml:
--------------------------------------------------------------------------------
1 |
2 |
17 |
18 |
19 |
20 |
21 |
22 |
23 | %d{HH:mm:ss.SSS} %5p %70(%c{50}.%M\(%L\)) - %m%n
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/domain/Song.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.domain;
18 |
19 | import org.springframework.data.keyvalue.annotation.KeySpace;
20 |
21 | /**
22 | *
A {@code Song} is a kind of {@link MyTitle}, as the fields here are just the
23 | * year and the song title.
24 | *
25 | *
26 | * Don't provide a value for the {@code @KeySpace} annotation, so the map name
27 | * will default to the fully qualified class name, "{@code test.utils.Song}".
28 | *
29 | *
30 | * @author Neil Stevenson
31 | */
32 | @KeySpace
33 | public class Song
34 | extends MyTitle {
35 | private static final long serialVersionUID = 1L;
36 |
37 | @Override
38 | public String toString() {
39 | return "Song [id=" + super.getId() + ", title=" + super.getTitle() + "]";
40 | }
41 |
42 | }
--------------------------------------------------------------------------------
/src/test/java/test/utils/domain/Movie.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.domain;
18 |
19 | import org.springframework.data.keyvalue.annotation.KeySpace;
20 | import test.utils.TestConstants;
21 |
22 | /**
23 | *
A {@code Movie} is a kind of {@link MyTitle}, as the fields here are just the
24 | * year and the movie title.
25 | *
26 | *
27 | * Give an argument {@code @KeySpace} to name the map used, "{@code Movie}"
28 | * rather than the default fully class name.
29 | *
30 | *
31 | * @author Neil Stevenson
32 | */
33 | @KeySpace(TestConstants.MOVIE_MAP_NAME)
34 | public class Movie
35 | extends MyTitle {
36 | private static final long serialVersionUID = 1L;
37 |
38 | @Override
39 | public String toString() {
40 | return "Movie [id=" + super.getId() + ", title=" + super.getTitle() + "]";
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/custom/SongRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.repository.custom;
18 |
19 | import test.utils.domain.Song;
20 |
21 | import java.util.List;
22 |
23 | /**
24 | *
Extend the {@link MyTitleRepository} for song titles,
25 | * inheriting some methods application to the base class {@link MyTitle}
26 | * and adding some methods specific to {@link Song} objects.
27 | *
36 | * This could be generic behaviour, but make specific
37 | * to {@link SongRepository} for tests.
38 | *
39 | *
40 | * @param text A year
41 | * @return Previous years
42 | */
43 | public List findByIdLessThan(String text);
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/custom/MovieRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.repository.custom;
18 |
19 | import test.utils.domain.Movie;
20 |
21 | import java.util.List;
22 |
23 | /**
24 | *
Extend the {@link MyTitleRepository} for movie titles,
25 | * inheriting some methods application to the base class {@link MyTitle}
26 | * and adding some methods specific to {@link Movie} objects.
27 | *
36 | * This could be generic behaviour, but make specific
37 | * to {@link MovieRepository} for tests.
38 | *
39 | *
40 | * @param text A word in the title
41 | * @return Matches
42 | */
43 | public List findByTitleLike(String text);
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/custom/MyTitleRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.repository.custom;
18 |
19 | import org.springframework.data.hazelcast.repository.HazelcastRepository;
20 | import org.springframework.data.repository.NoRepositoryBean;
21 |
22 | import java.io.Serializable;
23 |
24 | /**
25 | *
Define a generic repository bean for extension by other interfaces.
26 | * Mark with {@code @NoRepositoryBean} so Spring doesn't try to create one
27 | * of these at runtime.
28 | *
38 | * A method generic to {@code MyTitle} objects in
39 | * the {@code MovieRepository} and {@code SongRepository}
40 | *
41 | */
42 | public int wordsInTitle(String year);
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/repository/KeyValueIT.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.springframework.data.hazelcast.repository;
18 |
19 | import org.junit.Test;
20 | import org.springframework.data.keyvalue.repository.KeyValueRepository;
21 | import org.springframework.data.repository.PagingAndSortingRepository;
22 | import test.utils.domain.Person;
23 |
24 | import javax.annotation.Resource;
25 |
26 | /**
27 | *
28 | * Downcast a {@link HazelcastRepository} into a {@link KeyValueRepository} to test any Key-Value additions to
29 | * {@link PagingAndSortingRepository}. At the moment, there are none so this is empty.
30 | *
31 | *
32 | * @author Neil Stevenson
33 | */
34 | public class KeyValueIT {
35 |
36 | // PersonRepository is really a HazelcastRepository
37 | @Resource
38 | private KeyValueRepository personRepository;
39 |
40 | /* No tests required, see class comments above.
41 | */
42 | @Test
43 | public void no_op() {
44 | ;
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/TestConstants.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils;
18 |
19 | import test.utils.domain.Song;
20 |
21 | /**
22 | * @author Neil Stevenson
23 | */
24 | public class TestConstants {
25 |
26 | public static final String CLIENT_INSTANCE_NAME = "hazelcast-instance-client";
27 | public static final String SERVER_INSTANCE_NAME = "hazelcast-instance-server";
28 |
29 | public static final String SPRING_TEST_PROFILE_CLIENT_SERVER = "client-server";
30 | public static final String SPRING_TEST_PROFILE_CLUSTER = "cluster";
31 | public static final String SPRING_TEST_PROFILE_SINGLETON = "singleton";
32 |
33 | public static final String MAKEUP_MAP_NAME = "Make-up";
34 | public static final String MOVIE_MAP_NAME = "Movie";
35 | public static final String PERSON_MAP_NAME = "Actors";
36 | public static final String SONG_MAP_NAME = Song.class.getCanonicalName();
37 | public static final String CITY_MAP_NAME = "Cities";
38 |
39 | public static final String[] OSCAR_MAP_NAMES = {MAKEUP_MAP_NAME, MOVIE_MAP_NAME, PERSON_MAP_NAME, SONG_MAP_NAME, CITY_MAP_NAME};
40 | }
41 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/standard/CityRepository.java:
--------------------------------------------------------------------------------
1 | package test.utils.repository.standard;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.data.domain.Page;
6 | import org.springframework.data.domain.Pageable;
7 | import org.springframework.data.geo.Circle;
8 | import org.springframework.data.geo.Distance;
9 | import org.springframework.data.geo.Point;
10 | import org.springframework.data.geo.Shape;
11 | import org.springframework.data.hazelcast.repository.HazelcastRepository;
12 |
13 | import test.utils.domain.City;
14 |
15 | /**
16 | *
17 | * Repository class used for tests to test geospatial data.
18 | *
19 | *
20 | * The specified methods are implemented by Spring at run-time, using the method name and parameters to deduce the query
21 | * syntax.
22 | *
23 | *
24 | * See {@link org.springframework.data.repository.query.parser.PartTree PartTree} for details of the query syntax. A
25 | * simple example being a concatenation:
26 | *
27 | *
'{@code Near}' - sorts entities by distance from a given point
28 | *
'{@code Within}' - both sorts and filters entities, returning those within the given distance, range or shape
29 | *
30 | *
31 | *
32 | * @author Ulhas R Manekar
33 | */
34 | public interface CityRepository
35 | extends HazelcastRepository {
36 |
37 | public List findByLocationNear(Point point, Distance distance);
38 |
39 | public Long countByLocationNear(Point point, Distance distance);
40 |
41 | public List findByLocationNear(Point point, Object distance);
42 |
43 | public List findByLocationWithin(Circle circle);
44 |
45 | public Page findByLocationNear(Point point, Distance distance, Pageable pageable);
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/support/SimpleHazelcastRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.support;
17 |
18 | import org.springframework.data.hazelcast.repository.HazelcastRepository;
19 | import org.springframework.data.keyvalue.core.KeyValueOperations;
20 | import org.springframework.data.keyvalue.repository.support.SimpleKeyValueRepository;
21 | import org.springframework.data.repository.core.EntityInformation;
22 |
23 | import java.io.Serializable;
24 |
25 | /**
26 | *
A concrete implementation to instantiate directly rather than allow
27 | * Spring to generate.
28 | *
29 | *
30 | * @param The domain object
31 | * @param The key of the domain object
32 | * @author Neil Stevenson
33 | */
34 | public class SimpleHazelcastRepository
35 | extends SimpleKeyValueRepository
36 | implements HazelcastRepository {
37 |
38 | public SimpleHazelcastRepository(EntityInformation metadata, KeyValueOperations operations) {
39 | super(metadata, operations);
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/HazelcastUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast;
17 |
18 | import com.hazelcast.config.Config;
19 | import com.hazelcast.core.Hazelcast;
20 | import com.hazelcast.core.HazelcastInstance;
21 |
22 | /**
23 | * @author Christoph Strobl
24 | */
25 | public class HazelcastUtils {
26 |
27 | static Config hazelcastConfig() {
28 |
29 | Config hazelcastConfig = new Config();
30 | hazelcastConfig.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
31 | hazelcastConfig.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(false);
32 | hazelcastConfig.getNetworkConfig().getJoin().getAwsConfig().setEnabled(false);
33 |
34 | return hazelcastConfig;
35 | }
36 |
37 | public static HazelcastKeyValueAdapter preconfiguredHazelcastKeyValueAdapter() {
38 | HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance(hazelcastConfig());
39 | HazelcastKeyValueAdapter hazelcastKeyValueAdapter = new HazelcastKeyValueAdapter(hazelcastInstance);
40 | return hazelcastKeyValueAdapter;
41 | }
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/support/HazelcastEntityInformation.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.support;
17 |
18 | import org.springframework.data.annotation.Id;
19 | import org.springframework.data.mapping.MappingException;
20 | import org.springframework.data.mapping.PersistentEntity;
21 | import org.springframework.data.repository.core.EntityInformation;
22 | import org.springframework.data.repository.core.support.PersistentEntityInformation;
23 |
24 | /**
25 | * {@link EntityInformation} implementation checks for {@link Id} field eagarly to prevent NPE if not found.
26 | *
27 | * @author Gokhan Oner
28 | */
29 | class HazelcastEntityInformation
30 | extends PersistentEntityInformation {
31 |
32 | /**
33 | * @param entity must not be {@literal null}.
34 | */
35 | HazelcastEntityInformation(PersistentEntity entity) {
36 | super(entity);
37 | if (!entity.hasIdProperty()) {
38 | throw new MappingException(
39 | String.format("Entity %s requires a field annotated with %s", entity.getName(), Id.class.getName()));
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/HazelcastRepository.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository;
17 |
18 | import org.springframework.data.keyvalue.repository.KeyValueRepository;
19 | import org.springframework.data.repository.NoRepositoryBean;
20 |
21 | import java.io.Serializable;
22 |
23 | /**
24 | *
28 | * Although part of the rationale of the repository interface is to abstract the implementation, it is useful for type
29 | * checking to confirm the allowed type generics for the domain classes.
30 | *
31 | *
32 | * Note that {@link org.springframework.data.keyvalue.repository.KeyValueRepository KeyValueRepository} defines that the
33 | * {@code ID} class extends {@link Serializable}.
34 | *
35 | *
36 | * @param The type of the domain value class
37 | * @param The type of the domain key class
38 | * @author Neil Stevenson
39 | */
40 | @NoRepositoryBean
41 | public interface HazelcastRepository
42 | extends KeyValueRepository {
43 | }
44 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/domain/City.java:
--------------------------------------------------------------------------------
1 | package test.utils.domain;
2 |
3 | import java.io.Serializable;
4 | import java.util.Objects;
5 |
6 | import org.springframework.data.annotation.Id;
7 | import org.springframework.data.geo.Point;
8 | import org.springframework.data.keyvalue.annotation.KeySpace;
9 |
10 | import test.utils.TestConstants;
11 |
12 | /**
13 | * Domain class used for tests to test geospatial data.
14 | *
15 | * @author Ulhas R Manekar
16 | */
17 | @KeySpace(TestConstants.CITY_MAP_NAME)
18 | public class City
19 | implements Comparable, Serializable {
20 | private static final long serialVersionUID = 1L;
21 |
22 | @Id
23 | private String id;
24 | private String name;
25 | private Point location;
26 |
27 | public City() {
28 | super();
29 | }
30 |
31 | public City(String id, String name, Point location) {
32 | super();
33 | this.id = id;
34 | this.name = name;
35 | this.location = location;
36 | }
37 |
38 | public String getId() {
39 | return id;
40 | }
41 |
42 | public void setId(String id) {
43 | this.id = id;
44 | }
45 |
46 | public String getName() {
47 | return name;
48 | }
49 |
50 | public void setName(String name) {
51 | this.name = name;
52 | }
53 |
54 | public Point getLocation() {
55 | return location;
56 | }
57 |
58 | public void setLocation(Point location) {
59 | this.location = location;
60 | }
61 |
62 | @Override
63 | public int hashCode() {
64 | return Objects.hash(id, name, location);
65 | }
66 |
67 | @Override
68 | public boolean equals(Object o) {
69 | if (this == o) {
70 | return true;
71 | }
72 | if (o == null || getClass() != o.getClass()) {
73 | return false;
74 | }
75 | City city = (City) o;
76 | return Objects.equals(id, city.id) && Objects.equals(name, city.name)
77 | && Objects.equals(location, city.location);
78 | }
79 |
80 | // Sort by name
81 | @Override
82 | public int compareTo(City that) {
83 | return this.name.compareTo(that.getName());
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/repository/support/HazelcastEntityInformationTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.support;
17 |
18 | import com.hazelcast.core.Hazelcast;
19 | import org.junit.AfterClass;
20 | import org.junit.Before;
21 | import org.junit.Test;
22 | import org.springframework.data.hazelcast.HazelcastUtils;
23 | import org.springframework.data.keyvalue.core.KeyValueOperations;
24 | import org.springframework.data.keyvalue.core.KeyValueTemplate;
25 | import org.springframework.data.mapping.MappingException;
26 | import org.springframework.data.mapping.PersistentEntity;
27 | import test.utils.domain.NoIdEntity;
28 |
29 | public class HazelcastEntityInformationTest {
30 |
31 | private KeyValueOperations operations;
32 |
33 | @Before
34 | public void setUp() {
35 | this.operations = new KeyValueTemplate(HazelcastUtils.preconfiguredHazelcastKeyValueAdapter());
36 | }
37 |
38 | @Test(expected = MappingException.class)
39 | public void throwsMappingExceptionWhenNoIdPropertyPresent() {
40 | PersistentEntity, ?> persistentEntity = operations.getMappingContext().getPersistentEntity(NoIdEntity.class);
41 | new HazelcastEntityInformation<>(persistentEntity);
42 | }
43 |
44 | @AfterClass
45 | public static void close() {
46 | Hazelcast.shutdownAll();
47 | }
48 | }
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/config/HazelcastRepositoriesRegistrar.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.config;
17 |
18 | import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
19 | import org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport;
20 | import org.springframework.data.repository.config.RepositoryConfigurationExtension;
21 |
22 | import java.lang.annotation.Annotation;
23 |
24 | /**
25 | * Special {@link ImportBeanDefinitionRegistrar} to point the infrastructure to inspect
26 | * {@link EnableHazelcastRepositories}.
27 | *
28 | * @author Oliver Gierke
29 | * @author Neil Stevenson
30 | * @author Rafal Leszko
31 | */
32 | class HazelcastRepositoriesRegistrar extends RepositoryBeanDefinitionRegistrarSupport {
33 |
34 | /*
35 | * (non-Javadoc)
36 | * @see org.springframework.data.keyvalue.repository.config.KeyValueRepositoriesRegistrar#getAnnotation()
37 | */
38 | @Override
39 | protected Class extends Annotation> getAnnotation() {
40 | return EnableHazelcastRepositories.class;
41 | }
42 |
43 | /*
44 | * (non-Javadoc)
45 | * @see org.springframework.data.repository.config.RepositoryBeanDefinitionRegistrarSupport#getExtension()
46 | */
47 | @Override
48 | protected RepositoryConfigurationExtension getExtension() {
49 | return new HazelcastRepositoryConfigurationExtension();
50 | }
51 |
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/query/HazelcastCriteriaAccessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.query;
17 |
18 | import com.hazelcast.query.Predicate;
19 | import com.hazelcast.query.impl.predicates.PagingPredicateImpl;
20 | import org.springframework.data.keyvalue.core.CriteriaAccessor;
21 | import org.springframework.data.keyvalue.core.query.KeyValueQuery;
22 |
23 | /**
24 | *
25 | * Provide a mechanism to convert the abstract query into the direct implementation in Hazelcast.
26 | *
27 | *
28 | * @author Neil Stevenson
29 | * @author Viacheslav Petriaiev
30 | */
31 | public class HazelcastCriteriaAccessor
32 | implements CriteriaAccessor> {
33 |
34 | /**
35 | * @param A query in Spring form
36 | * @return The same in Hazelcast form
37 | */
38 | public Predicate, ?> resolve(KeyValueQuery> query) {
39 |
40 | if (query == null) {
41 | return null;
42 | }
43 |
44 | final Object criteria = query.getCriteria();
45 | if (criteria == null) {
46 | return null;
47 | }
48 |
49 | if (criteria instanceof PagingPredicateImpl) {
50 | PagingPredicateImpl pagingPredicate = (PagingPredicateImpl) criteria;
51 | query.limit(pagingPredicate.getPageSize());
52 | return pagingPredicate.getPredicate();
53 | }
54 |
55 | if (criteria instanceof Predicate) {
56 | return (Predicate, ?>) criteria;
57 | }
58 |
59 | throw new UnsupportedOperationException(query.toString());
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/custom/MyTitleRepositoryImpl.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.repository.custom;
18 |
19 | import org.springframework.data.hazelcast.repository.support.SimpleHazelcastRepository;
20 | import org.springframework.data.keyvalue.core.KeyValueOperations;
21 | import org.springframework.data.repository.core.EntityInformation;
22 | import test.utils.domain.MyTitle;
23 |
24 | import java.io.Serializable;
25 |
26 | /**
27 | *
Implement a custom repository for {@link MyTitleRepository}, but
28 | * inherit most of the behaviour from {@link SimpleHazelcastRepository}.
29 | *
30 | *
31 | * @param The domain object
32 | * @param The key of the domain object
33 | * @author Neil Stevenson
34 | */
35 | public class MyTitleRepositoryImpl
36 | extends SimpleHazelcastRepository
37 | implements MyTitleRepository {
38 |
39 | public MyTitleRepositoryImpl(EntityInformation metadata, KeyValueOperations keyValueOperations) {
40 | super(metadata, keyValueOperations);
41 | }
42 |
43 | /**
44 | *
45 | * Count the words in a particular title.
46 | *
47 | *
48 | * @param Key to lookup
49 | * @return Tokens in string, -1 if not found
50 | */
51 | public int wordsInTitle(String year) {
52 | @SuppressWarnings("unchecked") MyTitle myTitle = (MyTitle) super.findById((ID) year).orElse(null);
53 |
54 | if (myTitle == null) {
55 | return -1;
56 | } else {
57 | return myTitle.getTitle().split("[-\\s]").length;
58 | }
59 | }
60 |
61 | }
62 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/domain/Makeup.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.domain;
18 |
19 | import org.springframework.data.annotation.Id;
20 | import org.springframework.data.keyvalue.annotation.KeySpace;
21 | import test.utils.TestConstants;
22 |
23 | import java.io.Serializable;
24 |
25 | /**
26 | *
{@code Makeup} is for the prize for make-up and hair styling.
27 | *
28 | *
29 | * Give an argument {@code @KeySpace} to name the map used, "{@code Make-up}"
30 | * rather than the default fully class name.
31 | *
32 | *
33 | * @author Neil Stevenson
34 | */
35 | @KeySpace(TestConstants.MAKEUP_MAP_NAME)
36 | public class Makeup
37 | implements Comparable, Serializable {
38 |
39 | private static final long serialVersionUID = 1L;
40 |
41 | @Id
42 | private String id;
43 | private String filmTitle;
44 | private String artistOrArtists;
45 |
46 | public String getId() {
47 | return id;
48 | }
49 |
50 | public void setId(String id) {
51 | this.id = id;
52 | }
53 |
54 | public String getFilmTitle() {
55 | return filmTitle;
56 | }
57 |
58 | public void setFilmTitle(String filmTitle) {
59 | this.filmTitle = filmTitle;
60 | }
61 |
62 | public String getArtistOrArtists() {
63 | return artistOrArtists;
64 | }
65 |
66 | public void setArtistOrArtists(String artistOrArtists) {
67 | this.artistOrArtists = artistOrArtists;
68 | }
69 |
70 | @Override
71 | public String toString() {
72 | return "Makeup [id=" + id + ", filmTitle=" + filmTitle + ", artistOrArtists=" + artistOrArtists + "]";
73 | }
74 |
75 | // Sort by film title only
76 | @Override
77 | public int compareTo(Makeup that) {
78 | return this.filmTitle.compareTo(that.getFilmTitle());
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/support/HazelcastQueryMethod.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.support;
17 |
18 | import org.springframework.core.annotation.AnnotationUtils;
19 | import org.springframework.data.hazelcast.repository.query.Query;
20 | import org.springframework.data.keyvalue.annotation.KeySpace;
21 | import org.springframework.data.projection.ProjectionFactory;
22 | import org.springframework.data.repository.core.RepositoryMetadata;
23 | import org.springframework.data.repository.query.QueryMethod;
24 | import org.springframework.util.StringUtils;
25 |
26 | import java.lang.reflect.Method;
27 |
28 | /**
29 | * Hazelcast {@link QueryMethod} Implementation
30 | */
31 | public class HazelcastQueryMethod
32 | extends QueryMethod {
33 |
34 | private final Method method;
35 |
36 | public HazelcastQueryMethod(Method method, RepositoryMetadata metadata, ProjectionFactory factory) {
37 | super(method, metadata, factory);
38 | this.method = method;
39 | }
40 |
41 | public boolean hasAnnotatedQuery() {
42 | return StringUtils.hasText(getAnnotatedQuery());
43 | }
44 |
45 | String getAnnotatedQuery() {
46 | Query query = method.getAnnotation(Query.class);
47 | String queryString = (query != null ? (String) AnnotationUtils.getValue(query) : null);
48 | return (StringUtils.hasText(queryString) ? queryString : null);
49 | }
50 |
51 | String getKeySpace() {
52 | Class> clazz = getEntityInformation().getJavaType();
53 | KeySpace keySpace = clazz.getAnnotation(KeySpace.class);
54 | String queryString = (keySpace != null ? (String) AnnotationUtils.getValue(keySpace) : null);
55 | return (StringUtils.hasText(queryString) ? queryString : clazz.getName());
56 |
57 | }
58 |
59 | }
60 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/repository/config/EnableHazelcastRepositoriesIT.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.config;
17 |
18 | import org.junit.Test;
19 | import org.junit.runner.RunWith;
20 | import org.springframework.beans.factory.annotation.Autowired;
21 | import org.springframework.test.annotation.DirtiesContext;
22 | import org.springframework.test.context.ActiveProfiles;
23 | import org.springframework.test.context.ContextConfiguration;
24 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25 | import test.utils.InstanceHelper;
26 | import test.utils.TestConstants;
27 | import test.utils.domain.Person;
28 | import test.utils.repository.standard.PersonRepository;
29 |
30 | import java.util.List;
31 |
32 | import static org.hamcrest.Matchers.hasSize;
33 | import static org.hamcrest.Matchers.is;
34 | import static org.hamcrest.Matchers.notNullValue;
35 | import static org.junit.Assert.assertThat;
36 |
37 | /**
38 | *
39 | * Integration test for Hazelcast repositories.
40 | *
41 | *
42 | * Domain class {@link Person} and repository {@link PersonRepository} made into outer classes for use in other tests.
43 | *
44 | *
45 | * @author Christoph Strobl
46 | * @author Oliver Gierke
47 | * @author Neil Stevenson
48 | */
49 | @RunWith(SpringJUnit4ClassRunner.class)
50 | @ContextConfiguration(classes = {InstanceHelper.class})
51 | @ActiveProfiles(TestConstants.SPRING_TEST_PROFILE_SINGLETON)
52 | @DirtiesContext
53 | public class EnableHazelcastRepositoriesIT {
54 |
55 | @Autowired
56 | PersonRepository repo;
57 |
58 | @Test
59 | public void shouldEnableKeyValueRepositoryCorrectly() {
60 |
61 | assertThat(repo, notNullValue());
62 |
63 | Person person = new Person();
64 | person.setFirstname("foo");
65 | repo.save(person);
66 |
67 | List result = repo.findByFirstname("foo");
68 |
69 | assertThat(result, hasSize(1));
70 | assertThat(result.get(0).getFirstname(), is("foo"));
71 | }
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/custom/MyTitleRepositoryFactoryBean.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.repository.custom;
18 |
19 | import com.hazelcast.core.HazelcastInstance;
20 | import org.springframework.data.hazelcast.repository.support.HazelcastRepositoryFactoryBean;
21 | import org.springframework.data.keyvalue.core.KeyValueOperations;
22 | import org.springframework.data.repository.Repository;
23 | import org.springframework.data.repository.query.RepositoryQuery;
24 | import org.springframework.data.repository.query.parser.AbstractQueryCreator;
25 |
26 | import javax.annotation.Resource;
27 | import java.io.Serializable;
28 |
29 | /**
30 | *
Factory bean for creating instances of {@link MyTitleRepository},
31 | * being {@link MovieRepository} and {@link SongRepository}.
32 | *
33 | *
34 | * @param Repository type
35 | * @param Domain object type
36 | * @param Key of domain object type
37 | * @author Neil Stevenson
38 | */
39 | public class MyTitleRepositoryFactoryBean, S, ID extends Serializable>
40 | extends HazelcastRepositoryFactoryBean {
41 | @Resource
42 | private HazelcastInstance hazelcastInstance;
43 |
44 | /*
45 | * Creates a new {@link MyTitleRepositoryFactoryBean} for the given repository interface.
46 | */
47 | public MyTitleRepositoryFactoryBean(Class extends T> repositoryInterface) {
48 | super(repositoryInterface);
49 | }
50 |
51 | /* Create a specialised repository factory.
52 | *
53 | * (non-Javadoc)
54 | * @see org.springframework.data.hazelcast.repository.support.HazelcastRepositoryFactoryBean#createRepositoryFactory(org
55 | * .springframework.data.keyvalue.core.KeyValueOperations, java.lang.Class, java.lang.Class)
56 | */
57 | @Override
58 | protected MyTitleRepositoryFactory createRepositoryFactory(KeyValueOperations operations,
59 | Class extends AbstractQueryCreator, ?>> queryCreator,
60 | Class extends RepositoryQuery> repositoryQueryType) {
61 |
62 | return new MyTitleRepositoryFactory(operations, queryCreator, hazelcastInstance);
63 | }
64 |
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/topology/ClientServerIT.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.springframework.data.hazelcast.topology;
18 |
19 | import com.hazelcast.query.Predicate;
20 | import com.hazelcast.query.Predicates;
21 | import org.junit.Test;
22 | import org.springframework.test.context.ActiveProfiles;
23 | import test.utils.TestConstants;
24 | import test.utils.domain.Person;
25 |
26 | import java.util.Set;
27 |
28 | import static org.hamcrest.Matchers.equalTo;
29 | import static org.hamcrest.Matchers.notNullValue;
30 | import static org.junit.Assert.assertThat;
31 |
32 | /**
33 | *
34 | * Run the {@link AbstractTopologyIT} tests with the client-server profile.
35 | *
36 | *
37 | * Spring Data Hazelcast uses the client, so the tests examine the server content to confirm client operations are sent
38 | * there.
39 | *
40 | *
41 | * @author Neil Stevenson
42 | */
43 | @ActiveProfiles(TestConstants.SPRING_TEST_PROFILE_CLIENT_SERVER)
44 | public class ClientServerIT
45 | extends AbstractTopologyIT {
46 |
47 | /* Test data loaded into the client should exist on the
48 | * server.
49 | */
50 | @Test
51 | public void notJavaDuke() {
52 | String FIRST_NAME_IS_JOHN = "John";
53 | String LAST_NAME_IS_WAYNE = "Wayne";
54 | String NINETEEN_SIXTY_NINE = "1969";
55 |
56 | Predicate, ?> predicate = Predicates
57 | .and(Predicates.equal("firstname", FIRST_NAME_IS_JOHN), Predicates.equal("lastname", LAST_NAME_IS_WAYNE));
58 |
59 | // Force operation to server's content, not remote
60 | Set localKeySet = super.server_personMap.localKeySet((Predicate) predicate);
61 |
62 | assertThat("Entry exists", localKeySet.size(), equalTo(1));
63 | String key = localKeySet.iterator().next();
64 |
65 | assertThat("Correct key", key, equalTo(NINETEEN_SIXTY_NINE));
66 |
67 | Person person = super.server_personMap.get(key);
68 | assertThat("Not invalidated", person, notNullValue());
69 | assertThat("@Id matches key", person.getId(), equalTo(key));
70 | assertThat("First name", person.getFirstname(), equalTo(FIRST_NAME_IS_JOHN));
71 | assertThat("Last name", person.getLastname(), equalTo(LAST_NAME_IS_WAYNE));
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/domain/MyTitle.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.domain;
18 |
19 | import org.springframework.data.annotation.Id;
20 |
21 | import java.io.Serializable;
22 |
23 | /**
24 | *
25 | * An abstract base clase for key-value objects that have a string
26 | * key and another string field.
27 | *
28 | *
29 | * @author Neil Stevenson
30 | * @see {@link Movie}
31 | * @see {@link Song}
32 | */
33 | public abstract class MyTitle
34 | implements Comparable, Serializable {
35 | private static final long serialVersionUID = 1L;
36 |
37 | @Id
38 | private String id;
39 | private String title;
40 |
41 | public String getId() {
42 | return id;
43 | }
44 |
45 | public void setId(String id) {
46 | this.id = id;
47 | }
48 |
49 | public String getTitle() {
50 | return title;
51 | }
52 |
53 | public void setTitle(String title) {
54 | this.title = title;
55 | }
56 |
57 | @Override
58 | public int hashCode() {
59 | final int prime = 31;
60 | int result = 1;
61 | result = prime * result + ((id == null) ? 0 : id.hashCode());
62 | result = prime * result + ((title == null) ? 0 : title.hashCode());
63 | return result;
64 | }
65 |
66 | @Override
67 | public boolean equals(Object obj) {
68 | if (this == obj) {
69 | return true;
70 | }
71 | if (obj == null) {
72 | return false;
73 | }
74 | if (getClass() != obj.getClass()) {
75 | return false;
76 | }
77 | MyTitle other = (MyTitle) obj;
78 | if (id == null) {
79 | if (other.id != null) {
80 | return false;
81 | }
82 | } else if (!id.equals(other.id)) {
83 | return false;
84 | }
85 | if (title == null) {
86 | if (other.title != null) {
87 | return false;
88 | }
89 | } else if (!title.equals(other.title)) {
90 | return false;
91 | }
92 | return true;
93 | }
94 |
95 | // Sort by title only
96 | @Override
97 | public int compareTo(MyTitle that) {
98 | return this.title.compareTo(that.getTitle());
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/test/utils/repository/custom/MyTitleRepositoryFactory.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package test.utils.repository.custom;
18 |
19 | import com.hazelcast.core.HazelcastInstance;
20 | import org.springframework.data.hazelcast.repository.support.HazelcastRepositoryFactory;
21 | import org.springframework.data.keyvalue.core.KeyValueOperations;
22 | import org.springframework.data.repository.core.EntityInformation;
23 | import org.springframework.data.repository.core.RepositoryInformation;
24 | import org.springframework.data.repository.core.RepositoryMetadata;
25 | import org.springframework.data.repository.query.parser.AbstractQueryCreator;
26 |
27 | import java.io.Serializable;
28 |
29 | /**
30 | *
Factory for creating instances of {@link MyTitleRepository},
31 | * being {@link MovieRepository} and {@link SongRepository}.
32 | *
33 | *
34 | * @author Neil Stevenson
35 | */
36 | public class MyTitleRepositoryFactory
37 | extends HazelcastRepositoryFactory {
38 |
39 | private KeyValueOperations keyValueOperations;
40 |
41 | /* Delegate creation to super, but capture the KeyValueOperations
42 | * object for later use.
43 | */
44 | public MyTitleRepositoryFactory(KeyValueOperations keyValueOperations,
45 | Class extends AbstractQueryCreator, ?>> queryCreator,
46 | HazelcastInstance hazelcastInstance) {
47 | super(keyValueOperations, queryCreator, hazelcastInstance);
48 | this.keyValueOperations = keyValueOperations;
49 | }
50 |
51 | /* Create an implementation using captured KeyValueOperations
52 | * and the domain class metadata.
53 | *
54 | * (non-Javadoc)
55 | * @see org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory#getTargetRepository(org
56 | * .springframework.data.repository.core.RepositoryInformation)
57 | */
58 | @SuppressWarnings({"unchecked", "rawtypes"})
59 | protected Object getTargetRepository(RepositoryInformation repositoryInformation) {
60 | EntityInformation, Serializable> entityInformation = getEntityInformation(repositoryInformation.getDomainType());
61 | return new MyTitleRepositoryImpl(entityInformation, this.keyValueOperations);
62 | }
63 |
64 | protected Class> getRepositoryBaseClass(RepositoryMetadata metadata) {
65 | return MyTitleRepository.class;
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/support/StringBasedHazelcastRepositoryQuery.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.support;
17 |
18 | import com.hazelcast.core.HazelcastInstance;
19 | import com.hazelcast.map.IMap;
20 | import com.hazelcast.query.impl.predicates.SqlPredicate;
21 | import org.springframework.data.repository.query.QueryMethod;
22 | import org.springframework.data.repository.query.RepositoryQuery;
23 |
24 | import java.util.Collection;
25 | import java.util.stream.Collectors;
26 |
27 | /**
28 | * {@link RepositoryQuery} using String based {@link SqlPredicate} to query Hazelcast Cluster.
29 | */
30 | public class StringBasedHazelcastRepositoryQuery
31 | implements RepositoryQuery {
32 |
33 | private final HazelcastQueryMethod queryMethod;
34 | private final String keySpace;
35 | private final HazelcastInstance hazelcastInstance;
36 |
37 | public StringBasedHazelcastRepositoryQuery(HazelcastQueryMethod queryMethod, HazelcastInstance hazelcastInstance) {
38 | this.queryMethod = queryMethod;
39 | this.keySpace = queryMethod.getKeySpace();
40 | this.hazelcastInstance = hazelcastInstance;
41 | }
42 |
43 | @Override
44 | public Object execute(Object[] parameters) {
45 | String queryStringTemplate = queryMethod.getAnnotatedQuery();
46 | String queryString = String.format(queryStringTemplate, formatParameters(parameters));
47 | SqlPredicate sqlPredicate = new SqlPredicate(queryString);
48 | return getMap(keySpace).values(sqlPredicate);
49 | }
50 |
51 | private Object[] formatParameters(Object[] parameters) {
52 | Object[] result = new Object[parameters.length];
53 | for (int i = 0; i < parameters.length; i++) {
54 | if (parameters[i] instanceof Collection) {
55 | result[i] = formatCollection((Collection) parameters[i]);
56 | } else {
57 | result[i] = parameters[i];
58 | }
59 | }
60 | return result;
61 | }
62 |
63 | private static String formatCollection(Collection> collection) {
64 | return String.format("(%s)", collection.stream().map(Object::toString).collect(Collectors.joining(",")));
65 | }
66 |
67 | private IMap getMap(String keySpace) {
68 | return hazelcastInstance.getMap(keySpace);
69 | }
70 |
71 | @Override
72 | public QueryMethod getQueryMethod() {
73 | return queryMethod;
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/org/springframework/data/hazelcast/repository/query/HazelcastPropertyComparatorTest.java:
--------------------------------------------------------------------------------
1 | package org.springframework.data.hazelcast.repository.query;
2 |
3 | import org.junit.Test;
4 |
5 | import java.util.AbstractMap;
6 | import java.util.Map.Entry;
7 |
8 | import static org.junit.Assert.assertEquals;
9 |
10 | public class HazelcastPropertyComparatorTest {
11 |
12 | private final HazelcastPropertyComparator propertyComparator = new HazelcastPropertyComparator("value", true);
13 |
14 | @Test
15 | public void testNaturalOrder() {
16 | TestData testData1 = new TestData(1);
17 | TestData testData2 = new TestData(2);
18 | Entry entry1 = entryOf("testData1", testData1);
19 | Entry entry2 = entryOf("testData2", testData2);
20 | assertEquals(-1, propertyComparator.compare(entry1, entry2));
21 | assertEquals(1, propertyComparator.compare(entry2, entry1));
22 | }
23 |
24 | @Test
25 | public void compareTwoNullProperties() {
26 | TestData testData1 = new TestData(null);
27 | TestData testData2 = new TestData(null);
28 | Entry nullField1 = entryOf("testData1", testData1);
29 | Entry nullField2 = entryOf("testData2", testData2);
30 | assertEquals(0, propertyComparator.compare(nullField1, nullField2));
31 | assertEquals(0, propertyComparator.compare(nullField2, nullField1));
32 | }
33 |
34 | @Test
35 | public void compareNullPropertyWithNonNullProperty() {
36 | TestData testData1 = new TestData(null);
37 | TestData testData2 = new TestData(2);
38 | Entry nullField = entryOf("testData1", testData1);
39 | Entry notNullField = entryOf("testData2", testData2);
40 | assertEquals(1, propertyComparator.compare(nullField, notNullField));
41 | assertEquals(-1, propertyComparator.compare(notNullField, nullField));
42 | }
43 |
44 | @Test
45 | public void compareNotComparableProperties() {
46 | TestData testData1 = new TestData(new TestData(1));
47 | TestData testData2 = new TestData(2);
48 | TestData testData3 = new TestData(new TestData(3));
49 | Entry entry1NotComparable = entryOf("testData1", testData1);
50 | Entry entry2Comparable = entryOf("testData2", testData2);
51 | Entry entry3NotComparable = entryOf("testData3", testData3);
52 | assertEquals(0, propertyComparator.compare(entry1NotComparable, entry2Comparable));
53 | assertEquals(0, propertyComparator.compare(entry2Comparable, entry1NotComparable));
54 | assertEquals(0, propertyComparator.compare(entry1NotComparable, entry3NotComparable));
55 | }
56 |
57 | private static Entry entryOf(K key, V value) {
58 | return new AbstractMap.SimpleEntry<>(key, value);
59 | }
60 |
61 | private static class TestData {
62 | private final Object value;
63 |
64 | public TestData(Object value) {
65 | this.value = value;
66 | }
67 |
68 | public Object getValue() {
69 | return value;
70 | }
71 | }
72 |
73 | }
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/repository/query/HazelcastSortAccessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast.repository.query;
17 |
18 | import org.springframework.data.domain.Sort;
19 | import org.springframework.data.domain.Sort.NullHandling;
20 | import org.springframework.data.domain.Sort.Order;
21 | import org.springframework.data.keyvalue.core.SortAccessor;
22 | import org.springframework.data.keyvalue.core.query.KeyValueQuery;
23 |
24 | import java.util.Comparator;
25 | import java.util.Map.Entry;
26 |
27 | /**
28 | *
32 | * Although {@code SpelPropertyComparator} would do most of the work, this is not serializable so cannot work in a
33 | * cluster. Also, do not wish to assume anything other than Hazelcast classes are available on remote nodes.
34 | *
35 | *
36 | * @author Neil Stevenson
37 | */
38 | public class HazelcastSortAccessor
39 | implements SortAccessor>> {
40 |
41 | /**
42 | *
43 | * Sort on a sequence of fields, possibly none.
44 | *
45 | *
46 | * @param query If not null, will contain one of more {@link Sort.Order} objects.
47 | * @return A sequence of comparators or {@code null}
48 | */
49 | public Comparator> resolve(KeyValueQuery> query) {
50 |
51 | if (query == null || query.getSort() == Sort.unsorted()) {
52 | return null;
53 | }
54 |
55 | Comparator hazelcastPropertyComparator = null;
56 |
57 | for (Order order : query.getSort()) {
58 |
59 | if (order.getProperty().indexOf('.') > -1) {
60 | throw new UnsupportedOperationException("Embedded fields not implemented: " + order);
61 | }
62 |
63 | if (order.isIgnoreCase()) {
64 | throw new UnsupportedOperationException("Ignore case not implemented: " + order);
65 | }
66 |
67 | if (NullHandling.NATIVE != order.getNullHandling()) {
68 | throw new UnsupportedOperationException("Null handling not implemented: " + order);
69 | }
70 |
71 | if (hazelcastPropertyComparator == null) {
72 | hazelcastPropertyComparator = new HazelcastPropertyComparator(order.getProperty(),
73 | order.isAscending());
74 | } else {
75 | hazelcastPropertyComparator = hazelcastPropertyComparator.thenComparing(
76 | new HazelcastPropertyComparator(order.getProperty(),
77 | order.isAscending()));
78 | }
79 | }
80 |
81 | return hazelcastPropertyComparator;
82 | }
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/org/springframework/data/hazelcast/HazelcastKeyValueAdapter.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 | package org.springframework.data.hazelcast;
17 |
18 | import com.hazelcast.core.HazelcastInstance;
19 | import com.hazelcast.map.IMap;
20 | import org.springframework.data.keyvalue.core.AbstractKeyValueAdapter;
21 | import org.springframework.data.keyvalue.core.ForwardingCloseableIterator;
22 | import org.springframework.data.util.CloseableIterator;
23 | import org.springframework.util.Assert;
24 |
25 | import java.util.Iterator;
26 | import java.util.Map;
27 | import java.util.Map.Entry;
28 |
29 | /**
30 | * @author Christoph Strobl
31 | * @author Neil Stevenson
32 | * @author Viacheslav Petriaiev
33 | */
34 | public class HazelcastKeyValueAdapter
35 | extends AbstractKeyValueAdapter {
36 |
37 | private HazelcastInstance hzInstance;
38 |
39 | public HazelcastKeyValueAdapter(HazelcastInstance hzInstance) {
40 | super(new HazelcastQueryEngine());
41 | Assert.notNull(hzInstance, "hzInstance must not be 'null'.");
42 | this.hzInstance = hzInstance;
43 | }
44 |
45 | public void setHzInstance(HazelcastInstance hzInstance) {
46 | this.hzInstance = hzInstance;
47 | }
48 |
49 | @Override
50 | public Object put(Object id, Object item, String keyspace) {
51 | Assert.notNull(id, "Id must not be 'null' for adding.");
52 | Assert.notNull(item, "Item must not be 'null' for adding.");
53 |
54 | return getMap(keyspace).put(id, item);
55 | }
56 |
57 | @Override
58 | public boolean contains(Object id, String keyspace) {
59 | return getMap(keyspace).containsKey(id);
60 | }
61 |
62 | @Override
63 | public Object get(Object id, String keyspace) {
64 | return getMap(keyspace).get(id);
65 | }
66 |
67 | @Override
68 | public Object delete(Object id, String keyspace) {
69 | return getMap(keyspace).remove(id);
70 | }
71 |
72 | @Override
73 | public Iterable> getAllOf(String keyspace) {
74 | return getMap(keyspace).values();
75 | }
76 |
77 | @Override
78 | public void deleteAllOf(String keyspace) {
79 | getMap(keyspace).clear();
80 | }
81 |
82 | @Override
83 | public void clear() {
84 | this.hzInstance.shutdown();
85 | }
86 |
87 | protected IMap