diff --git a/ground/openpilotgcs/src/libs/opmapcontrol/src/core/pureimagecache.cpp b/ground/openpilotgcs/src/libs/opmapcontrol/src/core/pureimagecache.cpp index 676ad83a3..7bab34211 100644 --- a/ground/openpilotgcs/src/libs/opmapcontrol/src/core/pureimagecache.cpp +++ b/ground/openpilotgcs/src/libs/opmapcontrol/src/core/pureimagecache.cpp @@ -1,363 +1,363 @@ -/** -****************************************************************************** -* -* @file pureimagecache.cpp -* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. -* @brief -* @see The GNU Public License (GPL) Version 3 -* @defgroup OPMapWidget -* @{ -* -*****************************************************************************/ -/* -* This program 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; either version 3 of the License, or -* (at your option) any later version. -* -* This program 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 this program; if not, write to the Free Software Foundation, Inc., -* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ -#include "pureimagecache.h" -#include - -//#define DEBUG_PUREIMAGECACHE -namespace core { -qlonglong PureImageCache::ConnCounter=0; - -PureImageCache::PureImageCache() -{ - gtilecache=QDir::currentPath()+QDir::separator()+"mapscache"+QDir::separator(); -} - -void PureImageCache::setGtileCache(const QString &value) -{ - gtilecache=value; -} - -QString PureImageCache::GtileCache() -{ - return gtilecache; -} - - -bool PureImageCache::CreateEmptyDB(const QString &file) -{ -#ifdef DEBUG_PUREIMAGECACHE - qDebug()<<"Create database at!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:"< add; - bool ret=true; - QString dir=gtilecache; - - { - QString db=dir+"Data.qmdb"; - ret=QFileInfo(db).exists(); - if(ret) - { - QSqlDatabase cn; - Mcounter.lock(); - qlonglong id=++ConnCounter; - Mcounter.unlock(); - cn = QSqlDatabase::addDatabase("QSQLITE",QString::number(id)); - cn.setDatabaseName(db); - if(cn.open()) - { - { - QSqlQuery query(cn); - query.exec(QString("SELECT id, X, Y, Zoom, Type, Date FROM Tiles")); - while(query.next()) - { - if(QDateTime::fromString(query.value(5).toString()).daysTo(QDateTime::currentDateTime())>days) - add.append(query.value(0).toLongLong()); - } - foreach(long i,add) - { - query.exec(QString("DELETE FROM Tiles WHERE id = %1;").arg(i)); - } - } - - cn.close(); - } - QSqlDatabase::removeDatabase(QString::number(id)); - } - } -} -// PureImageCache::ExportMapDataToDB("C:/Users/Xapo/Documents/mapcontrol/debug/mapscache/data.qmdb","C:/Users/Xapo/Documents/mapcontrol/debug/mapscache/data2.qmdb"); -bool PureImageCache::ExportMapDataToDB(QString sourceFile, QString destFile) -{ - bool ret=true; - QList add; - if(!QFileInfo(destFile).exists()) - { -#ifdef DEBUG_PUREIMAGECACHE - qDebug()<<"Try to create EmptyDB"; -#endif //DEBUG_PUREIMAGECACHE - ret=CreateEmptyDB(destFile); - } - if(!ret) return false; - QSqlDatabase ca = QSqlDatabase::addDatabase("QSQLITE","ca"); - ca.setDatabaseName(sourceFile); - - if(ca.open()) - { - QSqlDatabase cb = QSqlDatabase::addDatabase("QSQLITE","cb"); - cb.setDatabaseName(destFile); - if(cb.open()) - { - QSqlQuery queryb(cb); - queryb.exec(QString("ATTACH DATABASE \"%1\" AS Source").arg(sourceFile)); - QSqlQuery querya(ca); - querya.exec("SELECT id, X, Y, Zoom, Type, Date FROM Tiles"); - while(querya.next()) - { - long id=querya.value(0).toLongLong(); - queryb.exec(QString("SELECT id FROM Tiles WHERE X=%1 AND Y=%2 AND Zoom=%3 AND Type=%4;").arg(querya.value(1).toLongLong()).arg(querya.value(2).toLongLong()).arg(querya.value(3).toLongLong()).arg(querya.value(4).toLongLong())); - if(!queryb.next()) - { - add.append(id); - } - - } - long f; - foreach(f,add) - { - queryb.exec(QString("INSERT INTO Tiles(X, Y, Zoom, Type, Date) SELECT X, Y, Zoom, Type, Date FROM Source.Tiles WHERE id=%1").arg(f)); - queryb.exec(QString("INSERT INTO TilesData(id, Tile) Values((SELECT last_insert_rowid()), (SELECT Tile FROM Source.TilesData WHERE id=%1))").arg(f)); - } - add.clear(); - ca.close(); - cb.close(); - - } - else return false; - } - else return false; - QSqlDatabase::removeDatabase("ca"); - QSqlDatabase::removeDatabase("cb"); - return true; - -} - -} +/** +****************************************************************************** +* +* @file pureimagecache.cpp +* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. +* @brief +* @see The GNU Public License (GPL) Version 3 +* @defgroup OPMapWidget +* @{ +* +*****************************************************************************/ +/* +* This program 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; either version 3 of the License, or +* (at your option) any later version. +* +* This program 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 this program; if not, write to the Free Software Foundation, Inc., +* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#include "pureimagecache.h" +#include + +//#define DEBUG_PUREIMAGECACHE +namespace core { + qlonglong PureImageCache::ConnCounter=0; + + PureImageCache::PureImageCache() + { + gtilecache=QDir::currentPath()+QDir::separator()+"mapscache"+QDir::separator(); + QDir d; + if(!d.exists(gtilecache)) + { + d.mkdir(gtilecache); +#ifdef DEBUG_PUREIMAGECACHE + qDebug()<<"Create Cache directory"; +#endif //DEBUG_PUREIMAGECACHE + } + { + QString db=gtilecache+"Data.qmdb"; + if(!QFileInfo(db).exists()) + { +#ifdef DEBUG_PUREIMAGECACHE + qDebug()<<"Try to create EmptyDB"; +#endif //DEBUG_PUREIMAGECACHE + CreateEmptyDB(db); + } + } + } + + void PureImageCache::setGtileCache(const QString &value) + { + gtilecache=value; + QDir d; + if(!d.exists(gtilecache)) + { + d.mkdir(gtilecache); +#ifdef DEBUG_PUREIMAGECACHE + qDebug()<<"Create Cache directory"; +#endif //DEBUG_PUREIMAGECACHE + } + { + QString db=gtilecache+"Data.qmdb"; + if(!QFileInfo(db).exists()) + { +#ifdef DEBUG_PUREIMAGECACHE + qDebug()<<"Try to create EmptyDB"; +#endif //DEBUG_PUREIMAGECACHE + CreateEmptyDB(db); + } + } + } + QString PureImageCache::GtileCache() + { + return gtilecache; + } + + + bool PureImageCache::CreateEmptyDB(const QString &file) + { +#ifdef DEBUG_PUREIMAGECACHE + qDebug()<<"Create database at!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!:"< add; + bool ret=true; + QString dir=gtilecache; + { + QString db=dir+"Data.qmdb"; + ret=QFileInfo(db).exists(); + if(ret) + { + QSqlDatabase cn; + Mcounter.lock(); + qlonglong id=++ConnCounter; + Mcounter.unlock(); + cn = QSqlDatabase::addDatabase("QSQLITE",QString::number(id)); + cn.setDatabaseName(db); + if(cn.open()) + { + { + QSqlQuery query(cn); + query.exec(QString("SELECT id, X, Y, Zoom, Type, Date FROM Tiles")); + while(query.next()) + { + if(QDateTime::fromString(query.value(5).toString()).daysTo(QDateTime::currentDateTime())>days) + add.append(query.value(0).toLongLong()); + } + foreach(long i,add) + { + query.exec(QString("DELETE FROM Tiles WHERE id = %1;").arg(i)); + } + } + + cn.close(); + } + QSqlDatabase::removeDatabase(QString::number(id)); + } + } + } + // PureImageCache::ExportMapDataToDB("C:/Users/Xapo/Documents/mapcontrol/debug/mapscache/data.qmdb","C:/Users/Xapo/Documents/mapcontrol/debug/mapscache/data2.qmdb"); + bool PureImageCache::ExportMapDataToDB(QString sourceFile, QString destFile) + { + bool ret=true; + QList add; + if(!QFileInfo(destFile).exists()) + { +#ifdef DEBUG_PUREIMAGECACHE + qDebug()<<"Try to create EmptyDB"; +#endif //DEBUG_PUREIMAGECACHE + ret=CreateEmptyDB(destFile); + } + if(!ret) return false; + QSqlDatabase ca = QSqlDatabase::addDatabase("QSQLITE","ca"); + ca.setDatabaseName(sourceFile); + + if(ca.open()) + { + QSqlDatabase cb = QSqlDatabase::addDatabase("QSQLITE","cb"); + cb.setDatabaseName(destFile); + if(cb.open()) + { + QSqlQuery queryb(cb); + queryb.exec(QString("ATTACH DATABASE \"%1\" AS Source").arg(sourceFile)); + QSqlQuery querya(ca); + querya.exec("SELECT id, X, Y, Zoom, Type, Date FROM Tiles"); + while(querya.next()) + { + long id=querya.value(0).toLongLong(); + queryb.exec(QString("SELECT id FROM Tiles WHERE X=%1 AND Y=%2 AND Zoom=%3 AND Type=%4;").arg(querya.value(1).toLongLong()).arg(querya.value(2).toLongLong()).arg(querya.value(3).toLongLong()).arg(querya.value(4).toLongLong())); + if(!queryb.next()) + { + add.append(id); + } + + } + long f; + foreach(f,add) + { + queryb.exec(QString("INSERT INTO Tiles(X, Y, Zoom, Type, Date) SELECT X, Y, Zoom, Type, Date FROM Source.Tiles WHERE id=%1").arg(f)); + queryb.exec(QString("INSERT INTO TilesData(id, Tile) Values((SELECT last_insert_rowid()), (SELECT Tile FROM Source.TilesData WHERE id=%1))").arg(f)); + } + add.clear(); + ca.close(); + cb.close(); + + } + else return false; + } + else return false; + QSqlDatabase::removeDatabase("ca"); + QSqlDatabase::removeDatabase("cb"); + return true; + + } + +} diff --git a/ground/openpilotgcs/src/libs/opmapcontrol/src/core/tilecachequeue.cpp b/ground/openpilotgcs/src/libs/opmapcontrol/src/core/tilecachequeue.cpp index 130963a24..166e66a29 100644 --- a/ground/openpilotgcs/src/libs/opmapcontrol/src/core/tilecachequeue.cpp +++ b/ground/openpilotgcs/src/libs/opmapcontrol/src/core/tilecachequeue.cpp @@ -1,128 +1,128 @@ -/** -****************************************************************************** -* -* @file tilecachequeue.cpp -* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. -* @brief -* @see The GNU Public License (GPL) Version 3 -* @defgroup OPMapWidget -* @{ -* -*****************************************************************************/ -/* -* This program 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; either version 3 of the License, or -* (at your option) any later version. -* -* This program 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 this program; if not, write to the Free Software Foundation, Inc., -* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -*/ -#include "tilecachequeue.h" - - -//#define DEBUG_TILECACHEQUEUE - -namespace core { -TileCacheQueue::TileCacheQueue() -{ - -} -TileCacheQueue::~TileCacheQueue() -{ - // QThread::wait(10000); -} - -void TileCacheQueue::EnqueueCacheTask(CacheItemQueue *task) -{ -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"DB Do I EnqueueCacheTask"<GetPosition().X()<<","<GetPosition().Y(); -#endif //DEBUG_TILECACHEQUEUE - if(!tileCacheQueue.contains(task)) - { -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"EnqueueCacheTask"<GetPosition().X()<<","<GetPosition().Y(); -#endif //DEBUG_TILECACHEQUEUE - mutex.lock(); - tileCacheQueue.enqueue(task); - mutex.unlock(); - if(this->isRunning()) - { -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Wake Thread"; -#endif //DEBUG_TILECACHEQUEUE - this->start(QThread::NormalPriority); - //waitmutex.lock(); - waitc.wakeAll(); - //waitmutex.unlock(); - } - else - { -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Start Thread"; -#endif //DEBUG_TILECACHEQUEUE - this->start(QThread::NormalPriority); - } - } - -} -void TileCacheQueue::run() -{ -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Cache Engine Start"; -#endif //DEBUG_TILECACHEQUEUE - while(true) - { - CacheItemQueue *task; -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Cache"; -#endif //DEBUG_TILECACHEQUEUE - if(tileCacheQueue.count()>0) - { - mutex.lock(); - task=tileCacheQueue.dequeue(); - mutex.unlock(); -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Cache engine Put:"<GetPosition().X()<<","<GetPosition().Y(); -#endif //DEBUG_TILECACHEQUEUE - Cache::Instance()->ImageCache.PutImageToCache(task->GetImg(),task->GetMapType(),task->GetPosition(),task->GetZoom()); - usleep(44); - delete task; - } - - else - { - qDebug()<<"Cache engine BEGIN WAIT"; - waitmutex.lock(); - int tout=4000; - if(!waitc.wait(&waitmutex,tout)) - { - waitmutex.unlock(); -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Cache Engine TimeOut"; -#endif //DEBUG_TILECACHEQUEUE - mutex.lock(); - if(tileCacheQueue.count()==0) - { - mutex.unlock(); - break; - } - mutex.unlock(); - } - qDebug()<<"Cache Engine DID NOT TimeOut"; - waitmutex.unlock(); - } - } -#ifdef DEBUG_TILECACHEQUEUE - qDebug()<<"Cache Engine Stopped"; -#endif //DEBUG_TILECACHEQUEUE -} - - -} +/** +****************************************************************************** +* +* @file tilecachequeue.cpp +* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. +* @brief +* @see The GNU Public License (GPL) Version 3 +* @defgroup OPMapWidget +* @{ +* +*****************************************************************************/ +/* +* This program 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; either version 3 of the License, or +* (at your option) any later version. +* +* This program 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 this program; if not, write to the Free Software Foundation, Inc., +* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +#include "tilecachequeue.h" + + +//#define DEBUG_TILECACHEQUEUE + +namespace core { +TileCacheQueue::TileCacheQueue() +{ + +} +TileCacheQueue::~TileCacheQueue() +{ + // QThread::wait(10000); +} + +void TileCacheQueue::EnqueueCacheTask(CacheItemQueue *task) +{ +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"DB Do I EnqueueCacheTask"<GetPosition().X()<<","<GetPosition().Y(); +#endif //DEBUG_TILECACHEQUEUE + if(!tileCacheQueue.contains(task)) + { +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"EnqueueCacheTask"<GetPosition().X()<<","<GetPosition().Y(); +#endif //DEBUG_TILECACHEQUEUE + mutex.lock(); + tileCacheQueue.enqueue(task); + mutex.unlock(); + if(this->isRunning()) + { +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Wake Thread"; +#endif //DEBUG_TILECACHEQUEUE + //this->start(QThread::NormalPriority); + //waitmutex.lock(); + waitc.wakeAll(); + //waitmutex.unlock(); + } + else + { +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Start Thread"; +#endif //DEBUG_TILECACHEQUEUE + this->start(QThread::NormalPriority); + } + } + +} +void TileCacheQueue::run() +{ +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Cache Engine Start"; +#endif //DEBUG_TILECACHEQUEUE + while(true) + { + CacheItemQueue *task; +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Cache"; +#endif //DEBUG_TILECACHEQUEUE + if(tileCacheQueue.count()>0) + { + mutex.lock(); + task=tileCacheQueue.dequeue(); + mutex.unlock(); +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Cache engine Put:"<GetPosition().X()<<","<GetPosition().Y(); +#endif //DEBUG_TILECACHEQUEUE + Cache::Instance()->ImageCache.PutImageToCache(task->GetImg(),task->GetMapType(),task->GetPosition(),task->GetZoom()); + usleep(44); + delete task; + } + + else + { + qDebug()<<"Cache engine BEGIN WAIT"; + waitmutex.lock(); + int tout=4000; + if(!waitc.wait(&waitmutex,tout)) + { + waitmutex.unlock(); +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Cache Engine TimeOut"; +#endif //DEBUG_TILECACHEQUEUE + mutex.lock(); + if(tileCacheQueue.count()==0) + { + mutex.unlock(); + break; + } + mutex.unlock(); + } + qDebug()<<"Cache Engine DID NOT TimeOut"; + waitmutex.unlock(); + } + } +#ifdef DEBUG_TILECACHEQUEUE + qDebug()<<"Cache Engine Stopped"; +#endif //DEBUG_TILECACHEQUEUE +} + + +}