33
33
import org .bson .BsonObjectId ;
34
34
import org .bson .BsonValue ;
35
35
import org .bson .Document ;
36
- import org .bson .codecs .configuration .CodecRegistry ;
37
36
import org .bson .conversions .Bson ;
38
37
import org .bson .types .ObjectId ;
39
38
49
48
50
49
@ SuppressWarnings ("deprecation" )
51
50
final class GridFSBucketImpl implements GridFSBucket {
52
- private final MongoDatabase database ;
51
+ private static final int DEFAULT_CHUNKSIZE_BYTES = 255 * 1024 ;
53
52
private final String bucketName ;
54
53
private final int chunkSizeBytes ;
55
- private final WriteConcern writeConcern ;
56
- private final ReadConcern readConcern ;
57
- private final ReadPreference readPreference ;
58
54
private final MongoCollection <Document > filesCollection ;
59
55
private final MongoCollection <Document > chunksCollection ;
60
- private final CodecRegistry codecRegistry ;
61
56
private volatile boolean checkedIndexes ;
62
57
63
58
GridFSBucketImpl (final MongoDatabase database ) {
64
59
this (database , "fs" );
65
60
}
66
61
67
62
GridFSBucketImpl (final MongoDatabase database , final String bucketName ) {
68
- this .database = notNull ("database" , database );
69
- this .bucketName = notNull ("bucketName" , bucketName );
70
- this .chunkSizeBytes = 255 * 1024 ;
71
- this .writeConcern = database .getWriteConcern ();
72
- this .readConcern = database .getReadConcern ();
73
- this .readPreference = database .getReadPreference ();
74
- this .codecRegistry = getCodecRegistry ();
75
- this .filesCollection = getFilesCollection ();
76
- this .chunksCollection = getChunksCollection ();
77
- }
78
-
79
- GridFSBucketImpl (final MongoDatabase database , final String bucketName , final int chunkSizeBytes , final CodecRegistry codecRegistry ,
80
- final ReadPreference readPreference , final WriteConcern writeConcern , final ReadConcern readConcern ,
81
- final MongoCollection <Document > filesCollection , final MongoCollection <Document > chunksCollection ,
82
- final boolean checkedIndexes ) {
83
- this .database = notNull ("database" , database );
63
+ this (notNull ("bucketName" , bucketName ), DEFAULT_CHUNKSIZE_BYTES ,
64
+ getFilesCollection (notNull ("database" , database ), bucketName ),
65
+ getChunksCollection (database , bucketName ));
66
+ }
67
+
68
+ GridFSBucketImpl (final String bucketName , final int chunkSizeBytes , final MongoCollection <Document > filesCollection ,
69
+ final MongoCollection <Document > chunksCollection ) {
84
70
this .bucketName = notNull ("bucketName" , bucketName );
85
71
this .chunkSizeBytes = chunkSizeBytes ;
86
- this .codecRegistry = notNull ("codecRegistry" , codecRegistry );
87
- this .readPreference = notNull ("readPreference" , readPreference );
88
- this .writeConcern = notNull ("writeConcern" , writeConcern );
89
- this .readConcern = notNull ("readConcern" , readConcern );
90
- this .checkedIndexes = checkedIndexes ;
91
- this .filesCollection = notNull ("filesCollection" , filesCollection );
72
+ this .filesCollection = notNull ("filesCollection" , filesCollection );
92
73
this .chunksCollection = notNull ("chunksCollection" , chunksCollection );
93
74
}
94
75
@@ -104,41 +85,40 @@ public int getChunkSizeBytes() {
104
85
105
86
@ Override
106
87
public ReadPreference getReadPreference () {
107
- return readPreference ;
88
+ return filesCollection . getReadPreference () ;
108
89
}
109
90
110
91
@ Override
111
92
public WriteConcern getWriteConcern () {
112
- return writeConcern ;
93
+ return filesCollection . getWriteConcern () ;
113
94
}
114
95
115
96
@ Override
116
97
public ReadConcern getReadConcern () {
117
- return readConcern ;
98
+ return filesCollection . getReadConcern () ;
118
99
}
119
100
120
101
@ Override
121
102
public GridFSBucket withChunkSizeBytes (final int chunkSizeBytes ) {
122
- return new GridFSBucketImpl (database , bucketName , chunkSizeBytes , codecRegistry , readPreference , writeConcern , readConcern ,
123
- filesCollection , chunksCollection , checkedIndexes );
103
+ return new GridFSBucketImpl (bucketName , chunkSizeBytes , filesCollection , chunksCollection );
124
104
}
125
105
126
106
@ Override
127
107
public GridFSBucket withReadPreference (final ReadPreference readPreference ) {
128
- return new GridFSBucketImpl (database , bucketName , chunkSizeBytes , codecRegistry , readPreference , writeConcern , readConcern ,
129
- filesCollection , chunksCollection , checkedIndexes );
108
+ return new GridFSBucketImpl (bucketName , chunkSizeBytes , filesCollection . withReadPreference ( readPreference ) ,
109
+ chunksCollection . withReadPreference ( readPreference ) );
130
110
}
131
111
132
112
@ Override
133
113
public GridFSBucket withWriteConcern (final WriteConcern writeConcern ) {
134
- return new GridFSBucketImpl (database , bucketName , chunkSizeBytes , codecRegistry , readPreference , writeConcern , readConcern ,
135
- filesCollection , chunksCollection , checkedIndexes );
114
+ return new GridFSBucketImpl (bucketName , chunkSizeBytes , filesCollection . withWriteConcern ( writeConcern ) ,
115
+ chunksCollection . withWriteConcern ( writeConcern ) );
136
116
}
137
117
138
118
@ Override
139
119
public GridFSBucket withReadConcern (final ReadConcern readConcern ) {
140
- return new GridFSBucketImpl (database , bucketName , chunkSizeBytes , codecRegistry , readPreference , writeConcern , readConcern ,
141
- filesCollection , chunksCollection , checkedIndexes );
120
+ return new GridFSBucketImpl (bucketName , chunkSizeBytes , filesCollection . withReadConcern ( readConcern ) ,
121
+ chunksCollection . withReadConcern ( readConcern ) );
142
122
}
143
123
144
124
@ Override
@@ -252,22 +232,13 @@ public void drop() {
252
232
chunksCollection .drop ();
253
233
}
254
234
255
- private CodecRegistry getCodecRegistry () {
256
- return fromRegistries (database .getCodecRegistry (), MongoClient .getDefaultCodecRegistry ());
257
- }
258
-
259
- private MongoCollection <Document > getFilesCollection () {
260
- return database .getCollection (bucketName + ".files" )
261
- .withCodecRegistry (codecRegistry )
262
- .withReadPreference (readPreference )
263
- .withWriteConcern (writeConcern );
235
+ private static MongoCollection <Document > getFilesCollection (final MongoDatabase database , final String bucketName ) {
236
+ return database .getCollection (bucketName + ".files" ).withCodecRegistry (fromRegistries (database .getCodecRegistry (),
237
+ MongoClient .getDefaultCodecRegistry ()));
264
238
}
265
239
266
- private MongoCollection <Document > getChunksCollection () {
267
- return database .getCollection (bucketName + ".chunks" )
268
- .withCodecRegistry (MongoClient .getDefaultCodecRegistry ())
269
- .withReadPreference (readPreference )
270
- .withWriteConcern (writeConcern );
240
+ private static MongoCollection <Document > getChunksCollection (final MongoDatabase database , final String bucketName ) {
241
+ return database .getCollection (bucketName + ".chunks" ).withCodecRegistry (MongoClient .getDefaultCodecRegistry ());
271
242
}
272
243
273
244
private void checkCreateIndex () {
0 commit comments