Commit c407dd5fb1029825430a75454e2be4ba9921d9f5
1 parent
063496f6
Update txtGallery and flatGallery to operate incrementally
Incrementally write a file instead of accumulating all lines of a text file, then outputting them in the destructor. This avoids maintaining a full copy of all metadata being written to disk. ALso, respect readBlockSize in these galleries when reading incrementally.
Showing
2 changed files
with
73 additions
and
18 deletions
openbr/core/qtutils.cpp
| @@ -99,7 +99,16 @@ void readFile(const QString &file, QByteArray &data, bool uncompress) | @@ -99,7 +99,16 @@ void readFile(const QString &file, QByteArray &data, bool uncompress) | ||
| 99 | 99 | ||
| 100 | void writeFile(const QString &file, const QStringList &lines) | 100 | void writeFile(const QString &file, const QStringList &lines) |
| 101 | { | 101 | { |
| 102 | - writeFile(file, lines.join("\n")); | 102 | + QFile f(file); |
| 103 | + touchDir(f); | ||
| 104 | + | ||
| 105 | + if (!f.open(QFile::WriteOnly)) | ||
| 106 | + qFatal("Failed to open %s for writing.", qPrintable(file)); | ||
| 107 | + | ||
| 108 | + foreach(const QString & line, lines) { | ||
| 109 | + f.write((line+"\n").toLocal8Bit() ); | ||
| 110 | + } | ||
| 111 | + f.close(); | ||
| 103 | } | 112 | } |
| 104 | 113 | ||
| 105 | void writeFile(const QString &file, const QString &data) | 114 | void writeFile(const QString &file, const QString &data) |
openbr/plugins/gallery.cpp
| @@ -574,32 +574,55 @@ class txtGallery : public Gallery | @@ -574,32 +574,55 @@ class txtGallery : public Gallery | ||
| 574 | Q_PROPERTY(QString label READ get_label WRITE set_label RESET reset_label STORED false) | 574 | Q_PROPERTY(QString label READ get_label WRITE set_label RESET reset_label STORED false) |
| 575 | BR_PROPERTY(QString, label, "") | 575 | BR_PROPERTY(QString, label, "") |
| 576 | 576 | ||
| 577 | - QStringList lines; | ||
| 578 | - | 577 | + QFile f; |
| 579 | ~txtGallery() | 578 | ~txtGallery() |
| 580 | { | 579 | { |
| 581 | - if (!lines.isEmpty()) | ||
| 582 | - QtUtils::writeFile(file.name, lines); | 580 | + f.close(); |
| 583 | } | 581 | } |
| 584 | 582 | ||
| 585 | TemplateList readBlock(bool *done) | 583 | TemplateList readBlock(bool *done) |
| 586 | { | 584 | { |
| 585 | + *done = false; | ||
| 586 | + if (f.atEnd()) | ||
| 587 | + f.seek(0); | ||
| 588 | + | ||
| 587 | TemplateList templates; | 589 | TemplateList templates; |
| 588 | - foreach (const QString &line, QtUtils::readLines(file)) { | ||
| 589 | - int splitIndex = line.lastIndexOf(' '); | ||
| 590 | - if (splitIndex == -1) templates.append(File(line)); | ||
| 591 | - else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1))); | 590 | + |
| 591 | + for (qint64 i = 0; i < readBlockSize; i++) | ||
| 592 | + { | ||
| 593 | + QByteArray lineBytes = f.readLine(); | ||
| 594 | + QString line(lineBytes); | ||
| 595 | + | ||
| 596 | + if (!line.isEmpty()){ | ||
| 597 | + int splitIndex = line.lastIndexOf(' '); | ||
| 598 | + if (splitIndex == -1) templates.append(File(line)); | ||
| 599 | + else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1))); | ||
| 600 | + } | ||
| 601 | + | ||
| 602 | + if (f.atEnd()) { | ||
| 603 | + *done=true; | ||
| 604 | + break; | ||
| 605 | + } | ||
| 592 | } | 606 | } |
| 593 | - *done = true; | 607 | + |
| 594 | return templates; | 608 | return templates; |
| 595 | } | 609 | } |
| 596 | 610 | ||
| 611 | + void init() | ||
| 612 | + { | ||
| 613 | + f.setFileName(file); | ||
| 614 | + QtUtils::touchDir(f); | ||
| 615 | + if (!f.open(QFile::ReadWrite)) | ||
| 616 | + qFatal("Failed to open %s for read/write.", qPrintable(file)); | ||
| 617 | + } | ||
| 618 | + | ||
| 597 | void write(const Template &t) | 619 | void write(const Template &t) |
| 598 | { | 620 | { |
| 599 | QString line = t.file.name; | 621 | QString line = t.file.name; |
| 600 | if (!label.isEmpty()) | 622 | if (!label.isEmpty()) |
| 601 | line += " " + t.file.get<QString>(label); | 623 | line += " " + t.file.get<QString>(label); |
| 602 | - lines.append(line); | 624 | + |
| 625 | + f.write((line+"\n").toLocal8Bit() ); | ||
| 603 | } | 626 | } |
| 604 | }; | 627 | }; |
| 605 | 628 | ||
| @@ -612,26 +635,49 @@ BR_REGISTER(Gallery, txtGallery) | @@ -612,26 +635,49 @@ BR_REGISTER(Gallery, txtGallery) | ||
| 612 | class flatGallery : public Gallery | 635 | class flatGallery : public Gallery |
| 613 | { | 636 | { |
| 614 | Q_OBJECT | 637 | Q_OBJECT |
| 615 | - QStringList lines; | ||
| 616 | 638 | ||
| 639 | + QFile f; | ||
| 617 | ~flatGallery() | 640 | ~flatGallery() |
| 618 | { | 641 | { |
| 619 | - if (!lines.isEmpty()) | ||
| 620 | - QtUtils::writeFile(file.name, lines); | 642 | + f.close(); |
| 621 | } | 643 | } |
| 622 | 644 | ||
| 623 | TemplateList readBlock(bool *done) | 645 | TemplateList readBlock(bool *done) |
| 624 | { | 646 | { |
| 647 | + *done = false; | ||
| 648 | + if (f.atEnd()) | ||
| 649 | + f.seek(0); | ||
| 650 | + | ||
| 625 | TemplateList templates; | 651 | TemplateList templates; |
| 626 | - foreach (const QString &line, QtUtils::readLines(file)) | ||
| 627 | - templates.append(File(line)); | ||
| 628 | - *done = true; | 652 | + |
| 653 | + for (qint64 i = 0; i < readBlockSize; i++) | ||
| 654 | + { | ||
| 655 | + QByteArray line = f.readLine(); | ||
| 656 | + | ||
| 657 | + if (!line.isEmpty()) | ||
| 658 | + templates.append(File(QString(line))); | ||
| 659 | + | ||
| 660 | + if (f.atEnd()) { | ||
| 661 | + *done=true; | ||
| 662 | + break; | ||
| 663 | + } | ||
| 664 | + } | ||
| 665 | + | ||
| 629 | return templates; | 666 | return templates; |
| 630 | } | 667 | } |
| 631 | 668 | ||
| 669 | + void init() | ||
| 670 | + { | ||
| 671 | + f.setFileName(file); | ||
| 672 | + QtUtils::touchDir(f); | ||
| 673 | + if (!f.open(QFile::ReadWrite)) | ||
| 674 | + qFatal("Failed to open %s for read/write.", qPrintable(file)); | ||
| 675 | + | ||
| 676 | + } | ||
| 677 | + | ||
| 632 | void write(const Template &t) | 678 | void write(const Template &t) |
| 633 | { | 679 | { |
| 634 | - lines.append(t.file.flat()); | 680 | + f.write((t.file.flat()+"\n").toLocal8Bit() ); |
| 635 | } | 681 | } |
| 636 | }; | 682 | }; |
| 637 | 683 |