|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.beam.sdk.io.mongodb; |
| 19 | + |
| 20 | +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument; |
| 21 | + |
| 22 | +import com.google.auto.value.AutoValue; |
| 23 | +import com.mongodb.BasicDBObject; |
| 24 | +import com.mongodb.MongoClient; |
| 25 | +import com.mongodb.client.MongoCollection; |
| 26 | +import com.mongodb.client.MongoCursor; |
| 27 | +import com.mongodb.client.model.Projections; |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.List; |
| 30 | +import org.apache.beam.sdk.transforms.SerializableFunction; |
| 31 | +import org.bson.BsonDocument; |
| 32 | +import org.bson.Document; |
| 33 | +import org.bson.conversions.Bson; |
| 34 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 35 | +import org.checkerframework.dataflow.qual.Pure; |
| 36 | + |
| 37 | +/** Builds a MongoDB FindQueryTest object. */ |
| 38 | +@AutoValue |
| 39 | +public abstract class FindQueryTest |
| 40 | + implements SerializableFunction<MongoCollection<Document>, MongoCursor<Document>> { |
| 41 | + |
| 42 | + @Pure |
| 43 | + abstract @Nullable BsonDocument filters(); |
| 44 | + |
| 45 | + @Pure |
| 46 | + abstract int limit(); |
| 47 | + |
| 48 | + @Pure |
| 49 | + abstract List<String> projection(); |
| 50 | + |
| 51 | + private static Builder builder() { |
| 52 | + return new AutoValue_FindQueryTest.Builder() |
| 53 | + .setLimit(0) |
| 54 | + .setProjection(Collections.emptyList()) |
| 55 | + .setFilters(new BsonDocument()); |
| 56 | + } |
| 57 | + |
| 58 | + abstract Builder toBuilder(); |
| 59 | + |
| 60 | + public static FindQueryTest create() { |
| 61 | + return builder().build(); |
| 62 | + } |
| 63 | + |
| 64 | + @AutoValue.Builder |
| 65 | + abstract static class Builder { |
| 66 | + abstract Builder setFilters(@Nullable BsonDocument filters); |
| 67 | + |
| 68 | + abstract Builder setLimit(int limit); |
| 69 | + |
| 70 | + abstract Builder setProjection(List<String> projection); |
| 71 | + |
| 72 | + abstract FindQueryTest build(); |
| 73 | + } |
| 74 | + |
| 75 | + /** Sets the filters to find. */ |
| 76 | + private FindQueryTest withFilters(BsonDocument filters) { |
| 77 | + return toBuilder().setFilters(filters).build(); |
| 78 | + } |
| 79 | + |
| 80 | + /** Convert the Bson filters into a BsonDocument via default encoding. */ |
| 81 | + static BsonDocument bson2BsonDocument(Bson filters) { |
| 82 | + return filters.toBsonDocument(BasicDBObject.class, MongoClient.getDefaultCodecRegistry()); |
| 83 | + } |
| 84 | + |
| 85 | + /** Sets the filters to find. */ |
| 86 | + public FindQueryTest withFilters(Bson filters) { |
| 87 | + return withFilters(bson2BsonDocument(filters)); |
| 88 | + } |
| 89 | + |
| 90 | + /** Sets the limit of documents to find. */ |
| 91 | + public FindQueryTest withLimit(int limit) { |
| 92 | + return toBuilder().setLimit(limit).build(); |
| 93 | + } |
| 94 | + |
| 95 | + /** Sets the projection. */ |
| 96 | + public FindQueryTest withProjection(List<String> projection) { |
| 97 | + checkArgument(projection != null, "projection can not be null"); |
| 98 | + return toBuilder().setProjection(projection).build(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public MongoCursor<Document> apply(MongoCollection<Document> collection) { |
| 103 | + return collection |
| 104 | + .find() |
| 105 | + .filter(filters()) |
| 106 | + .limit(limit()) |
| 107 | + .projection(Projections.include(projection())) |
| 108 | + .iterator(); |
| 109 | + } |
| 110 | +} |
0 commit comments