Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions src/main/java/org/jongo/Jongo.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,12 @@ public Command runCommand(String query) {
public Command runCommand(String query, Object... parameters) {
return new Command(database, mapper.getUnmarshaller(), mapper.getQueryFactory(), query, parameters);
}

public static JongoSync useSync(MongoDatabase database) {
return new JongoSync(database);
}

public static JongoSync useSync(MongoDatabase database, Mapper mapper) {
return new JongoSync(database, mapper);
}
}
88 changes: 88 additions & 0 deletions src/main/java/org/jongo/JongoCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
*
* 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 org.jongo;


import org.bson.*;
import org.bson.codecs.Codec;
import org.bson.codecs.CollectibleCodec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.types.ObjectId;
import org.jongo.bson.Bson;
import org.jongo.marshall.Marshaller;
import org.jongo.marshall.Unmarshaller;

public class JongoCodec<T> implements CollectibleCodec<T> {

private final Codec<RawBsonDocument> codec;
private final Class<T> clazz;
private final ObjectIdUpdater objectIdUpdater;
private final Unmarshaller unmarshaller;
private final Marshaller marshaller;

public JongoCodec(Mapper mapper, Class<T> clazz, CodecRegistry codecRegistry) {
this.codec = codecRegistry.get(RawBsonDocument.class);
this.clazz = clazz;
objectIdUpdater = mapper.getObjectIdUpdater();
unmarshaller = mapper.getUnmarshaller();
marshaller = mapper.getMarshaller();
}

public T decode(BsonReader reader, DecoderContext decoderContext) {
RawBsonDocument raw = codec.decode(reader, decoderContext);
org.jongo.bson.BsonDocument bsonDocument = Bson.createDocument(raw.getByteBuffer().array());
return unmarshaller.unmarshall(bsonDocument, clazz);
}

public void encode(BsonWriter writer, Object pojo, EncoderContext encoderContext) {
org.jongo.bson.BsonDocument document = marshaller.marshall(pojo);
codec.encode(writer, new RawBsonDocument(document.toByteArray()), encoderContext);
}

public Class<T> getEncoderClass() {
return this.clazz;
}

public T generateIdIfAbsentFromDocument(T document) {
if (objectIdUpdater.mustGenerateObjectId(document)) {
ObjectId newOid = ObjectId.get();
objectIdUpdater.setObjectId(document, newOid);
}
return document;
}

public boolean documentHasId(T document) {
return objectIdUpdater.mustGenerateObjectId(document);
}

public BsonValue getDocumentId(T document) {

Object id = objectIdUpdater.getId(document);

if (id instanceof BsonValue) {
return (BsonValue) id;
}
if (id instanceof ObjectId) {
return new BsonObjectId((ObjectId) id);
}

throw new UnsupportedOperationException("Unable to get document id");
}

}
41 changes: 41 additions & 0 deletions src/main/java/org/jongo/JongoCodecProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
*
* 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 org.jongo;


import org.bson.codecs.Codec;
import org.bson.codecs.configuration.CodecProvider;
import org.bson.codecs.configuration.CodecRegistry;

import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper;

public class JongoCodecProvider implements CodecProvider {

private final Mapper mapper;

public JongoCodecProvider() {
this(jacksonMapper().build());
}

public JongoCodecProvider(Mapper mapper) {
this.mapper = mapper;
}

public <T> Codec<T> get(final Class<T> type, final CodecRegistry registry) {
return new JongoCodec<T>(mapper, type, registry);
}
}
78 changes: 78 additions & 0 deletions src/main/java/org/jongo/JongoSync.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
*
* 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 org.jongo;


import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.BsonDocument;
import org.bson.Document;
import org.bson.codecs.BsonValueCodecProvider;
import org.bson.codecs.configuration.CodecRegistries;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.conversions.Bson;
import org.jongo.query.Query;

import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper;

public class JongoSync {

private final Mapper mapper;
private final CodecRegistry codecRegistry;
private final MongoDatabase database;

public JongoSync(MongoDatabase database) {
this(database, jacksonMapper().build());
}

public JongoSync(MongoDatabase database, Mapper mapper) {
this.mapper = mapper;
this.database = database;
this.codecRegistry = createCodecRegistry(mapper);
}

public MongoCollection<Document> getCollection(String collectionName) {
return this.database.getCollection(collectionName);
}

public <T> MongoCollection<T> getCollection(String collectionName, Class<T> documentClass) {
MongoCollection<T> collection = this.database.getCollection(collectionName, documentClass);
return wrapCollection(collection);
}

public BsonDocument query(String query, Object... parameters) {
Query q = mapper.getQueryFactory().createQuery(query, parameters);
return q.toBsonDocument();
}

public Bson id(Object id) {
return query("{_id:#}", id);
}

private <T> com.mongodb.client.MongoCollection<T> wrapCollection(com.mongodb.client.MongoCollection<T> collection) {
return collection.withCodecRegistry(codecRegistry);
}

private CodecRegistry createCodecRegistry(Mapper mapper) {

CodecRegistry defaultRegistry = MongoClient.getDefaultCodecRegistry();
CodecRegistry jongoRegistry = CodecRegistries.fromProviders(new BsonValueCodecProvider(), new JongoCodecProvider(mapper));

return CodecRegistries.fromRegistries(defaultRegistry, jongoRegistry);
}
}
14 changes: 7 additions & 7 deletions src/test/java/org/jongo/bench/BenchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ public static DBObject asDBObject(Friend friend) {
return dbo;
}

