Search in sources :

Example 6 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class IndexOptions method unmarshall.

public static IndexOptions unmarshall(BsonDocument requestDoc) throws BadValueException, TypesMismatchException, NoSuchKeyException {
    IndexVersion version = IndexVersion.V1;
    String name = null;
    String namespace = null;
    boolean background = false;
    boolean unique = false;
    boolean sparse = false;
    int expireAfterSeconds = 0;
    BsonDocument keyDoc = null;
    BsonDocument storageEngine = null;
    BsonDocumentBuilder otherBuilder = new BsonDocumentBuilder();
    for (Entry<?> entry : requestDoc) {
        String key = entry.getKey();
        switch(key) {
            case VERSION_FIELD_NAME:
                {
                    int vInt = BsonReaderTool.getNumeric(requestDoc, VERSION_FIELD).intValue();
                    try {
                        version = IndexVersion.fromInt(vInt);
                    } catch (IndexOutOfBoundsException ex) {
                        throw new BadValueException("Value " + vInt + " is not a valid version");
                    }
                    break;
                }
            case NAME_FIELD_NAME:
                {
                    name = BsonReaderTool.getString(entry);
                    break;
                }
            case NAMESPACE_FIELD_NAME:
                {
                    namespace = BsonReaderTool.getString(entry);
                    break;
                }
            case BACKGROUND_FIELD_NAME:
                {
                    background = BsonReaderTool.getBooleanOrNumeric(entry, BACKGROUND_FIELD);
                    break;
                }
            case UNIQUE_FIELD_NAME:
                {
                    unique = BsonReaderTool.getBooleanOrNumeric(entry, UNIQUE_FIELD);
                    break;
                }
            case SPARSE_FIELD_NAME:
                {
                    sparse = BsonReaderTool.getBooleanOrNumeric(entry, SPARSE_FIELD);
                    break;
                }
            case EXPIRE_AFTER_SECONDS_FIELD_NAME:
                {
                    expireAfterSeconds = BsonReaderTool.getNumeric(entry, EXPIRE_AFTER_SECONDS_FIELD.getFieldName()).intValue();
                    break;
                }
            case KEYS_FIELD_NAME:
                {
                    keyDoc = BsonReaderTool.getDocument(entry);
                    break;
                }
            case STORAGE_ENGINE_FIELD_NAME:
                {
                    storageEngine = BsonReaderTool.getDocument(entry);
                    break;
                }
            default:
                {
                    otherBuilder.appendUnsafe(key, entry.getValue());
                    break;
                }
        }
    }
    String db = null;
    String collection = null;
    if (namespace != null) {
        int dotIndex = namespace.indexOf('.');
        if (dotIndex < 1 || dotIndex > namespace.length() - 2) {
            throw new BadValueException("The not valid namespace " + namespace + " found");
        }
        db = namespace.substring(0, dotIndex);
        collection = namespace.substring(dotIndex + 1);
    }
    if (name == null) {
        throw new NoSuchKeyException(NAME_FIELD_NAME, "Indexes need names");
    }
    if (keyDoc == null) {
        throw new NoSuchKeyException(KEYS_FIELD_NAME, "Indexes need at least one key to index");
    }
    List<Key> keys = unmarshllKeys(keyDoc);
    return new IndexOptions(version, name, db, collection, background, unique, sparse, expireAfterSeconds, keys, storageEngine, otherBuilder.build());
}
Also used : BadValueException(com.eightkdata.mongowp.exceptions.BadValueException) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder) NoSuchKeyException(com.eightkdata.mongowp.exceptions.NoSuchKeyException) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument)

Example 7 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class DeleteCommand method marshallResult.

@Override
public BsonDocument marshallResult(Long result) throws MarshalException {
    if (result == null) {
        return null;
    }
    BsonDocumentBuilder builder = new BsonDocumentBuilder();
    builder.appendNumber(N_FIELD, result);
    return builder.build();
}
Also used : BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 8 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class ReplSetHeartbeatReplyMarshaller method marshall.

