You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
3.4KB

  1. from .lib import TestBase
  2. class TestTutorial(TestBase):
  3. def test_example(self):
  4. # Memory Managers
  5. ##################
  6. import smmap
  7. # This instance should be globally available in your application
  8. # It is configured to be well suitable for 32-bit or 64 bit applications.
  9. mman = smmap.SlidingWindowMapManager()
  10. # the manager provides much useful information about its current state
  11. # like the amount of open file handles or the amount of mapped memory
  12. assert mman.num_file_handles() == 0
  13. assert mman.mapped_memory_size() == 0
  14. # and many more ...
  15. # Cursors
  16. ##########
  17. import smmap.test.lib
  18. with smmap.test.lib.FileCreator(1024 * 1024 * 8, "test_file") as fc:
  19. # obtain a cursor to access some file.
  20. c = mman.make_cursor(fc.path)
  21. # the cursor is now associated with the file, but not yet usable
  22. assert c.is_associated()
  23. assert not c.is_valid()
  24. # before you can use the cursor, you have to specify a window you want to
  25. # access. The following just says you want as much data as possible starting
  26. # from offset 0.
  27. # To be sure your region could be mapped, query for validity
  28. assert c.use_region().is_valid() # use_region returns self
  29. # once a region was mapped, you must query its dimension regularly
  30. # to assure you don't try to access its buffer out of its bounds
  31. assert c.size()
  32. c.buffer()[0] # first byte
  33. c.buffer()[1:10] # first 9 bytes
  34. c.buffer()[c.size() - 1] # last byte
  35. # its recommended not to create big slices when feeding the buffer
  36. # into consumers (e.g. struct or zlib).
  37. # Instead, either give the buffer directly, or use pythons buffer command.
  38. from smmap.util import buffer
  39. buffer(c.buffer(), 1, 9) # first 9 bytes without copying them
  40. # you can query absolute offsets, and check whether an offset is included
  41. # in the cursor's data.
  42. assert c.ofs_begin() < c.ofs_end()
  43. assert c.includes_ofs(100)
  44. # If you are over out of bounds with one of your region requests, the
  45. # cursor will be come invalid. It cannot be used in that state
  46. assert not c.use_region(fc.size, 100).is_valid()
  47. # map as much as possible after skipping the first 100 bytes
  48. assert c.use_region(100).is_valid()
  49. # You can explicitly free cursor resources by unusing the cursor's region
  50. c.unuse_region()
  51. assert not c.is_valid()
  52. # Buffers
  53. #########
  54. # Create a default buffer which can operate on the whole file
  55. buf = smmap.SlidingWindowMapBuffer(mman.make_cursor(fc.path))
  56. # you can use it right away
  57. assert buf.cursor().is_valid()
  58. buf[0] # access the first byte
  59. buf[-1] # access the last ten bytes on the file
  60. buf[-10:] # access the last ten bytes
  61. # If you want to keep the instance between different accesses, use the
  62. # dedicated methods
  63. buf.end_access()
  64. assert not buf.cursor().is_valid() # you cannot use the buffer anymore
  65. assert buf.begin_access(offset=10) # start using the buffer at an offset