Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mongodb/mongo-java-driver
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: rhenus-fl/mongo-java-driver
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: release-2.8.x
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 9 commits
  • 10 files changed
  • 1 contributor

Commits on Aug 28, 2012

  1. Copy the full SHA
    d35b065 View commit details
  2. Copy the full SHA
    505fee6 View commit details
  3. Copy the full SHA
    9352ece View commit details
  4. Copy the full SHA
    3eeed9b View commit details
  5. Copy the full SHA
    d6806c1 View commit details

Commits on Aug 29, 2012

  1. Copy the full SHA
    c025cbb View commit details
  2. removed sysos...

    jensbehrens committed Aug 29, 2012
    Copy the full SHA
    fb9eb7b View commit details
  3. created overriden methods findandmodify and count to tokenize

    incoming field names...
    jensbehrens committed Aug 29, 2012
    Copy the full SHA
    89aa351 View commit details

Commits on Aug 30, 2012

  1. Copy the full SHA
    8138c9d View commit details
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ driver developers who would rather use Maven than Ant as their build tool.
<artifactId>mongo-java-driver</artifactId>
<packaging>bundle</packaging>
<name>MongoDB Java Driver</name>
<version>2.8.0</version>
<version>2.8.1-SNAPSHOT</version>
<description>The MongoDB Java driver</description>
<url>http://www.mongodb.org</url>

69 changes: 63 additions & 6 deletions src/main/com/mongodb/DBApiLayer.java
Original file line number Diff line number Diff line change
@@ -36,13 +36,15 @@

import com.mongodb.util.JSON;


