@Nonnull public static OpTime fromBson(BsonDocument doc) throws TypesMismatchException, NoSuchKeyException { BsonTimestamp ts = BsonReaderTool.getTimestamp(doc, TIMESTAMP_FIELD); //TODO(gortiz): check precision lost long term = BsonReaderTool.getNumeric(doc, TERM_FIELD) .getValue() .longValue(); return new OpTime(ts, term); }
public static boolean getBooleanOrNumeric(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isNumber()) { return object.asNumber().intValue() != 0; } if (object.isBoolean()) { return object.asBoolean().getPrimitiveValue(); } throw new TypesMismatchException( fieldId, BsonType.BOOLEAN, object.getType(), "Expected boolean or numeric type for field " + fieldId + ". Found " + object.getType().toString().toLowerCase(Locale.ROOT) ); }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return diff; } assert obj instanceof BsonNumber; BsonNumber<?> other = obj.asNumber(); return Doubles.compare(this.doubleValue(), other.doubleValue()); }
public static long getLong(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (!object.isNumber()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected long type for field " + fieldId + ". Found" + foundType ); } return object.asNumber().longValue(); }
@Nonnull public static OpTime fromOplogEntry(BsonDocument doc) throws TypesMismatchException, NoSuchKeyException { BsonTimestamp ts = BsonReaderTool.getTimestamp(doc, TIMESTAMP_FIELD); //TODO(gortiz): check precision lost long term = BsonReaderTool.getNumeric(doc, TERM_FIELD, UNINITIALIZED_TERM_BSON) .getValue() .longValue(); return new OpTime(ts, term); }