Commit 879b2db4e70b05736a25287288d8c5ecf2c6d6ab

Authored by bhklein
1 parent a8eb6794

Construct QList of TemplatePair futures to maintain metadata file order

Showing 1 changed file with 21 additions and 11 deletions
openbr/janus_io.cpp
... ... @@ -37,29 +37,39 @@ janus_error janus_create_gallery(const char *data_path, janus_metadata metadata,
37 37 return JANUS_SUCCESS;
38 38 }
39 39  
  40 +typedef QPair<janus_template_id, FlatTemplate> TemplatePair;
  41 +
  42 +TemplatePair _janus_create_flat_template(const char *data_path, TemplateData templateData, bool verbose)
  43 +{
  44 + janus_template template_;
  45 + janus_template_id templateID;
  46 + JANUS_ASSERT(TemplateIterator::create(data_path, templateData, &template_, &templateID, verbose))
  47 + return TemplatePair(templateID, FlatTemplate(template_));
  48 +}
  49 +
40 50 janus_error janus_create_templates(const char *data_path, janus_metadata metadata, const char *gallery_file, int verbose)
41 51 {
42 52 TemplateIterator ti(metadata, true);
43 53 TemplateData templateData = ti.next();
44   - unsigned int num_templates = 1;
45 54  
46   - janus_gallery gallery;
47   - JANUS_ASSERT(janus_allocate_gallery(&gallery))
48   - QFutureSynchronizer<void> futures;
  55 + QFutureSynchronizer<TemplatePair> futures;
49 56 while (!templateData.templateIDs.empty()) {
50   - futures.addFuture(QtConcurrent::run(_janus_create_template, data_path, templateData, gallery, verbose));
  57 + futures.addFuture(QtConcurrent::run(_janus_create_flat_template, data_path, templateData, verbose));
51 58 templateData = ti.next();
52   - num_templates++;
53 59 }
54 60 futures.waitForFinished();
55   - janus_flat_gallery flat_gallery = new janus_data[num_templates*janus_max_template_size()];
56   - size_t bytes;
57   - JANUS_ASSERT(janus_flatten_gallery(gallery, flat_gallery, &bytes))
  61 + QList< QFuture<TemplatePair> > flat_templates = futures.futures();
58 62  
59 63 std::ofstream file;
60 64 file.open(gallery_file, std::ios::out | std::ios::binary);
61   - file.write((char*)flat_gallery, bytes);
  65 + foreach (const QFuture<TemplatePair> &future, flat_templates) {
  66 + janus_template_id templateID = future.result().first;
  67 + FlatTemplate flatTemplate = future.result().second;
  68 + file.write((char*)&templateID, sizeof(templateID));
  69 + file.write((char*)&flatTemplate.data->bytes, sizeof(flatTemplate.data->bytes));
  70 + file.write((char*)flatTemplate.data->flat_template, flatTemplate.data->bytes);
  71 + }
  72 +
62 73 file.close();
63   - delete[] flat_gallery;
64 74 return JANUS_SUCCESS;
65 75 }
... ...