Skip to content

Add sample for reusable repository extensions #690

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Initial outline for reusable repository extensions.
  • Loading branch information
christophstrobl committed Nov 22, 2024
commit 92a94c30f52c426cd366fcd52ba06c815c80abf7
Empty file added mongodb/fragment-spi/README.md
Empty file.
14 changes: 14 additions & 0 deletions mongodb/fragment-spi/atlas-api/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>

<artifactId>spring-data-mongodb-fragment-spi-atlas</artifactId>
<name>Spring Data MongoDB - Fragment SPI</name>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.example.spi.mongodb.atlas;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.data.domain.Limit;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.stereotype.Component;

/**
* @author Christoph Strobl
*/
public interface AtlasRepository<T> {

List<T> vectorSearch(String index, String path, List<Double> vector, Limit limit);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright 2024 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 com.example.spi.mongodb.atlas;

import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.context.annotation.Bean;

/**
* @author Christoph Strobl
*/
@AutoConfiguration
public class AtlasRepositoryAutoConfiguration {

@Bean
static BeanPostProcessor atlasRepositoryPostProcessor() {
System.out.println("XXXX");
return new AtlasRepositoryPostProcessor();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* Copyright 2024 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 com.example.spi.mongodb.atlas;

import java.util.List;
import java.util.Objects;

import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Limit;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.repository.core.support.RepositoryMethodContext;
import org.springframework.lang.Nullable;

class AtlasRepositoryFragment<T> implements AtlasRepository<T> {

private MongoOperations mongoOperations;

public AtlasRepositoryFragment(@Autowired MongoOperations mongoOperations) {
this.mongoOperations = mongoOperations;
}

@Override
@SuppressWarnings("unchecked")
public List<T> vectorSearch(String index, String path, List<Double> vector, Limit limit) {

RepositoryMethodContext metadata = RepositoryMethodContext.currentMethod();
Objects.requireNonNull(metadata);

Class<?> domainType = metadata.getRepository().getDomainType();
System.out.println("domainType: " + domainType);

Document $vectorSearch = createDocument(index, path, vector, limit, null, null, null);
Aggregation aggregation = Aggregation.newAggregation(ctx -> $vectorSearch);

return (List<T>) mongoOperations.aggregate(aggregation, mongoOperations.getCollectionName(domainType), domainType);
}

private static Document createDocument(String indexName, String path, List<Double> vector, Limit limit, @Nullable Boolean exact, @Nullable CriteriaDefinition filter, @Nullable Integer numCandidates) {

Document $vectorSearch = new Document();

$vectorSearch.append("index", indexName);
$vectorSearch.append("path", path);
$vectorSearch.append("queryVector", vector);
$vectorSearch.append("limit", limit.max());

if (exact != null) {
$vectorSearch.append("exact", exact);
}

if (filter != null) {
$vectorSearch.append("filter", filter.getCriteriaObject());
}

if (numCandidates != null) {
$vectorSearch.append("numCandidates", numCandidates);
}

return new Document("$vectorSearch", $vectorSearch);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2024 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 com.example.spi.mongodb.atlas;

import java.lang.reflect.Field;
import java.util.Arrays;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

/**
* @author Christoph Strobl
*/
class AtlasRepositoryPostProcessor implements BeanPostProcessor {

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {

if (bean instanceof RepositoryFactoryBeanSupport rfbs) {

Field field = ReflectionUtils.findField(RepositoryFactoryBeanSupport.class, "repositoryInterface");
ReflectionUtils.makeAccessible(field);
if (Arrays.stream(((Class<?>) ReflectionUtils.getField(field, rfbs)).getInterfaces())
.anyMatch(iface -> {
return iface.equals(AtlasRepository.class);
})) {
rfbs.setExposeMetadata(true);
}
}
return bean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.spi.mongodb.atlas.AtlasRepositoryAutoConfiguration
com.example.spi.mongodb.atlas.AtlasRepository=com.example.spi.mongodb.atlas.AtlasRepositoryFragment

18 changes: 18 additions & 0 deletions mongodb/fragment-spi/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>

<artifactId>spring-data-mongodb-fragment-spi</artifactId>
<packaging>pom</packaging>

<modules>
<module>atlas-api</module>
<module>sample</module>
</modules>
</project>
26 changes: 26 additions & 0 deletions mongodb/fragment-spi/sample/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-examples</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</parent>

<artifactId>spring-data-mongodb-fragment-spi-usage</artifactId>
<name>Spring Data MongoDB - Fragment Usage</name>

<dependencies>
<dependency>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-fragment-spi-atlas</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data.examples</groupId>
<artifactId>spring-data-mongodb-example-utils</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2024 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 com.example.data.mongodb;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author Christoph Strobl
*/
@SpringBootApplication
public class ApplicationConfiguration {

// @Bean
// BeanFactoryPostProcessor postProcessor() {
// return new BeanFactoryPostProcessor() {
// @Override
// public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//
// BeanDefinition movieRepository = beanFactory.getBeanDefinition("movieRepository");
// movieRepository.getPropertyValues().add("exposeMetadata", true);
// }
// };
// }

// @Bean
// BeanPostProcessor postProcessor() {
// return new AtlasRepositoryPostProcessor();
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2024 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 com.example.data.mongodb;

import org.springframework.data.mongodb.core.mapping.Document;

/**
* @author Christoph Strobl
* @since 2024/08
*/
@Document
public class Movie {

String id;
String title;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.example.data.mongodb;

import com.example.spi.mongodb.atlas.AtlasRepository;
import org.springframework.data.repository.CrudRepository;

/**
* @author Christoph Strobl
*/
public interface MovieRepository extends CrudRepository<Movie, String>, AtlasRepository<Movie> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 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 com.example.data.mongodb;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Limit;

/**
* @author Christoph Strobl
*/
@SpringBootTest
public class MovieRepositoryTests {

@Autowired MovieRepository repository;

@Test
void xxx() {
repository.vectorSearch("idx", "embeddings", List.of(100D), Limit.of(10));
}
}
1 change: 1 addition & 0 deletions mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<module>querydsl</module>
<module>linking</module>
<module>util</module>
<module>fragment-spi</module>
</modules>

<dependencyManagement>
Expand Down