static BsonDocument marshall(ReplSetHeartbeatReply reply, boolean asV1) {
    BsonDocumentBuilder doc = new BsonDocumentBuilder();
    if (reply.isMismatch()) {
        doc.append(OK_FIELD, MongoConstants.KO).append(MISMATCH_FIELD, true);
        return doc.build();
    }
    if (reply.getErrorCode().isOk()) {
        doc.append(OK_FIELD, MongoConstants.OK);
    } else {
        doc.append(OK_FIELD, MongoConstants.KO);
        doc.append(ERROR_CODE_FIELD, reply.getErrorCode().getErrorCode());
        if (reply.getErrMsg().isPresent()) {
            doc.append(ERR_MSG_FIELD, reply.getErrMsg().get());
        }
    }
    reply.getTime().ifPresent(time -> doc.append(TIME_FIELD, time.getSeconds()));
    reply.getElectionTime().ifPresent(electionTime -> doc.append(ELECTION_TIME_FIELD, electionTime));
    reply.getConfig().ifPresent(config -> doc.append(CONFIG_FIELD, config.toBson()));
    reply.getElectable().ifPresent(electable -> doc.append(IS_ELECTABLE_FIELD, electable));
    reply.getIsReplSet().ifPresent(isRepSet -> doc.append(IS_REPL_SET_FIELD, isRepSet));
    doc.append(HAS_STATE_DISAGREEMENT_FIELD, reply.isStateDisagreement());
    reply.getState().ifPresent(state -> doc.append(MEMBER_STATE_FIELD, state.getId()));
    doc.append(CONFIG_VERSION_FIELD, reply.getConfigVersion());
    doc.append(HB_MESSAGE_FIELD, reply.getHbmsg());
    reply.getSetName().ifPresent(setName -> doc.append(REPL_SET_FIELD, setName));
    reply.getSyncingTo().ifPresent(syncingTo -> doc.append(SYNC_SOURCE_FIELD, syncingTo.toString()));
    reply.getHasData().ifPresent(hasData -> doc.append(HAS_DATA_FIELD, hasData));
    if (reply.getTerm() != -1) {
        doc.append(TERM_FIELD, reply.getTerm());
    }
    reply.getPrimaryId().ifPresent(primaryId -> doc.append(PRIMARY_ID_FIELD, primaryId));
    reply.getDurableOptime().ifPresent(durableOptime -> doc.append(DURABLE_OP_TIME_FIELD, durableOptime.toBson()));
    reply.getAppliedOpTime().ifPresent(applyied -> {
        if (asV1) {
            doc.append(APPLIED_OP_TIME_DOC_FIELD, applyied.toBson());
        } else {
            applyied.appendAsOldBson(doc, APPLIED_OP_TIME_FIELD_NAME);
        }
    });
    return doc.build();
}
Also used : BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 9 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class ReplicaSetConfig method toBson.

public BsonDocument toBson() {
    BsonDocumentBuilder result = new BsonDocumentBuilder();
    result.append(ID_FIELD, setName);
    result.append(VERSION_FIELD, version);
    BsonArrayBuilder membersList = new BsonArrayBuilder();
    for (MemberConfig member : members) {
        membersList.add(member.toBson());
    }
    result.append(MEMBERS_FIELD, membersList.build());
    BsonDocumentBuilder settingsBuilder = new BsonDocumentBuilder();
    settingsBuilder.append(CHAINING_ALLOWED_FIELD, chainingAllowed);
    settingsBuilder.append(HEARTHBEAT_TIMEOUT_FIELD, heartbeatTimeoutPeriod);
    BsonDocumentBuilder customWrites = new BsonDocumentBuilder();
    for (Map.Entry<String, ReplicaSetTagPattern> entry : customWriteConcern.entrySet()) {
        String customWriteName = entry.getKey();
        if (customWriteName.startsWith("$")) {
            //MongoDB uses $ as an internal mode
            continue;
        }
        BsonDocument tagMap = entry.getValue().toBson();
        customWrites.appendUnsafe(customWriteName, tagMap);
    }
    settingsBuilder.append(GET_LAST_ERROR_MODES_FIELD, customWrites);
    settingsBuilder.append(GET_LAST_ERROR_DEFAULTS_FIELD, defaultWriteConcern.toDocument());
    settingsBuilder.append(PROTOCOL_VERSION_FIELD, protocolVersion);
    result.append(SETTINGS_FIELD, settingsBuilder);
    return result.build();
}
Also used : BsonArrayBuilder(com.eightkdata.mongowp.utils.BsonArrayBuilder) BsonDocument(com.eightkdata.mongowp.bson.BsonDocument) HashMap(java.util.HashMap) Map(java.util.Map) BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Example 10 with BsonDocumentBuilder

use of com.eightkdata.mongowp.utils.BsonDocumentBuilder in project torodb by torodb.

the class WriteError method marshall.

public BsonValue<?> marshall() {
    BsonDocumentBuilder bsonWriteError = new BsonDocumentBuilder();
    bsonWriteError.append(INDEX_FIELD, index);
    bsonWriteError.append(CODE_FIELD, code);
    bsonWriteError.append(ERR_MSG_FIELD, errMsg);
    return bsonWriteError.build();
}
Also used : BsonDocumentBuilder(com.eightkdata.mongowp.utils.BsonDocumentBuilder)

Aggregations

BsonDocumentBuilder (com.eightkdata.mongowp.utils.BsonDocumentBuilder)13 BsonDocument (com.eightkdata.mongowp.bson.BsonDocument)3 MongoException (com.eightkdata.mongowp.exceptions.MongoException)2 BsonArrayBuilder (com.eightkdata.mongowp.utils.BsonArrayBuilder)2 WriteMongodTransaction (com.torodb.mongodb.core.WriteMongodTransaction)2 Map (java.util.Map)2 BsonInt32 (com.eightkdata.mongowp.bson.BsonInt32)1 BsonValue (com.eightkdata.mongowp.bson.BsonValue)1 MongoBsonTranslator (com.eightkdata.mongowp.bson.org.bson.utils.MongoBsonTranslator)1 DefaultBsonValues (com.eightkdata.mongowp.bson.utils.DefaultBsonValues)1 DefaultBsonValues.newLong (com.eightkdata.mongowp.bson.utils.DefaultBsonValues.newLong)1 BadValueException (com.eightkdata.mongowp.exceptions.BadValueException)1 NoSuchKeyException (com.eightkdata.mongowp.exceptions.NoSuchKeyException)1 Request (com.eightkdata.mongowp.server.api.Request)1 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)1 Charsets (com.google.common.base.Charsets)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Truth (com.google.common.truth.Truth)1 UserException (com.torodb.core.exceptions.user.UserException)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1