@@ -41,10 +41,14 @@ defmodule Mongo.GridFs.Bucket do
41
41
42
42
Keyword . merge ( @ defaults , options )
43
43
|> Enum . reduce ( % Bucket { topology_pid: topology_pid , opts: options } , fn { k , v } , bucket -> Map . put ( bucket , k , v ) end )
44
- |> check_indexes
44
+ |> check_indexes ( )
45
45
46
46
end
47
47
48
+ def add_session ( % Bucket { opts: opts } = bucket , session_opts ) do
49
+ % Bucket { bucket | opts: opts ++ session_opts }
50
+ end
51
+
48
52
@ doc """
49
53
Returns the collection name for the files collection, default is fs.files.
50
54
"""
@@ -127,19 +131,41 @@ defmodule Mongo.GridFs.Bucket do
127
131
#
128
132
defp check_indexes ( bucket ) do
129
133
130
- case files_collection_empty? ( bucket ) do
131
- true ->
132
- _ = create_files_index ( { bucket , false } )
133
- _ = create_chunks_index ( { bucket , false } )
134
-
134
+ case files_collection_empty? ( bucket ) do
135
+ true -> create_indexes ( bucket )
135
136
false ->
136
- bucket
137
- |> check_files_index ( )
138
- |> create_files_index ( )
139
- |> check_chunks_index ( )
140
- |> create_chunks_index ( )
137
+ with :ok <- check_and_create_files_index ( bucket ) ,
138
+ :ok <- check_and_chunks_files_index ( bucket ) do
139
+ bucket
140
+ end
141
+ end
142
+
143
+ end
144
+
145
+ ##
146
+ # create the collection and indexes for files and chunks
147
+ #
148
+ defp create_indexes ( % Bucket { topology_pid: topology_pid , opts: opts } = bucket ) do
149
+ with :ok <- Mongo . create ( topology_pid , files_collection_name ( bucket ) , opts ) ,
150
+ :ok <- Mongo . create ( topology_pid , chunks_collection_name ( bucket ) , opts ) ,
151
+ :ok <- create_files_index ( bucket ) ,
152
+ :ok <- create_chunks_index ( bucket ) do
153
+ bucket
141
154
end
155
+ end
142
156
157
+ def check_and_create_files_index ( bucket ) do
158
+ case check_files_index ( bucket ) do
159
+ true -> create_files_index ( bucket )
160
+ false -> :ok
161
+ end
162
+ end
163
+
164
+ def check_and_chunks_files_index ( bucket ) do
165
+ case check_chunks_index ( bucket ) do
166
+ true -> create_chunks_index ( bucket )
167
+ false -> :ok
168
+ end
143
169
end
144
170
145
171
##
@@ -150,33 +176,33 @@ defmodule Mongo.GridFs.Bucket do
150
176
# db.fs.files.findOne({}, { _id : 1 })
151
177
#
152
178
defp files_collection_empty? ( % Bucket { topology_pid: topology_pid , opts: opts } = bucket ) do
153
-
179
+ coll_name = files_collection_name ( bucket )
154
180
topology_pid
155
- |> Mongo . find_one ( files_collection_name ( bucket ) , % { } , Keyword . merge ( opts , projection: % { _id: 1 } ) )
181
+ |> Mongo . show_collections ( )
182
+ |> Enum . find ( fn name -> name == coll_name end )
156
183
|> is_nil ( )
157
-
158
184
end
159
185
160
186
##
161
187
# Checks the indexes for the fs.files collection
162
188
#
163
189
defp check_files_index ( % Bucket { topology_pid: topology_pid , opts: opts } = bucket ) do
164
- { bucket , index_member? ( topology_pid , files_collection_name ( bucket ) , @ files_index_name , opts ) }
190
+ index_member? ( topology_pid , files_collection_name ( bucket ) , @ files_index_name , opts )
165
191
end
166
192
167
193
##
168
194
# Checks the indexes for the fs.chunks collection
169
195
#
170
196
defp check_chunks_index ( % Bucket { topology_pid: topology_pid , opts: opts } = bucket ) do
171
- { bucket , index_member? ( topology_pid , chunks_collection_name ( bucket ) , @ chunks_index_name , opts ) }
197
+ index_member? ( topology_pid , chunks_collection_name ( bucket ) , @ chunks_index_name , opts )
172
198
end
173
199
174
200
# returns true if the collection contains a index with the given name
175
201
def index_member? ( topology_pid , coll , index , opts ) do
176
202
topology_pid
177
203
|> Mongo . list_indexes ( coll , opts )
178
204
|> Enum . map ( fn
179
- % { "name" => name } -> name
205
+ % { "name" => name } -> name
180
206
_ -> ""
181
207
end )
182
208
|> Enum . member? ( index )
@@ -185,34 +211,23 @@ defmodule Mongo.GridFs.Bucket do
185
211
##
186
212
# Creates the indexes for the fs.chunks collection
187
213
#
188
- defp create_chunks_index ( { % Bucket { topology_pid: topology_pid , opts: opts } = bucket , false } ) do
189
-
190
- cmd = [ createIndexes: chunks_collection_name ( bucket ) , indexes: [ [ key: [ files_id: 1 , n: 1 ] , name: @ chunks_index_name , unique: true ] ] ]
191
- { :ok , _ } = Mongo . issue_command ( topology_pid , cmd , :write , opts )
192
-
193
- bucket
214
+ defp create_chunks_index ( % Bucket { topology_pid: topology_pid , opts: opts } = bucket ) do
215
+ cmd = [ createIndexes: chunks_collection_name ( bucket ) , indexes: [ [ key: [ files_id: 1 , n: 1 ] , name: @ chunks_index_name , unique: true ] ] ]
216
+ with { :ok , _ } <- Mongo . issue_command ( topology_pid , cmd , :write , opts ) do
217
+ :ok
218
+ end
194
219
end
195
220
196
- ##
197
- # index exists, nothing to do
198
- #
199
- defp create_chunks_index ( { bucket , true } ) , do: bucket
200
-
201
221
##
202
222
# Creates the indexes for the fs.files collection
203
223
#
204
- defp create_files_index ( { % Bucket { topology_pid: topology_pid , opts: opts } = bucket , false } ) do
205
-
206
- cmd = [ createIndexes: files_collection_name ( bucket ) , indexes: [ [ key: [ filename: 1 , uploadDate: 1 ] , name: @ files_index_name ] ] ]
207
- { :ok , _ } = Mongo . issue_command ( topology_pid , cmd , :write , opts )
208
-
209
- bucket
224
+ defp create_files_index ( % Bucket { topology_pid: topology_pid , opts: opts } = bucket ) do
225
+ cmd = [ createIndexes: files_collection_name ( bucket ) , indexes: [ [ key: [ filename: 1 , uploadDate: 1 ] , name: @ files_index_name ] ] ]
226
+ with { :ok , _ } <- Mongo . issue_command ( topology_pid , cmd , :write , opts ) do
227
+ :ok
228
+ end
210
229
end
211
230
212
- ##
213
- # index exists, nothing to do
214
- #
215
- defp create_files_index ( { bucket , true } ) , do: bucket
216
231
217
232
defimpl Inspect , for: Bucket do
218
233
0 commit comments