use of com.eightkdata.mongowp.exceptions.BadValueException in project torodb by torodb.
the class AbstractMongoOplogReader method getFirstOrLastOp.
private OplogOperation getFirstOrLastOp(boolean first) throws OplogStartMissingException, OplogOperationUnsupported, MongoException {
Preconditions.checkState(!isClosed(), "You have to connect this client before");
BsonDocument query = DefaultBsonValues.EMPTY_DOC;
BsonDocument orderBy = first ? NATURAL_ORDER_SORT : INVERSE_ORDER_SORT;
EnumSet<QueryOption> flags = EnumSet.of(QueryOption.SLAVE_OK);
BsonDocument doc;
MongoConnection connection = consumeConnection();
try {
MongoCursor<BsonDocument> cursor = connection.query(DATABASE, COLLECTION, query, 0, 1, new QueryOptions(flags), orderBy, null);
try {
Batch<BsonDocument> batch = cursor.fetchBatch();
try {
if (!batch.hasNext()) {
throw new OplogStartMissingException(getSyncSource());
}
doc = batch.next();
} finally {
batch.close();
}
} finally {
cursor.close();
}
try {
return OplogOperationParser.fromBson(doc);
} catch (BadValueException | TypesMismatchException | NoSuchKeyException ex) {
throw new OplogOperationUnsupported(doc, ex);
}
} finally {
releaseConnection(connection);
}
}
use of com.eightkdata.mongowp.exceptions.BadValueException in project torodb by torodb.
the class CollectionOptions method unmarshal.
public static CollectionOptions unmarshal(BsonDocument doc) throws BadValueException {
boolean capped;
try {
capped = BsonReaderTool.getBooleanOrNumeric(doc, CAPPED_FIELD, false);
} catch (TypesMismatchException ex) {
capped = true;
}
long cappedSize;
try {
cappedSize = BsonReaderTool.getLong(doc, CAPPED_SIZE_FIELD, 0);
if (cappedSize < 0) {
throw new BadValueException("size has to be >= 0");
}
} catch (TypesMismatchException ex) {
//backward compatibility
cappedSize = 0;
}
long cappedMaxDocs;
try {
cappedMaxDocs = BsonReaderTool.getLong(doc, CAPPED_MAX_DOCS_FIELD, 0);
if (cappedMaxDocs < 0) {
throw new BadValueException("max has to be >= 0");
}
} catch (TypesMismatchException ex) {
//backward compatibility
cappedMaxDocs = 0;
}
final ImmutableList.Builder<Long> initialExtentSizes = ImmutableList.builder();
Long initialNumExtends;
BsonArray array;
try {
array = BsonReaderTool.getArray(doc, INITIAL_EXTENT_SIZES_FIELD, null);
if (array == null) {
initialNumExtends = null;
} else {
initialNumExtends = null;
for (int i = 0; i < array.size(); i++) {
BsonValue element = array.get(i);
if (!element.isNumber()) {
throw new BadValueException("'$nExtents'.'" + i + "' has " + "the value " + element.toString() + " which is " + "not a number");
}
initialExtentSizes.add(element.asNumber().longValue());
}
}
} catch (TypesMismatchException ex) {
try {
initialNumExtends = BsonReaderTool.getLong(doc, INITIAL_NUM_EXTENDS_FIELD);
} catch (NoSuchKeyException | TypesMismatchException ex1) {
initialNumExtends = null;
}
}
AutoIndexMode autoIndexMode;
try {
if (BsonReaderTool.getBoolean(doc, AUTO_INDEX_MODE_FIELD)) {
autoIndexMode = AutoIndexMode.YES;
} else {
autoIndexMode = AutoIndexMode.NO;
}
} catch (NoSuchKeyException | TypesMismatchException ex) {
autoIndexMode = AutoIndexMode.DEFAULT;
}
EnumSet<Flag> flags = EnumSet.noneOf(Flag.class);
try {
int flagInt = BsonReaderTool.getInteger(doc, FLAGS_FIELD, 0);
for (Flag value : Flag.values()) {
if ((flagInt & value.bit) != 0) {
flags.add(value);
}
}
} catch (TypesMismatchException ignore) {
//Nothing to do here
}
BsonDocument storageEngine;
try {
storageEngine = BsonReaderTool.getDocument(doc, STORAGE_ENGINE_FIELD, null);
} catch (TypesMismatchException ex) {
throw new BadValueException("'storageEngine' has to be a document.");
}
if (storageEngine != null) {
if (storageEngine.isEmpty()) {
throw new BadValueException("Empty 'storageEngine' options are invalid. " + "Please remove, or include valid options");
}
for (Entry<?> entry : storageEngine) {
if (!entry.getValue().isDocument()) {
throw new BadValueException("'storageEngie'.'" + entry.getKey() + "' has to be an embedded document");
}
}
}
boolean temp;
try {
temp = BsonReaderTool.getBoolean(doc, TEMP_FIELD, false);
} catch (TypesMismatchException ex) {
throw new BadValueException("Temp field must be a boolean");
}
return new DefaultCollectionOptions(capped, cappedSize, cappedMaxDocs, initialNumExtends, initialExtentSizes.build(), autoIndexMode, flags, storageEngine, temp);
}
use of com.eightkdata.mongowp.exceptions.BadValueException in project torodb by torodb.
the class OplogOperationParser method fromBson.
public static OplogOperation fromBson(@Nonnull BsonValue uncastedOp) throws BadValueException, TypesMismatchException, NoSuchKeyException {
if (!uncastedOp.isDocument()) {
throw new BadValueException("found a " + uncastedOp.getType().toString().toLowerCase(Locale.ROOT) + " where a document that represents a oplog operation " + "was expected");
}
BsonDocument doc = uncastedOp.asDocument();
OplogOperationType opType;
String opString = BsonReaderTool.getString(doc, "op");
try {
opType = OplogOperationType.fromOplogName(opString);
} catch (IllegalArgumentException ex) {
throw new BadValueException("Unknown oplog operation with type '" + opString + "'");
}
String ns;
try {
ns = BsonReaderTool.getString(doc, "ns");
} catch (NoSuchKeyException ex) {
throw new NoSuchKeyException("ns", "op does not contain required \"ns\" field: " + uncastedOp);
} catch (TypesMismatchException ex) {
throw ex.newWithMessage("\"ns\" field is not a string: " + uncastedOp);
}
if (ns.isEmpty() && !opType.equals(OplogOperationType.NOOP)) {
throw new BadValueException("\"ns\" field value cannot be empty " + "when op type is not 'n': " + doc);
}
String db;
String collection;
int firstDotIndex = ns.indexOf('.');
if (firstDotIndex == -1 || firstDotIndex + 1 == ns.length()) {
db = ns;
collection = null;
} else {
db = ns.substring(0, firstDotIndex);
collection = ns.substring(firstDotIndex + 1);
}
OpTime optime = OpTime.fromOplogEntry(doc);
long h = BsonReaderTool.getLong(doc, "h");
OplogVersion version = OplogVersion.valueOf(BsonReaderTool.getInteger(doc, "v"));
//Note: Mongodb v3 checks if the key exists or not, but doesn't check the value
boolean fromMigrate = doc.containsKey("fromMigrate");
BsonDocument o = BsonReaderTool.getDocument(doc, "o");
switch(opType) {
case DB:
return new DbOplogOperation(db, optime, h, version, fromMigrate);
case DB_CMD:
return new DbCmdOplogOperation(o, db, optime, h, version, fromMigrate);
case DELETE:
return new DeleteOplogOperation(o, db, collection, optime, h, version, fromMigrate, BsonReaderTool.getBoolean(doc, "b", false));
case INSERT:
//TODO: parse b
return new InsertOplogOperation(o, db, collection, optime, h, version, fromMigrate);
case NOOP:
return new NoopOplogOperation(o, db, optime, h, version, fromMigrate);
case UPDATE:
return new UpdateOplogOperation(BsonReaderTool.getDocument(doc, "o2"), db, collection, optime, h, version, fromMigrate, o, BsonReaderTool.getBoolean(doc, "b", false));
default:
throw new AssertionError(OplogOperationParser.class + " is not prepared to work with oplog operations of type " + opType);
}
}
use of com.eightkdata.mongowp.exceptions.BadValueException 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());
}
use of com.eightkdata.mongowp.exceptions.BadValueException 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;
}
Aggregations