41 , _flushInterval(flushInterval)
43 , _expireInterval(expireInterval)
47 if (flushInterval > 0)
48 _flushID = startTimer(flushInterval);
62 printf(
"WARNING: deleting dirty cache entry: '%s'\n",qUtf8Printable(
_key));
77 int id =
event->timerId();
114 printf(
"Bad timerId: %d!\n",
id);
138 _data.setObject(json);
291 quint64 size =
object->size();
294 if (QCache<QString, VFS_datastore_cache_entry>::totalCost() + size > (quint64) maxCost())
299 bool i = QCache<QString, VFS_datastore_cache_entry>::insert(key,
object,size);
304 object->connect(
object,SIGNAL(
touched()),
this,SLOT(
touched()),Qt::UniqueConnection);
319 bool s = QCache<QString, VFS_datastore_cache_entry>::remove(e->
_key);
322 VFS::WARN(
"WARN: VFS_datastore_cache tried to remove an entry that wasn't cached.\n");
345 VFS::WARN(
"A cache entry failed to be touched.");
413 QString s = QString(
"Max Cost: %1\nTotal cost: %2\nTotal entries: %3").arg(
_cache.maxCost()).arg(
_cache.totalCost()).arg(
_cache.size());
437 QJsonObject::iterator i =
diff.begin();
441 while (i !=
diff.end())
452 VFS::WARN( QString(
"%1 => %2").arg(i.key()).arg(i.value().toString()) );
454 QString var = i.key();
455 QJsonValue val = i.value();
466 VFS::WARN( QString(
"%1%2 => (removed)").arg(trace).arg(var), L, user );
468 else if (val.isArray())
475 VFS::WARN( QString(
"%1%2 => '%3'").arg(trace).arg(var).arg( QString(QJsonDocument(val.toArray()).toJson()).replace(
'\n',
"") ), L, user );
477 else if (val.isObject() && obj.contains(var) && obj[var].isObject())
481 obj.insert(var,
applyJsonDiff( obj[var].toObject(), val.toObject(), trace+var+
".", user ));
488 if (obj.contains(var))
490 QJsonValue vv = obj[var];
494 QJsonObject oo = vv.toObject();
508 VFS::WARN( QString(
"%1%2 => '%3'").arg(trace).arg(var).arg(val.toString()), L, user );
509 else if (val.isDouble())
510 VFS::WARN( QString(
"%1%2 => %3").arg(trace).arg(var).arg(val.toDouble()), L, user );
511 else if (val.isBool())
512 VFS::WARN( QString(
"%1%2 => %3").arg(trace).arg(var).arg(val.toBool() ?
"true" :
"false"), L, user );
513 else if (val.isObject())
514 VFS::WARN( QString(
"%1%2 => %3").arg(trace).arg(var).arg(
"[object]"), L, user );
516 VFS::WARN( QString(
"%1%2 => %3").arg(trace).arg(var).arg(
"UNKNOWN TYPE"), L, user );
544 QJsonObject delta =
diff[
"delta"].toObject();
546 QString action = delta[
"action"].toString();
547 int srow = delta[
"start"].toObject()[
"row"].toInt();
548 int scol = delta[
"start"].toObject()[
"column"].toInt();
549 int erow = delta[
"end"].toObject()[
"row"].toInt();
550 int ecol = delta[
"end"].toObject()[
"column"].toInt();
552 foreach (
const QJsonValue & value, delta[
"lines"].toArray()) {
553 dlines << value.toString();
557 QStringList lines = data.split(
"\n");
560 VFS::WARN( QString(
"Delta: %1%2 => '%3'").arg(trace).arg(action).arg(dlines.join(
"\n")), L, user );
562 if (action==
"remove")
564 if (srow >= 0 && srow < lines.length())
568 lines[srow].remove(scol,ecol-scol);
570 else if (erow > srow)
574 lines[srow] = lines[srow].left(scol) + lines[erow].remove(0,ecol);
575 for (
int i=srow+1,j=srow+1;i<=erow;i++)
579 VFS::ERROR( QString(
"%1Bad delta. Remove start row greater than end row.").arg(trace), 0, user );
582 VFS::ERROR( QString(
"%1Bad delta. Remove start row out of bounds.").arg(trace), 0, user );
584 else if (action==
"insert")
586 if (srow >= 0 && srow < lines.length())
587 lines[srow] = lines[srow].insert(scol,dlines.join(
"\n"));
589 VFS::ERROR( QString(
"%1Bad delta. Insert start row out of bounds.").arg(trace), 0, user );
595 return lines.join(
"\n");
A pure virtual class which must be subclassed for data storage.
quint64 _size
The size of the data. Always initialized to zero. Must be set in a subclass.
virtual bool valid(QString &reason)
virtual void setData(QJsonObject json)
Write data to the entry.
virtual ~VFS_datastore_cache_entry()
Destroy the cache entry.
void touched()
Signal emitted when a cache entry has made a change and should climb the cache queue.
virtual QByteArray getRawData()
Return the data from an entry.
QString _reason
The reason this may be invalid. Set by a subclass.
void expired(VFS_datastore_cache_entry *)
Signal emitted when a cache entry has expired and is no longer in use.
int _expireInterval
Expire interval, in milliseconds.
QByteArray _rawData
Raw binary data. Used if _raw=true.
virtual QJsonDocument getData()
Return the data from an entry.
bool _valid
Defaults to false. A subclass will determine if _valid is true, like for instance if an underlying fi...
bool _dirty
Changes have occurred but they are not commited to storage yet.
QBasicTimer _expireTimer
The expiration timer.
QString _key
The cache entry key.
virtual void touch()
Reset the expiration timer.
QJsonDocument _data
The actual cache data.
int _flushID
Timer ID for flush events.
virtual void timerEvent(QTimerEvent *e)
Either a flush or expire has happened.
int _expireID
Timer ID for expire events.
virtual void flush()=0
A pure virtual function that must be overridden in subclasses. This is the function that actually com...
VFS_datastore_cache_entry(bool debug, int flushInterval, int expireInterval, bool raw=false)
Construct the cache entry.
void expired(VFS_datastore_cache_entry *)
A cache entry has exipred.
bool insert(const QString &key, VFS_datastore_cache_entry *object)
Insert a new entry.
virtual ~VFS_datastore_cache()
VFS_datastore_cache(quint64 size)
void touched()
A cache entry has been touched.
VFS_datastore_cache _cache
The data cache.
virtual bool isContainer()
All datastore subclasses are containers.
static QString applyDeltaDiff(QString data, QJsonObject delta, QString trace="", QString user="server")
Apply a delta diff to a json object.
VFS_datastore(quint64 size, bool debug=false)
static QJsonObject applyJsonDiff(QJsonObject obj, QJsonObject diff, QString trace="", QString user="server")
Apply a json diff to a json object.
virtual QString reportDetails()
Report the current cache usage.
virtual VFS_node * find(VFS_request *r)
VFS_node is the base class from which all other VFS_node classes derive.
void diff(VFS_node *origin, VFS_request *t)
Emit a diff, which will trigger notifySubscribers() for a mounted node.
The base class for all requests between nodes.
static void ERROR(QString message, int level=0, QString user="server")
Send a message to the VFS::_errors VFS_stream.
static void CRITICAL(QString message)
Send a message to the VFS::_critical VFS_stream.
static void WARN(QString message, int level=0, QString user="server")
Send a message to the VFS::_warnings VFS_stream.