Threaded the storage of the scene. Asynchronous reading of the previous and next scene to be stored on cache.

preferencesAboutTextFull
-f 11 years ago
parent 817b69fd4f
commit 61e588bb94
  1. 31
      src/adaptors/UBSvgSubsetAdaptor.cpp
  2. 5
      src/adaptors/UBSvgSubsetAdaptor.h
  3. 49
      src/core/UBPersistenceManager.cpp
  4. 9
      src/core/UBPersistenceManager.h
  5. 69
      src/core/UBPersistenceWorker.cpp
  6. 68
      src/core/UBPersistenceWorker.h
  7. 6
      src/core/core.pri

@ -256,6 +256,29 @@ UBGraphicsScene* UBSvgSubsetAdaptor::loadScene(UBDocumentProxy* proxy, const int
}
QByteArray UBSvgSubsetAdaptor::loadSceneAsText(UBDocumentProxy* proxy, const int pageIndex)
{
QString fileName = proxy->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.svg", pageIndex);
qDebug() << fileName;
QFile file(fileName);
if (file.exists())
{
if (!file.open(QIODevice::ReadOnly))
{
qWarning() << "Cannot open file " << fileName << " for reading ...";
return "";
}
return file.readAll();
file.close();
}
return "";
}
QUuid UBSvgSubsetAdaptor::sceneUuid(UBDocumentProxy* proxy, const int pageIndex)
{
QString fileName = proxy->persistencePath() + UBFileSystemUtils::digitFileFormat("/page%1.svg", pageIndex);
@ -1019,8 +1042,6 @@ void UBSvgSubsetAdaptor::UBSvgSubsetWriter::writeSvgElement()
bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistScene(int pageIndex)
{
Q_UNUSED(pageIndex);
if (mScene->isModified())
{
//Creating dom structure to store information
QDomDocument groupDomDocument;
@ -1315,12 +1336,6 @@ bool UBSvgSubsetAdaptor::UBSvgSubsetWriter::persistScene(int pageIndex)
file.flush();
file.close();
}
else
{
qDebug() << "ignoring unmodified page" << UBApplication::boardController->pageFromSceneIndex(mPageIndex);
}
return true;
}

@ -65,6 +65,9 @@ class UBSvgSubsetAdaptor
public:
static UBGraphicsScene* loadScene(UBDocumentProxy* proxy, const int pageIndex);
static QByteArray loadSceneAsText(UBDocumentProxy* proxy, const int pageIndex);
static UBGraphicsScene* loadScene(UBDocumentProxy* proxy, const QByteArray& pArray);
static void persistScene(UBDocumentProxy* proxy, UBGraphicsScene* pScene, const int pageIndex);
static void upgradeScene(UBDocumentProxy* proxy, const int pageIndex);
@ -88,8 +91,6 @@ class UBSvgSubsetAdaptor
private:
static UBGraphicsScene* loadScene(UBDocumentProxy* proxy, const QByteArray& pArray);
static QDomDocument loadSceneDocument(UBDocumentProxy* proxy, const int pPageIndex);
static QString uniboardDocumentNamespaceUriFromVersion(int fileVersion);

@ -75,6 +75,17 @@ UBPersistenceManager::UBPersistenceManager(QObject *pParent)
documentProxies = allDocumentProxies();
mThread = new QThread;
mWorker = new UBPersistenceWorker();
mWorker->moveToThread(mThread);
connect(mWorker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(mThread, SIGNAL(started()), mWorker, SLOT(process()));
connect(mWorker, SIGNAL(finished()), mThread, SLOT(quit()));
connect(mWorker, SIGNAL(finished()), mWorker, SLOT(deleteLater()));
connect(mThread, SIGNAL(finished()), mThread, SLOT(deleteLater()));
connect(mWorker,SIGNAL(sceneLoaded(QByteArray,UBDocumentProxy*,int)),this,SLOT(onSceneLoaded(QByteArray,UBDocumentProxy*,int)));
mThread->start();
}
UBPersistenceManager* UBPersistenceManager::persistenceManager()
@ -96,11 +107,31 @@ void UBPersistenceManager::destroy()
UBPersistenceManager::~UBPersistenceManager()
{
if(mWorker)
mWorker->applicationWillClose();
foreach(QPointer<UBDocumentProxy> proxyGuard, documentProxies)
{
if (!proxyGuard.isNull())
delete proxyGuard.data();
}
// to be sure that all the scenes are stored on disk
mThread->wait(10*1000);
}
void UBPersistenceManager::errorString(QString error)
{
qDebug() << "peristence thread return the error " << error;
}
void UBPersistenceManager::onSceneLoaded(QByteArray scene, UBDocumentProxy* proxy, int sceneIndex)
{
qDebug() << "scene loaded " << sceneIndex;
QTime time;
time.start();
mSceneCache.insert(proxy,sceneIndex,UBSvgSubsetAdaptor::loadScene(proxy,scene));
qDebug() << "millisecond for sceneCache " << time.elapsed();
}
QList<QPointer<UBDocumentProxy> > UBPersistenceManager::allDocumentProxies()
@ -653,16 +684,24 @@ void UBPersistenceManager::moveSceneToIndex(UBDocumentProxy* proxy, int source,
UBGraphicsScene* UBPersistenceManager::loadDocumentScene(UBDocumentProxy* proxy, int sceneIndex)
{
UBGraphicsScene* scene = NULL;
if (mSceneCache.contains(proxy, sceneIndex))
return mSceneCache.value(proxy, sceneIndex);
scene = mSceneCache.value(proxy, sceneIndex);
else {
UBGraphicsScene* scene = UBSvgSubsetAdaptor::loadScene(proxy, sceneIndex);
scene = UBSvgSubsetAdaptor::loadScene(proxy, sceneIndex);
if (scene)
mSceneCache.insert(proxy, sceneIndex, scene);
}
if(sceneIndex + 1 < proxy->pageCount() && !mSceneCache.contains(proxy, sceneIndex + 1))
mWorker->readScene(proxy,sceneIndex+1);
if(sceneIndex - 1 >= 0 && !mSceneCache.contains(proxy, sceneIndex - 1))
mWorker->readScene(proxy,sceneIndex-1);
return scene;
}
}
void UBPersistenceManager::persistDocumentScene(UBDocumentProxy* pDocumentProxy, UBGraphicsScene* pScene, const int pSceneIndex, bool isAnAutomaticBackup)
@ -682,10 +721,8 @@ void UBPersistenceManager::persistDocumentScene(UBDocumentProxy* pDocumentProxy,
if (pScene->isModified())
{
UBSvgSubsetAdaptor::persistScene(pDocumentProxy, pScene, pSceneIndex);
UBThumbnailAdaptor::persistScene(pDocumentProxy, pScene, pSceneIndex);
mWorker->saveScene(pDocumentProxy, pScene, pSceneIndex);
pScene->setModified(false);
}

@ -32,6 +32,8 @@
#include "UBSceneCache.h"
#include "UBPersistenceWorker.h"
class UBDocument;
class UBDocumentProxy;
class UBGraphicsScene;
@ -148,11 +150,14 @@ class UBPersistenceManager : public QObject
QString mDocumentRepositoryPath;
QHash<int,QString>teacherBarNodeString;
UBPersistenceWorker* mWorker;
QThread* mThread;
private slots:
void documentRepositoryChanged(const QString& path);
void errorString(QString error);
void onSceneLoaded(QByteArray,UBDocumentProxy*,int);
};

@ -0,0 +1,69 @@
/*
* Copyright (C) 2013 Open Education Foundation
*
*
* This file is part of OpenBoard.
*
* OpenBoard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* OpenBoard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UBPersistenceWorker.h"
#include "adaptors/UBSvgSubsetAdaptor.h"
#include "adaptors/UBThumbnailAdaptor.h"
UBPersistenceWorker::UBPersistenceWorker(QObject *parent) :
QObject(parent)
, mReceivedApplicationClosing(false)
{
}
void UBPersistenceWorker::saveScene(UBDocumentProxy* proxy, UBGraphicsScene* scene, const int pageIndex)
{
saves.append({WriteScene,proxy,scene,pageIndex});
mSemaphore.release();
}
void UBPersistenceWorker::readScene(UBDocumentProxy* proxy, const int pageIndex)
{
saves.append({ReadScene,proxy,0,pageIndex});
mSemaphore.release();
}
void UBPersistenceWorker::applicationWillClose()
{
qDebug() << "applicaiton Will close signal received";
mReceivedApplicationClosing = true;
mSemaphore.release();
}
void UBPersistenceWorker::process()
{
qDebug() << "process starts";
mSemaphore.acquire();
do{
PersistenceInformation info = saves.takeFirst();
if(info.action == WriteScene)
UBSvgSubsetAdaptor::persistScene(info.proxy, info.scene, info.sceneIndex);
else{
emit sceneLoaded(UBSvgSubsetAdaptor::loadSceneAsText(info.proxy,info.sceneIndex), info.proxy, info.sceneIndex);
}
mSemaphore.acquire();
}while(!mReceivedApplicationClosing);
qDebug() << "process will stop";
emit finished();
}

@ -0,0 +1,68 @@
/*
* Copyright (C) 2013-2014 Open Education Foundation
*
*
* This file is part of OpenBoard.
*
* OpenBoard is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License,
* with a specific linking exception for the OpenSSL project's
* "OpenSSL" library (or with modified versions of it that use the
* same license as the "OpenSSL" library).
*
* OpenBoard is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenBoard. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UBPERSISTENCEWORKER_H
#define UBPERSISTENCEWORKER_H
#include <QObject>
#include <QSemaphore>
#include "document/UBDocumentProxy.h"
#include "domain/UBGraphicsScene.h"
typedef enum{
WriteScene = 0,
ReadScene
}ActionType;
typedef struct{
ActionType action;
UBDocumentProxy* proxy;
UBGraphicsScene* scene;
int sceneIndex;
}PersistenceInformation;
class UBPersistenceWorker : public QObject
{
Q_OBJECT
public:
explicit UBPersistenceWorker(QObject *parent = 0);
void saveScene(UBDocumentProxy* proxy, UBGraphicsScene* scene, const int pageIndex);
void readScene(UBDocumentProxy* proxy, const int pageIndex);
signals:
void finished();
void error(QString string);
void sceneLoaded(QByteArray text,UBDocumentProxy* proxy, const int pageIndex);
public slots:
void process();
void applicationWillClose();
protected:
bool mReceivedApplicationClosing;
QSemaphore mSemaphore;
QList<PersistenceInformation> saves;
};
#endif // UBPERSISTENCEWORKER_H

@ -14,7 +14,8 @@ HEADERS += src/core/UB.h \
src/core/UBDownloadManager.h \
src/core/UBDownloadThread.h \
src/core/UBOpenSankoreImporter.h \
src/core/UBTextTools.h
src/core/UBTextTools.h \
src/core/UBPersistenceWorker.h
SOURCES += src/core/main.cpp \
src/core/UBApplication.cpp \
@ -31,6 +32,7 @@ SOURCES += src/core/main.cpp \
src/core/UBDownloadManager.cpp \
src/core/UBDownloadThread.cpp \
src/core/UBOpenSankoreImporter.cpp \
src/core/UBTextTools.cpp
src/core/UBTextTools.cpp \
src/core/UBPersistenceWorker.cpp

Loading…
Cancel
Save