public static DBCollection getCollectionFromDriver() throws UnknownHostException {
MongoClient nativeMongo = new MongoClient();
return nativeMongo.getDB("jongo").getCollection("benchmark");
public static DBCollection getDBCollection() throws UnknownHostException {
MongoClient client = new MongoClient();
return client.getDB("jongo").getCollection("benchmark");
}

public static MongoCollection getCollectionFromJongo(Mapper mapper) throws UnknownHostException {
MongoClient mongo = new MongoClient();
DB db = mongo.getDB("jongo");
public static MongoCollection getMongoCollection(Mapper mapper) throws UnknownHostException {
MongoClient client = new MongoClient();
DB db = client.getDB("jongo");
Jongo jongo = new Jongo(db, mapper);
return jongo.getCollection("benchmark");
}

public static void injectFriendsIntoDB(int nbDocuments) throws UnknownHostException {
MongoCollection collection = getCollectionFromJongo(jacksonMapper().build());
MongoCollection collection = getMongoCollection(jacksonMapper().build());
collection.drop();
for (int i = 0; i < nbDocuments; i++) {
collection.withWriteConcern(WriteConcern.MAJORITY).save(createFriend(i));
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/org/jongo/bench/FindBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import org.jongo.model.Coordinate;
import org.jongo.model.Friend;

import static org.jongo.bench.BenchUtil.getCollectionFromDriver;
import static org.jongo.bench.BenchUtil.getCollectionFromJongo;
import static org.jongo.bench.BenchUtil.getDBCollection;
import static org.jongo.bench.BenchUtil.getMongoCollection;
import static org.jongo.marshall.jackson.JacksonMapper.Builder.jacksonMapper;

public class FindBench extends SimpleBenchmark {
Expand All @@ -39,8 +39,8 @@ public class FindBench extends SimpleBenchmark {
private DBCollection dbCollection;

protected void setUp() throws Exception {
bsonCollection = getCollectionFromJongo(jacksonMapper().build());
dbCollection = getCollectionFromDriver();
bsonCollection = getMongoCollection(jacksonMapper().build());
dbCollection = getDBCollection();

if (dbCollection.count() < NB_DOCUMENTS) {
BenchUtil.injectFriendsIntoDB(NB_DOCUMENTS);
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/org/jongo/bench/SaveBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ public class SaveBench extends SimpleBenchmark {
private MongoCollection bsonCollection;

protected void setUp() throws Exception {

bsonCollection = getCollectionFromJongo(jacksonMapper().build()).withWriteConcern(WriteConcern.MAJORITY);
dbCollection = getCollectionFromDriver();
bsonCollection = getMongoCollection(jacksonMapper().build()).withWriteConcern(WriteConcern.MAJORITY);
dbCollection = getDBCollection();

bsonCollection.drop();
}
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/org/jongo/sync/AggregateSyncTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2011 Benoit GUEROUT <bguerout at gmail dot com> and Yves AMSELLEM <amsellem dot yves at gmail dot com>
*
* 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 org.jongo.sync;

import com.mongodb.client.MongoCollection;
import org.bson.conversions.Bson;
import org.jongo.model.Article;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;

import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;

public class AggregateSyncTest extends SyncTestBase {

private MongoCollection<Article> collection;

@Before
public void setUp() throws Exception {
collection = jongo.getCollection("articles", Article.class);
collection.insertOne(new Article("Zombie Panic", "Kirsty Mckay", "horror", "virus"));
collection.insertOne(new Article("Apocalypse Zombie", "Maberry Jonathan", "horror", "dead"));
collection.insertOne(new Article("World War Z", "Max Brooks", "horror", "virus", "pandemic"));
}

@After
public void tearDown() throws Exception {
this.collection.drop();
}

@Test
public void canAggregate() throws Exception {

List<Bson> pipeline = asList(q("{$match:{tags:'virus'}}"));
Iterable<Article> articles = collection.aggregate(pipeline);

assertThat(articles.iterator().hasNext()).isTrue();
int size = 0;
for (Article article : articles) {
assertThat(article.getTags()).contains("virus");
size++;
}
assertThat(size).isEqualTo(2);
}
}
Loading