pluralize mappings in AssetServer naming

This commit is contained in:
Stephen Birarda 2016-03-08 10:27:35 -08:00
parent a3533b0b7d
commit 7bf6b8405c
2 changed files with 25 additions and 25 deletions

View file

@ -106,7 +106,7 @@ void AssetServer::completeSetup() {
_filesDirectory.cd(ASSET_FILES_SUBDIR); _filesDirectory.cd(ASSET_FILES_SUBDIR);
// load whatever mappings we currently have from the local file // load whatever mappings we currently have from the local file
loadMappingFromFile(); loadMappingsFromFile();
qInfo() << "Serving files from: " << _filesDirectory.path(); qInfo() << "Serving files from: " << _filesDirectory.path();
@ -155,12 +155,12 @@ void AssetServer::performMappingMigration() {
qDebug() << "\tAdding a migration mapping from" << fakeFileName << "to" << hash; qDebug() << "\tAdding a migration mapping from" << fakeFileName << "to" << hash;
auto it = _fileMapping.find(fakeFileName); auto it = _fileMappings.find(fakeFileName);
if (it == _fileMapping.end()) { if (it == _fileMappings.end()) {
_fileMapping[fakeFileName] = hash; _fileMappings[fakeFileName] = hash;
if (writeMappingToFile()) { if (writeMappingsToFile()) {
// mapping added and persisted, we can remove the migrated file // mapping added and persisted, we can remove the migrated file
oldFile.remove(); oldFile.remove();
qDebug() << "\tMigration completed for" << oldFilename; qDebug() << "\tMigration completed for" << oldFilename;
@ -206,8 +206,8 @@ void AssetServer::handleAssetMappingOperation(QSharedPointer<ReceivedMessage> me
void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket) { void AssetServer::handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket) {
QString assetPath = message.readString(); QString assetPath = message.readString();
auto it = _fileMapping.find(assetPath); auto it = _fileMappings.find(assetPath);
if (it != _fileMapping.end()) { if (it != _fileMappings.end()) {
auto assetHash = it->toString(); auto assetHash = it->toString();
qDebug() << "Found mapping for: " << assetPath << "=>" << assetHash; qDebug() << "Found mapping for: " << assetPath << "=>" << assetHash;
replyPacket.writePrimitive(AssetServerError::NoError); replyPacket.writePrimitive(AssetServerError::NoError);
@ -395,7 +395,7 @@ void AssetServer::sendStatsPacket() {
static const QString MAP_FILE_NAME = "map.json"; static const QString MAP_FILE_NAME = "map.json";
void AssetServer::loadMappingFromFile() { void AssetServer::loadMappingsFromFile() {
auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME);
@ -403,9 +403,9 @@ void AssetServer::loadMappingFromFile() {
if (mapFile.exists()) { if (mapFile.exists()) {
if (mapFile.open(QIODevice::ReadOnly)) { if (mapFile.open(QIODevice::ReadOnly)) {
auto jsonDocument = QJsonDocument::fromJson(mapFile.readAll()); auto jsonDocument = QJsonDocument::fromJson(mapFile.readAll());
_fileMapping = jsonDocument.object().toVariantHash(); _fileMappings = jsonDocument.object().toVariantHash();
qInfo() << "Loaded" << _fileMapping.count() << "mappings from map file at" << mapFilePath; qInfo() << "Loaded" << _fileMappings.count() << "mappings from map file at" << mapFilePath;
} else { } else {
qCritical() << "Failed to read mapping file at" << mapFilePath << "- assignment with not continue."; qCritical() << "Failed to read mapping file at" << mapFilePath << "- assignment with not continue.";
setFinished(true); setFinished(true);
@ -415,12 +415,12 @@ void AssetServer::loadMappingFromFile() {
} }
} }
bool AssetServer::writeMappingToFile() { bool AssetServer::writeMappingsToFile() {
auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME); auto mapFilePath = _resourcesDirectory.absoluteFilePath(MAP_FILE_NAME);
QFile mapFile { mapFilePath }; QFile mapFile { mapFilePath };
if (mapFile.open(QIODevice::WriteOnly)) { if (mapFile.open(QIODevice::WriteOnly)) {
auto jsonObject = QJsonObject::fromVariantHash(_fileMapping); auto jsonObject = QJsonObject::fromVariantHash(_fileMappings);
QJsonDocument jsonDocument { jsonObject }; QJsonDocument jsonDocument { jsonObject };
if (mapFile.write(jsonDocument.toJson()) != -1) { if (mapFile.write(jsonDocument.toJson()) != -1) {
@ -437,26 +437,26 @@ bool AssetServer::writeMappingToFile() {
} }
AssetHash AssetServer::getMapping(AssetPath path) { AssetHash AssetServer::getMapping(AssetPath path) {
return _fileMapping.value(path).toString(); return _fileMappings.value(path).toString();
} }
bool AssetServer::setMapping(AssetPath path, AssetHash hash) { bool AssetServer::setMapping(AssetPath path, AssetHash hash) {
// remember what the old mapping was in case persistence fails // remember what the old mapping was in case persistence fails
auto oldMapping = _fileMapping.value(path).toString(); auto oldMapping = _fileMappings.value(path).toString();
// update the in memory QHash // update the in memory QHash
_fileMapping[path] = hash; _fileMappings[path] = hash;
// attempt to write to file // attempt to write to file
if (writeMappingToFile()) { if (writeMappingsToFile()) {
// persistence succeeded, we are good to go // persistence succeeded, we are good to go
return true; return true;
} else { } else {
// failed to persist this mapping to file - put back the old one in our in-memory representation // failed to persist this mapping to file - put back the old one in our in-memory representation
if (oldMapping.isEmpty()) { if (oldMapping.isEmpty()) {
_fileMapping.remove(path); _fileMappings.remove(path);
} else { } else {
_fileMapping[path] = oldMapping; _fileMappings[path] = oldMapping;
} }
return false; return false;
@ -465,16 +465,16 @@ bool AssetServer::setMapping(AssetPath path, AssetHash hash) {
bool AssetServer::deleteMapping(AssetPath path) { bool AssetServer::deleteMapping(AssetPath path) {
// keep the old mapping in case the delete fails // keep the old mapping in case the delete fails
auto oldMapping = _fileMapping.take(path); auto oldMapping = _fileMappings.take(path);
if (!oldMapping.isNull()) { if (!oldMapping.isNull()) {
// deleted the old mapping, attempt to persist to file // deleted the old mapping, attempt to persist to file
if (writeMappingToFile()) { if (writeMappingsToFile()) {
// persistence succeeded we are good to go // persistence succeeded we are good to go
return true; return true;
} else { } else {
// we didn't delete the previous mapping, put it back in our in-memory representation // we didn't delete the previous mapping, put it back in our in-memory representation
_fileMapping[path] = oldMapping.toString(); _fileMappings[path] = oldMapping.toString();
} }
} }

View file

@ -39,15 +39,15 @@ private slots:
void sendStatsPacket(); void sendStatsPacket();
private: private:
using Mapping = QVariantHash; using Mappings = QVariantHash;
void handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket); void handleGetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket);
void handleSetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket); void handleSetMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket);
void handleDeleteMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket); void handleDeleteMappingOperation(ReceivedMessage& message, SharedNodePointer senderNode, NLPacket& replyPacket);
// Mapping file operations must be called from main assignment thread only // Mapping file operations must be called from main assignment thread only
void loadMappingFromFile(); void loadMappingsFromFile();
bool writeMappingToFile(); bool writeMappingsToFile();
/// Return the hash mapping for AssetPath `path` /// Return the hash mapping for AssetPath `path`
AssetHash getMapping(AssetPath path); AssetHash getMapping(AssetPath path);
@ -62,7 +62,7 @@ private:
void performMappingMigration(); void performMappingMigration();
QVariantHash _fileMapping; Mappings _fileMappings;
QDir _resourcesDirectory; QDir _resourcesDirectory;
QDir _filesDirectory; QDir _filesDirectory;
QThreadPool _taskPool; QThreadPool _taskPool;