Skip to content

Commit

Permalink
Add Implementation of Collection Sharded Mongo Client & Database (#41)
Browse files Browse the repository at this point in the history
* Add Implementation of Collection Sharded Mongo Client & Database

* Fix Sharding + Add Unittests
  • Loading branch information
shashankrnr32 authored Apr 12, 2023
1 parent a277817 commit d3b83e7
Show file tree
Hide file tree
Showing 28 changed files with 1,101 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/javadoc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ jobs:
java-version: 11
project: maven
custom-command: >
echo "sharding-core sharding-spring" | xargs -d " " -I % mvn -pl % -am javadoc:aggregate
echo "sharding-core sharding-spring sharding-driver" | xargs -d " " -I % mvn -pl % -am javadoc:aggregate
5 changes: 4 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@
<apache.commons.lang3.version>3.12.0</apache.commons.lang3.version>
<apache.commons.collection.version>3.2.2</apache.commons.collection.version>
<spring.data.mongodb.version>3.3.10</spring.data.mongodb.version>
<mongodb.driver.version>4.4.2</mongodb.driver.version>
<mongodb.driver.executable.version>4.4.2</mongodb.driver.executable.version>
<reactor.core.version>3.5.4</reactor.core.version>
<mongodb.driver.reactive.version>4.9.0</mongodb.driver.reactive.version>
<mockito.inline.version>3.12.4</mockito.inline.version>

</properties>
Expand All @@ -49,6 +51,7 @@
<module>sharding-core</module>
<module>sharding-spring</module>
<module>sharding-example</module>
<module>sharding-driver</module>
</modules>
</profile>
</profiles>
Expand Down
112 changes: 112 additions & 0 deletions sharding-driver/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mongodb-application-sharding</artifactId>
<groupId>com.alpha.mongodb</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>sharding-driver</artifactId>

<properties>
<maven.compiler.source>${project.java.version}</maven.compiler.source>
<maven.compiler.target>${project.java.version}</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>com.alpha.mongodb</groupId>
<artifactId>sharding-core</artifactId>
<version>${revision}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb.driver.executable.version}</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-reactivestreams</artifactId>
<version>${mongodb.driver.reactive.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>${reactor.core.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.0.0</version>
<configuration>
<outputDirectory>target/docs</outputDirectory>
<addOutputDirectory>false</addOutputDirectory>
<sourceDirectory>${project.basedir}/src/main/java/</sourceDirectory>
<skip>${maven.plugin.delombok.skip}</skip>
</configuration>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>delombok</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.1</version>
<configuration>
<noqualifier>all</noqualifier>
<sourcepath>${delombok.output}</sourcepath>
<stylesheet>java</stylesheet>
</configuration>
<executions>
<execution>
<id>aggregate</id>
<goals>
<goal>aggregate</goal>
</goals>
<configuration>
<!-- Specific configuration for the aggregate report -->
<reportOutputDirectory>${project.build.directory}/docs</reportOutputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.6</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.alpha.mongodb.sharding.core.client;

import com.alpha.mongodb.sharding.core.configuration.CollectionShardingOptions;
import com.alpha.mongodb.sharding.core.database.CollectionShardedMongoDatabase;
import com.mongodb.ClientSessionOptions;
import com.mongodb.client.ChangeStreamIterable;
import com.mongodb.client.ClientSession;
import com.mongodb.client.ListDatabasesIterable;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;
import com.mongodb.connection.ClusterDescription;
import org.bson.Document;
import org.bson.conversions.Bson;

import java.util.List;

/**
* Collection Sharded Mongo Client that is used to access databases that
* are sharded by the collection.
*
* @author Shashank Sharma
*/
public class CollectionShardedMongoClient implements ShardedMongoClient {
private final CollectionShardingOptions shardingOptions;

private final MongoClient delegatedMongoClient;

public CollectionShardedMongoClient(MongoClient mongoClient,
CollectionShardingOptions shardingOptions) {
super();
this.shardingOptions = shardingOptions;
this.delegatedMongoClient = mongoClient;
}

@Override
public MongoDatabase getDatabase(String s) {
return new CollectionShardedMongoDatabase(delegatedMongoClient.getDatabase(s), shardingOptions);
}

@Override
public ClientSession startSession() {
return delegatedMongoClient.startSession();
}

@Override
public ClientSession startSession(ClientSessionOptions clientSessionOptions) {
return delegatedMongoClient.startSession(clientSessionOptions);
}

@Override
public void close() {
delegatedMongoClient.close();
}

@Override
public MongoIterable<String> listDatabaseNames() {
return delegatedMongoClient.listDatabaseNames();
}

@Override
public MongoIterable<String> listDatabaseNames(ClientSession clientSession) {
return delegatedMongoClient.listDatabaseNames(clientSession);
}

@Override
public ListDatabasesIterable<Document> listDatabases() {
return delegatedMongoClient.listDatabases();
}

@Override
public ListDatabasesIterable<Document> listDatabases(ClientSession clientSession) {
return delegatedMongoClient.listDatabases(clientSession);
}

@Override
public <T> ListDatabasesIterable<T> listDatabases(Class<T> aClass) {
return delegatedMongoClient.listDatabases(aClass);
}

@Override
public <T> ListDatabasesIterable<T> listDatabases(ClientSession clientSession, Class<T> aClass) {
return delegatedMongoClient.listDatabases(clientSession, aClass);
}

@Override
public ChangeStreamIterable<Document> watch() {
return delegatedMongoClient.watch();
}

@Override
public <T> ChangeStreamIterable<T> watch(Class<T> aClass) {
return delegatedMongoClient.watch(aClass);
}

@Override
public ChangeStreamIterable<Document> watch(List<? extends Bson> list) {
return delegatedMongoClient.watch(list);
}

@Override
public <T> ChangeStreamIterable<T> watch(List<? extends Bson> list, Class<T> aClass) {
return delegatedMongoClient.watch(list, aClass);
}

@Override
public ChangeStreamIterable<Document> watch(ClientSession clientSession) {
return delegatedMongoClient.watch(clientSession);
}

@Override
public <T> ChangeStreamIterable<T> watch(ClientSession clientSession, Class<T> aClass) {
return delegatedMongoClient.watch(clientSession, aClass);
}

@Override
public ChangeStreamIterable<Document> watch(ClientSession clientSession, List<? extends Bson> list) {
return delegatedMongoClient.watch(clientSession, list);
}

@Override
public <T> ChangeStreamIterable<T> watch(ClientSession clientSession, List<? extends Bson> list, Class<T> aClass) {
return delegatedMongoClient.watch(clientSession, list, aClass);
}

@Override
public ClusterDescription getClusterDescription() {
return delegatedMongoClient.getClusterDescription();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.alpha.mongodb.sharding.core.client;

import com.alpha.mongodb.sharding.core.configuration.CompositeShardingOptions;
import com.mongodb.client.MongoClient;

import java.util.Map;
import java.util.stream.Collectors;

/**
* Composite Sharded Mongo Client
*
* @author Shashank Sharma
*/
public class CompositeShardedMongoClient extends DatabaseShardedMongoClient {

public CompositeShardedMongoClient(Map<String, MongoClient> delegatedMongoClientMap, CompositeShardingOptions shardingOptions) {
super(delegatedMongoClientMap.entrySet().stream().collect(
Collectors.toMap(Map.Entry::getKey, v -> new CollectionShardedMongoClient(
v.getValue(), shardingOptions.getDelegatedCollectionShardingOptions()))), shardingOptions);
}

public CompositeShardedMongoClient(MongoClient delegatedMongoClient, CompositeShardingOptions shardingOptions) {
this(shardingOptions.getDefaultDatabaseHintsSet().stream().collect(Collectors.toMap(h -> h, h -> delegatedMongoClient)), shardingOptions);
}
}
Loading

0 comments on commit d3b83e7

Please sign in to comment.