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