forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGridFSBucketImpl.java
336 lines (282 loc) · 15.1 KB
/
GridFSBucketImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
* Copyright 2008-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mongodb.reactivestreams.client.internal.gridfs;
import com.mongodb.ReadConcern;
import com.mongodb.ReadPreference;
import com.mongodb.WriteConcern;
import com.mongodb.client.gridfs.model.GridFSDownloadOptions;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.mongodb.client.gridfs.model.GridFSUploadOptions;
import com.mongodb.internal.time.Timeout;
import com.mongodb.reactivestreams.client.ClientSession;
import com.mongodb.reactivestreams.client.MongoClients;
import com.mongodb.reactivestreams.client.MongoCollection;
import com.mongodb.reactivestreams.client.MongoDatabase;
import com.mongodb.reactivestreams.client.gridfs.GridFSBucket;
import com.mongodb.reactivestreams.client.gridfs.GridFSDownloadPublisher;
import com.mongodb.reactivestreams.client.gridfs.GridFSFindPublisher;
import com.mongodb.reactivestreams.client.gridfs.GridFSUploadPublisher;
import org.bson.BsonDocument;
import org.bson.BsonObjectId;
import org.bson.BsonValue;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;
import org.reactivestreams.Publisher;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import static com.mongodb.assertions.Assertions.notNull;
import static com.mongodb.reactivestreams.client.internal.gridfs.GridFSPublisherCreator.createDeletePublisher;
import static com.mongodb.reactivestreams.client.internal.gridfs.GridFSPublisherCreator.createDropPublisher;
import static com.mongodb.reactivestreams.client.internal.gridfs.GridFSPublisherCreator.createGridFSDownloadPublisher;
import static com.mongodb.reactivestreams.client.internal.gridfs.GridFSPublisherCreator.createGridFSFindPublisher;
import static com.mongodb.reactivestreams.client.internal.gridfs.GridFSPublisherCreator.createGridFSUploadPublisher;
import static com.mongodb.reactivestreams.client.internal.gridfs.GridFSPublisherCreator.createRenamePublisher;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.bson.codecs.configuration.CodecRegistries.fromRegistries;
/**
* The internal GridFSBucket implementation.
*
* <p>This class is not part of the public API and may be removed or changed at any time</p>
*/
public final class GridFSBucketImpl implements GridFSBucket {
private static final int DEFAULT_CHUNKSIZE_BYTES = 255 * 1024;
private final String bucketName;
private final int chunkSizeBytes;
private final MongoCollection<GridFSFile> filesCollection;
private final MongoCollection<Document> chunksCollection;
public GridFSBucketImpl(final MongoDatabase database) {
this(database, "fs");
}
public GridFSBucketImpl(final MongoDatabase database, final String bucketName) {
this(notNull("bucketName", bucketName), DEFAULT_CHUNKSIZE_BYTES,
getFilesCollection(notNull("database", database), bucketName),
getChunksCollection(database, bucketName));
}
private GridFSBucketImpl(final String bucketName, final int chunkSizeBytes, final MongoCollection<GridFSFile> filesCollection,
final MongoCollection<Document> chunksCollection) {
this.bucketName = notNull("bucketName", bucketName);
this.chunkSizeBytes = chunkSizeBytes;
this.filesCollection = notNull("filesCollection", filesCollection);
this.chunksCollection = notNull("chunksCollection", chunksCollection);
}
private static MongoCollection<GridFSFile> getFilesCollection(final MongoDatabase database, final String bucketName) {
return database.getCollection(bucketName + ".files", GridFSFile.class).withCodecRegistry(
fromRegistries(database.getCodecRegistry(), MongoClients.getDefaultCodecRegistry())
);
}
private static MongoCollection<Document> getChunksCollection(final MongoDatabase database, final String bucketName) {
return database.getCollection(bucketName + ".chunks").withCodecRegistry(MongoClients.getDefaultCodecRegistry());
}
@Override
public String getBucketName() {
return bucketName;
}
@Override
public int getChunkSizeBytes() {
return chunkSizeBytes;
}
@Override
public ReadPreference getReadPreference() {
return filesCollection.getReadPreference();
}
@Override
public WriteConcern getWriteConcern() {
return filesCollection.getWriteConcern();
}
@Override
public ReadConcern getReadConcern() {
return filesCollection.getReadConcern();
}
@Override
public Long getTimeout(final TimeUnit timeUnit) {
Long timeoutMS = filesCollection.getTimeout(MILLISECONDS);
return timeoutMS == null ? null : notNull("timeUnit", timeUnit).convert(timeoutMS, MILLISECONDS);
}
@Override
public GridFSBucket withChunkSizeBytes(final int chunkSizeBytes) {
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection, chunksCollection);
}
@Override
public GridFSBucket withReadPreference(final ReadPreference readPreference) {
notNull("readPreference", readPreference);
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withReadPreference(readPreference),
chunksCollection.withReadPreference(readPreference));
}
@Override
public GridFSBucket withWriteConcern(final WriteConcern writeConcern) {
notNull("writeConcern", writeConcern);
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withWriteConcern(writeConcern),
chunksCollection.withWriteConcern(writeConcern));
}
@Override
public GridFSBucket withReadConcern(final ReadConcern readConcern) {
notNull("readConcern", readConcern);
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withReadConcern(readConcern),
chunksCollection.withReadConcern(readConcern));
}
@Override
public GridFSBucket withTimeout(final long timeout, final TimeUnit timeUnit) {
return new GridFSBucketImpl(bucketName, chunkSizeBytes, filesCollection.withTimeout(timeout, timeUnit),
chunksCollection.withTimeout(timeout, timeUnit));
}
@Override
public GridFSUploadPublisher<ObjectId> uploadFromPublisher(final String filename, final Publisher<ByteBuffer> source) {
return uploadFromPublisher(filename, source, new GridFSUploadOptions());
}
@Override
public GridFSUploadPublisher<ObjectId> uploadFromPublisher(final String filename,
final Publisher<ByteBuffer> source,
final GridFSUploadOptions options) {
return createGridFSUploadPublisher(chunkSizeBytes, filesCollection, chunksCollection,
null, new BsonObjectId(), filename, options, source).withObjectId();
}
@Override
public GridFSUploadPublisher<Void> uploadFromPublisher(final BsonValue id, final String filename, final Publisher<ByteBuffer> source) {
return uploadFromPublisher(id, filename, source, new GridFSUploadOptions());
}
@Override
public GridFSUploadPublisher<Void> uploadFromPublisher(final BsonValue id, final String filename, final Publisher<ByteBuffer> source,
final GridFSUploadOptions options) {
return createGridFSUploadPublisher(chunkSizeBytes, filesCollection, chunksCollection, null, id,
filename, options, source);
}
@Override
public GridFSUploadPublisher<ObjectId> uploadFromPublisher(final ClientSession clientSession, final String filename,
final Publisher<ByteBuffer> source) {
return uploadFromPublisher(clientSession, filename, source, new GridFSUploadOptions());
}
@Override
public GridFSUploadPublisher<ObjectId> uploadFromPublisher(final ClientSession clientSession, final String filename,
final Publisher<ByteBuffer> source, final GridFSUploadOptions options) {
return createGridFSUploadPublisher(chunkSizeBytes, filesCollection, chunksCollection,
notNull("clientSession", clientSession), new BsonObjectId(), filename, options, source)
.withObjectId();
}
@Override
public GridFSUploadPublisher<Void> uploadFromPublisher(final ClientSession clientSession, final BsonValue id,
final String filename, final Publisher<ByteBuffer> source) {
return uploadFromPublisher(clientSession, id, filename, source, new GridFSUploadOptions());
}
@Override
public GridFSUploadPublisher<Void> uploadFromPublisher(final ClientSession clientSession, final BsonValue id,
final String filename,
final Publisher<ByteBuffer> source,
final GridFSUploadOptions options) {
return createGridFSUploadPublisher(chunkSizeBytes, filesCollection, chunksCollection,
notNull("clientSession", clientSession), id, filename, options, source);
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final ObjectId id) {
return downloadToPublisher(new BsonObjectId(id));
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final BsonValue id) {
Function<Timeout, GridFSFindPublisher> findPublisherCreator =
operationTimeout -> createGridFSFindPublisher(filesCollection, null, new BsonDocument("_id", id), operationTimeout);
return createGridFSDownloadPublisher(chunksCollection, null, findPublisherCreator);
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final String filename) {
return downloadToPublisher(filename, new GridFSDownloadOptions());
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final String filename, final GridFSDownloadOptions options) {
Function<Timeout, GridFSFindPublisher> findPublisherCreator =
operationTimeout -> createGridFSFindPublisher(filesCollection, null, filename, options, operationTimeout);
return createGridFSDownloadPublisher(chunksCollection, null, findPublisherCreator);
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final ClientSession clientSession, final ObjectId id) {
return downloadToPublisher(clientSession, new BsonObjectId(id));
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final ClientSession clientSession, final BsonValue id) {
Function<Timeout, GridFSFindPublisher> findPublisherCreator =
operationTimeout -> createGridFSFindPublisher(filesCollection, clientSession, new BsonDocument("_id", id), operationTimeout);
return createGridFSDownloadPublisher(chunksCollection, notNull("clientSession", clientSession), findPublisherCreator);
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final ClientSession clientSession, final String filename) {
return downloadToPublisher(clientSession, filename, new GridFSDownloadOptions());
}
@Override
public GridFSDownloadPublisher downloadToPublisher(final ClientSession clientSession,
final String filename,
final GridFSDownloadOptions options) {
Function<Timeout, GridFSFindPublisher> findPublisherCreator =
operationTimeout -> createGridFSFindPublisher(filesCollection, clientSession, filename,
options, operationTimeout);
return createGridFSDownloadPublisher(chunksCollection, notNull("clientSession", clientSession), findPublisherCreator);
}
@Override
public GridFSFindPublisher find() {
return createGridFSFindPublisher(filesCollection, null, null);
}
@Override
public GridFSFindPublisher find(final Bson filter) {
return createGridFSFindPublisher(filesCollection, null, notNull("filter", filter));
}
@Override
public GridFSFindPublisher find(final ClientSession clientSession) {
return createGridFSFindPublisher(filesCollection, notNull("clientSession", clientSession), null);
}
@Override
public GridFSFindPublisher find(final ClientSession clientSession, final Bson filter) {
return createGridFSFindPublisher(filesCollection, notNull("clientSession", clientSession), notNull("filter", filter));
}
@Override
public Publisher<Void> delete(final ObjectId id) {
return delete(new BsonObjectId(id));
}
@Override
public Publisher<Void> delete(final BsonValue id) {
return createDeletePublisher(filesCollection, chunksCollection, null, id);
}
@Override
public Publisher<Void> delete(final ClientSession clientSession, final ObjectId id) {
return delete(clientSession, new BsonObjectId(id));
}
@Override
public Publisher<Void> delete(final ClientSession clientSession, final BsonValue id) {
return createDeletePublisher(filesCollection, chunksCollection, notNull("clientSession", clientSession), id);
}
@Override
public Publisher<Void> rename(final ObjectId id, final String newFilename) {
return rename(new BsonObjectId(id), newFilename);
}
@Override
public Publisher<Void> rename(final BsonValue id, final String newFilename) {
return createRenamePublisher(filesCollection, null, id, newFilename);
}
@Override
public Publisher<Void> rename(final ClientSession clientSession, final ObjectId id, final String newFilename) {
return rename(clientSession, new BsonObjectId(id), newFilename);
}
@Override
public Publisher<Void> rename(final ClientSession clientSession, final BsonValue id, final String newFilename) {
return createRenamePublisher(filesCollection, notNull("clientSession", clientSession), id, newFilename);
}
@Override
public Publisher<Void> drop() {
return createDropPublisher(filesCollection, chunksCollection, null);
}
@Override
public Publisher<Void> drop(final ClientSession clientSession) {
return createDropPublisher(filesCollection, chunksCollection, notNull("clientSession", clientSession));
}
}