Add common object API wrappers to use the virHashSearch API to
search the LookupHash for specific data defined in the @opaque
parameter. Once data is found, the search would end and the
object that is represented is returned locked with it's reference
's reference count incremented.

The virObjectLookupHashSearch{UUID|Name}Locked is the workhorse, but
similar to the virObjectLookupFind* APIs may be required an Add or
AssignDef caller has the Write lock already to ensure no other thread
will grab the lock and add the same or competing object.

It is up to the caller to unlock and lower the refcnt once done
with the object and of course handle a NULL return indicating
no object found.

Signed-off-by: John Ferlan <jfer...@redhat.com>
---
 src/libvirt_private.syms |   4 ++
 src/util/virobject.c     | 131 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util/virobject.h     |  24 +++++++++
 3 files changed, 159 insertions(+)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4d0883c..b32004e 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2337,6 +2337,10 @@ virObjectLookupHashForEachName;
 virObjectLookupHashForEachUUID;
 virObjectLookupHashNew;
 virObjectLookupHashRemove;
+virObjectLookupHashSearchName;
+virObjectLookupHashSearchNameLocked;
+virObjectLookupHashSearchUUID;
+virObjectLookupHashSearchUUIDLocked;
 virObjectNew;
 virObjectRef;
 virObjectRWLockableNew;
diff --git a/src/util/virobject.c b/src/util/virobject.c
index dfd0bec..b20e938 100644
--- a/src/util/virobject.c
+++ b/src/util/virobject.c
@@ -1048,3 +1048,134 @@ virObjectLookupHashForEachName(void *anyobj,
 
     return ret;
 }
+
+
+static void *
+virObjectLookupHashSearchInternal(virHashTablePtr objsTable,
+                                  virHashSearcher iter,
+                                  void *opaque)
+{
+    virObjectLockablePtr obj;
+
+    obj = virHashSearch(objsTable, iter, opaque, NULL);
+    virObjectRef(obj);
+
+    if (obj)
+        virObjectLock(obj);
+
+    return obj;
+}
+
+
+/**
+ * virObjectLookupHashSearch{UUID|Name}Locked
+ * @anyobj: LookupHash object
+ * @iter: callback function to handle the object specific checks
+ * @opaque: callback data
+ *
+ * Search the hash table UUID or Name table calling the specified @iter
+ * routine with an object and @opaque data in order to determine whether
+ * the object is represented by the @opaque data.
+ *
+ * NB: Caller assumes the responsibility for locking LookupHash
+ *
+ * Returns locked/refcnt incremented object on success, NULL on failure
+ */
+void *
+virObjectLookupHashSearchUUIDLocked(void *anyobj,
+                                    virHashSearcher iter,
+                                    void *opaque)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+
+    if (!hashObj)
+        return NULL;
+
+    if (!hashObj->objsUUID) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("no objsUUID for hashObj=%p"), hashObj);
+        return NULL;
+    }
+
+    return virObjectLookupHashSearchInternal(hashObj->objsUUID, iter, opaque);
+}
+
+
+void *
+virObjectLookupHashSearchNameLocked(void *anyobj,
+                                    virHashSearcher iter,
+                                    void *opaque)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+
+    if (!hashObj)
+        return NULL;
+
+    if (!hashObj->objsName) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("no objsName for hashObj=%p"), hashObj);
+        return NULL;
+    }
+
+    return virObjectLookupHashSearchInternal(hashObj->objsName, iter, opaque);
+}
+
+
+/**
+ * virObjectLookupHashSearch{UUID|Name}
+ * @anyobj: LookupHash object
+ * @iter: callback function to handle the object specific checks
+ * @opaque: callback data
+ *
+ * Call virObjectLookupHashSearchLocked with a locked hash table
+ *
+ * Returns @obj from virObjectLookupHashSearchLocked
+ */
+void *
+virObjectLookupHashSearchUUID(void *anyobj,
+                              virHashSearcher iter,
+                              void *opaque)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+    virObjectLockablePtr obj;
+
+    if (!hashObj)
+        return NULL;
+
+    if (!hashObj->objsUUID) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("no objsUUID for hashObj=%p"), hashObj);
+        return NULL;
+    }
+
+    virObjectRWLockRead(hashObj);
+    obj = virObjectLookupHashSearchInternal(hashObj->objsUUID, iter, opaque);
+    virObjectRWUnlock(hashObj);
+
+    return obj;
+}
+
+
+void *
+virObjectLookupHashSearchName(void *anyobj,
+                              virHashSearcher iter,
+                              void *opaque)
+{
+    virObjectLookupHashPtr hashObj = virObjectGetLookupHashObj(anyobj);
+    virObjectLockablePtr obj;
+
+    if (!hashObj)
+        return NULL;
+
+    if (!hashObj->objsName) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("no objsName for hashObj=%p"), hashObj);
+        return NULL;
+    }
+
+    virObjectRWLockRead(hashObj);
+    obj = virObjectLookupHashSearchInternal(hashObj->objsName, iter, opaque);
+    virObjectRWUnlock(hashObj);
+
+    return obj;
+}
diff --git a/src/util/virobject.h b/src/util/virobject.h
index a35cf3c..03a23e0 100644
--- a/src/util/virobject.h
+++ b/src/util/virobject.h
@@ -232,4 +232,28 @@ virObjectLookupHashForEachName(void *anyobj,
                                virObjectLookupHashForEachDataPtr data)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
 
+void *
+virObjectLookupHashSearchUUIDLocked(void *anyobj,
+                                    virHashSearcher iter,
+                                    void *opaque)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+void *
+virObjectLookupHashSearchNameLocked(void *anyobj,
+                                    virHashSearcher iter,
+                                    void *opaque)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+void *
+virObjectLookupHashSearchUUID(void *anyobj,
+                              virHashSearcher iter,
+                              void *opaque)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
+void *
+virObjectLookupHashSearchName(void *anyobj,
+                              virHashSearcher iter,
+                              void *opaque)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
+
 #endif /* __VIR_OBJECT_H */
-- 
2.9.5

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list

Reply via email to