]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd.py: Add rbd_read2/rbd_write2 funcion which can handle fadvise flags. 3626/head
authorJianpeng Ma <jianpeng.ma@intel.com>
Fri, 6 Feb 2015 01:19:20 +0000 (09:19 +0800)
committerJianpeng Ma <jianpeng.ma@intel.com>
Fri, 6 Feb 2015 01:19:20 +0000 (09:19 +0800)
Signed-off-by: Jianpeng Ma <jianpeng.ma@intel.com>
src/pybind/rbd.py
src/test/pybind/test_rbd.py

index 82f5033e41deab37c862bfbf4ac70b2d527aa7a3..0a9fb04e772c6b512131b27934f81f02105dcaac 100644 (file)
@@ -682,7 +682,7 @@ class Image(object):
         if ret != 0:
             raise make_ex(ret, 'error setting image %s to snapshot %s' % (self.name, name))
 
-    def read(self, offset, length):
+    def read(self, offset, length, fadvise_flags=0):
         """
         Read data from the image. Raises :class:`InvalidArgument` if
         part of the range specified is outside the image.
@@ -691,12 +691,18 @@ class Image(object):
         :type offset: int
         :param length: how many bytes to read
         :type length: int
+       :param fadvise_flags: fadvise flags for this read
+       :type fadvise_flags: int
         :returns: str - the data read
         :raises: :class:`InvalidArgument`, :class:`IOError`
         """
         ret_buf = create_string_buffer(length)
-        ret = self.librbd.rbd_read(self.image, c_uint64(offset),
-                                   c_size_t(length), byref(ret_buf))
+       if fadvise_flags == 0:
+         ret = self.librbd.rbd_read(self.image, c_uint64(offset),
+                                     c_size_t(length), byref(ret_buf))
+       else:
+         ret = self.librbd.rbd_read2(self.image, c_uint64(offset),
+                                       c_size_t(length), byref(ret_buf), c_int(fadvise_flags))
         if ret < 0:
             raise make_ex(ret, 'error reading %s %ld~%ld' % (self.image, offset, length))
         return ctypes.string_at(ret_buf, ret)
@@ -753,7 +759,7 @@ class Image(object):
             msg = 'error generating diff from snapshot %s' % from_snapshot
             raise make_ex(ret, msg)
 
-    def write(self, data, offset):
+    def write(self, data, offset, fadvise_flags=0):
         """
         Write data to the image. Raises :class:`InvalidArgument` if
         part of the write would fall outside the image.
@@ -762,6 +768,8 @@ class Image(object):
         :type data: str
         :param offset: where to start writing data
         :type offset: int
+       :param fadvise_flags: fadvise flags for this write
+       :type fadvise_flags: int
         :returns: int - the number of bytes written
         :raises: :class:`IncompleteWriteError`, :class:`LogicError`,
                  :class:`InvalidArgument`, :class:`IOError`
@@ -769,8 +777,13 @@ class Image(object):
         if not isinstance(data, str):
             raise TypeError('data must be a string')
         length = len(data)
-        ret = self.librbd.rbd_write(self.image, c_uint64(offset),
-                                    c_size_t(length), c_char_p(data))
+       if fadvise_flags == 0:
+         ret = self.librbd.rbd_write(self.image, c_uint64(offset),
+                                     c_size_t(length), c_char_p(data))
+       else:
+         ret = self.librbd.rbd_write2(self.image, c_uint64(offset),
+                                     c_size_t(length), c_char_p(data), c_int(fadvise_flags))
+
         if ret == length:
             return ret
         elif ret < 0:
index 84d5902623b09d429065104726667bf266d99b95..a66b945324283569dff1357a6e9edd9e7b54a8f1 100644 (file)
@@ -300,10 +300,21 @@ class TestImage(object):
         data = rand_data(256)
         self.image.write(data, 0)
 
+    def test_write_with_fadivse_flags(self):
+        data = rand_data(256)
+        self.image.write(data, 0, LIBRADOS_OP_FLAG_FADVISE_DONTNEED)
+        self.image.write(data, 0, LIBRADOS_OP_FLAG_FADVISE_NOCACHE)
+
     def test_read(self):
         data = self.image.read(0, 20)
         eq(data, '\0' * 20)
 
+     def test_read_with_fadivse_flags(self):
+        data = self.image.read(0, 20, LIBRADOS_OP_FLAG_FADIVSE_DONTNEED)
+        eq(data, '\0' * 20)
+        data = self.image.read(0, 20, LIBRADOS_OP_FLAG_FADIVSE_RANDOM)
+        eq(data, '\0' * 20)
+
     def test_large_write(self):
         data = rand_data(IMG_SIZE)
         self.image.write(data, 0)