/** * Confirms that the given bson document only contains fields whose names given. * * @param objectName the container's name, that will be included in the error messages * @param bson the document whose fields are going to be checked * @param fields the fields we are going to accept * @throws BadValueException if an unexpected field is found */ public static void checkOnlyHasFields( String objectName, BsonDocument bson, Set<String> fields) throws BadValueException { for (Entry<?> entry : bson) { if (!fields.contains(entry.getKey())) { throw new BadValueException( "Unexpected field " + entry.getKey() + " in " + objectName ); } } }
@Override public int compareTo(BsonValue<?> obj) { if (obj == this) { return 0; } int diff = BsonTypeComparator.INSTANCE.compare(getType(), obj.getType()); if (diff != 0) { return 0; } assert obj.isDocument(); BsonDocument other = obj.asDocument(); // TODO: Check how MongoDB compares documents! diff = this.size() - other.size(); if (diff != 0) { return diff; } Iterator<Entry<?>> myIt = this.iterator(); Iterator<Entry<?>> otherIt = other.iterator(); while (myIt.hasNext() && otherIt.hasNext()) { diff = myIt.next().getValue().compareTo(otherIt.next().getValue()); if (diff != 0) { return diff; } } assert !myIt.hasNext() : "the other document has more entries than ourself!"; assert !otherIt.hasNext() : "the other document has less entries than ourself!"; return 0; }
throw new CommandNotFoundException("Empty document query"); String firstKey = document.iterator().next().getKey(); throw new CommandNotFoundException(firstKey);
switch (entry.getKey().toLowerCase(Locale.ENGLISH)) { case "$query": { messageBuilder.setQuery(getQuery(entry.getValue())); break; messageBuilder.setComment(getComment(entry.getValue())); break; messageBuilder.setExplainOption(getExplain(entry.getValue())); break; messageBuilder.setHint(getHint(entry.getValue())); break; messageBuilder.setMaxScan(getMaxScan(entry.getValue())); break; messageBuilder.setMaxTimeMs(getMaxTimeMs(entry.getValue())); break; messageBuilder.setMax(getMax(entry.getValue())); break; messageBuilder.setMin(getMin(entry.getValue())); break; messageBuilder.setOrderBy(getOrderBy(entry.getValue())); break;
@Override public Void visit(BsonDocument value, ByteBuf arg) { final int docStart = arg.writerIndex(); arg.writeInt(0); // reserve space for doc size for (Entry<?> entry : value) { BsonValue<?> child = entry.getValue(); try { arg.writeByte(ParsingTools.getByte(child.getType())); } catch (NettyBsonReaderException ex) { throw new AssertionError(ex); } writeCString(arg, entry.getKey()); child.accept(this, arg); } arg.writeByte(0x00); int docEnd = arg.writerIndex(); arg.writerIndex(docStart).writeInt(docEnd - docStart).writerIndex(docEnd); return null; }
public static boolean isPseudoTrue(Entry<?> entry) { if (entry == null) { return false; } BsonValue<?> value = entry.getValue(); switch (value.getType()) { case INT64: return !value.equals(DefaultBsonValues.newLong(0)); case DOUBLE: return !value.equals(DefaultBsonValues.newDouble(0)); case INT32: return !value.equals(DefaultBsonValues.newInt(0)); case BOOLEAN: return value.asBoolean().getPrimitiveValue(); case UNDEFINED: return false; default: return true; } }
public static boolean getBooleanOrUndefined(Entry<?> entry, String fieldId, boolean defaultValue) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isUndefined()) { return defaultValue; } if (object.isBoolean()) { return object.asBoolean().getPrimitiveValue(); } throw new TypesMismatchException( fieldId, BsonType.BOOLEAN, object.getType(), "Expected boolean or undefined type for field " + fieldId + ". Found " + object.getType().toString().toLowerCase(Locale.ROOT) ); }
@Nonnull public A unmarshallArg(@Nonnull BsonDocument requestDoc, String aliasedAs) throws MongoException;
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 BsonDocument readDocument(@Loose @ModifiesIndexes ByteBuf byteBuf) throws NettyBsonReaderException { int length = byteBuf.readInt(); int significantLenght = length - 4 - 1; ByteBuf significantSlice = byteBuf.readSlice(significantLenght); byte b = byteBuf.readByte(); assert b == 0x00; LinkedHashMap<String, BsonValue<?>> values = new LinkedHashMap<>(); while (significantSlice.readableBytes() > 0) { Entry<?> entry = readDocumentEntry(significantSlice); values.put(entry.getKey(), entry.getValue()); } return new MapBasedBsonDocument(values); }
public Optional<String> getCommandName() { return Optional.ofNullable(getRequest().getFirstEntry().getKey()); }
@Override public String toString() { StringBuilder sb = new StringBuilder(size() * 20); sb.append('{'); for (Entry<?> entry : this) { sb.append(entry.getKey()).append(": ").append(entry.getValue()); sb.append(", "); } if (!isEmpty()) { sb.delete(sb.length() - 2, sb.length()); } sb.append('}'); return sb.toString(); }
@Nonnull public static BsonNumber<?> getNumeric(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isNumber()) { String foundType = object.getType().toString().toLowerCase(Locale.ROOT); throw new TypesMismatchException( fieldId, "numeric", object.getType(), fieldId + " field has non-numeric type " + foundType ); } return object.asNumber(); }
@Override public LibraryEntry find(BsonDocument requestDocument) { if (requestDocument.isEmpty()) { return null; } String commandAlias = requestDocument.getFirstEntry().getKey(); String key = commandAlias.toLowerCase(Locale.ENGLISH); Command<?, ?> command = commandsMap.get(key); if (command == null) { return null; } return new PojoLibraryEntry(commandAlias, command); }
@Override public String getKey() { return entry.getKey(); }
@Nonnull public static Instant getInstant(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> value = entry.getValue(); if (!value.isDateTime()) { throw new TypesMismatchException( fieldId, "date", value.getType(), "Expected date type for field " + fieldId + ". Found" + value ); } return value.asDateTime().getValue(); }
@Override public BsonValue<?> get(String key) { Entry<?> entry = getEntry(key); if (entry == null) { return null; } return entry.getValue(); }
public static boolean getBoolean(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue object = entry.getValue(); if (object.isBoolean()) { return object.asBoolean().getValue(); } String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "boolean", object.getType(), "Expected boolean type for field " + fieldId + ". Found " + foundType ); }
public static int getInteger(Entry<?> entry, String fieldId) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isInt32()) { String foundType = toStringBsonType(object.getType()); throw new TypesMismatchException( fieldId, "integer", object.getType(), "Expected integer type for field " + fieldId + ". Found" + foundType ); } return object.asInt32().intValue(); }