@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(); }
public BsonDocumentBuilder copy(@Nonnull BsonDocument otherDoc) { Preconditions.checkState(!built); for (Entry<?> entrySet : otherDoc) { map.put(entrySet.getKey(), entrySet.getValue()); } return this; }
@Override public Entry<?> getEntry(String key) { for (Entry<?> entry : this) { if (entry.getKey().equals(key)) { return entry; } } return null; }
BsonDocument result = new org.bson.BsonDocument(); for (Entry<?> entry : casted) { result.append(entry.getKey(), translatePrivate(entry.getValue()));
throw new CommandNotFoundException("Empty document query"); String firstKey = document.iterator().next().getKey(); throw new CommandNotFoundException(firstKey);
/** * 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 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; }
@Nonnull public A unmarshallArg(@Nonnull BsonDocument requestDoc, String aliasedAs) throws MongoException;
@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 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(); }
public BsonDocumentBuilder(BsonDocument doc) { this.map = new LinkedHashMap<>(doc.size()); for (Entry<?> entry : doc) { map.put(entry.getKey(), entry.getValue()); } built = false; }
public static BsonTimestamp getTimestampFromDateTime(Entry<?> entry) throws TypesMismatchException { BsonValue<?> value = entry.getValue(); if (!value.isDateTime()) { throw new TypesMismatchException(entry.getKey(), BsonType.DATETIME, value.getType()); } return TimestampToDateTime.toTimestamp( value.asDateTime(), DefaultBsonValues::newTimestamp); } }
public static KVDocument toEagerDocument(BsonDocument doc) { LinkedHashMap<String, KVValue<?>> map = new LinkedHashMap<>(doc.size()); for (Entry<?> entry : doc) { map.put(entry.getKey(), MongoWPConverter.translate(entry.getValue())); } return new MapKVDocument(map); }
@Nonnull public static BsonDocument getDocument(Entry<?> entry) throws TypesMismatchException { BsonValue<?> object = entry.getValue(); if (!object.isDocument()) { throw new TypesMismatchException(entry.getKey(), BsonType.DOCUMENT, object.getType()); } return object.asDocument(); }
public static final BsonDocument newDocument(@NotMutable List<Entry<?>> list) { switch (list.size()) { case 0: return EMPTY_DOC; case 1: Entry<?> entry = list.get(0); return new SingleEntryBsonDocument(entry.getKey(), entry.getValue()); default: return new ListBasedBsonDocument(list); } }
@Nonnull public static String getString(Entry<?> entry) throws TypesMismatchException { BsonValue object = entry.getValue(); if (!object.isString()) { throw new TypesMismatchException(entry.getKey(), "string", object.getType()); } return object.asString().getValue(); }