http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpInsert.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpInsert.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpInsert.java deleted file mode 100644 index fdd72ab..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpInsert.java +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.protocol; - - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.bson.BSONObject; -import org.bson.BasicBSONObject; -import org.bson.types.ObjectId; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferInputStream; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.MessageEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.management.ApplicationInfo; -import org.apache.usergrid.mongo.MongoChannelHandler; -import org.apache.usergrid.mongo.utils.BSONUtils; -import org.apache.usergrid.persistence.EntityManager; -import org.apache.usergrid.persistence.index.query.Identifier; -import org.apache.usergrid.security.shiro.utils.SubjectUtils; - - -public class OpInsert extends OpCrud { - - private static final Logger logger = LoggerFactory.getLogger( OpInsert.class ); - - protected int flags; - protected List<BSONObject> documents = new ArrayList<BSONObject>(); - - - public OpInsert() { - opCode = OP_INSERT; - } - - - public int getFlags() { - return flags; - } - - - public void setFlags( int flags ) { - this.flags = flags; - } - - - public List<BSONObject> getDocuments() { - return documents; - } - - - public void setDocuments( List<BSONObject> documents ) { - if ( documents == null ) { - documents = new ArrayList<BSONObject>(); - } - this.documents = documents; - } - - - public void addDocument( BSONObject document ) { - documents.add( document ); - } - - - public void addDocument( Map<?, ?> map ) { - BSONObject b = new BasicBSONObject(); - b.putAll( map ); - documents.add( b ); - } - - - @Override - public void decode( ChannelBuffer buffer ) throws IOException { - super.decode( buffer ); - - flags = buffer.readInt(); - fullCollectionName = readCString( buffer ); - - while ( buffer.readable() ) { - documents.add( BSONUtils.decoder().readObject( new ChannelBufferInputStream( buffer ) ) ); - } - } - - - @Override - public ChannelBuffer encode( ChannelBuffer buffer ) { - int l = 20; // 5 ints * 4 bytes - - ByteBuffer fullCollectionNameBytes = getCString( fullCollectionName ); - l += fullCollectionNameBytes.capacity(); - - List<ByteBuffer> encodedDocuments = encodeDocuments( documents ); - l += buffersSize( encodedDocuments ); - - messageLength = l; - - buffer = super.encode( buffer ); - - buffer.writeInt( flags ); - - buffer.writeBytes( fullCollectionNameBytes ); - - for ( ByteBuffer d : encodedDocuments ) { - buffer.writeBytes( d ); - } - - return buffer; - } - - - /* (non-Javadoc) - * @see org.apache.usergrid.mongo.protocol.OpCrud#doOp(org.apache.usergrid.mongo.MongoChannelHandler, - * org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.MessageEvent) - */ - @SuppressWarnings("unchecked") - @Override - public OpReply doOp( MongoChannelHandler handler, ChannelHandlerContext ctx, MessageEvent messageEvent ) { - - ApplicationInfo application = SubjectUtils.getApplication( Identifier.from( getDatabaseName() ) ); - - if ( application == null ) { - ctx.setAttachment( new IllegalArgumentException( - String.format( "Could not find application with name '%s' ", getDatabaseName() ) ) ); - return null; - } - - - EntityManager em = handler.getEmf().getEntityManager( application.getId() ); - - - for ( BSONObject document : documents ) { - try { - //special case to serialize mongo ObjectId if required - Object id = document.get( "_id" ); - - if ( id instanceof ObjectId ) { - document.put( "_id", ( ( ObjectId ) id ).toStringMongod() ); - } - - em.create( getCollectionName(), document.toMap() ); - } - catch ( Exception e ) { - logger.error( "Unable to insert mongo document {}", document, e ); - ctx.setAttachment( e ); - } - } - - //insert never returns a response in mongo - return null; - } - - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "OpInsert [flags=" + flags + ", documents=" + documents + ", fullCollectionName=" + fullCollectionName - + ", messageLength=" + messageLength + ", requestID=" + requestID + ", responseTo=" + responseTo - + ", opCode=" + opCode + "]"; - } -}
http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpKillCursors.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpKillCursors.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpKillCursors.java deleted file mode 100644 index f3e0fac..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpKillCursors.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.protocol; - - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -import org.jboss.netty.buffer.ChannelBuffer; - - -public class OpKillCursors extends Message { - - int numberOfCursorIDs; - List<Long> cursorIDs = new ArrayList<Long>(); - - - public OpKillCursors() { - opCode = OP_KILL_CURSORS; - } - - - public int getNumberOfCursorIDs() { - return numberOfCursorIDs; - } - - - public void setNumberOfCursorIDs( int numberOfCursorIDs ) { - this.numberOfCursorIDs = numberOfCursorIDs; - } - - - public List<Long> getCursorIDs() { - return cursorIDs; - } - - - public void setCursorIDs( List<Long> cursorIDs ) { - if ( cursorIDs == null ) { - cursorIDs = new ArrayList<Long>(); - } - this.cursorIDs = cursorIDs; - numberOfCursorIDs = cursorIDs.size(); - } - - - public void addCursorIDs( long cursorID ) { - cursorIDs.add( cursorID ); - numberOfCursorIDs = cursorIDs.size(); - } - - - @Override - public void decode( ChannelBuffer buffer ) throws IOException { - super.decode( buffer ); - - buffer.readInt(); - numberOfCursorIDs = buffer.readInt(); - while ( buffer.readable() ) { - cursorIDs.add( buffer.readLong() ); - } - } - - - @Override - public ChannelBuffer encode( ChannelBuffer buffer ) { - int l = 24; // (6 ints * 4 bytes) - - numberOfCursorIDs = 0; - if ( cursorIDs != null ) { - numberOfCursorIDs = cursorIDs.size(); - l += numberOfCursorIDs * 8; - } - messageLength = l; - - buffer = super.encode( buffer ); - - buffer.writeInt( 0 ); - - buffer.writeInt( numberOfCursorIDs ); - - if ( cursorIDs != null ) { - for ( Long cursorID : cursorIDs ) { - buffer.writeLong( cursorID ); - } - } - - return buffer; - } - - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "OpKillCursors [numberOfCursorIDs=" + numberOfCursorIDs + ", cursorIDs=" + cursorIDs + ", messageLength=" - + messageLength + ", requestID=" + requestID + ", responseTo=" + responseTo + ", opCode=" + opCode - + "]"; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpMsg.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpMsg.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpMsg.java deleted file mode 100644 index 631d103..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpMsg.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.protocol; - - -import java.io.IOException; -import java.nio.ByteBuffer; - -import org.jboss.netty.buffer.ChannelBuffer; - - -public class OpMsg extends Message { - - String message; - - - public OpMsg() { - opCode = OP_MSG; - } - - - public String getMessage() { - return message; - } - - - public void setMessage( String message ) { - this.message = message; - } - - - @Override - public void decode( ChannelBuffer buffer ) throws IOException { - super.decode( buffer ); - message = readCString( buffer ); - } - - - @Override - public ChannelBuffer encode( ChannelBuffer buffer ) { - int l = 16; // 4 ints * 4 bytes - - ByteBuffer messageBytes = getCString( message ); - l += messageBytes.capacity(); - - messageLength = l; - - buffer = super.encode( buffer ); - - buffer.writeBytes( messageBytes ); - - return buffer; - } - - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "OpMsg [message=" + message + ", messageLength=" + messageLength + ", requestID=" + requestID - + ", responseTo=" + responseTo + ", opCode=" + opCode + "]"; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpQuery.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpQuery.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpQuery.java deleted file mode 100644 index e411c28..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpQuery.java +++ /dev/null @@ -1,451 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.protocol; - - -import java.io.IOException; -import java.net.InetSocketAddress; -import java.nio.ByteBuffer; -import java.util.HashSet; -import java.util.Map; -import java.util.Random; -import java.util.Set; - -import org.bson.BSONObject; -import org.bson.BasicBSONObject; -import org.bson.types.ObjectId; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferInputStream; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.MessageEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.management.ApplicationInfo; -import org.apache.usergrid.management.UserInfo; -import org.apache.usergrid.mongo.MongoChannelHandler; -import org.apache.usergrid.mongo.commands.MongoCommand; -import org.apache.usergrid.mongo.query.MongoQueryParser; -import org.apache.usergrid.mongo.utils.BSONUtils; -import org.apache.usergrid.persistence.Entity; -import org.apache.usergrid.persistence.EntityManager; -import org.apache.usergrid.persistence.index.query.Query; -import org.apache.usergrid.persistence.Results; -import org.apache.usergrid.persistence.Schema; -import org.apache.usergrid.security.shiro.PrincipalCredentialsToken; -import org.apache.usergrid.security.shiro.utils.SubjectUtils; -import org.apache.usergrid.utils.MapUtils; - -import org.apache.shiro.authc.AuthenticationException; -import org.apache.shiro.subject.Subject; -import org.apache.usergrid.persistence.index.query.Identifier; -import org.apache.usergrid.persistence.index.query.Query.Level; - -import static org.apache.usergrid.utils.JsonUtils.toJsonMap; -import static org.apache.usergrid.utils.MapUtils.entry; -import static org.apache.usergrid.utils.MapUtils.map; - - -public class OpQuery extends OpCrud { - - private static final Logger logger = LoggerFactory.getLogger( OpQuery.class ); - - int flags; - int numberToSkip; - int numberToReturn; - BSONObject query; - BSONObject returnFieldSelector; - - static Set<String> operators = new HashSet<String>(); - - - static { - operators.add( "all" ); - operators.add( "and" ); - operators.add( "elemMatch" ); - operators.add( "exists" ); - operators.add( "gt" ); - operators.add( "gte" ); - operators.add( "in" ); - operators.add( "lt" ); - operators.add( "lte" ); - operators.add( "mod" ); - operators.add( "ne" ); - operators.add( "nin" ); - operators.add( "nor" ); - operators.add( "not" ); - operators.add( "or" ); - operators.add( "regex" ); - operators.add( "size" ); - operators.add( "type" ); - operators.add( "where" ); - } - - - public OpQuery() { - opCode = OP_QUERY; - } - - - public int getFlags() { - return flags; - } - - - public void setFlags( int flags ) { - this.flags = flags; - } - - - public int getNumberToSkip() { - return numberToSkip; - } - - - public void setNumberToSkip( int numberToSkip ) { - this.numberToSkip = numberToSkip; - } - - - public int getNumberToReturn() { - return numberToReturn; - } - - - public void setNumberToReturn( int numberToReturn ) { - this.numberToReturn = numberToReturn; - } - - - public BSONObject getQuery() { - return query; - } - - - public void setQuery( BSONObject query ) { - this.query = query; - } - - - public void setQuery( Map<?, ?> map ) { - query = new BasicBSONObject(); - query.putAll( map ); - } - - - public BSONObject getReturnFieldSelector() { - return returnFieldSelector; - } - - - public void setReturnFieldSelector( BSONObject returnFieldSelector ) { - this.returnFieldSelector = returnFieldSelector; - } - - - public void setReturnFieldSelector( Map<?, ?> map ) { - returnFieldSelector = new BasicBSONObject(); - returnFieldSelector.putAll( map ); - } - - - @Override - public void decode( ChannelBuffer buffer ) throws IOException { - super.decode( buffer ); - flags = buffer.readInt(); - fullCollectionName = readCString( buffer ); - numberToSkip = buffer.readInt(); - numberToReturn = buffer.readInt(); - query = BSONUtils.decoder().readObject( new ChannelBufferInputStream( buffer ) ); - if ( buffer.readable() ) { - returnFieldSelector = BSONUtils.decoder().readObject( new ChannelBufferInputStream( buffer ) ); - logger.info( "found fieldSeclector: {}", returnFieldSelector ); - } - } - - - @Override - public ChannelBuffer encode( ChannelBuffer buffer ) { - int l = 28; // 7 ints * 4 bytes - - ByteBuffer fullCollectionNameBytes = getCString( fullCollectionName ); - l += fullCollectionNameBytes.capacity(); - - ByteBuffer queryBytes = encodeDocument( query ); - l += queryBytes.capacity(); - - ByteBuffer returnFieldSelectorBytes = encodeDocument( returnFieldSelector ); - l += returnFieldSelectorBytes.capacity(); - - messageLength = l; - - buffer = super.encode( buffer ); - - buffer.writeInt( flags ); - - buffer.writeBytes( fullCollectionNameBytes ); - - buffer.writeInt( numberToSkip ); - buffer.writeInt( numberToReturn ); - - buffer.writeBytes( queryBytes ); - - buffer.writeBytes( returnFieldSelectorBytes ); - - return buffer; - } - - - /* - * (non-Javadoc) - * - * @see org.apache.usergrid.mongo.protocol.OpCrud#doOp() - */ - @Override - public OpReply doOp( MongoChannelHandler handler, ChannelHandlerContext ctx, MessageEvent messageEvent ) { - logger.debug( "In OpQuery.doOp with fullCollectionName: {}", fullCollectionName ); - Subject currentUser = SubjectUtils.getSubject(); - - String collectionName = getCollectionName(); - - if ( "$cmd".equals( collectionName ) ) { - - @SuppressWarnings("unchecked") String commandName = ( String ) MapUtils.getFirstKey( getQuery().toMap() ); - - if ( "authenticate".equals( commandName ) ) { - return handleAuthenticate( handler, getDatabaseName() ); - } - - if ( "getnonce".equals( commandName ) ) { - return handleGetnonce(); - } - - if ( !currentUser.isAuthenticated() ) { - return handleUnauthorizedCommand( messageEvent ); - } - - MongoCommand command = MongoCommand.getCommand( commandName ); - - if ( command != null ) { - logger.info( "found command {} from name {}", command.getClass().getName(), commandName ); - return command.execute( handler, ctx, messageEvent, this ); - } - else { - logger.info( "No command for " + commandName ); - } - } - - if ( !currentUser.isAuthenticated() ) { - return handleUnauthorizedQuery( messageEvent ); - } - - if ( "system.namespaces".equals( collectionName ) ) { - return handleListCollections( handler, getDatabaseName() ); - } - - if ( "system.users".equals( collectionName ) ) { - return handleListUsers(); - } - - return handleQuery( handler ); - } - - - private OpReply handleAuthenticate( MongoChannelHandler handler, String databaseName ) { - logger.info( "Authenticating for database " + databaseName + "... " ); - String name = ( String ) query.get( "user" ); - String nonce = ( String ) query.get( "nonce" ); - String key = ( String ) query.get( "key" ); - - UserInfo user = null; - try { - user = handler.getOrganizations().verifyMongoCredentials( name, nonce, key ); - } - catch ( Exception e1 ) { - return handleAuthFails( this ); - } - if ( user == null ) { - return handleAuthFails( this ); - } - - PrincipalCredentialsToken token = - PrincipalCredentialsToken.getFromAdminUserInfoAndPassword( - user, key, handler.getEmf().getManagementAppId() ); - Subject subject = SubjectUtils.getSubject(); - - try { - subject.login( token ); - } - catch ( AuthenticationException e2 ) { - return handleAuthFails( this ); - } - - OpReply reply = new OpReply( this ); - reply.addDocument( map( "ok", 1.0 ) ); - return reply; - } - - - private OpReply handleGetnonce() { - String nonce = String.format( "%04x", ( new Random() ).nextLong() ); - OpReply reply = new OpReply( this ); - reply.addDocument( map( entry( "nonce", nonce ), entry( "ok", 1.0 ) ) ); - return reply; - } - - - private OpReply handleUnauthorizedCommand( MessageEvent e ) { - // { "assertion" : "unauthorized db:admin lock type:-1 client:127.0.0.1" - // , "assertionCode" : 10057 , "errmsg" : "db assertion failure" , "ok" - // : 0.0} - OpReply reply = new OpReply( this ); - reply.addDocument( map( entry( "assertion", - "unauthorized db:" + getDatabaseName() + " lock type:-1 client:" + ( ( InetSocketAddress ) e - .getRemoteAddress() ).getAddress().getHostAddress() ), entry( "assertionCode", 10057 ), - entry( "errmsg", "db assertion failure" ), entry( "ok", 0.0 ) ) ); - return reply; - } - - - private OpReply handleUnauthorizedQuery( MessageEvent e ) { - // { "$err" : "unauthorized db:test lock type:-1 client:127.0.0.1" , - // "code" : 10057} - OpReply reply = new OpReply( this ); - reply.addDocument( map( entry( "$err", - "unauthorized db:" + getDatabaseName() + " lock type:-1 client:" + ( ( InetSocketAddress ) e - .getRemoteAddress() ).getAddress().getHostAddress() ), entry( "code", 10057 ) ) ); - return reply; - } - - - private OpReply handleAuthFails( OpQuery opQuery ) { - // { "errmsg" : "auth fails" , "ok" : 0.0} - OpReply reply = new OpReply( opQuery ); - reply.addDocument( map( entry( "errmsg", "auth fails" ), entry( "ok", 0.0 ) ) ); - return reply; - } - - - private OpReply handleListCollections( MongoChannelHandler handler, String databaseName ) { - logger.info( "Handling list collections for database {} ... ", databaseName ); - Identifier id = Identifier.from( databaseName ); - - OpReply reply = new OpReply( this ); - - ApplicationInfo application = SubjectUtils.getApplication( id ); - - if ( application == null ) { - return reply; - } - - EntityManager em = handler.getEmf().getEntityManager( application.getId() ); - - - try { - Set<String> collections = em.getApplicationCollections(); - for ( String colName : collections ) { - if ( Schema.isAssociatedEntityType( colName ) ) { - continue; - } - reply.addDocument( map( "name", String.format( "%s.%s", databaseName, colName ) ) ); - reply.addDocument( map( "name", String.format( "%s.%s.$_id_", databaseName, colName ) ) ); - } - // reply.addDocument(map("name", databaseName + ".system.indexes")); - } - catch ( Exception ex ) { - logger.error( "Unable to retrieve collections", ex ); - } - return reply; - } - - - private OpReply handleListUsers() { - logger.info( "Handling list users for database {} ... ", getDatabaseName() ); - - OpReply reply = new OpReply( this ); - return reply; - } - - - private OpReply handleQuery( MongoChannelHandler handler ) { - logger.info( "Handling a query... " ); - OpReply reply = new OpReply( this ); - - ApplicationInfo application = SubjectUtils.getApplication( Identifier.from( getDatabaseName() ) ); - if ( application == null ) { - return reply; - } - - int count = getNumberToReturn(); - if ( count <= 0 ) { - count = 30; - } - - EntityManager em = handler.getEmf().getEntityManager( application.getId() ); - - try { - Results results = null; - Query q = MongoQueryParser.toNativeQuery( query, returnFieldSelector, numberToReturn ); - if ( q != null ) { - results = em.searchCollection( em.getApplicationRef(), getCollectionName(), q ); - } - else { - results = em.getCollection( em.getApplicationRef(), getCollectionName(), null, count, - Level.ALL_PROPERTIES, false ); - } - if ( !results.isEmpty() ) { - for ( Entity entity : results.getEntities() ) { - - Object savedId = entity.getProperty( "_id" ); - Object mongoId = null; - - //try to parse it into an ObjectId - if ( savedId == null ) { - mongoId = entity.getUuid(); - } - else { - try { - mongoId = new ObjectId( savedId.toString() ); - //it's not a mongo Id, use it as is - } - catch ( IllegalArgumentException iae ) { - mongoId = savedId; - } - } - - reply.addDocument( map( entry( "_id", mongoId ), toJsonMap( entity ), - entry( Schema.PROPERTY_UUID, entity.getUuid().toString() ) ) ); - } - } - } - catch ( Exception ex ) { - logger.error( "Unable to retrieve collections", ex ); - } - return reply; - } - - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "OpQuery [flags=" + flags + ", numberToSkip=" + numberToSkip + ", numberToReturn=" + numberToReturn - + ", query=" + query + ", returnFieldSelector=" + returnFieldSelector + ", fullCollectionName=" - + fullCollectionName + ", messageLength=" + messageLength + ", requestID=" + requestID + ", responseTo=" - + responseTo + ", opCode=" + opCode + "]"; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpReply.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpReply.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpReply.java deleted file mode 100644 index 49e2259..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpReply.java +++ /dev/null @@ -1,190 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.protocol; - - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -import org.bson.BSONObject; -import org.bson.BasicBSONObject; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferInputStream; -import org.apache.usergrid.mongo.utils.BSONUtils; - - -public class OpReply extends Message { - - int responseFlags = 8; - long cursorID; - int startingFrom; - int numberReturned; - List<BSONObject> documents = new ArrayList<BSONObject>(); - - - public OpReply() { - opCode = OP_REPLY; - } - - - public OpReply( Message message ) { - opCode = OP_REPLY; - responseTo = message.getRequestID(); - } - - - public int getResponseFlags() { - return responseFlags; - } - - - public void setResponseFlags( int responseFlags ) { - this.responseFlags = responseFlags; - } - - - public long getCursorID() { - return cursorID; - } - - - public void setCursorID( long cursorID ) { - this.cursorID = cursorID; - } - - - public int getStartingFrom() { - return startingFrom; - } - - - public void setStartingFrom( int startingFrom ) { - this.startingFrom = startingFrom; - } - - - public int getNumberReturned() { - return numberReturned; - } - - - public void setNumberReturned( int numberReturned ) { - this.numberReturned = numberReturned; - } - - - public List<BSONObject> getDocuments() { - return documents; - } - - - public void setDocuments( List<BSONObject> documents ) { - if ( documents == null ) { - documents = new ArrayList<BSONObject>(); - } - this.documents = documents; - numberReturned = documents.size(); - } - - - public void addDocument( BSONObject document ) { - documents.add( document ); - numberReturned = documents.size(); - } - - - public void addDocument( Map<?, ?> map ) { - BSONObject b = new BasicBSONObject(); - b.putAll( map ); - documents.add( b ); - numberReturned = documents.size(); - } - - - @Override - public void decode( ChannelBuffer buffer ) throws IOException { - super.decode( buffer ); - - responseFlags = buffer.readInt(); - cursorID = buffer.readLong(); - startingFrom = buffer.readInt(); - numberReturned = buffer.readInt(); - - while ( buffer.readable() ) { - documents.add( BSONUtils.decoder().readObject( new ChannelBufferInputStream( buffer ) ) ); - } - } - - - @Override - public ChannelBuffer encode( ChannelBuffer buffer ) { - int l = 36; // (9 ints * 4 bytes) - - List<ByteBuffer> encodedDocuments = encodeDocuments( documents ); - l += buffersSize( encodedDocuments ); - numberReturned = encodedDocuments.size(); - - messageLength = l; - - buffer = super.encode( buffer ); - - buffer.writeInt( responseFlags ); - buffer.writeLong( cursorID ); - buffer.writeInt( startingFrom ); - buffer.writeInt( numberReturned ); - - for ( ByteBuffer d : encodedDocuments ) { - buffer.writeBytes( d ); - } - - return buffer; - } - - - public static OpReply errorReply( String message ) { - OpReply reply = new OpReply(); - // reply.responseFlags = 1; - BSONObject b = new BasicBSONObject(); - b.put( "$err", message ); - b.put( "ok", 0.0 ); - reply.documents.add( b ); - return reply; - } - - - /* (non-Javadoc) - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - String docs_str = null; - try { - docs_str = documents.toString(); - } - catch ( Exception e ) { - docs_str = "error(" + e.getMessage() + ")"; - } - - return "OpReply [responseFlags=" + responseFlags + ", cursorID=" + cursorID + ", startingFrom=" + startingFrom - + ", numberReturned=" + numberReturned + ", documents=" + documents + ", messageLength=" + messageLength - + ", requestID=" + requestID + ", responseTo=" + responseTo + ", opCode=" + opCode + ", documents=" - + docs_str + "]"; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpUpdate.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpUpdate.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpUpdate.java deleted file mode 100644 index 8062f86..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/protocol/OpUpdate.java +++ /dev/null @@ -1,207 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.protocol; - - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Map; - -import org.bson.BSONObject; -import org.bson.BasicBSONObject; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBufferInputStream; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.MessageEvent; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.management.ApplicationInfo; -import org.apache.usergrid.mongo.MongoChannelHandler; -import org.apache.usergrid.mongo.query.MongoQueryParser; -import org.apache.usergrid.mongo.utils.BSONUtils; -import org.apache.usergrid.persistence.Entity; -import org.apache.usergrid.persistence.EntityManager; -import org.apache.usergrid.persistence.index.query.Query; -import org.apache.usergrid.persistence.Results; -import org.apache.usergrid.persistence.index.query.Identifier; -import org.apache.usergrid.security.shiro.utils.SubjectUtils; - - -public class OpUpdate extends OpCrud { - - private static final Logger logger = LoggerFactory.getLogger( OpUpdate.class ); - private int flags; - private BSONObject selector; - private BSONObject update; - - - public OpUpdate() { - opCode = OP_UPDATE; - } - - - public int getFlags() { - return flags; - } - - - public void setFlags( int flags ) { - this.flags = flags; - } - - - public BSONObject getSelector() { - return selector; - } - - - public void setSelector( BSONObject selector ) { - this.selector = selector; - } - - - public void setSelector( Map<?, ?> map ) { - selector = new BasicBSONObject(); - selector.putAll( map ); - } - - - public BSONObject getUpdate() { - return update; - } - - - public void setUpdate( BSONObject update ) { - this.update = update; - } - - - public void setUpdate( Map<?, ?> map ) { - update = new BasicBSONObject(); - update.putAll( map ); - } - - - @Override - public void decode( ChannelBuffer buffer ) throws IOException { - super.decode( buffer ); - buffer.readInt(); - fullCollectionName = readCString( buffer ); - flags = buffer.readInt(); - selector = BSONUtils.decoder().readObject( new ChannelBufferInputStream( buffer ) ); - update = BSONUtils.decoder().readObject( new ChannelBufferInputStream( buffer ) ); - } - - - @Override - public ChannelBuffer encode( ChannelBuffer buffer ) { - int l = 24; // 6 ints * 4 bytes - - ByteBuffer fullCollectionNameBytes = getCString( fullCollectionName ); - l += fullCollectionNameBytes.capacity(); - - ByteBuffer selectorBytes = encodeDocument( selector ); - l += selectorBytes.capacity(); - - ByteBuffer updateBytes = encodeDocument( update ); - l += updateBytes.capacity(); - - messageLength = l; - - buffer = super.encode( buffer ); - - buffer.writeInt( 0 ); - - buffer.writeBytes( fullCollectionNameBytes ); - - buffer.writeInt( flags ); - - buffer.writeBytes( selectorBytes ); - - buffer.writeBytes( updateBytes ); - - return buffer; - } - - - /* - * (non-Javadoc) - * - * @see org.apache.usergrid.mongo.protocol.OpCrud#doOp(org.apache.usergrid.mongo. - * MongoChannelHandler, org.jboss.netty.channel.ChannelHandlerContext, - * org.jboss.netty.channel.MessageEvent) - */ - @SuppressWarnings("unchecked") - @Override - public OpReply doOp( MongoChannelHandler handler, ChannelHandlerContext ctx, MessageEvent messageEvent ) { - - ApplicationInfo application = SubjectUtils.getApplication( Identifier.from( getDatabaseName() ) ); - - if ( application == null ) { - ctx.setAttachment( new IllegalArgumentException( - String.format( "Could not find application with name '%s' ", getDatabaseName() ) ) ); - return null; - } - - EntityManager em = handler.getEmf().getEntityManager( application.getId() ); - - Results results = null; - Query q = MongoQueryParser.toNativeQuery( selector, 1000 ); - - if ( q == null ) { - ctx.setAttachment( new IllegalArgumentException( "Could not parse query" ) ); - return null; - } - - try { - do { - if ( results != null ) { - q.setCursor( results.getCursor() ); - } - - results = em.searchCollection( em.getApplicationRef(), getCollectionName(), q ); - - // apply the update - - for ( Entity entity : results.getEntities() ) { - em.updateProperties( entity, update.toMap() ); - } - } - while ( results != null && results.getCursor() != null ); - } - catch ( Exception e ) { - logger.error( "Unable to perform update with query {} and update {}", - new Object[] { selector, update, e } ); - ctx.setAttachment( e ); - } - - return null; - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - return "OpUpdate [flags=" + flags + ", selector=" + selector + ", update=" + update + ", fullCollectionName=" - + fullCollectionName + ", messageLength=" + messageLength + ", requestID=" + requestID + ", responseTo=" - + responseTo + ", opCode=" + opCode + "]"; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/query/MongoQueryParser.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/query/MongoQueryParser.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/query/MongoQueryParser.java deleted file mode 100644 index 6ceb0e4..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/query/MongoQueryParser.java +++ /dev/null @@ -1,292 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.query; - - -import java.util.Stack; - -import org.antlr.runtime.ClassicToken; -import org.bson.BSONObject; -import org.bson.BasicBSONObject; -import org.bson.types.BasicBSONList; -import org.apache.usergrid.persistence.index.query.Query; -import org.apache.usergrid.persistence.index.query.Query.SortDirection; -import org.apache.usergrid.persistence.index.query.tree.AndOperand; -import org.apache.usergrid.persistence.index.query.tree.Equal; -import org.apache.usergrid.persistence.index.query.tree.GreaterThan; -import org.apache.usergrid.persistence.index.query.tree.GreaterThanEqual; -import org.apache.usergrid.persistence.index.query.tree.LessThan; -import org.apache.usergrid.persistence.index.query.tree.LessThanEqual; -import org.apache.usergrid.persistence.index.query.tree.Operand; -import org.apache.usergrid.persistence.index.query.tree.OrOperand; - -import static org.apache.commons.collections.MapUtils.getIntValue; - - -/** - * Parser class to parse mongo queries into usergrid EM queries - * - * @author tnine - */ -public class MongoQueryParser { - - /** - * Convert the bson object query to a native usergrid query - * - * @return The query - */ - public static Query toNativeQuery( BSONObject query, int numberToReturn ) { - return toNativeQuery( query, null, numberToReturn ); - } - - - /** - * Overloaded form which takes a FieldSelector as the second query argument - * - * @return The query - */ - public static Query toNativeQuery( BSONObject query, BSONObject fieldSelector, int numberToReturn ) { - // TODO overload? or add? - if ( query == null ) { - return null; - } - - BasicBSONObject query_expression = null; - BasicBSONObject field_selector = null; - BasicBSONObject sort_order = null; - - Object o = query.get( "$query" ); - if ( !( o instanceof BasicBSONObject ) ) { - o = query.get( "query" ); - } - if ( o instanceof BasicBSONObject ) { - query_expression = ( BasicBSONObject ) o; - } - - o = query.get( "$orderby" ); - if ( !( o instanceof BasicBSONObject ) ) { - o = query.get( "orderby" ); - } - if ( o instanceof BasicBSONObject ) { - sort_order = ( BasicBSONObject ) o; - } - - if ( ( query_expression == null ) && ( query instanceof BasicBSONObject ) ) { - query_expression = ( BasicBSONObject ) query; - query_expression.removeField( "$orderby" ); - query_expression.removeField( "$max" ); - query_expression.removeField( "$min" ); - } - - - if ( ( query_expression == null ) && ( sort_order == null ) ) { - return null; - } - - if ( query_expression.size() == 0 && sort_order != null ) { - if ( sort_order.size() == 0 ) { - return null; - } - if ( ( sort_order.size() == 1 ) && sort_order.containsField( "_id" ) ) { - return null; - } - } - - Query q = new Query(); - - if ( numberToReturn > 0 ) { - q.setLimit( numberToReturn ); - } - - if ( query_expression != null ) { - Operand root = eval( query_expression ); - q.setRootOperand( root ); - } - - if ( fieldSelector != null ) { - for ( String field : fieldSelector.keySet() ) { - q.addSelect( field, field ); - } - } - - - if ( sort_order != null ) { - for ( String sort : sort_order.keySet() ) { - if ( !"_id".equals( sort ) ) { - int s = getIntValue( sort_order.toMap(), "_id", 1 ); - q.addSort( sort, s >= 0 ? SortDirection.ASCENDING : SortDirection.DESCENDING ); - } - } - } - - return q; - } - - - /** Evaluate an expression part */ - private static Operand eval( BSONObject exp ) { - Operand current = null; - Object fieldValue = null; - - for ( String field : exp.keySet() ) { - fieldValue = exp.get( field ); - - if ( field.startsWith( "$" ) ) { - // same as OR with multiple values - - // same as OR with multiple values - if ( "$or".equals( field ) ) { - BasicBSONList values = ( BasicBSONList ) fieldValue; - - int size = values.size(); - - Stack<Operand> expressions = new Stack<Operand>(); - - for (Object value : values) { - expressions.push(eval((BSONObject) value)); - } - - // we need to build a tree of expressions - while ( expressions.size() > 1 ) { - OrOperand or = new OrOperand(); - or.addChild( expressions.pop() ); - or.addChild( expressions.pop() ); - expressions.push( or ); - } - - current = expressions.pop(); - } - - else if ( "$and".equals( field ) ) { - - BasicBSONList values = ( BasicBSONList ) fieldValue; - - int size = values.size(); - - Stack<Operand> expressions = new Stack<Operand>(); - - for (Object value : values) { - expressions.push(eval((BSONObject) value)); - } - - while ( expressions.size() > 1 ) { - AndOperand and = new AndOperand(); - and.addChild( expressions.pop() ); - and.addChild( expressions.pop() ); - expressions.push( and ); - } - - current = expressions.pop(); - } - } - // we have a nested object - else if ( fieldValue instanceof BSONObject ) { - current = handleOperand( field, ( BSONObject ) fieldValue ); - } - - else if ( !field.equals( "_id" ) ) { - Equal equality = new Equal( new ClassicToken( 0, "=" ) ); - equality.setProperty( field ); - equality.setLiteral( exp.get( field ) ); - - current = equality; - } - } - return current; - } - - - /** Handle an operand */ - private static Operand handleOperand( String sourceField, BSONObject exp ) { - - Operand current = null; - Object value = null; - - for ( String field : exp.keySet() ) { - if ( field.startsWith( "$" ) ) { - if ( "$gt".equals( field ) ) { - value = exp.get( field ); - - GreaterThan gt = new GreaterThan(); - gt.setProperty( sourceField ); - gt.setLiteral( value ); - - current = gt; - } - else if ( "$gte".equals( field ) ) { - value = exp.get( field ); - - GreaterThanEqual gte = new GreaterThanEqual(); - gte.setProperty( sourceField ); - gte.setLiteral( exp.get( field ) ); - - current = gte; - // http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%3C%2C%3C%3D%2C%3E%2C%3E%3D - // greater than equals - // { "field" : { $gte: value } } - } - else if ( "$lt".equals( field ) ) { - value = exp.get( field ); - - LessThan lt = new LessThan(); - lt.setProperty( sourceField ); - lt.setLiteral( value ); - - current = lt; - } - else if ( "$lte".equals( field ) ) { - value = exp.get( field ); - - LessThanEqual lte = new LessThanEqual(); - lte.setProperty( sourceField ); - lte.setLiteral( value ); - - current = lte; - } - else if ( "$in".equals( field ) ) { - value = exp.get( field ); - - BasicBSONList values = ( BasicBSONList ) value; - - int size = values.size(); - - Stack<Operand> expressions = new Stack<Operand>(); - - for (Object value1 : values) { - Equal equal = new Equal(); - equal.setProperty(sourceField); - equal.setLiteral(value1); - - expressions.push(equal); - } - - // we need to build a tree of expressions - while ( expressions.size() > 1 ) { - OrOperand or = new OrOperand(); - or.addChild( expressions.pop() ); - or.addChild( expressions.pop() ); - expressions.push( or ); - } - - current = expressions.pop(); - } - } - } - - return current; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoMessageFrame.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoMessageFrame.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoMessageFrame.java deleted file mode 100644 index e114e11..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoMessageFrame.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.testproxy; - - -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.handler.codec.frame.FrameDecoder; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class MongoMessageFrame extends FrameDecoder { - - @SuppressWarnings("unused") - private static final Logger logger = LoggerFactory.getLogger( MongoMessageFrame.class ); - - - @Override - protected Object decode( ChannelHandlerContext ctx, Channel channel, ChannelBuffer buf ) throws Exception { - - if ( buf.readableBytes() < 4 ) { - return null; - } - - // logger.info("Mongo message decoding..."); - - int length = buf.getInt( buf.readerIndex() ); - - if ( length < 0 ) { - return null; - } - - if ( buf.readableBytes() < length ) { - return null; - } - - ChannelBuffer frame = buf.readSlice( length ); - return frame; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyInboundHandler.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyInboundHandler.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyInboundHandler.java deleted file mode 100644 index 8aeca1a..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyInboundHandler.java +++ /dev/null @@ -1,198 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.testproxy; - - -import java.net.InetSocketAddress; -import java.nio.ByteOrder; - -import org.jboss.netty.bootstrap.ClientBootstrap; -import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; -import org.jboss.netty.buffer.HeapChannelBufferFactory; -import org.jboss.netty.channel.Channel; -import org.jboss.netty.channel.ChannelFuture; -import org.jboss.netty.channel.ChannelFutureListener; -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ChannelStateEvent; -import org.jboss.netty.channel.ExceptionEvent; -import org.jboss.netty.channel.MessageEvent; -import org.jboss.netty.channel.SimpleChannelUpstreamHandler; -import org.jboss.netty.channel.socket.ClientSocketChannelFactory; -import org.apache.usergrid.mongo.MongoMessageDecoder; -import org.apache.usergrid.mongo.protocol.Message; - - -public class MongoProxyInboundHandler extends SimpleChannelUpstreamHandler { - - private final ClientSocketChannelFactory cf; - private final String remoteHost; - private final int remotePort; - - // This lock guards against the race condition that overrides the - // OP_READ flag incorrectly. - // See the related discussion: http://markmail.org/message/x7jc6mqx6ripynqf - final Object trafficLock = new Object(); - - private volatile Channel outboundChannel; - - - public MongoProxyInboundHandler( ClientSocketChannelFactory cf, String remoteHost, int remotePort ) { - this.cf = cf; - this.remoteHost = remoteHost; - this.remotePort = remotePort; - } - - - @Override - public void channelOpen( ChannelHandlerContext ctx, ChannelStateEvent e ) throws Exception { - // Suspend incoming traffic until connected to the remote host. - final Channel inboundChannel = e.getChannel(); - inboundChannel.setReadable( false ); - - // Start the connection attempt. - ClientBootstrap cb = new ClientBootstrap( cf ); - cb.setOption( "bufferFactory", HeapChannelBufferFactory.getInstance( ByteOrder.LITTLE_ENDIAN ) ); - cb.getPipeline().addLast( "framer", new MongoMessageFrame() ); - cb.getPipeline().addLast( "handler", new OutboundHandler( e.getChannel() ) ); - ChannelFuture f = cb.connect( new InetSocketAddress( remoteHost, remotePort ) ); - - outboundChannel = f.getChannel(); - f.addListener( new ChannelFutureListener() { - @Override - public void operationComplete( ChannelFuture future ) throws Exception { - if ( future.isSuccess() ) { - // Connection attempt succeeded: - // Begin to accept incoming traffic. - inboundChannel.setReadable( true ); - } - else { - // Close the connection if the connection attempt has - // failed. - inboundChannel.close(); - } - } - } ); - } - - - @Override - public void messageReceived( ChannelHandlerContext ctx, final MessageEvent e ) throws Exception { - ChannelBuffer msg = ( ChannelBuffer ) e.getMessage(); - Message mongo_message = MongoMessageDecoder.decode( msg ); - if ( mongo_message != null ) { - System.out.println( ">>> " + mongo_message.toString() ); - } - synchronized ( trafficLock ) { - outboundChannel.write( msg ); - // If outboundChannel is saturated, do not read until notified in - // OutboundHandler.channelInterestChanged(). - if ( !outboundChannel.isWritable() ) { - e.getChannel().setReadable( false ); - } - } - } - - - @Override - public void channelInterestChanged( ChannelHandlerContext ctx, ChannelStateEvent e ) throws Exception { - // If inboundChannel is not saturated anymore, continue accepting - // the incoming traffic from the outboundChannel. - synchronized ( trafficLock ) { - if ( e.getChannel().isWritable() ) { - outboundChannel.setReadable( true ); - } - } - } - - - @Override - public void channelClosed( ChannelHandlerContext ctx, ChannelStateEvent e ) throws Exception { - if ( outboundChannel != null ) { - closeOnFlush( outboundChannel ); - } - } - - - @Override - public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e ) throws Exception { - e.getCause().printStackTrace(); - closeOnFlush( e.getChannel() ); - } - - - private class OutboundHandler extends SimpleChannelUpstreamHandler { - - private final Channel inboundChannel; - - - OutboundHandler( Channel inboundChannel ) { - this.inboundChannel = inboundChannel; - } - - - @Override - public void messageReceived( ChannelHandlerContext ctx, final MessageEvent e ) throws Exception { - ChannelBuffer msg = ( ChannelBuffer ) e.getMessage(); - Message mongo_message = MongoMessageDecoder.decode( msg ); - if ( mongo_message != null ) { - System.out.println( "<<< " + mongo_message.toString() + "\n" ); - } - synchronized ( trafficLock ) { - inboundChannel.write( msg ); - // If inboundChannel is saturated, do not read until notified in - // HexDumpProxyInboundHandler.channelInterestChanged(). - if ( !inboundChannel.isWritable() ) { - e.getChannel().setReadable( false ); - } - } - } - - - @Override - public void channelInterestChanged( ChannelHandlerContext ctx, ChannelStateEvent e ) throws Exception { - // If outboundChannel is not saturated anymore, continue accepting - // the incoming traffic from the inboundChannel. - synchronized ( trafficLock ) { - if ( e.getChannel().isWritable() ) { - inboundChannel.setReadable( true ); - } - } - } - - - @Override - public void channelClosed( ChannelHandlerContext ctx, ChannelStateEvent e ) throws Exception { - closeOnFlush( inboundChannel ); - } - - - @Override - public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e ) throws Exception { - e.getCause().printStackTrace(); - closeOnFlush( e.getChannel() ); - } - } - - - /** Closes the specified channel after all queued write requests are flushed. */ - static void closeOnFlush( Channel ch ) { - if ( ch.isConnected() ) { - ch.write( ChannelBuffers.EMPTY_BUFFER ).addListener( ChannelFutureListener.CLOSE ); - } - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyPipelineFactory.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyPipelineFactory.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyPipelineFactory.java deleted file mode 100644 index 8fb4544..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyPipelineFactory.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.testproxy; - - -import org.jboss.netty.channel.ChannelPipeline; -import org.jboss.netty.channel.ChannelPipelineFactory; -import org.jboss.netty.channel.socket.ClientSocketChannelFactory; - -import static org.jboss.netty.channel.Channels.pipeline; - - -/** - * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> - * @author <a href="http://gleamynode.net/">Trustin Lee</a> - * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $ - */ -public class MongoProxyPipelineFactory implements ChannelPipelineFactory { - - private final ClientSocketChannelFactory cf; - private final String remoteHost; - private final int remotePort; - - - public MongoProxyPipelineFactory( ClientSocketChannelFactory cf, String remoteHost, int remotePort ) { - this.cf = cf; - this.remoteHost = remoteHost; - this.remotePort = remotePort; - } - - - @Override - public ChannelPipeline getPipeline() throws Exception { - ChannelPipeline p = pipeline(); // Note the static import. - // p.addLast("encoder", new MongoMessageEncoder()); - p.addLast( "framer", new MongoMessageFrame() ); - p.addLast( "handler", new MongoProxyInboundHandler( cf, remoteHost, remotePort ) ); - return p; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServer.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServer.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServer.java deleted file mode 100644 index bfe7b8c..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServer.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.testproxy; - - -import java.net.InetSocketAddress; -import java.nio.ByteOrder; -import java.util.concurrent.Executor; -import java.util.concurrent.Executors; - -import org.jboss.netty.bootstrap.ServerBootstrap; -import org.jboss.netty.buffer.HeapChannelBufferFactory; -import org.jboss.netty.channel.socket.ClientSocketChannelFactory; -import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory; -import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class MongoProxyServer { - - private static final Logger logger = LoggerFactory.getLogger( MongoProxyServer.class ); - - - public static void main( String[] args ) throws Exception { - logger.info( "Starting Usergrid Mongo Proxy Server" ); - - // Configure the server. - Executor executor = Executors.newCachedThreadPool(); - ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( executor, executor ) ); - - bootstrap.setOption( "child.bufferFactory", HeapChannelBufferFactory.getInstance( ByteOrder.LITTLE_ENDIAN ) ); - - ClientSocketChannelFactory cf = new NioClientSocketChannelFactory( executor, executor ); - - bootstrap.setPipelineFactory( new MongoProxyPipelineFactory( cf, "localhost", 12345 ) ); - - bootstrap.bind( new InetSocketAddress( 27017 ) ); - - logger.info( "Usergrid Mongo Proxy Server accepting connections..." ); - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServerHandler.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServerHandler.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServerHandler.java deleted file mode 100644 index 206be0c..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/testproxy/MongoProxyServerHandler.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.testproxy; - - -import org.jboss.netty.channel.ChannelHandlerContext; -import org.jboss.netty.channel.ExceptionEvent; -import org.jboss.netty.channel.MessageEvent; -import org.jboss.netty.channel.SimpleChannelUpstreamHandler; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.mongo.protocol.Message; -import org.apache.usergrid.mongo.protocol.OpReply; - - -public class MongoProxyServerHandler extends SimpleChannelUpstreamHandler { - - private static final Logger logger = LoggerFactory.getLogger( MongoProxyServerHandler.class ); - - - @Override - public void messageReceived( ChannelHandlerContext ctx, MessageEvent e ) { - - Message message = null; - if ( e.getMessage() instanceof Message ) { - message = ( Message ) e.getMessage(); - } - - OpReply reply = OpReply.errorReply( "not implemented" ); - - if ( message != null ) { - logger.info( message.getClass().getName() ); - reply.setResponseTo( message.getRequestID() ); - } - - e.getChannel().write( reply ); - } - - - @Override - public void exceptionCaught( ChannelHandlerContext ctx, ExceptionEvent e ) { - logger.warn( "Unexpected exception from downstream.", e.getCause() ); - e.getChannel().close(); - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/utils/BSONUtils.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/utils/BSONUtils.java b/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/utils/BSONUtils.java deleted file mode 100644 index 025ea93..0000000 --- a/stack/mongo-emulator/src/main/java/org/apache/usergrid/mongo/utils/BSONUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo.utils; - - -import org.bson.BSONDecoder; -import org.bson.BSONEncoder; -import org.bson.BasicBSONDecoder; -import org.bson.BasicBSONEncoder; - - -public class BSONUtils { - - public static BSONEncoder encoder() { - return _staticEncoder.get(); - } - - - public static BSONDecoder decoder() { - return _staticDecoder.get(); - } - - - static ThreadLocal<BSONEncoder> _staticEncoder = new ThreadLocal<BSONEncoder>() { - @Override - protected BSONEncoder initialValue() { - return new BasicBSONEncoder(); - } - }; - - static ThreadLocal<BSONDecoder> _staticDecoder = new ThreadLocal<BSONDecoder>() { - @Override - protected BSONDecoder initialValue() { - return new BasicBSONDecoder(); - } - }; -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/AbstractMongoTest.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/AbstractMongoTest.java b/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/AbstractMongoTest.java deleted file mode 100644 index fd30b35..0000000 --- a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/AbstractMongoTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo; - - -import org.apache.usergrid.mongo.MongoServer; -import java.net.UnknownHostException; -import java.util.Properties; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.apache.usergrid.persistence.cassandra.EntityManagerFactoryImpl; -import org.apache.usergrid.services.ServiceManagerFactory; - -import com.mongodb.DB; -import com.mongodb.Mongo; -import com.mongodb.MongoException; -import com.mongodb.WriteConcern; - - -public abstract class AbstractMongoTest { - private static final Logger LOG = LoggerFactory.getLogger( AbstractMongoTest.class ); - - static MongoServer server = null; - static boolean usersSetup = false; - protected static Properties properties; - - protected static String access_token; - - EntityManagerFactoryImpl emf; - ServiceManagerFactory smf; - - - public AbstractMongoTest() { - super(); - smf = new ServiceManagerFactory( emf, properties, null, null, null ); - } - - - /** Get a db instance for testing */ - public static DB getDb() throws UnknownHostException, MongoException { - Mongo m = new Mongo( "localhost", 27017 ); - m.setWriteConcern( WriteConcern.SAFE ); - - DB db = m.getDB( "test-organization/test-app" ); - db.authenticate( "test", "test".toCharArray() ); - return db; - } -} http://git-wip-us.apache.org/repos/asf/usergrid/blob/5f552e7e/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/BasicMongoTest.java ---------------------------------------------------------------------- diff --git a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/BasicMongoTest.java b/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/BasicMongoTest.java deleted file mode 100644 index bcd4d2b..0000000 --- a/stack/mongo-emulator/src/test/java/org/apache/usergrid/mongo/BasicMongoTest.java +++ /dev/null @@ -1,356 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You 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.apache.usergrid.mongo; - - -import java.util.ArrayList; -import java.util.List; -import java.util.Set; -import java.util.UUID; - -import org.bson.types.ObjectId; -import org.junit.Ignore; -import org.junit.Test; -import org.apache.usergrid.mongo.protocol.OpDelete; -import org.apache.usergrid.persistence.Entity; -import org.apache.usergrid.persistence.EntityManager; -import org.apache.usergrid.persistence.index.query.Query; -import org.apache.usergrid.persistence.Results; -import org.apache.usergrid.persistence.SimpleEntityRef; - -import com.mongodb.BasicDBObject; -import com.mongodb.DB; -import com.mongodb.DBCollection; -import com.mongodb.DBCursor; -import com.mongodb.DBObject; -import com.mongodb.MongoException; -import com.mongodb.WriteConcern; -import com.mongodb.WriteResult; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - - -@Ignore -public class BasicMongoTest extends AbstractMongoTest { - - @Test - public void insertTest() throws Exception { - - DB db = getDb(); - - BasicDBObject doc = new BasicDBObject(); - - doc.put( "name", "nico" ); - doc.put( "color", "tabby" ); - - WriteResult result = db.getCollection( "inserttests" ).insert( doc ); - - ObjectId savedOid = doc.getObjectId( "_id" ); - - assertNull( result.getError() ); - - // check we've created the collection - - Set<String> colls = db.getCollectionNames(); - - assertTrue( colls.contains( "inserttests" ) ); - - // iterate the collection to ensure we can retrieve the object - DBCollection coll = db.getCollection( "inserttests" ); - DBCursor cur = coll.find(); - - BasicDBObject returnedObject = null; - - assertTrue( cur.hasNext() ); - - returnedObject = ( BasicDBObject ) cur.next(); - - assertFalse( cur.hasNext() ); - - UUID id = UUID.fromString( returnedObject.get( "uuid" ).toString() ); - - //this should work. Appears to be the type of ObjectId getting lost on column serialization - ObjectId returnedOid = new ObjectId( returnedObject.getString( "_id" ) ); - - assertEquals( "nico", returnedObject.get( "name" ) ); - assertEquals( "tabby", returnedObject.get( "color" ) ); - assertEquals( savedOid, returnedOid ); - assertNotNull( id ); - - BasicDBObject query = new BasicDBObject(); - query.put( "_id", savedOid ); - - // now load by the mongo Id. Users will use this the most to read data. - - returnedObject = new BasicDBObject( db.getCollection( "inserttests" ).findOne( query ).toMap() ); - - assertEquals( "nico", returnedObject.get( "name" ) ); - assertEquals( "tabby", returnedObject.get( "color" ) ); - - assertEquals( savedOid, new ObjectId( returnedObject.getString( "_id" ) ) ); - assertEquals( id.toString(), returnedObject.get( "uuid" ) ); - - // check we can find it when using the native entity manager - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Entity entity = em.get( new SimpleEntityRef( (String)returnedObject.get("type"), id )); - - assertNotNull( entity ); - assertEquals( "nico", entity.getProperty( "name" ) ); - assertEquals( "tabby", entity.getProperty( "color" ) ); - } - - - @Test - public void insertDuplicateTest() throws Exception { - - DB db = getDb(); - - BasicDBObject doc = new BasicDBObject(); - - doc.put( "username", "insertduplicate" ); - - WriteResult result = db.getCollection( "users" ).insert( doc ); - - - assertNull( result.getError() ); - - // check we've created the collection - - Set<String> colls = db.getCollectionNames(); - - assertTrue( colls.contains( "users" ) ); - - // iterate the collection to ensure we can retrieve the object - doc = new BasicDBObject(); - - doc.put( "username", "insertduplicate" ); - - - String message = null; - - try { - result = db.getCollection( "users" ).insert( doc ); - } - catch ( MongoException me ) { - message = me.getMessage(); - } - - assertNotNull( message ); - assertTrue( message.contains( - "Entity users requires that property named username be unique, value of insertduplicate exists" ) ); - } - - - @Test - public void updateTest() throws Exception { - - DB db = getDb(); - - BasicDBObject doc = new BasicDBObject(); - - doc.put( "name", "nico" ); - doc.put( "color", "tabby" ); - - WriteResult result = db.getCollection( "updatetests" ).insert( doc ); - - ObjectId savedOid = doc.getObjectId( "_id" ); - - assertNull( result.getError() ); - - // check we've created the collection - Set<String> colls = db.getCollectionNames(); - - assertTrue( colls.contains( "updatetests" ) ); - - // iterate the collection to ensure we can retrieve the object - DBCollection coll = db.getCollection( "updatetests" ); - DBCursor cur = coll.find(); - - BasicDBObject returnedObject = null; - - assertTrue( cur.hasNext() ); - - returnedObject = ( BasicDBObject ) cur.next(); - - assertFalse( cur.hasNext() ); - - UUID id = UUID.fromString( returnedObject.get( "uuid" ).toString() ); - - //this should work. Appears to be the type of ObjectId getting lost on column serialization - ObjectId returnedOid = new ObjectId( returnedObject.getString( "_id" ) ); - - assertEquals( "nico", returnedObject.get( "name" ) ); - assertEquals( "tabby", returnedObject.get( "color" ) ); - assertEquals( savedOid, returnedOid ); - assertNotNull( id ); - - BasicDBObject query = new BasicDBObject(); - query.put( "_id", savedOid ); - - // now load by the mongo Id. Users will use this the most to read data. - - returnedObject = new BasicDBObject( db.getCollection( "updatetests" ).findOne( query ).toMap() ); - - assertEquals( "nico", returnedObject.get( "name" ) ); - assertEquals( "tabby", returnedObject.get( "color" ) ); - assertEquals( savedOid, new ObjectId( returnedObject.getString( "_id" ) ) ); - assertEquals( id.toString(), returnedObject.get( "uuid" ) ); - - //now update the object and save it - BasicDBObject object = new BasicDBObject(); - object.put( "newprop", "newvalue" ); - object.put( "color", "black" ); - - db.getCollection( "updatetests" ).update( query, object ); - - // check we can find it when using the native entity manager - - Thread.sleep( 5000 ); - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Entity entity = em.get( new SimpleEntityRef( (String)returnedObject.get("type"), id ) ); - - assertNotNull( entity ); - assertEquals( "nico", entity.getProperty( "name" ) ); - assertEquals( "black", entity.getProperty( "color" ) ); - assertEquals( "newvalue", entity.getProperty( "newprop" ) ); - - - //now check it in the client - returnedObject = new BasicDBObject( db.getCollection( "updatetests" ).findOne( query ).toMap() ); - - assertEquals( "nico", returnedObject.get( "name" ) ); - assertEquals( "black", returnedObject.get( "color" ) ); - assertEquals( "newvalue", returnedObject.get( "newprop" ) ); - assertEquals( savedOid, new ObjectId( returnedObject.getString( "_id" ) ) ); - assertEquals( id.toString(), returnedObject.get( "uuid" ) ); - } - - - @Test - public void deleteTest() throws Exception { - - DB db = getDb(); - - BasicDBObject doc = new BasicDBObject(); - - doc.put( "name", "nico" ); - doc.put( "color", "tabby" ); - - WriteResult result = db.getCollection( "deletetests" ).insert( doc ); - - ObjectId savedOid = doc.getObjectId( "_id" ); - - assertNull( result.getError() ); - - BasicDBObject query = new BasicDBObject(); - query.put( "_id", savedOid ); - - // now load by the mongo Id. Users will use this the most to read data. - - BasicDBObject returnedObject = new BasicDBObject( db.getCollection( "deletetests" ).findOne( query ).toMap() ); - - assertEquals( "nico", returnedObject.get( "name" ) ); - assertEquals( "tabby", returnedObject.get( "color" ) ); - - // TODO uncomment me assertEquals(savedOid, - // returnedObject.getObjectId("_id")); - - UUID id = UUID.fromString( returnedObject.get( "uuid" ).toString() ); - - // now delete the object - db.getCollection( "deletetests" ).remove( returnedObject, WriteConcern.SAFE ); - - DBObject searched = db.getCollection( "deletetests" ).findOne( query ); - - assertNull( searched ); - - // check it has been deleted - - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Entity entity = em.get( new SimpleEntityRef( (String)returnedObject.get("type"), id ) ); - - assertNull( entity ); - } - - - @Test - @Ignore("Really slow on the delete, not a good unit tests atm") - public void deleteBatchTest() throws Exception { - - DB db = getDb(); - - int count = ( int ) ( OpDelete.BATCH_SIZE * 1.5 ); - - List<DBObject> docs = new ArrayList<DBObject>( count ); - - for ( int i = 0; i < count; i++ ) { - BasicDBObject doc = new BasicDBObject(); - - doc.put( "index", i ); - - docs.add( doc ); - } - - - WriteResult result = db.getCollection( "deletebatchtests" ).insert( docs ); - - assertNull( result.getLastError().getErrorMessage() ); - - //iterate over all the data to make sure it's been inserted - - DBCursor cursor = db.getCollection( "deletebatchtests" ).find(); - - for ( int i = 0; i < count && cursor.hasNext(); i++ ) { - int index = new BasicDBObject( cursor.next().toMap() ).getInt( "index" ); - - assertEquals( i, index ); - } - - - BasicDBObject query = new BasicDBObject(); - query.put( "index", new BasicDBObject( "$lte", count ) ); - - // now delete the objects - db.getCollection( "deletebatchtests" ).remove( query, WriteConcern.SAFE ); - - //now try and iterate, there should be no results - cursor = db.getCollection( "deletebatchtests" ).find(); - - assertFalse( cursor.hasNext() ); - - // check it has been deleted - UUID appId = emf.lookupApplication( "test-organization/test-app" ); - EntityManager em = emf.getEntityManager( appId ); - - Results results = - em.searchCollection( new SimpleEntityRef( "application", appId ), "deletebatchtests", new Query() ); - - assertEquals( 0, results.size() ); - } -}
