-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Implementation of Collection Sharded Mongo Client & Database (#41)
* Add Implementation of Collection Sharded Mongo Client & Database * Fix Sharding + Add Unittests
- Loading branch information
1 parent
a277817
commit d3b83e7
Showing
28 changed files
with
1,101 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
130 changes: 130 additions & 0 deletions
130
...er/src/main/java/com/alpha/mongodb/sharding/core/client/CollectionShardedMongoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...ver/src/main/java/com/alpha/mongodb/sharding/core/client/CompositeShardedMongoClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.