On Fri, Jul 21, 2017 at 15:30:09 +0200, Pavel Hrdina wrote:
The new virFileCache will nicely handle the caching logic for any
data
that we would like to cache. For each type of data we will just need
to implement few handlers that will take care of creating, validating,
loading and saving the cached data.
The cached data must be an instance of virObject.
Currently we cache QEMU capabilities which will start using
virFileCache.
Signed-off-by: Pavel Hrdina <phrdina(a)redhat.com>
---
Notes:
Changes in v2:
- added suffix argument into virFileCacheNew()
- fixed locking for virFileCache APIs
- moved virObjectUnref() into virFileCacheLoad()
po/POTFILES.in | 1 +
src/Makefile.am | 1 +
src/libvirt_private.syms | 9 +
src/util/virfilecache.c | 439 +++++++++++++++++++++++++++++++++++++++++++++++
src/util/virfilecache.h | 137 +++++++++++++++
5 files changed, 587 insertions(+)
create mode 100644 src/util/virfilecache.c
create mode 100644 src/util/virfilecache.h
...
diff --git a/src/util/virfilecache.c b/src/util/virfilecache.c
new file mode 100644
index 0000000000..0401253146
--- /dev/null
+++ b/src/util/virfilecache.c
@@ -0,0 +1,439 @@
...
+/**
+ * virFileCacheNew:
+ * @dir: the cache directory where all the cache files will be stored
+ * @suffix: the cache file suffix or NULL if no suffix is required
+ * @handlers: filled structure with all required handlers
+ *
+ * Creates a new cache object which handles caching any data to files
+ * stored on a filesystem.
+ *
+ * Returns new cache object or NULL on error.
+ */
+virFileCachePtr
+virFileCacheNew(const char *dir,
+ const char *suffix,
+ virFileCacheHandlers handlers)
What about const virFileCacheHandlers *handlers?
+{
+ virFileCachePtr cache;
+
+ if (virFileCacheInitialize() < 0)
+ return NULL;
+
+ if (!(cache = virObjectNew(virFileCacheClass)))
+ return NULL;
+
+ if (!(cache->table = virHashCreate(10, virObjectFreeHashData)))
+ goto cleanup;
+
+ if (VIR_STRDUP(cache->dir, dir) < 0)
+ goto cleanup;
+
+ if (VIR_STRDUP(cache->suffix, suffix) < 0)
+ goto cleanup;
+
+ cache->handlers = handlers;
And cache->handlers = *handlers;
+
+ return cache;
+
+ cleanup:
+ virObjectUnref(cache);
+ return NULL;
+}
...
+/**
+ * virFileCacheInsertData:
+ * @cache: existing cache object
+ * @name: name of the new data
+ * @data: the actual data object to store in cache
+ *
+ * Adds a new data into a cache but doesn't store the data into
+ * a file. This function should be used only by testing code.
+ *
+ * Returns 0 on success, -1 on error.
+ */
+int
+virFileCacheInsertData(virFileCachePtr cache,
+ const char *name,
+ void *data)
+{
+ int rc;
+
+ virObjectLock(cache);
+
+ rc = virHashUpdateEntry(cache->table, name, data);
+
+ virObjectUnlock(cache);
+
+ if (rc < 0)
+ return -1;
+ return 0;
s/rc/ret/ and you can just directly return ret here.
+}
Reviewed-by: Jiri Denemark <jdenemar(a)redhat.com>