A convenience API that will utilize the virHashForEach API for the
LookupHash in order to remove elements from the hash table(s) that
match some requirement from the callback.
NB: Once elements are removed from objsUUID - if something goes wrong
in objsName, the best that can be done is to issue an error message
and hope for the best.
Signed-off-by: John Ferlan <jferlan(a)redhat.com>
---
src/libvirt_private.syms | 1 +
src/util/virobject.c | 39 +++++++++++++++++++++++++++++++++++++++
src/util/virobject.h | 5 +++++
3 files changed, 45 insertions(+)
diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index ecbd84a..e4c465f 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2337,6 +2337,7 @@ virObjectLookupHashFindLocked;
virObjectLookupHashForEachName;
virObjectLookupHashForEachUUID;
virObjectLookupHashNew;
+virObjectLookupHashPrune;
virObjectLookupHashRemove;
virObjectLookupHashSearchName;
virObjectLookupHashSearchNameLocked;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index 7152b17..c259ccc 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -1262,3 +1262,42 @@ virObjectLookupHashClone(void *srcAnyobj,
return 0;
}
+
+
+/**
+ * virObjectLookupHashPrune
+ * @anyobj: LookupHash object
+ * @callback: callback function to handle the object specific checks
+ * @opaque: callback data
+ *
+ * Call the callback function from virHashRemoveSet in order to determine
+ * if the incoming opaque data should be removed from the hash table(s)
+ * for the LookupHash. If a second hash table exists and we fail to remove
+ * the same number of elements, then issue an ERROR message, but continue
+ * on as there's not much we can do now to restore the data other than
+ * attempt to let someone know.
+ */
+void
+virObjectLookupHashPrune(void *anyobj,
+ virHashSearcher callback,
+ void *opaque)
+{
+ virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+ ssize_t cntUUIDs = 0;
+ ssize_t cntNames = 0;
+
+ if (!hashObj)
+ return;
+
+ virObjectRWLockWrite(hashObj);
+ if (hashObj->objsUUID)
+ cntUUIDs = virHashRemoveSet(hashObj->objsUUID, callback, opaque);
+
+ if (hashObj->objsName)
+ cntNames = virHashRemoveSet(hashObj->objsName, callback, opaque);
+
+ if (hashObj->objsUUID && hashObj->objsName && cntNames !=
cntUUIDs)
+ VIR_ERROR(_("removed %zd elems from objsUUID and %zd elems "
+ "from objsName"), cntUUIDs, cntNames);
+ virObjectRWUnlock(hashObj);
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index 66b44ca..a6038fc 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -264,5 +264,10 @@ virObjectLookupHashClone(void *srcAnyobj,
virObjectLookupHashCloneCallback cb)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
+void
+virObjectLookupHashPrune(void *anyobj,
+ virHashSearcher callback,
+ void *opaque)
+ ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
#endif /* __VIR_OBJECT_H */
--
2.9.5