new operations for Storage:

* expire, with age and group as parameters
* delete, with group and key as parameters

new db structure: creation time (still unused, but would be hard to add in future releases)
and access time, used for expire

svn path=/trunk/KDE/kdelibs/; revision=1192402
This commit is contained in:
Marco Martin 2010-11-02 22:05:48 +00:00
parent ce3e6edaeb
commit dd6b783e13
4 changed files with 72 additions and 20 deletions

View File

@ -4,10 +4,10 @@
<kcfg>
<group name="save">
<entry name="group" type="String">
<label>Name of the source</label>
<label>Group the key/value saved belongs to</label>
</entry>
<entry name="key" type="String">
<label>Fill</label>
<label>The key that indicizes the value in the storage</label>
</entry>
<entry name="data" type="Int">
<label>The actual data to be stored.</label>
@ -21,10 +21,27 @@
</group>
<group name="retrieve">
<entry name="group" type="String">
<label>Name of the source</label>
<label>Group the key/value saved belongs to</label>
</entry>
<entry name="key" type="String">
<label>Fill</label>
</entry>
<label>The key that indicizes the value in the storage. If empty all group will be retrieved.</label>
</entry>
</group>
<group name="delete">
<entry name="group" type="String">
<label>Group the key/value saved belongs to</label>
</entry>
<entry name="key" type="String">
<label>The key that indicizes the value in the storage. If empty all group will be deleted</label>
</entry>
</group>
<group name="expire">
<entry name="group" type="String">
<label>Group the key/value saved belongs to. If empty all ntries will be checked for expiration.</label>
</entry>
<entry name="age" type="UInt">
<label>Set the age the stored data will expire in seconds. Default is 4 days.</label>
<default>345600</default>
</entry>
</group>
</kcfg>

View File

@ -275,8 +275,12 @@ void DataContainerPrivate::populateFromStoredData(KJob *job)
}
}
if (!(data.isEmpty()))
{
KConfigGroup expireGroup = storage->operationDescription("expire");
//expire things older than 4 days
expireGroup.writeEntry("age", 345600);
storage->startOperationCall(expireGroup);
if (!(data.isEmpty())) {
//Do not fill the source with old stored
//data if it is already populated with new data.
return;

View File

@ -53,7 +53,7 @@ StorageJob::StorageJob(const QString& destination,
} else if (!m_db.tables().contains(m_clientName)) {
QSqlQuery query(m_db);
//bindValue doesn't seem to be able to replace stuff in create table
query.prepare(QString("create table ")+m_clientName+" (valueGroup varchar(256), id varchar(256), data clob, date datetime, primary key (valueGroup, id))");
query.prepare(QString("create table ")+m_clientName+" (valueGroup varchar(256), id varchar(256), data clob, creationTime datetime, accessTime datetime, primary key (valueGroup, id))");
query.exec();
}
}
@ -79,7 +79,7 @@ void StorageJob::start()
query.bindValue(":id", params["key"].toString());
query.exec();
query.prepare("insert into "+m_clientName+" values(:valueGroup, :id, :datavalue, date('now'))");
query.prepare("insert into "+m_clientName+" values(:valueGroup, :id, :datavalue, date('now'), date('now'))");
query.bindValue(":id", params["key"].toString());
query.bindValue(":valueGroup", valueGroup);
query.bindValue(":datavalue", params["data"]);
@ -89,17 +89,23 @@ void StorageJob::start()
} else if (operationName() == "retrieve") {
QSqlQuery query(m_db);
query.prepare("delete from "+m_clientName+" where date < :date");
QDateTime time(QDateTime::currentDateTime());
time.addDays(-2);
query.bindValue(":date", time.toTime_t());
query.exec();
//a bit redundant but should be the faster way with less string concatenation as possible
if (params["key"].toString().isEmpty()) {
//update modification time
query.prepare("update "+m_clientName+" set accessTime=date('now') where valueGroup=:valueGroup");
query.bindValue(":valueGroup", valueGroup);
query.exec();
query.prepare("select * from "+m_clientName+" where valueGroup=:valueGroup");
query.bindValue(":valueGroup", valueGroup);
} else {
//update modification time
query.prepare("update "+m_clientName+" set accessTime=date('now') where valueGroup=:valueGroup and id=:key");
query.bindValue(":valueGroup", valueGroup);
query.bindValue(":key", params["key"].toString());
query.exec();
query.prepare("select * from "+m_clientName+" where valueGroup=:valueGroup and id=:key");
query.bindValue(":valueGroup", valueGroup);
query.bindValue(":key", params["key"].toString());
@ -123,13 +129,38 @@ void StorageJob::start()
return;
}
} else if (operationName() == "delete") {
QSqlQuery query(m_db);
if (params["key"].toString().isEmpty()) {
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup");
query.bindValue(":valueGroup", valueGroup);
} else {
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and id=:key");
query.bindValue(":valueGroup", valueGroup);
query.bindValue(":key", params["key"].toString());
}
const bool success = query.exec();
setResult(success);
} else if (operationName() == "expire") {
QSqlQuery query(m_db);
query.prepare("delete from "+m_clientName+" where date < :date");
QDateTime time(QDateTime::currentDateTime());
time.addDays(-2);
query.bindValue(":date", time.toTime_t());
query.exec();
if (valueGroup.isEmpty()) {
query.prepare("delete from "+m_clientName+" where accessTime < :date");
QDateTime time(QDateTime::currentDateTime());
time.addSecs(-params["age"].toUInt());
query.bindValue(":date", time.toTime_t());
} else {
query.prepare("delete from "+m_clientName+" where valueGroup=:valueGroup and accessTime < :date");
query.bindValue(":valueGroup", valueGroup);
QDateTime time(QDateTime::currentDateTime());
time.addSecs(-params["age"].toUInt());
query.bindValue(":date", time.toTime_t());
}
const bool success = query.exec();
setResult(success);
} else {
setError(true);

View File

@ -235,7 +235,7 @@ ServiceJob *Service::startOperationCall(const KConfigGroup &description, QObject
job = createJob(op, params);
}
} else {
kDebug() << "Not a valid group!";
kDebug() << "Not a valid group!"<<d->config->groupList();
}
if (!job) {