#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: #275.
This commit is contained in:
Mark Paluch
2017-11-21 13:03:04 +01:00
parent 5ba65308a7
commit b61875f97c
13 changed files with 371 additions and 155 deletions

View File

@@ -1,35 +0,0 @@
package example.springdata.couchbase;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.couchbase.config.AbstractCouchbaseConfiguration;
import org.springframework.data.couchbase.repository.config.EnableCouchbaseRepositories;
import java.util.Collections;
import java.util.List;
/**
* Couchbase Configuration to connect to Couchbase data store
*
* @author Chandana Kithalagama
*/
@SpringBootApplication
@Configuration
@EnableCouchbaseRepositories
public class CouchbaseConfiguration extends AbstractCouchbaseConfiguration {
@Override
protected List<String> getBootstrapHosts() {
return Collections.singletonList("192.168.99.100");
}
@Override
protected String getBucketName() {
return "travel-sample";
}
@Override
protected String getBucketPassword() {
return "";
}
}

View File

@@ -1,9 +1,26 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.couchbase.model;
import lombok.Data;
import org.springframework.data.couchbase.core.mapping.Document;
import com.couchbase.client.java.repository.annotation.Field;
import com.couchbase.client.java.repository.annotation.Id;
import lombok.*;
import org.springframework.data.couchbase.core.mapping.Document;
/**
* A domain object representing an Airline
@@ -11,13 +28,11 @@ import org.springframework.data.couchbase.core.mapping.Document;
* @author Chandana Kithalagama
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document
public class Airline {
@Id
private int id;
private String id;
@Field
private String type;
@@ -36,5 +51,4 @@ public class Airline {
@Field
private String country;
}

View File

@@ -1,18 +1,52 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.couchbase.repository;
import example.springdata.couchbase.model.Airline;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.repository.CrudRepository;
import java.util.List;
import org.springframework.data.couchbase.core.query.N1qlPrimaryIndexed;
import org.springframework.data.couchbase.core.query.View;
import org.springframework.data.couchbase.core.query.ViewIndexed;
import org.springframework.data.repository.CrudRepository;
/**
* Repository interface to manage {@link Airline} instances.
*
* @author Chandana Kithalagama
* @author Mark Paluch
*/
public interface AirlineRepository extends CrudRepository<Airline, Integer>{
@N1qlPrimaryIndexed
@ViewIndexed(designDoc = "airlines")
public interface AirlineRepository extends CrudRepository<Airline, String> {
@Query("#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND name = $1")
List<Airline> findAirlineByName(String name);
/**
* Derived query selecting by {@code iataCode}.
*
* @param code
* @return
*/
Airline findAirlineByIataCode(String code);
/**
* Query method using {@code airlines/all} view.
*
* @return
*/
@View(designDocument = "airlines", viewName = "all")
List<Airline> findAllBy();
}

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.couchbase.repository;
import example.springdata.couchbase.model.Airline;
import lombok.RequiredArgsConstructor;
import java.util.List;
import javax.annotation.PostConstruct;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.couchbase.config.BeanNames;
import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.couchbase.repository.support.IndexManager;
import com.couchbase.client.java.query.N1qlQuery;
/**
* Simple configuration class.
*
* @author Chandana Kithalagama
* @author Mark Paluch
*/
@SpringBootApplication
@RequiredArgsConstructor
public class CouchbaseConfiguration {
private final CouchbaseOperations couchbaseOperations;
/**
* Create an {@link IndexManager} that allows index creation.
*
* @return
*/
@Bean(name = BeanNames.COUCHBASE_INDEX_MANAGER)
public IndexManager indexManager() {
return new IndexManager(true, true, false);
}
@PostConstruct
private void postConstruct() {
// Need to post-process travel data to add _class attribute
List<Airline> airlinesWithoutClassAttribute = couchbaseOperations.findByN1QL(N1qlQuery.simple( //
"SELECT META(`travel-sample`).id AS _ID, META(`travel-sample`).cas AS _CAS, `travel-sample`.* " + //
"FROM `travel-sample` " + //
"WHERE type = \"airline\" AND _class IS MISSING;"),
Airline.class);
airlinesWithoutClassAttribute.forEach(couchbaseOperations::save);
}
}

View File

@@ -0,0 +1,9 @@
spring.couchbase.bucket.name=travel-sample
spring.couchbase.bootstrap-hosts=localhost
# Required for Couchbase 5
spring.couchbase.bucket.password=password
# Increased timeout to fit slower environments like TravisCI
spring.couchbase.env.timeouts.view=15000
spring.couchbase.env.timeouts.query=15000