@@ -165,8 +165,9 @@ def __call__(self, command, client, opts, args):
165165
166166class CreateDatasetCommand (CLICommand ):
167167
168- Opts = ("m:q:jf:" , ["flags=" , "metadata=" , "query=" , "json" ])
168+ Opts = ("b: m:q:jf:" , ["batchsize=" , "flags=" , "metadata=" , "query=" , "json" ])
169169 Usage = """[<options>] <namespace>:<name> [<description>] -- create dataset
170+ -b|--batchsize N - optional, add files in batches of N
170171 -f|--flags (monotonic|frozen) - optional, dataset flags
171172 -m|--metadata '<JSON expression>'
172173 -m|--metadata <JSON file>
@@ -184,24 +185,66 @@ def __call__(self, command, client, opts, args):
184185 desc = " " .join (desc )
185186 else :
186187 desc = ""
188+ batchsize = int (opts .get ("--batchsize" ) or opts .get ("-b" ) or "0" )
187189 flags = opts .get ("-f" ) or opts .get ("--flags" )
188190 monotonic = flags == "monotonic"
189191 frozen = flags == "frozen"
190192 metadata = load_json (opts .get ("-m" ) or opts .get ("--metadata" )) or {}
191193 files_query = load_text (opts .get ("-q" ) or opts .get ("--query" )) or None
194+ batch = 0
195+
192196 try :
197+
198+ if batchsize and files_query :
199+ # if we were given a batch size, we need the count to
200+ # know how many batches, etc.
201+
202+ qr = client .query ( files_query , summary = "count" )
203+ qr = list (qr )
204+ totfiles = qr [0 ]["count" ]
205+
206+ # if we don't have that many files, just ignore batchsize
207+ if batchsize >= totfiles :
208+ batchsize = 0
209+ else :
210+ nbatches = totfiles // batchsize
211+
212+ if batchsize and files_query :
213+ # if batching, limit initial query and don't freeze yet...
214+ base_query = files_query
215+ files_query = f"({ base_query } ) ordered limit { batchsize } "
216+ frozen2 = frozen
217+ frozen = False
218+
193219 out = client .create_dataset (dataset_spec , monotonic = monotonic , frozen = frozen , description = desc , metadata = metadata ,
194220 files_query = files_query
195221 )
222+
223+ if batchsize and files_query :
224+ # now add remaining files in batches
225+ for batch in range (1 ,nbatches + 1 ):
226+ files_query = f"({ base_query } ) ordered skip { batch * batchsize } limit { batchsize } "
227+ af = client .add_files (dataset_spec , query = files_query )
228+ out ["file_count" ] += af
229+
230+
231+ # finally set frozen flag if requested
232+ if frozen2 :
233+ client .update_datset (dataset_spec , frozen = frozen2 )
234+
196235 except MCError as e :
197- print (e )
236+ if batchsize and files_query :
237+ print (f"{ e } on batch { batch } " )
238+ else :
239+ print (e )
240+
198241 sys .exit (1 )
199242 else :
200243 if "-j" in opts or "--json" in opts :
201244 print (json .dumps (out , indent = 4 , sort_keys = True ))
202245 else :
203246 nfiles = out .get ("file_count" )
204- print (f"Dataset { dataset_spec } cteated " , f"with { nfiles } files" if nfiles is not None else "" )
247+ print (f"Dataset { dataset_spec } created " , f"with { nfiles } files" if nfiles is not None else "" )
205248
206249class UpdateDatasetCommand (CLICommand ):
207250
0 commit comments