Example usage for io.netty.util AttributeMap attr

List of usage examples for io.netty.util AttributeMap attr

Introduction

In this page you can find the example usage for io.netty.util AttributeMap attr.

Prototype

<T> Attribute<T> attr(AttributeKey<T> key);

Source Link

Document

Get the Attribute for the given AttributeKey .

Usage

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java

License:Open Source License

@Override
public void listDatabases(MessageReplier messageReplier)
        throws ExecutionException, InterruptedException, ImplementationDbException {
    AttributeMap attributeMap = messageReplier.getAttributeMap();
    ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get();

    ToroTransaction transaction = connection.createTransaction();

    try {/*from   www. jav a 2  s  .  c o  m*/
        List<? extends Database> databases = transaction.getDatabases().get();

        double totalSize = 0;
        List<BSONObject> databaseDocs = Lists.newArrayListWithCapacity(databases.size());
        for (Database database : databases) {
            BSONObject databaseDoc = new BasicBSONObject();
            databaseDoc.put("name", database.getName());
            databaseDoc.put("sizeOnDisk", Long.valueOf(database.getSize()).doubleValue());
            //TODO: This is not true, but...
            databaseDoc.put("empty", database.getSize() == 0);

            totalSize += database.getSize();
            databaseDocs.add(databaseDoc);
        }
        BSONObject response = new BasicBSONObject(3);

        response.put("databases", databaseDocs);
        response.put("totalSize", totalSize);
        response.put("ok", MongoWP.OK);

        BSONDocument document = new MongoBSONDocument(response.toMap());
        messageReplier.replyMessageNoCursor(document);
    } finally {
        transaction.close();
    }
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java

License:Open Source License

@Override
public CollStatsReply collStats(CollStatsRequest request) throws Exception {
    CollStatsReply.Builder replyBuilder = new CollStatsReply.Builder(request.getDatabase(),
            request.getCollection());/* w  w  w  .  j a va2 s  . c om*/
    replyBuilder.setCapped(false).setLastExtentSize(1).setNumExtents(1).setPaddingFactor(0)
            .setScale(request.getScale().intValue()).setUsePowerOf2Sizes(false);

    AttributeMap attributeMap = request.getAttributes();
    ToroConnection connection = attributeMap.attr(ToroRequestProcessor.CONNECTION).get();

    ToroTransaction transaction = connection.createTransaction();
    int scale = replyBuilder.getScale();
    try {
        Collection<? extends NamedToroIndex> indexes = transaction.getIndexes(request.getCollection());
        Map<String, Long> sizeByMap = Maps.newHashMapWithExpectedSize(indexes.size());
        for (NamedToroIndex index : indexes) {
            Long indexSize = transaction.getIndexSize(request.getCollection(), index.getName()).get();
            sizeByMap.put(index.getName(), indexSize / scale);
        }

        replyBuilder.setSizeByIndex(sizeByMap);

        replyBuilder.setIdIndexExists(sizeByMap.containsKey("_id"));
        replyBuilder
                .setCount(transaction.count(request.getCollection(), TrueQueryCriteria.getInstance()).get());
        replyBuilder.setSize(transaction.getDocumentsSize(request.getCollection()).get() / scale);
        replyBuilder.setStorageSize(transaction.getCollectionSize(request.getCollection()).get() / scale);

        return replyBuilder.build();
    } finally {
        transaction.close();
    }
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroQueryCommandProcessor.java

License:Open Source License

@Override
public void unimplemented(QueryCommand userCommand, MessageReplier messageReplier) throws Exception {
    //TODO: Map real mongo error codes      
    ErrorCode errorCode = ErrorCode.UNIMPLEMENTED_COMMAND;
    AttributeMap attributeMap = messageReplier.getAttributeMap();
    LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, userCommand, null, null, true, errorCode);
    attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError);
    messageReplier.replyQueryCommandFailure(errorCode, userCommand.getKey());
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
public void onChannelActive(AttributeMap attributeMap) {
    ToroConnection connection = torod.openConnection();
    attributeMap.attr(MessageReplier.CONNECTION_ID).set(connectionId.incrementAndGet());
    attributeMap.attr(CONNECTION).set(connection);
    attributeMap.attr(LAST_ERROR).set(new ToroLastError(RequestOpCode.RESERVED, null, null, null, false, null));
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
public void onChannelInactive(AttributeMap attributeMap) {
    ToroConnection connection = attributeMap.attr(CONNECTION).get();
    if (connection != null) {
        connection.close();//w w w.j av a 2s. co  m
        attributeMap.attr(CONNECTION).set(null);
    }
    attributeMap.attr(MessageReplier.CONNECTION_ID).set(null);
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
@SuppressFBWarnings("NP_LOAD_OF_KNOWN_NULL_VALUE")
public QueryReply query(QueryRequest request) throws Exception {
    AttributeMap attributeMap = request.getAttributes();
    ToroConnection connection = attributeMap.attr(CONNECTION).get();

    if (request.getProjection() != null) {
        throw new UserToroException("Projections are not supported");
    }/*from w w w .ja  v a2  s  . c o  m*/
    QueryCriteria queryCriteria;
    if (request.getQuery() == null) {
        queryCriteria = TrueQueryCriteria.getInstance();
    } else {
        queryCriteria = queryCriteriaTranslator.translate(request.getQuery());
    }
    Projection projection = null;

    UserCursor<ToroDocument> cursor;
    BSONDocuments results;

    if (request.isTailable()) {
        throw new UserToroException("TailableCursors are not supported");
    }

    if (request.getLimit() == 0) {
        cursor = connection.openUnlimitedCursor(request.getCollection(), queryCriteria, projection,
                request.getNumberToSkip(), request.isAutoclose(), !request.isNoCursorTimeout());
    } else {
        cursor = connection.openLimitedCursor(request.getCollection(), queryCriteria, projection,
                request.getNumberToSkip(), request.getLimit(), request.isAutoclose(),
                !request.isNoCursorTimeout());
    }

    results = new BSONDocuments(cursor.read(MongoWP.MONGO_CURSOR_LIMIT));

    long cursorIdReturned = 0;

    if (results.size() >= MongoWP.MONGO_CURSOR_LIMIT) {
        cursorIdReturned = cursor.getId().getNumericId();
    } else {
        cursor.close();
    }

    return new QueryReply.Builder().setCursorId(cursorIdReturned).setStartingFrom(0).setDocuments(results)
            .build();
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
public void noSuchCommand(BSONDocument query, MessageReplier messageReplier) throws Exception {
    AttributeMap attributeMap = messageReplier.getAttributeMap();
    MongoWP.ErrorCode errorCode = MongoWP.ErrorCode.NO_SUCH_COMMAND;
    LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, null, null, null, true, errorCode);
    attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError);
    messageReplier.replyQueryCommandFailure(errorCode,
            query.getKeys().iterator().hasNext() ? query.getKeys().iterator().next() : "");
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
public void adminOnlyCommand(QueryCommand queryCommand, MessageReplier messageReplier) throws Exception {
    AttributeMap attributeMap = messageReplier.getAttributeMap();
    MongoWP.ErrorCode errorCode = MongoWP.ErrorCode.MUST_RUN_ON_ADMIN;
    LastError lastError = new ToroLastError(RequestOpCode.OP_QUERY, queryCommand, null, null, true, errorCode);
    attributeMap.attr(ToroRequestProcessor.LAST_ERROR).set(lastError);
    messageReplier.replyQueryCommandFailure(errorCode, queryCommand.getKey());
}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
public void getMore(GetMoreMessage getMoreMessage, MessageReplier messageReplier) throws Exception {
    AttributeMap attributeMap = messageReplier.getAttributeMap();
    ToroConnection connection = attributeMap.attr(CONNECTION).get();

    CursorId cursorId = new CursorId(getMoreMessage.getCursorId());

    try {//from  w  w w  .jav a 2s.  com
        UserCursor cursor = connection.getCursor(cursorId);

        BSONDocuments results = new BSONDocuments(cursor.read(MongoWP.MONGO_CURSOR_LIMIT));

        boolean cursorEmptied = results.size() < MongoWP.MONGO_CURSOR_LIMIT;

        Integer position = cursor.getPosition();
        if (cursorEmptied) {
            cursor.close();
        }

        messageReplier.replyMessageMultipleDocumentsWithFlags(cursorEmptied ? 0 : cursorId.getNumericId(), // Signal "don't read more from cursor" if emptied
                position, results, EnumSet.noneOf(ReplyMessage.Flag.class));
    } catch (CursorNotFoundException ex) {
        throw ex; //TODO: change that to translate the exception to whatever mongo protocol used to notify a closed or no existing cursor
    }

}

From source file:com.torodb.mongowp.mongoserver.api.toro.ToroRequestProcessor.java

License:Open Source License

@Override
public void killCursors(KillCursorsMessage killCursorsMessage, MessageReplier messageReplier) throws Exception {
    AttributeMap attributeMap = messageReplier.getAttributeMap();
    ToroConnection connection = attributeMap.attr(CONNECTION).get();

    if (connection == null) {
        return;//www.  j a v  a  2  s  . com
    }

    int numberOfCursors = killCursorsMessage.getNumberOfCursors();
    long[] cursorIds = killCursorsMessage.getCursorIds();
    for (int index = 0; index < numberOfCursors; index++) {
        CursorId cursorId = new CursorId(cursorIds[index]);

        try {
            connection.getCursor(cursorId).close();
        } catch (CursorNotFoundException ex) {
        }
    }
}