Skip to content

Commit b61875f

Browse files
committed
spring-projects#265 - Polishing.
Add license headers. Replace CouchbaseConfiguration with Spring Boot properties. Post-process example data to make it accessible for repository use. Fix Id type. Add examples for N1ql and view access. Enable couchbase examples in parent pom. Add test rule to skip tests if Couchbase is not available. Original pull request: spring-projects#275.
1 parent 5ba6530 commit b61875f

File tree

13 files changed

+371
-155
lines changed

13 files changed

+371
-155
lines changed

couchbase/example/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Spring Data Couchbase - Examples
2+
3+
This project contains samples of data access features with Spring Data (Couchbase).
4+
5+
## Prerequisites
6+
7+
The examples require a running [Couchbase Server](https://www.couchbase.com/downloads) server with the travel sample bucket imported. We assume you're running Couchbase 5 and we have `spring.couchbase.bucket.password=…` accordingly to adapt RBAC authentication.

couchbase/example/pom.xml

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,26 @@
22
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

5-
<artifactId>spring-data-couchbase-example</artifactId>
6-
<name>Basic sample for Spring Data Couchbase</name>
7-
85
<parent>
96
<artifactId>spring-data-couchbase-examples</artifactId>
107
<groupId>org.springframework.data.examples</groupId>
11-
<version>1.0.0.BUILD-SNAPSHOT</version>
8+
<version>2.0.0.BUILD-SNAPSHOT</version>
9+
<relativePath>../pom.xml</relativePath>
1210
</parent>
1311

14-
<build>
15-
<plugins>
16-
<plugin>
17-
<groupId>org.apache.maven.plugins</groupId>
18-
<artifactId>maven-surefire-plugin</artifactId>
19-
<configuration>
20-
<excludes>
21-
<exclude>**/*IntegrationTest*</exclude>
22-
</excludes>
23-
</configuration>
24-
</plugin>
25-
</plugins>
26-
</build>
12+
<artifactId>spring-data-couchbase-example</artifactId>
13+
<name>Basic sample for Spring Data Couchbase</name>
14+
<description>Small sample project showing the usage of Spring Data Couchbase.</description>
15+
16+
<dependencies>
17+
18+
<dependency>
19+
<groupId>${project.groupId}</groupId>
20+
<artifactId>spring-data-couchbase-example-utils</artifactId>
21+
<version>${project.version}</version>
22+
<scope>test</scope>
23+
</dependency>
24+
25+
</dependencies>
2726

2827
</project>

couchbase/example/src/main/java/example/springdata/couchbase/CouchbaseConfiguration.java

Lines changed: 0 additions & 35 deletions
This file was deleted.

couchbase/example/src/main/java/example/springdata/couchbase/model/Airline.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,38 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
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+
*/
116
package example.springdata.couchbase.model;
217

18+
import lombok.Data;
19+
20+
import org.springframework.data.couchbase.core.mapping.Document;
21+
322
import com.couchbase.client.java.repository.annotation.Field;
423
import com.couchbase.client.java.repository.annotation.Id;
5-
import lombok.*;
6-
import org.springframework.data.couchbase.core.mapping.Document;
724

825
/**
926
* A domain object representing an Airline
1027
*
1128
* @author Chandana Kithalagama
1229
*/
1330
@Data
14-
@AllArgsConstructor
15-
@NoArgsConstructor
1631
@Document
1732
public class Airline {
1833

1934
@Id
20-
private int id;
35+
private String id;
2136

2237
@Field
2338
private String type;
@@ -36,5 +51,4 @@ public class Airline {
3651

3752
@Field
3853
private String country;
39-
4054
}
Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,52 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
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+
*/
116
package example.springdata.couchbase.repository;
217

318
import example.springdata.couchbase.model.Airline;
4-
import org.springframework.data.couchbase.core.query.Query;
5-
import org.springframework.data.repository.CrudRepository;
619

720
import java.util.List;
821

22+
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
23+
import org.springframework.data.couchbase.core.query.View;
24+
import org.springframework.data.couchbase.core.query.ViewIndexed;
25+
import org.springframework.data.repository.CrudRepository;
26+
927
/**
1028
* Repository interface to manage {@link Airline} instances.
1129
*
1230
* @author Chandana Kithalagama
31+
* @author Mark Paluch
1332
*/
14-
public interface AirlineRepository extends CrudRepository<Airline, Integer>{
33+
@N1qlPrimaryIndexed
34+
@ViewIndexed(designDoc = "airlines")
35+
public interface AirlineRepository extends CrudRepository<Airline, String> {
36+
37+
/**
38+
* Derived query selecting by {@code iataCode}.
39+
*
40+
* @param code
41+
* @return
42+
*/
43+
Airline findAirlineByIataCode(String code);
1544

16-
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND name = $1")
17-
List<Airline> findAirlineByName(String name);
45+
/**
46+
* Query method using {@code airlines/all} view.
47+
*
48+
* @return
49+
*/
50+
@View(designDocument = "airlines", viewName = "all")
51+
List<Airline> findAllBy();
1852
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
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 example.springdata.couchbase.repository;
17+
18+
import example.springdata.couchbase.model.Airline;
19+
import lombok.RequiredArgsConstructor;
20+
21+
import java.util.List;
22+
23+
import javax.annotation.PostConstruct;
24+
25+
import org.springframework.boot.autoconfigure.SpringBootApplication;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.data.couchbase.config.BeanNames;
28+
import org.springframework.data.couchbase.core.CouchbaseOperations;
29+
import org.springframework.data.couchbase.repository.support.IndexManager;
30+
31+
import com.couchbase.client.java.query.N1qlQuery;
32+
33+
/**
34+
* Simple configuration class.
35+
*
36+
* @author Chandana Kithalagama
37+
* @author Mark Paluch
38+
*/
39+
@SpringBootApplication
40+
@RequiredArgsConstructor
41+
public class CouchbaseConfiguration {
42+
43+
private final CouchbaseOperations couchbaseOperations;
44+
45+
/**
46+
* Create an {@link IndexManager} that allows index creation.
47+
*
48+
* @return
49+
*/
50+
@Bean(name = BeanNames.COUCHBASE_INDEX_MANAGER)
51+
public IndexManager indexManager() {
52+
return new IndexManager(true, true, false);
53+
}
54+
55+
@PostConstruct
56+
private void postConstruct() {
57+
58+
// Need to post-process travel data to add _class attribute
59+
List<Airline> airlinesWithoutClassAttribute = couchbaseOperations.findByN1QL(N1qlQuery.simple( //
60+
"SELECT META(`travel-sample`).id AS _ID, META(`travel-sample`).cas AS _CAS, `travel-sample`.* " + //
61+
"FROM `travel-sample` " + //
62+
"WHERE type = \"airline\" AND _class IS MISSING;"),
63+
Airline.class);
64+
65+
airlinesWithoutClassAttribute.forEach(couchbaseOperations::save);
66+
}
67+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
spring.couchbase.bucket.name=travel-sample
2+
spring.couchbase.bootstrap-hosts=localhost
3+
4+
# Required for Couchbase 5
5+
spring.couchbase.bucket.password=password
6+
7+
# Increased timeout to fit slower environments like TravisCI
8+
spring.couchbase.env.timeouts.view=15000
9+
spring.couchbase.env.timeouts.query=15000

couchbase/example/src/test/java/example/springdata/couchbase/repository/AirlineRepositoryIntegrationTest.java

Lines changed: 0 additions & 84 deletions
This file was deleted.

0 commit comments

Comments
 (0)