Blame SOURCES/0006-python-Implement-cache.patch

efa7a1
From c5b1fac4c67078f0164bd23eab6d4d2b8c9830b0 Mon Sep 17 00:00:00 2001
efa7a1
From: "Richard W.M. Jones" <rjones@redhat.com>
efa7a1
Date: Thu, 21 Nov 2019 16:42:02 +0000
efa7a1
Subject: [PATCH] python: Implement cache.
efa7a1
efa7a1
However this does not implement can_cache, since that is not a simple
efa7a1
boolean.
efa7a1
efa7a1
(cherry picked from commit e61ffb73c7a0af0c383184fdb8f08d30784a195e)
efa7a1
---
efa7a1
 plugins/python/nbdkit-python-plugin.pod | 14 ++++++++++-
efa7a1
 plugins/python/python.c                 | 31 +++++++++++++++++++++++++
efa7a1
 2 files changed, 44 insertions(+), 1 deletion(-)
efa7a1
efa7a1
diff --git a/plugins/python/nbdkit-python-plugin.pod b/plugins/python/nbdkit-python-plugin.pod
efa7a1
index 4923d9d..0ea8dee 100644
efa7a1
--- a/plugins/python/nbdkit-python-plugin.pod
efa7a1
+++ b/plugins/python/nbdkit-python-plugin.pod
efa7a1
@@ -289,6 +289,19 @@ because there is nothing to optimize if
efa7a1
 S<C<flags & nbdkit.FLAG_MAY_TRIM>> is false), use
efa7a1
 S<C<nbdkit.set_error (errno.EOPNOTSUPP)>>.
efa7a1
 
efa7a1
+=item C<cache>
efa7a1
+
efa7a1
+(Optional)
efa7a1
+
efa7a1
+ def cache(h, count, offset, flags):
efa7a1
+   # no return value
efa7a1
+
efa7a1
+The body of your C<cache> function should prefetch data in the
efa7a1
+indicated range.
efa7a1
+
efa7a1
+If the cache operation fails, your function should throw an exception,
efa7a1
+optionally using C<nbdkit.set_error> first.
efa7a1
+
efa7a1
 =back
efa7a1
 
efa7a1
 =head2 Missing callbacks
efa7a1
@@ -317,7 +330,6 @@ C<can_zero>,
efa7a1
 C<can_fast_zero>,
efa7a1
 C<can_extents>,
efa7a1
 C<can_multi_conn>,
efa7a1
-C<cache>,
efa7a1
 C<extents>.
efa7a1
 
efa7a1
 These are not yet supported.
efa7a1
diff --git a/plugins/python/python.c b/plugins/python/python.c
efa7a1
index 0f28595..c5cf38e 100644
efa7a1
--- a/plugins/python/python.c
efa7a1
+++ b/plugins/python/python.c
efa7a1
@@ -714,6 +714,36 @@ py_zero (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
efa7a1
   return -1;
efa7a1
 }
efa7a1
 
efa7a1
+static int
efa7a1
+py_cache (void *handle, uint32_t count, uint64_t offset, uint32_t flags)
efa7a1
+{
efa7a1
+  PyObject *obj = handle;
efa7a1
+  PyObject *fn;
efa7a1
+  PyObject *r;
efa7a1
+
efa7a1
+  if (callback_defined ("cache", &fn)) {
efa7a1
+    PyErr_Clear ();
efa7a1
+
efa7a1
+    switch (py_api_version) {
efa7a1
+    case 1:
efa7a1
+    case 2:
efa7a1
+      r = PyObject_CallFunction (fn, "OiLI", obj, count, offset, flags, NULL);
efa7a1
+      break;
efa7a1
+    default: abort ();
efa7a1
+    }
efa7a1
+    Py_DECREF (fn);
efa7a1
+    if (check_python_failure ("cache") == -1)
efa7a1
+      return -1;
efa7a1
+    Py_DECREF (r);
efa7a1
+  }
efa7a1
+  else {
efa7a1
+    nbdkit_error ("%s not implemented", "cache");
efa7a1
+    return -1;
efa7a1
+  }
efa7a1
+
efa7a1
+  return 0;
efa7a1
+}
efa7a1
+
efa7a1
 static int
efa7a1
 boolean_callback (void *handle, const char *can_fn, const char *plain_fn)
efa7a1
 {
efa7a1
@@ -799,6 +829,7 @@ static struct nbdkit_plugin plugin = {
efa7a1
   .flush             = py_flush,
efa7a1
   .trim              = py_trim,
efa7a1
   .zero              = py_zero,
efa7a1
+  .cache             = py_cache,
efa7a1
 };
efa7a1
 
efa7a1
 NBDKIT_REGISTER_PLUGIN (plugin)
efa7a1
-- 
efa7a1
2.18.2
efa7a1