Skip to content

Commit

Permalink
Genericized Serializer interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jyemin committed Dec 12, 2012
1 parent 9e61b5b commit fb16fe7
Show file tree
Hide file tree
Showing 45 changed files with 540 additions and 324 deletions.
63 changes: 63 additions & 0 deletions src/main/com/mongodb/BasicDBObject.java
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.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 com.mongodb;

import java.util.LinkedHashMap;
import java.util.Map;

public class BasicDBObject extends LinkedHashMap<String, Object> implements DBObject {
@Override
public void putAll(final Map m) {
throw new UnsupportedOperationException();
}

@Override
public Object get(final String key) {
throw new UnsupportedOperationException();
}

@Override
public Map toMap() {
throw new UnsupportedOperationException();
}

@Override
public Object removeField(final String key) {
throw new UnsupportedOperationException();
}

@Override
public boolean containsKey(final String s) {
throw new UnsupportedOperationException();
}

@Override
public boolean containsField(final String s) {
throw new UnsupportedOperationException();
}

@Override
public void markAsPartialObject() {
throw new UnsupportedOperationException();
}

@Override
public boolean isPartialObject() {
throw new UnsupportedOperationException();
}
}
4 changes: 3 additions & 1 deletion src/main/com/mongodb/DB.java
Expand Up @@ -18,6 +18,7 @@
package com.mongodb;

import org.mongodb.MongoDatabase;
import org.mongodb.serialization.Serializers;

import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -70,7 +71,8 @@ public DBCollection getCollection(String name) {
if (collection != null)
return collection;

collection = new DBCollection(database.getTypedCollection(name, DBObject.class), this);
Serializers serializers = Serializers.createDefaultSerializers();
collection = new DBCollection(database.getTypedCollection(name, serializers, new DBObjectSerializer(serializers)), this);
DBCollection old = collectionCache.putIfAbsent(name, collection);
return old != null ? old : collection;
}
Expand Down
78 changes: 77 additions & 1 deletion src/main/com/mongodb/DBObject.java
Expand Up @@ -17,5 +17,81 @@

package com.mongodb;

public class DBObject {
import java.util.Map;
import java.util.Set;

public interface DBObject {
/**
* Sets a name/value pair in this object.
* @param key Name to set
* @param v Corresponding value
* @return <tt>v</tt>
*/
public Object put( String key , Object v );

/**
* Sets all key/value pairs from an object into this object
* @param o the object
*/
// public void putAll( BSONObject o );

/**
* Sets all key/value pairs from a map into this object
* @param m the map
*/
public void putAll( Map m );

/**
* Gets a field from this object by a given name.
* @param key The name of the field fetch
* @return The field, if found
*/
public Object get( String key );

/**
* Returns a map representing this BSONObject.
* @return the map
*/
public Map toMap();

/**
* Removes a field with a given name from this object.
* @param key The name of the field to remove
* @return The value removed from this object
*/
public Object removeField( String key );

/**
* Deprecated
* @param s
* @return True if the key is present
* @deprecated
*/
@Deprecated
public boolean containsKey( String s );

/**
* Checks if this object contains a field with the given name.
* @param s Field name for which to check
* @return True if the field is present
*/
public boolean containsField(String s);

/**
* Returns this object's fields' names
* @return The names of the fields in this object
*/
public Set<String> keySet();

/**
* if this object was retrieved with only some fields (using a field filter)
* this method will be called to mark it as such.
*/
public void markAsPartialObject();

/**
* whether markAsPartialObject was ever called
* only matters if you are going to upsert and do not want to risk losing fields
*/
public boolean isPartialObject();
}
71 changes: 71 additions & 0 deletions src/main/com/mongodb/DBObjectSerializer.java
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.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 com.mongodb;

import org.bson.BSONReader;
import org.bson.BSONWriter;
import org.bson.BsonType;
import org.mongodb.serialization.BsonSerializationOptions;
import org.mongodb.serialization.Serializer;
import org.mongodb.serialization.Serializers;

public class DBObjectSerializer implements Serializer<DBObject> {
private final Serializers serializers;

public DBObjectSerializer(final Serializers serializers) {
this.serializers = serializers;
}

// TODO: deal with options. C# driver sends different options. For one, to write _id field first
@Override
public void serialize(final BSONWriter bsonWriter, final DBObject document, final BsonSerializationOptions options) {
bsonWriter.writeStartDocument();
for (String field : document.keySet()) {
bsonWriter.writeName(field);
Object value = document.get(field);
if (value instanceof DBObject) {
serialize(bsonWriter, (DBObject) value, options);
} else {
serializers.serialize(bsonWriter, value, options);
}
}
bsonWriter.writeEndDocument();
}

@Override
public DBObject deserialize(final BSONReader reader, final BsonSerializationOptions options) {
DBObject document = new BasicDBObject();

reader.readStartDocument();
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
String fieldName = reader.readName();
BsonType bsonType = reader.getNextBsonType();
if (bsonType.equals(BsonType.DOCUMENT)) {
deserialize(reader, options);
} else {
Object value = serializers.deserialize(reader, options);
document.put(fieldName, value);
}
}

reader.readEndDocument();

return document;
}

}
2 changes: 1 addition & 1 deletion src/main/com/mongodb/DBObjects.java
Expand Up @@ -36,7 +36,7 @@ public static MongoQueryFilterDocument toQueryFilterDocument(DBObject obj) {
}

