use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class MemberConfig method fromDocument.
public static MemberConfig fromDocument(BsonDocument bson) throws TypesMismatchException, NoSuchKeyException, BadValueException {
int id = BsonReaderTool.getNumeric(bson, "_id").intValue();
HostAndPort host = BsonReaderTool.getHostAndPort(bson, "host");
Builder builder = new Builder(id, host).setVotes(BsonReaderTool.getInteger(bson, "votes", DEFAULT_VOTES)).setPriority(BsonReaderTool.getDouble(bson, "priority", DEFAULT_PRIORITY)).setArbiterOnly(BsonReaderTool.getBooleanOrNumeric(bson, "arbiterOnly", DEFAULT_ARBITER_ONLY)).setSlaveDelay(BsonReaderTool.getNumeric(bson, "slaveDelay", DEFAULT_SLAVE_DELAY).longValue()).setHidden(BsonReaderTool.getBooleanOrNumeric(bson, "hidden", DEFAULT_HIDDEN)).setBuildIndexes(BsonReaderTool.getBooleanOrNumeric(bson, "buildIndexes", DEFAULT_BUILD_INDEXES));
BsonDocument castedTags = BsonReaderTool.getDocument(bson, "tags");
for (Entry entry : castedTags) {
BsonValue value = entry.getValue();
if (value.isString()) {
throw new TypesMismatchException(entry.getKey(), "string", value.getType());
}
String castedValue = value.asString().getValue();
builder.putTag(entry.getKey(), castedValue);
}
return builder.build();
}
use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class ReplicaSetConfig method parseCustomWriteConcerns.
private static Map<String, ReplicaSetTagPattern> parseCustomWriteConcerns(BsonDocument bson) throws TypesMismatchException, NoSuchKeyException, BadValueException {
Map<String, ReplicaSetTagPattern> map = new HashMap<>(bson.size());
for (Entry<?> customWriteNameEntry : bson) {
BsonDocument constraintDoc = BsonReaderTool.getDocument(bson, customWriteNameEntry.getKey());
Map<String, Integer> constraintMap = new HashMap<>(constraintDoc.size());
for (Entry<?> tagEntry : constraintDoc) {
int intValue;
try {
intValue = tagEntry.getValue().asNumber().intValue();
} catch (UnsupportedOperationException ex) {
String fieldName = SETTINGS_FIELD.getFieldName() + '.' + GET_LAST_ERROR_MODES_FIELD.getFieldName() + '.' + customWriteNameEntry + '.' + constraintDoc;
BsonType tagType = tagEntry.getValue().getType();
throw new TypesMismatchException(fieldName, "number", tagType, "Expected " + fieldName + " to be a number, not " + tagType.toString().toLowerCase(Locale.ROOT));
}
if (intValue <= 0) {
String fieldName = SETTINGS_FIELD.getFieldName() + '.' + GET_LAST_ERROR_MODES_FIELD.getFieldName() + '.' + customWriteNameEntry + '.' + constraintDoc;
throw new BadValueException("Value of " + fieldName + " must be positive, but found " + intValue);
}
constraintMap.put(tagEntry.getKey(), intValue);
}
map.put(customWriteNameEntry.getKey(), new ReplicaSetTagPattern(constraintMap));
}
return map;
}
use of com.eightkdata.mongowp.bson.BsonDocument 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();
}
use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class CollectionOptions method marshall.
public void marshall(BsonDocumentBuilder builder) {
if (isCapped()) {
builder.append(CAPPED_FIELD, true);
long cappedSize = getCappedSize();
if (cappedSize != 0) {
builder.append(CAPPED_SIZE_FIELD, cappedSize);
}
long cappedMaxDocs = getCappedMaxDocs();
if (cappedMaxDocs != 0) {
builder.append(CAPPED_MAX_DOCS_FIELD, cappedMaxDocs);
}
}
Long initialNumExtents = getInitialNumExtents();
if (initialNumExtents != null && initialNumExtents != 0) {
builder.append(INITIAL_NUM_EXTENDS_FIELD, initialNumExtents);
}
List<Long> initialExtentSizes = getInitialExtentSizes();
if (initialExtentSizes != null && !initialExtentSizes.isEmpty()) {
BsonArrayBuilder arrBuilder = new BsonArrayBuilder(initialExtentSizes.size());
for (Long initialExtentSize : initialExtentSizes) {
arrBuilder.add(initialExtentSize);
}
builder.append(INITIAL_EXTENT_SIZES_FIELD, arrBuilder.build());
}
AutoIndexMode autoIndexMode = getAutoIndexMode();
if (!autoIndexMode.equals(AutoIndexMode.DEFAULT)) {
boolean value = autoIndexMode.equals(AutoIndexMode.YES);
builder.append(AUTO_INDEX_MODE_FIELD, value);
}
Set<Flag> flags = getFlags();
if (!flags.isEmpty()) {
int value = 0;
for (Flag flag : flags) {
value |= flag.getBit();
}
builder.append(FLAGS_FIELD, value);
}
BsonDocument storageEngine = getStorageEngine();
if (storageEngine != null && storageEngine.isEmpty()) {
builder.append(STORAGE_ENGINE_FIELD, storageEngine);
}
boolean temp = isTemp();
if (temp) {
builder.append(TEMP_FIELD, temp);
}
}
use of com.eightkdata.mongowp.bson.BsonDocument in project torodb by torodb.
the class AbstractMongoOplogReader method query.
public MongoCursor<OplogOperation> query(BsonDocument query, EnumSet<QueryOption> flags, BsonDocument sortBy) throws MongoException {
Preconditions.checkState(!isClosed(), "You have to connect this client before");
MongoConnection connection = consumeConnection();
MongoCursor<BsonDocument> cursor = connection.query(DATABASE, COLLECTION, query, 0, 0, new QueryOptions(flags), sortBy, null);
return new MyCursor<>(connection, TransformationMongoCursor.create(cursor, OplogOperationParser.asFunction()));
}
Aggregations