legacy.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright 2012 Matt Chaput. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are met:
  5. #
  6. # 1. Redistributions of source code must retain the above copyright notice,
  7. # this list of conditions and the following disclaimer.
  8. #
  9. # 2. Redistributions in binary form must reproduce the above copyright
  10. # notice, this list of conditions and the following disclaimer in the
  11. # documentation and/or other materials provided with the distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY MATT CHAPUT ``AS IS'' AND ANY EXPRESS OR
  14. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  16. # EVENT SHALL MATT CHAPUT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  17. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  19. # OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  20. # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  21. # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  22. # EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. #
  24. # The views and conclusions contained in the software and documentation are
  25. # those of the authors and should not be interpreted as representing official
  26. # policies, either expressed or implied, of Matt Chaput.
  27. """
  28. This module contains code for maintaining backwards compatibility with old
  29. index formats.
  30. """
  31. from whoosh.util.loading import RenamingUnpickler
  32. def load_110_toc(stream, gen, schema, version):
  33. # Between version -110 and version -111, I reorganized the modules and
  34. # changed the implementation of the NUMERIC field, so we have to change the
  35. # classes the unpickler tries to load if we need to read an old schema
  36. # Read the length of the pickled schema
  37. picklen = stream.read_varint()
  38. if schema:
  39. # If the user passed us a schema, use it and skip the one on disk
  40. stream.seek(picklen, 1)
  41. else:
  42. # Remap the old classes and functions to their moved versions as we
  43. # unpickle the schema
  44. scuts = {"wf": "whoosh.fields",
  45. "wsn": "whoosh.support.numeric",
  46. "wcw2": "whoosh.codec.whoosh2"}
  47. objmap = {"%(wf)s.NUMERIC": "%(wcw2)s.OLD_NUMERIC",
  48. "%(wf)s.DATETIME": "%(wcw2)s.OLD_DATETIME",
  49. "%(wsn)s.int_to_text": "%(wcw2)s.int_to_text",
  50. "%(wsn)s.text_to_int": "%(wcw2)s.text_to_int",
  51. "%(wsn)s.long_to_text": "%(wcw2)s.long_to_text",
  52. "%(wsn)s.text_to_long": "%(wcw2)s.text_to_long",
  53. "%(wsn)s.float_to_text": "%(wcw2)s.float_to_text",
  54. "%(wsn)s.text_to_float": "%(wcw2)s.text_to_float", }
  55. ru = RenamingUnpickler(stream, objmap, shortcuts=scuts)
  56. schema = ru.load()
  57. # Read the generation number
  58. index_gen = stream.read_int()
  59. assert gen == index_gen
  60. # Unused number
  61. _ = stream.read_int()
  62. # Unpickle the list of segment objects
  63. segments = stream.read_pickle()
  64. return schema, segments
  65. # Map TOC version numbers to functions to load that version
  66. toc_loaders = {-110: load_110_toc}
  67. # Map segment class names to functions to load the segment
  68. segment_loaders = {}