Commit 4d6920b6fa94542d1aa091f6ddf5337fe90abb55

Authored by Charles Otto
1 parent 7ac44304

Refactor emptyRead

Move emptyRead to a higher visibility scope, and make it handle caching
metadata in addition to reading galleries. Caching still leverages
memGallery since re-implementing a cache just for FileLists would largely
be redundant.
openbr/core/core.cpp
... ... @@ -207,38 +207,6 @@ struct AlgorithmCore
207 207 data >> *transform;
208 208 }
209 209  
210   - // Read metadata for all templates stored in the specified gallery, return the read
211   - // TeamplateList. If the gallery contains matrices, they are dropped.
212   - void emptyRead(const File & file, TemplateList & templates)
213   - {
214   - // Is this a gallery type containing matrices?
215   - if ((QStringList() << "gal" << "mem" << "template").contains(file.suffix())) {
216   - // Retrieve it block by block, dropping matrices from read templates.
217   - QScopedPointer<Gallery> gallery(Gallery::make(file));
218   - gallery->set_readBlockSize(10);
219   - bool done = false;
220   - while (!done)
221   - {
222   - TemplateList tList = gallery->readBlock(&done);
223   - for (int i=0; i < tList.size();i++)
224   - {
225   - tList[i].clear();
226   - templates.append(tList[i]);
227   - }
228   - }
229   - }
230   - else {
231   - // The file may have already been enrolled to a memory gallery
232   - emptyRead(getMemoryGallery(file), templates);
233   - if (!templates.empty())
234   - return;
235   -
236   - // Nope, just retrieve the metadata
237   - QScopedPointer<Gallery> gallery(Gallery::make(file));
238   - templates = gallery->read();
239   - }
240   - }
241   -
242 210 void retrieveOrEnroll(const File &file, QScopedPointer<Gallery> &gallery, FileList &galleryFiles)
243 211 {
244 212 if (!file.getBool("enroll") && (QStringList() << "gal" << "mem" << "template").contains(file.suffix())) {
... ... @@ -378,32 +346,13 @@ struct AlgorithmCore
378 346  
379 347 // To decide which gallery is larger, we need to read both, but at this point we just want the
380 348 // metadata, and don't need the enrolled matrices.
381   - TemplateList targetMetadata;
382   - TemplateList queryMetadata;
  349 + FileList targetMetadata;
  350 + FileList queryMetadata;
383 351  
384 352 // Emptyread reads a gallery, and discards any matrices present, keeping only the metadata.
385 353 emptyRead(targetGallery, targetMetadata);
386 354 emptyRead(queryGallery, queryMetadata);
387 355  
388   - // We store the metadata for the target and query sets as separate memory galleries.
389   - // This is necessary because we need to intialize the Output with this data, but we don't
390   - // create the Output directly in this function (going instead through OutputTransform)
391   - File targetMetaMem = targetGallery;
392   - targetMetaMem.name = name + targetMetaMem.baseName()+ "_meta" + targetMetaMem.hash()+ ".mem";
393   - File queryMetaMem = queryGallery;
394   - queryMetaMem.name = name + queryMetaMem.baseName() + "_meta" + queryMetaMem.hash() + ".mem";
395   -
396   -
397   -
398   - // Store the metadata in memory galleries.
399   - QScopedPointer<Gallery> targetMeta(Gallery::make(targetMetaMem));
400   - if (targetMeta->files().isEmpty() )
401   - targetMeta->writeBlock(targetMetadata);
402   -
403   - QScopedPointer<Gallery> queryMeta(Gallery::make(queryMetaMem));
404   - if (queryMeta->files().isEmpty())
405   - queryMeta->writeBlock(queryMetadata);
406   -
407 356  
408 357 // Is the target or query set larger? We will use the larger as the rows of our comparison matrix (and transpose the output if necessary)
409 358 transposeMode = targetMetadata.size() > queryMetadata.size();
... ... @@ -524,7 +473,7 @@ struct AlgorithmCore
524 473 // The output transform takes the metadata memGalleries we set up previously as input, along with the
525 474 // output specification we were passed. Gallery metadata is necessary for some Outputs to function correctly.
526 475 QString outputString = output.flat().isEmpty() ? "Empty" : output.flat();
527   - QString outputRegionDesc = "Output("+ outputString +"," + targetMetaMem.flat() +"," + queryMetaMem.flat() + ","+ QString::number(transposeMode ? 1 : 0) + ")";
  476 + QString outputRegionDesc = "Output("+ outputString +"," + targetGallery.flat() +"," + queryGallery.flat() + ","+ QString::number(transposeMode ? 1 : 0) + ")";
528 477 // The ProgressCounter transform will simply provide a display about the number of rows completed.
529 478 outputRegionDesc += "+ProgressCounter("+QString::number(rowSize)+")+Discard";
530 479 QScopedPointer<Transform> outputTform(Transform::make(outputRegionDesc, NULL));
... ... @@ -770,4 +719,5 @@ QSharedPointer&lt;br::Distance&gt; br::Distance::fromAlgorithm(const QString &amp;algorith
770 719 return AlgorithmManager::getAlgorithm(algorithm)->distance;
771 720 }
772 721  
  722 +
773 723 #include "core.moc"
... ...
openbr/plugins/gallery.cpp
... ... @@ -393,6 +393,49 @@ class memGallery : public Gallery
393 393  
394 394 BR_REGISTER(Gallery, memGallery)
395 395  
  396 +void emptyRead(const File & file, FileList & fileData, bool cache)
  397 +{
  398 + File targetMeta = file;
  399 + targetMeta.name = targetMeta.path() + targetMeta.baseName() + "_meta" + targetMeta.hash() + ".mem";
  400 +
  401 + // Did we already read the data?
  402 + if (MemoryGalleries::galleries.contains(targetMeta))
  403 + {
  404 + fileData = MemoryGalleries::galleries[targetMeta].files();
  405 + return;
  406 + }
  407 +
  408 + TemplateList templates;
  409 + // OK we read the data in some form, does the gallery type containing matrices?
  410 + if ((QStringList() << "gal" << "mem" << "template").contains(file.suffix())) {
  411 + // Retrieve it block by block, dropping matrices from read templates.
  412 + QScopedPointer<Gallery> gallery(Gallery::make(file));
  413 + gallery->set_readBlockSize(10);
  414 + bool done = false;
  415 + while (!done)
  416 + {
  417 + TemplateList tList = gallery->readBlock(&done);
  418 + for (int i=0; i < tList.size();i++)
  419 + {
  420 + tList[i].clear();
  421 + templates.append(tList[i].file);
  422 + }
  423 + }
  424 + }
  425 + else {
  426 + // this is a gallery format that doesn't include matrices, so we can just read it
  427 + QScopedPointer<Gallery> gallery(Gallery::make(file));
  428 + templates= gallery->read();
  429 + }
  430 +
  431 + if (cache)
  432 + {
  433 + QScopedPointer<Gallery> memOutput(Gallery::make(targetMeta));
  434 + memOutput->writeBlock(templates);
  435 + }
  436 + fileData = templates.files();
  437 +}
  438 +
396 439 /*!
397 440 * \ingroup galleries
398 441 * \brief Treats each line as a file.
... ...
openbr/plugins/misc.cpp
... ... @@ -636,11 +636,10 @@ class OutputTransform : public TimeVaryingTransform
636 636 if (targetName.isEmpty() || queryName.isEmpty() || outputString.isEmpty())
637 637 return;
638 638  
639   - QScopedPointer<Gallery> tGallery(Gallery::make(targetName));
640   - QScopedPointer<Gallery> qGallery(Gallery::make(queryName));
641   -
642   - FileList targetFiles = tGallery->files();
643   - FileList queryFiles = qGallery->files();
  639 + FileList targetFiles;
  640 + FileList queryFiles;
  641 + emptyRead(targetName, targetFiles, false);
  642 + emptyRead(queryName, queryFiles, false);
644 643  
645 644 currentBlockRow = 0;
646 645 currentBlockCol = 0;
... ...
openbr/plugins/openbr_internal.h
... ... @@ -314,6 +314,11 @@ struct WorkerProcess
314 314 void mainLoop();
315 315 };
316 316  
  317 +
  318 +// Read metadata for all templates stored in the specified gallery, return the read
  319 +// TeamplateList. If the gallery contains matrices, they are dropped.
  320 +void emptyRead(const File & file, FileList & templates, bool cache = true);
  321 +
317 322 }
318 323  
319 324 #endif // OPENBR_INTERNAL_H
... ...