/** Database API
* This cannot be directly instantiated, but the functions are available
* through instances of Mongo.
*/
public class DBApiLayer extends DB {

static final boolean D = Boolean.getBoolean( "DEBUG.DB" );

TokenConverter converter;

/** The maximum number of cursors allowed */
static final int NUM_CURSORS_BEFORE_KILL = 100;
static final int NUM_CURSORS_PER_BATCH = 20000;
@@ -98,6 +100,8 @@ protected DBApiLayer( Mongo mongo, String name , DBConnector connector ){
_rootPlusDot = _root + ".";

_connector = connector;

converter = new TokenConverter(_mongo);
}

public void requestStart(){
@@ -249,6 +253,9 @@ protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.W

for ( ; cur<arr.length; cur++ ){
DBObject o = arr[cur];

converter.transformAttrs(o, true, true, false);

om.putObject( o );

// limit for batch insert is 4 x maxbson on server, use 2 x to be safe
@@ -285,7 +292,9 @@ public WriteResult remove( DBObject o , com.mongodb.WriteConcern concern, DBEnco
om.writeInt( 1 );
else
om.writeInt( 0 );


converter.transformAttrs(o, true, false, false);

om.putObject( o );

return _connector.say( _db , om , concern );
@@ -305,6 +314,9 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
if ( ref == null )
ref = new BasicDBObject();

converter.transformAttrs(ref, true, false, false);
converter.transformAttrs(fields, true, false, true);

if ( willTrace() ) trace( "find: " + _fullNameSpace + " " + JSON.serialize( ref ) );

OutMessage query = OutMessage.query( _mongo , options , _fullNameSpace , numToSkip , chooseBatchSize(batchSize, limit, 0) , ref , fields, readPref,
@@ -319,7 +331,7 @@ Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int
throw e;
}

return new Result( this , res , batchSize, limit , options, decoder );
return new Result( this , res , batchSize, limit , options, decoder);
}

@Override
@@ -329,7 +341,9 @@ public WriteResult update( DBObject query , DBObject o , boolean upsert , boolea
if (encoder == null)
encoder = DefaultDBEncoder.FACTORY.create();

if (o != null && !o.keySet().isEmpty()) {
if (!o.keySet().isEmpty()) {
converter.transformAttrs(o, true, true, false);

// if 1st key doesn't start with $, then object will be inserted as is, need to check it
String key = o.keySet().iterator().next();
if (!key.startsWith("$"))
@@ -338,6 +352,8 @@ public WriteResult update( DBObject query , DBObject o , boolean upsert , boolea

if ( willTrace() ) trace( "update: " + _fullNameSpace + " " + JSON.serialize( query ) + " " + JSON.serialize( o ) );

converter.transformAttrs(query, true, false, false);

OutMessage om = new OutMessage( _mongo , 2001, encoder );
om.writeInt( 0 ); // reserved
om.writeCString( _fullNameSpace );
@@ -359,6 +375,8 @@ public void createIndex( final DBObject keys, final DBObject options, DBEncoder
if (encoder == null)
encoder = DefaultDBEncoder.FACTORY.create();

converter.transformAttrs(keys, true, false, false);

DBObject full = new BasicDBObject();
for ( String k : options.keySet() )
full.put( k , options.get( k ) );
@@ -371,6 +389,41 @@ public void createIndex( final DBObject keys, final DBObject options, DBEncoder
}

final String _fullNameSpace;

@Override
public DBCursor find(DBObject ref) {
return new TokenizedDBCursor( this, ref, null, getReadPreference(), converter);
}

@Override
public DBCursor find(DBObject ref, DBObject keys) {
return new TokenizedDBCursor( this, ref, keys, getReadPreference(), converter);
}

@Override
public DBCursor find() {
return new TokenizedDBCursor( this, null, null, getReadPreference(), converter);
}

@Override
public long getCount(DBObject query, DBObject fields, long limit, long skip) throws MongoException {
converter.transformAttrs(query, true, false, false);
converter.transformAttrs(fields, true, false, true);
return super.getCount(query, fields, limit, skip);
}

@Override
public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort, boolean remove, DBObject update,
boolean returnNew, boolean upsert) {
converter.transformAttrs(query, true, false, false);
converter.transformAttrs(fields, true, false, true);
converter.transformAttrs(sort, true, false, false);
converter.transformAttrs(update, true, true, false);
return super.findAndModify(query, fields, sort, remove, update, returnNew, upsert);
}



}

class Result implements Iterator<DBObject> {
@@ -404,7 +457,11 @@ private void init( Response res ){

public DBObject next(){
if ( _cur.hasNext() ) {
return _cur.next();
DBObject ret = _cur.next();

converter.transformAttrs(ret, false, false, false);

return ret;
}

if ( ! _curResult.hasGetMore( _options ) )
2 changes: 1 addition & 1 deletion src/main/com/mongodb/DBCollection.java
Original file line number Diff line number Diff line change
@@ -639,7 +639,7 @@ public DBCursor find( DBObject ref , DBObject keys ){
public DBCursor find(){
return new DBCursor( this, null, null, getReadPreference());
}

/**
* Returns a single object from this collection.
* @return the object found, or <code>null</code> if the collection is empty
155 changes: 155 additions & 0 deletions src/main/com/mongodb/TokenConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package com.mongodb;

import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.bson.types.BasicBSONList;

public class TokenConverter {

public static final String TOKENIZE = "TOKENIZE";

private Map<String, String> mappedFieldsToTokens;
private Map<String, String> mappedTokenToFields;
private Mongo _mongo;


public TokenConverter(Mongo mongo) {
this._mongo = mongo;
}

@SuppressWarnings("unchecked")
private void initMapping() {
synchronized (this._mongo) {
DBCollection mapping = this._mongo.getDB("test").getCollection("keyMapping");
DBCursor result = mapping.find();
for (DBObject o : result) {
if (o.get("_id").equals("toToken"))
this.mappedFieldsToTokens = o.toMap();
if (o.get("_id").equals("toField"))
this.mappedTokenToFields = o.toMap();
}
if (this.mappedFieldsToTokens == null)
this.mappedFieldsToTokens = Collections.synchronizedMap(new HashMap<String, String>());
if (this.mappedTokenToFields == null)
this.mappedTokenToFields = Collections.synchronizedMap(new HashMap<String, String>());
}
}


@SuppressWarnings("unchecked")
public void transformAttrs(DBObject o, boolean toToken, boolean createMapping, boolean preserveTokenizeAttr) {

if (o == null)
return;

if (!(o instanceof BasicBSONList) && o.get(TOKENIZE) == null)
return;

if (this.mappedFieldsToTokens == null || this.mappedTokenToFields == null)
initMapping();

Map<String, Object> m = o.toMap();
Iterator<Map.Entry<String, Object>> it = m.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();

if (entry.getKey().equals("_id"))
continue;
if (!preserveTokenizeAttr && toToken && !createMapping && entry.getKey().equals(TOKENIZE)) {
synchronized (this._mongo) {
o.removeField(TOKENIZE);
}
continue;
} else if (entry.getKey().equals(TOKENIZE)) {
continue;
}

String field = null;
Object value = null;

synchronized (this._mongo) {
value = o.removeField(entry.getKey());

if (toToken)
field = mapFieldToToken(entry.getKey(), createMapping);
else if (!(o instanceof BasicBSONList))
field = mapTokenToField(entry.getKey());

if (field == null)
field = entry.getKey();

o.put(field, value);
}

if (value == null)
continue;

if (value instanceof List || o instanceof BasicBSONList) {
for (Object dbo : (List<?>) value)
if (dbo instanceof DBObject)
transformAttrs((DBObject) dbo, toToken, createMapping, preserveTokenizeAttr);
} else if (value.getClass().isArray()) {
for (Object dbo : (Object[]) value)
if (dbo instanceof DBObject)
transformAttrs((DBObject) dbo, toToken, createMapping, preserveTokenizeAttr);
} else if (value instanceof DBObject)
transformAttrs((DBObject) value, toToken, createMapping, preserveTokenizeAttr);
}
}


private String mapFieldToToken(String field, boolean createMapping) {

String token = this.mappedFieldsToTokens.get(field);
if (token == null && !createMapping)
return field;
else if (token != null)
return token;
else {
token = toString(this.mappedFieldsToTokens.size(), 16);

this.mappedFieldsToTokens.put(field, token);
this.mappedTokenToFields.put(token, field);

DBCollection mapping = this._mongo.getDB("test").getCollection("keyMapping");

DBObject toToken = new BasicDBObject("_id", "toToken");
toToken.putAll(this.mappedFieldsToTokens);

DBObject toFields = new BasicDBObject("_id", "toField");
toFields.putAll(this.mappedTokenToFields);

mapping.save(toToken);
mapping.save(toFields);

return token;

}

}

private String mapTokenToField(String token) {
return this.mappedTokenToFields.get(token);
}

// converts integer n into a base b string
private String toString(int n, int base) {
// special case
if (n == 0)
return "0";

String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String s = "";
while (n > 0) {
int d = n % base;
s = digits.charAt(d) + s;
n = n / base;
}
return s;
}

}
19 changes: 19 additions & 0 deletions src/main/com/mongodb/TokenizedDBCursor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mongodb;



public class TokenizedDBCursor extends DBCursor {
TokenConverter converter;
public TokenizedDBCursor(DBCollection collection, DBObject q, DBObject k, ReadPreference preference, TokenConverter converter) {
super(collection, q, k, preference);
this.converter = converter;
}

@Override
public DBObject next() {
DBObject next = super.next();
converter.transformAttrs(next, false, false, false);
return next;
}

}
Loading