Matt Chaput avatar Matt Chaput committed 7cffc94

Fixed bad attribute reference.
Fixed fieldcache saving.
Added test for persistent field caching.
Fixes issue #200.

Comments (0)

Files changed (2)

src/whoosh/filedb/fieldcache.py

         :param default: the value to use for documents without the field.
         """
 
-        self.order = order or array(self.code)
+        self.order = order or array(typecode)
+        self.typecode = typecode
+
         self.hastexts = hastexts
         self.texts = None
         if hastexts:
             self.texts = texts or [default]
-        self.typecode = typecode
 
     def __eq__(self, other):
         return (other and self.__class__ is other.__class__
         return cache
 
     def is_loaded(self, key):
-        if key in self.caches:
-            return True
-
-        with self.sharedlock:
-            return key in self.shared_cache
+        return key in self.caches or key in self.shared_cache
 
     def put(self, key, cache, save=True):
         self.caches[key] = cache
 
         if self._file_exists(key):
             try:
-                return self._load(key)
+                fc = self._load(key)
+                self.put(key, fc)
+                return fc
             except (OSError, BadFieldCache):
                 return None
 

tests/test_sorting.py

 
 # 
 
+def test_persistent_cache():
+    schema = fields.Schema(id=fields.ID(stored=True))
+    st = RamStorage()
+    ix = st.create_index(schema)
+    with ix.writer() as w:
+        for term in u("charlie alfa echo bravo delta").split():
+            w.add_document(id=term)
+
+    ix = st.open_index()
+    with ix.reader() as r:
+        _ = r.fieldcache("id")
+        del _
+
+    ix = st.open_index()
+    with ix.reader() as r:
+        assert r.fieldcache_available("id")
+        assert not r.fieldcache_loaded("id")
+        fc = r.fieldcache("id")
+        assert r.fieldcache_loaded("id")
+        assert_equal(list(fc.order), [3, 1, 5, 2, 4])
+        assert_equal(list(fc.texts), [u('\uffff'), u'alfa', u'bravo',
+                                      u'charlie', u'delta', u'echo'])
+
 def test_float_cache():
     schema = fields.Schema(id=fields.STORED, num=fields.NUMERIC(type=float))
     with TempIndex(schema, "floatcache") as ix:
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.