Skip to content

Commit

Permalink
libcore: Banks can decide whether to serialize individual items
Browse files Browse the repository at this point in the history
If a serialized version of the data already exists, there’s no point
in rewriting it.
  • Loading branch information
skyjake committed Feb 15, 2017
1 parent 78a9452 commit c081590
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
5 changes: 3 additions & 2 deletions doomsday/sdk/libcore/include/de/data/bank.h
Expand Up @@ -170,11 +170,12 @@ class DENG2_PUBLIC Bank
public:
virtual ~IData() {}

enum SerialMode { Serializing, Deserializing };
/// Serialization should be done?
virtual bool shouldBeSerialized() const { return true; }

/// Returns an ISerializable pointer to the object. Required
/// for putting the data in hot storage.
virtual ISerializable *asSerializable(SerialMode) { return 0; }
virtual ISerializable *asSerializable() { return 0; }

/// Returns the size of the data that it occupies in memory.
virtual duint sizeInMemory() const { return 0; }
Expand Down
18 changes: 15 additions & 3 deletions doomsday/sdk/libcore/src/data/bank.cpp
Expand Up @@ -222,7 +222,7 @@ DENG2_PIMPL(Bank)
if (isValidSerialTime(timestamp))
{
std::unique_ptr<IData> blank(bank->newData());
reader >> *blank->asSerializable(IData::Deserializing);
reader >> *blank->asSerializable();
setData(blank.release());
LOG_RES_XVERBOSE("Deserialized \"%s\" in %.2f seconds",
path(bank->d->sepChar) << startedAt.since());
Expand Down Expand Up @@ -258,21 +258,33 @@ DENG2_PIMPL(Bank)
loadFromSource();
}

DENG2_ASSERT(data->asSerializable(IData::Serializing) != 0);
DENG2_ASSERT(data->asSerializable() != 0);

try
{
// Make sure the correct folder exists.
Folder &containingFolder = FileSystem::get()
.makeFolder(folderPath / path().toString().fileNamePath());

if (!data->shouldBeSerialized())
{
// Not necessary; the serialized version already exists?
if (File *existing = containingFolder.tryLocate<File>(name()))
{
serial.reset(existing);
return;
}
}

// Source timestamp is included in the serialization
// to check later whether the data is still fresh.
serial.reset(&containingFolder.newFile(name(), Folder::ReplaceExisting));

qDebug() << "Writing to cache:" << serial->description();

Writer(*serial).withHeader()
<< source->modifiedAt()
<< *data->asSerializable(IData::Serializing);
<< *data->asSerializable();
}
catch (...)
{
Expand Down
19 changes: 12 additions & 7 deletions doomsday/sdk/libcore/src/filesys/metadatabank.cpp
Expand Up @@ -33,12 +33,12 @@ DENG2_PIMPL(MetadataBank), public Lockable
struct Data : public IData
{
Block metadata;
bool isChanged = false;

ISerializable *asSerializable(SerialMode mode) override {
if (mode == Serializing && metadata.isEmpty()) {
// Never serialize empty metadata, since it hasn't been set.
return nullptr;
}
bool shouldBeSerialized() const override {
return isChanged;
}
ISerializable *asSerializable() override {
return &metadata;
}
virtual duint sizeInMemory() const override {
Expand All @@ -52,7 +52,7 @@ DENG2_PIMPL(MetadataBank), public Lockable
{
DENG2_ASSERT(!id.isEmpty());
String const hex = id.asHexadecimalText();
return String("%1.%2.%3").arg(category).arg(hex.at(0)).arg(hex);
return String("%1.%2.%3").arg(category).arg(hex.last()).arg(hex);
}
};

Expand Down Expand Up @@ -87,7 +87,12 @@ void MetadataBank::setMetadata(String const &category, Block const &id, Block co
{
Bank::add(path, new Impl::Source(id));
}
data(path).as<Impl::Data>().metadata = metadata;
auto &entry = data(path).as<Impl::Data>();
entry.metadata = metadata;
entry.isChanged = true;

qDebug() << "Set metadata for" << category << id.asHexadecimalText();
DENG2_PRINT_BACKTRACE();
}

Block MetadataBank::metadata(String const &category, Block const &id) const
Expand Down
2 changes: 1 addition & 1 deletion doomsday/sdk/libgui/src/graphics/imagebank.cpp
Expand Up @@ -52,7 +52,7 @@ DENG2_PIMPL_NOREF(ImageBank)
ImageData() {}
ImageData(Image const &img) : image(img) {}

ISerializable *asSerializable(SerialMode)
ISerializable *asSerializable()
{
return &image;
}
Expand Down

0 comments on commit c081590

Please sign in to comment.