Skip to content

Commit 124ce3c

Browse files
christophstroblmp911de
authored andcommitted
Polishing.
Rename tests, replace space indents with tabs. Closes #707
1 parent 220f8de commit 124ce3c

File tree

26 files changed

+387
-432
lines changed

26 files changed

+387
-432
lines changed

README.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ We have separate folders for the samples of individual modules:
88

99
== Spring Data for Apache Cassandra
1010

11+
* `aot-optimization` - Use Ahead-Of-Time Repositories with Spring Data for Apache Cassandra.
1112
* `example` - Shows core Spring Data support for Apache Cassandra.
1213
* `kotlin` - Example for using Cassandra with Kotlin.
1314
* `reactive` - Example project to show reactive template and repository support.
@@ -23,6 +24,7 @@ Local Elasticsearch instance must be running to run the tests.
2324

2425
== Spring Data JDBC
2526

27+
* `aot-optimization` - Use Ahead-Of-Time Repositories with Spring Data JDBC.
2628
* `basic` - Basic usage of Spring Data JDBC.
2729
* `graalvm-native` - This example compiles a basic Spring Data JDBC application into a GraalVM native image.
2830
* `howto` - A collection of projects to go with the https://spring.io/blog/2021/09/09/spring-data-jdbc-how-to-use-custom-id-generation[Spring Data JDBC - How to blog posts].
@@ -35,6 +37,7 @@ Local Elasticsearch instance must be running to run the tests.
3537

3638
== Spring Data JPA
3739

40+
* `aot-optimization` - Use Ahead-Of-Time Repositories with Spring Data JPA.
3841
* `eclipselink` - Sample project to show how to use Spring Data JPA with Spring Boot and https://www.eclipse.org/eclipselink/[Eclipselink].
3942
* `example` - Probably the project you want to have a look at first.
4043
Contains a variety of sample packages, showcasing the different levels at which you can use Spring Data JPA.
@@ -55,6 +58,7 @@ Contains also examples running on Virtual Threads.
5558

5659
== Spring Data MongoDB
5760

61+
* `aot-optimization` - Use Ahead-Of-Time Repositories with Spring Data MongoDB.
5862
* `aggregation` - Example project to showcase the MongoDB aggregation framework support.
5963
* `example` - Example project for general repository functionality (including geo-spatial functionality), Querydsl integration and advanced topics.
6064
* `fluent-api` - Example project to show the new fluent API (`MongoTemplate`-alternative) to interact with MongoDB.