public static DBObject toDBObject(MongoDocument document) {
return new DBObject();
return new BasicDBObject();
}

public static MongoFieldSelector toFieldSelectorDocument(final DBObject fields) {
Expand Down
9 changes: 3 additions & 6 deletions src/main/org/mongodb/MongoCollection.java
Expand Up @@ -24,6 +24,7 @@
import org.mongodb.operation.MongoRemove;
import org.mongodb.result.InsertResult;
import org.mongodb.result.RemoveResult;
import org.mongodb.serialization.Serializer;
import org.mongodb.serialization.Serializers;

// TODO: add these
Expand Down Expand Up @@ -81,13 +82,9 @@ public interface MongoCollection<T> {

RemoveResult remove(MongoRemove remove);

/**
* The same collection but with a different default write concern.
* TODO: not sure this is such a good idea
*/
MongoCollection<T> withWriteConcern(WriteConcern writeConcern);
Serializers getBaseSerializers();

Serializers getSerializers();
Serializer<T> getSerializer();
}


Expand Down
10 changes: 5 additions & 5 deletions src/main/org/mongodb/MongoCursor.java
Expand Up @@ -20,6 +20,7 @@
import org.mongodb.operation.MongoFind;
import org.mongodb.operation.MongoKillCursor;
import org.mongodb.result.QueryResult;
import org.mongodb.serialization.serializers.MongoDocumentSerializer;

import java.io.Closeable;
import java.util.Iterator;
Expand All @@ -28,16 +29,15 @@
public class MongoCursor<T> implements Iterator<T>, Closeable {
private final MongoCollection<T> collection;
private final MongoFind find;
private final Class<T> clazz;
private QueryResult<T> currentResult;
private Iterator<T> currentIterator;

public MongoCursor(final MongoCollection<T> collection, final MongoFind find, Class<T> clazz) {
public MongoCursor(final MongoCollection<T> collection, final MongoFind find) {
this.collection = collection;
this.find = find;
this.clazz = clazz;
currentResult = collection.getClient().getOperations().query(collection.getNamespace(), find, clazz,
collection.getSerializers());
currentResult = collection.getClient().getOperations().query(collection.getNamespace(), find,
new MongoDocumentSerializer(collection.getBaseSerializers()),
collection.getSerializer());
currentIterator = currentResult.getResults().iterator();
}

Expand Down
12 changes: 6 additions & 6 deletions src/main/org/mongodb/MongoDatabase.java
Expand Up @@ -19,6 +19,7 @@

import org.mongodb.operation.MongoCommandOperation;
import org.mongodb.result.CommandResult;
import org.mongodb.serialization.Serializer;
import org.mongodb.serialization.Serializers;

/**
Expand All @@ -33,11 +34,10 @@ public interface MongoDatabase {

MongoCollection<MongoDocument> getCollection(String name);

<T> MongoCollection<T> getTypedCollection(String name, Class<T> clazz);
<T> MongoCollection<T> getTypedCollection(String name, final Serializers baseSerializers,
final Serializer<T> serializer);

<T> MongoCollection<T> getTypedCollection(String findAndUpdateWithGenerics, Class<T> clazz, Serializers serializers);

MongoDatabase withClient(MongoClient client);

MongoDatabase withWriteConcern(WriteConcern writeConcern);
// MongoDatabase withClient(MongoClient client);
//
// MongoDatabase withWriteConcern(WriteConcern writeConcern);
}
13 changes: 7 additions & 6 deletions src/main/org/mongodb/MongoOperations.java
Expand Up @@ -33,20 +33,21 @@
public interface MongoOperations {

// TODO: should this really be a separate call from query?
MongoDocument executeCommand(String database, MongoCommandOperation commandOperation, Serializer serializer);
MongoDocument executeCommand(String database, MongoCommandOperation commandOperation, Serializer<MongoDocument> serializer);

<T> QueryResult<T> query(final MongoNamespace namespace, MongoFind find, Class<T> clazz, Serializer serializer);
<T> QueryResult<T> query(final MongoNamespace namespace, MongoFind find, Serializer<MongoDocument> baseSerializer,
Serializer<T> serializer);

// TODO: needs a ServerAddress or doesn't make sense for some MongoClient implementations
<T> GetMoreResult<T> getMore(final MongoNamespace namespace, GetMore getMore, Class<T> clazz, Serializer serializer);
<T> GetMoreResult<T> getMore(final MongoNamespace namespace, GetMore getMore, Serializer<T> serializer);

// TODO: needs a ServerAddress or doesn't make sense for some MongoClient implementations
void killCursors(MongoKillCursor killCursor);

<T> InsertResult insert(MongoNamespace namespace, MongoInsert<T> insert, Class<T> clazz, Serializer serializer);
<T> InsertResult insert(MongoNamespace namespace, MongoInsert<T> insert, Serializer<T> serializer);

// TODO: Need to handle update where you have to custom serialize the update document
UpdateResult update(final MongoNamespace namespace, MongoUpdate update, Serializer serializer);
UpdateResult update(final MongoNamespace namespace, MongoUpdate update, Serializer<MongoDocument> serializer);

RemoveResult delete(final MongoNamespace namespace, MongoRemove remove, Serializer serializer);
RemoveResult remove(final MongoNamespace namespace, MongoRemove remove, Serializer<MongoDocument> serializer);
}
12 changes: 11 additions & 1 deletion src/main/org/mongodb/command/AbstractCommand.java
Expand Up @@ -18,9 +18,12 @@
package org.mongodb.command;

import org.mongodb.MongoClient;
import org.mongodb.MongoDocument;
import org.mongodb.operation.MongoCommand;
import org.mongodb.operation.MongoCommandOperation;
import org.mongodb.result.CommandResult;
import org.mongodb.serialization.Serializer;
import org.mongodb.serialization.serializers.MongoDocumentSerializer;

public abstract class AbstractCommand implements Command {
private final MongoClient mongoClient;
Expand All @@ -40,9 +43,16 @@ public String getDatabase() {
}

public CommandResult execute() {
return new CommandResult(mongoClient.getOperations().executeCommand(database, new MongoCommandOperation(asMongoCommand()), null));
return new CommandResult(mongoClient.getOperations().executeCommand(database,
new MongoCommandOperation(asMongoCommand()), createResultSerializer()));
}

// TODO: the class of the return type is weird for a command
public abstract MongoCommand asMongoCommand();

protected Serializer<MongoDocument> createResultSerializer() {
return new MongoDocumentSerializer(mongoClient.getSerializers());
}


}

0 comments on commit fb16fe7

Please sign in to comment.