cassandra/vector-search/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ public interface CommentRepository extends Repository<Comment, String> {
3434
}
3535
```
3636

37-
This example contains a test class to illustrate vector search with a Repository in `VectorAppTest`.
37+
This example contains a test class to illustrate vector search with a Repository in `CassandraVectorSearchTest`.

cassandra/vector-search/pom.xml

Lines changed: 28 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,41 @@
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
44
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
55
<modelVersion>4.0.0</modelVersion>
6-
76
<parent>
87
<groupId>org.springframework.data.examples</groupId>
98
<artifactId>spring-data-cassandra-examples</artifactId>
109
<version>4.0.0-SNAPSHOT</version>
11-
</parent>
10+
</parent>
1211

13-
<artifactId>spring-data-cassandra-vector-search</artifactId>
12+
<artifactId>spring-data-cassandra-vector-search</artifactId>
13+
<name>Spring Data Cassandra - Vector Search</name>
1414

1515
<dependencies>
1616

17-
<dependency>
18-
<groupId>org.jspecify</groupId>
19-
<artifactId>jspecify</artifactId>
20-
<version>1.0.0</version>
21-
</dependency>
22-
23-
<dependency>
24-
<groupId>org.springframework.boot</groupId>
25-
<artifactId>spring-boot-starter-test</artifactId>
26-
<scope>test</scope>
27-
</dependency>
28-
29-
<dependency>
30-
<groupId>org.springframework.boot</groupId>
31-
<artifactId>spring-boot-testcontainers</artifactId>
32-
<scope>test</scope>
33-
</dependency>
34-
35-
<dependency>
36-
<groupId>org.testcontainers</groupId>
37-
<artifactId>testcontainers-cassandra</artifactId>
38-
<exclusions>
39-
<exclusion>
40-
<groupId>com.datastax.cassandra</groupId>
41-
<artifactId>cassandra-driver-core</artifactId>
42-
</exclusion>
43-
</exclusions>
44-
</dependency>
45-
46-
<dependency>
47-
<groupId>org.springframework.data.examples</groupId>
48-
<artifactId>spring-data-cassandra-example-utils</artifactId>
49-
<version>${project.version}</version>
50-
<scope>test</scope>
51-
</dependency>
52-
53-
</dependencies>
54-
55-
<build>
56-
<plugins>
57-
<plugin>
58-
<groupId>org.apache.maven.plugins</groupId>
59-
<artifactId>maven-compiler-plugin</artifactId>
60-
</plugin>
61-
<plugin>
62-
<groupId>org.springframework.boot</groupId>
63-
<artifactId>spring-boot-maven-plugin</artifactId>
64-
</plugin>
65-
</plugins>
66-
</build>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-testcontainers</artifactId>
20+
<scope>test</scope>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.testcontainers</groupId>
25+
<artifactId>testcontainers-cassandra</artifactId>
26+
<exclusions>
27+
<exclusion>
28+
<groupId>com.datastax.cassandra</groupId>
29+
<artifactId>cassandra-driver-core</artifactId>
30+
</exclusion>
31+
</exclusions>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.data.examples</groupId>
36+
<artifactId>spring-data-cassandra-example-utils</artifactId>
37+
<version>${project.version}</version>
38+
<scope>test</scope>
39+
</dependency>
40+
41+
</dependencies>
6742

6843
</project>

cassandra/vector-search/src/main/java/example/springdata/vector/Comment.java

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -29,48 +29,45 @@
2929
@Table
3030
public class Comment {
3131

32-
@Id
33-
private String id;
32+
private @Id String id;
3433

35-
private String country;
36-
private String description;
34+
private String country;
35+
private String description;
3736

38-
@SaiIndexed
39-
@VectorType(dimensions = 5)
40-
private Vector embedding;
37+
@SaiIndexed
38+
@VectorType(dimensions = 5) private Vector embedding;
4139

42-
public Comment() {
43-
}
40+
public Comment() {}
4441

45-
public Comment(String country, String description, Vector embedding) {
46-
this.id = UUID.randomUUID().toString();
47-
this.country = country;
48-
this.description = description;
49-
this.embedding = embedding;
50-
}
42+
public Comment(String country, String description, Vector embedding) {
43+
this.id = UUID.randomUUID().toString();
44+
this.country = country;
45+
this.description = description;
46+
this.embedding = embedding;
47+
}
5148

52-
public static Comment of(Comment source) {
53-
return new Comment(source.getCountry(), source.getDescription(), source.getEmbedding());
54-
}
49+
public static Comment of(Comment source) {
50+
return new Comment(source.getCountry(), source.getDescription(), source.getEmbedding());
51+
}
5552

56-
public String getId() {
57-
return id;
58-
}
53+
public String getId() {
54+
return id;
55+
}
5956

60-
public String getCountry() {
61-
return country;
62-
}
57+
public String getCountry() {
58+
return country;
59+
}
6360

64-
public String getDescription() {
65-
return description;
66-
}
61+
public String getDescription() {
62+
return description;
63+
}
6764

68-
public Vector getEmbedding() {
69-
return embedding;
70-
}
65+
public Vector getEmbedding() {
66+
return embedding;
67+
}
7168

72-
@Override
73-
public String toString() {
74-
return "%s (%s)".formatted(getDescription(), getCountry());
75-
}
69+
@Override
70+
public String toString() {
71+
return "%s (%s)".formatted(getDescription(), getCountry());
72+
}
7673
}

cassandra/vector-search/src/main/java/example/springdata/vector/CommentRepository.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,8 +25,8 @@
2525

2626
public interface CommentRepository extends CrudRepository<Comment, String> {
2727

28-
SearchResults<Comment> searchTop10ByEmbeddingNear(Vector embedding, ScoringFunction function);
28+
SearchResults<Comment> searchTop10ByEmbeddingNear(Vector embedding, ScoringFunction function);
2929

30-
@Query("SELECT id, description, country, similarity_cosine(embedding,:embedding) AS score FROM comment ORDER BY embedding ANN OF :embedding LIMIT :limit")
31-
SearchResults<Comment> searchAnnotated(Vector embedding, Score distance, Limit limit);
30+
@Query("SELECT id, description, country, similarity_cosine(embedding,:embedding) AS score FROM comment ORDER BY embedding ANN OF :embedding LIMIT :limit")
31+
SearchResults<Comment> searchAnnotated(Vector embedding, Score distance, Limit limit);
3232
}

cassandra/vector-search/src/main/java/example/springdata/vector/VectorApp.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -25,28 +25,28 @@
2525
@SpringBootApplication
2626
public class VectorApp {
2727

28-
public static void main(String[] args) {
29-
SpringApplication.run(VectorApp.class, args);
30-
}
28+
public static void main(String[] args) {
29+
SpringApplication.run(VectorApp.class, args);
30+
}
3131

32-
@Component
33-
static class DbInitializer implements CommandLineRunner {
32+
@Component
33+
static class DbInitializer implements CommandLineRunner {
3434

35-
private final CassandraTemplate template;
35+
private final CassandraTemplate template;
3636

37-
DbInitializer(CassandraTemplate template) {
38-
this.template = template;
39-
}
37+
DbInitializer(CassandraTemplate template) {
38+
this.template = template;
39+
}
4040

41-
@Override
42-
public void run(String... args) {
41+
@Override
42+
public void run(String... args) {
4343

44-
template.truncate(Comment.class);
44+
template.truncate(Comment.class);
4545

46-
template.insert(new Comment("de", "comment 'one'", Vector.of(0.1001f, 0.22345f, 0.33456f, 0.44567f, 0.55678f)));
47-
template.insert(new Comment("de", "comment 'two'", Vector.of(0.2001f, 0.32345f, 0.43456f, 0.54567f, 0.65678f)));
48-
template.insert(new Comment("en", "comment 'three'", Vector.of(0.9001f, 0.82345f, 0.73456f, 0.64567f, 0.55678f)));
49-
template.insert(new Comment("de", "comment 'four'", Vector.of(0.9001f, 0.92345f, 0.93456f, 0.94567f, 0.95678f)));
50-
}
51-
}
46+
template.insert(new Comment("de", "comment 'one'", Vector.of(0.1001f, 0.22345f, 0.33456f, 0.44567f, 0.55678f)));
47+
template.insert(new Comment("de", "comment 'two'", Vector.of(0.2001f, 0.32345f, 0.43456f, 0.54567f, 0.65678f)));
48+
template.insert(new Comment("en", "comment 'three'", Vector.of(0.9001f, 0.82345f, 0.73456f, 0.64567f, 0.55678f)));
49+
template.insert(new Comment("de", "comment 'four'", Vector.of(0.9001f, 0.92345f, 0.93456f, 0.94567f, 0.95678f)));
50+
}
51+
}
5252
}

cassandra/vector-search/src/main/resources/application.properties

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ logging.level.org=WARN
22
logging.level.com.datastax=WARN
33

44
spring.cassandra.schema-action=recreate
5-
spring.cassandra.
65
spring.cassandra.keyspace-name=vector_search_keyspace

cassandra/vector-search/src/test/java/example/springdata/vector/CassandraDBConfiguration.java

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
77
*
8-
* https://www.apache.org/licenses/LICENSE-2.0
8+
* http://www.apache.org/licenses/LICENSE-2.0
99
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -17,27 +17,42 @@
1717

1818
import java.net.InetSocketAddress;
1919

20-
import com.datastax.oss.driver.api.core.CqlSession;
21-
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties;
22-
import org.springframework.boot.autoconfigure.cassandra.CqlSessionBuilderCustomizer;
20+
import org.springframework.boot.cassandra.autoconfigure.CassandraConnectionDetails;
21+
import org.springframework.boot.cassandra.autoconfigure.CassandraProperties;
22+
import org.springframework.boot.cassandra.autoconfigure.CqlSessionBuilderCustomizer;
23+
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
2324
import org.springframework.context.annotation.Bean;
2425
import org.springframework.context.annotation.Configuration;
2526

27+
import org.testcontainers.cassandra.CassandraContainer;
28+
import org.testcontainers.utility.DockerImageName;
29+
30+
import com.datastax.oss.driver.api.core.CqlSession;
31+
2632
@Configuration
2733
public class CassandraDBConfiguration {
2834

29-
@Bean
30-
CqlSessionBuilderCustomizer sessionBuilderCustomizer(CassandraProperties properties) {
31-
return sessionBuilder -> {
35+
@Bean
36+
@ServiceConnection
37+
CassandraContainer pgVectorContainer() {
38+
return new CassandraContainer(DockerImageName.parse("cassandra:5")).withReuse(true);
39+
}
40+
41+
@Bean
42+
CqlSessionBuilderCustomizer sessionBuilderCustomizer(CassandraConnectionDetails connectionDetails,
43+
CassandraProperties properties) {
44+
45+
return sessionBuilder -> {
3246

33-
InetSocketAddress contactPoint = new InetSocketAddress(properties.getContactPoints().iterator().next(), properties.getPort());
47+
CqlSession session = CqlSession.builder()
48+
.addContactPoints(connectionDetails.getContactPoints().stream()
49+
.map(it -> new InetSocketAddress(it.host(), it.port())).toList())
50+
.withLocalDatacenter(connectionDetails.getLocalDatacenter()).build();
3451

35-
CqlSession session = CqlSession.builder().addContactPoint(contactPoint)
36-
.withLocalDatacenter(properties.getLocalDatacenter()).build();
52+
session.execute("CREATE KEYSPACE IF NOT EXISTS " + properties.getKeyspaceName() + " WITH replication = \n"
53+
+ "{'class':'SimpleStrategy','replication_factor':'1'};");
54+
session.close();
55+
};
56+
}
3757

38-
session.execute("CREATE KEYSPACE IF NOT EXISTS " + properties.getKeyspaceName() + " WITH replication = \n"
39-
+ "{'class':'SimpleStrategy','replication_factor':'1'};");
40-
session.close();
41-
};
42-
}
4358
}

0 commit comments

Comments
 (0)