Matt Chaput avatar Matt Chaput committed bfb6805

Fixed handling of Q codes in fieldcaches. Thanks Thomas!
Fixes issue #200.

Comments (0)

Files changed (2)

src/whoosh/filedb/fieldcache.py

     return arry
 
 
+def make_array(typecode, size=0, default=None):
+    if typecode.lower() == "q":
+        # Python does not support arrays of long long see Issue 1172711
+        if default is not None and size:
+            arry = [default] * size
+        else:
+            arry = []
+    else:
+        if default is not None and size:
+            arry = array(typecode, (default for _ in xrange(size)))
+        else:
+            arry = array(typecode)
+    return arry
+
+
 class FieldCache(object):
     """Keeps a list of the sorted text values of a field and an array of ints
     where each place in the array corresponds to a document, and the value
         :param default: the value to use for documents without the field.
         """
 
-        self.order = order or array(typecode)
+        self.order = order or make_array(typecode)
         self.typecode = typecode
 
         self.hastexts = hastexts
             defaultnum = field.sortable_default()
 
         doccount = ixreader.doc_count_all()
-        # Python does not support arrays of long long see Issue 1172711
-        if typecode.lower() == "q":
-            order = [defaultnum] * doccount
-        else:
-            order = array(typecode, [defaultnum] * doccount)
+        order = make_array(typecode, doccount, defaultnum)
 
         enum = enumerate(field.sortable_values(ixreader, fieldname))
         for i, (text, sortable) in enum:
     def __init__(self, dbfile, size=0, hastexts=True, code="I",
                  default=u('\uFFFF')):
         self.dbfile = dbfile
-        self.order = array(self.code, [0] * size)
         self.hastexts = hastexts
         self.code = code
+        self.order = make_array(code, size, 0)
 
         self.key = 0
         self.keycount = 1

tests/test_sorting.py

 
         with ix.reader() as r:
             r.fieldcache("num")
+            assert r.fieldcache_loaded("num")
             r.unload_fieldcache("num")
+            assert not r.fieldcache_loaded("num")
+            assert r.fieldcache_available("num")
 
             fc = r.fieldcache("num")
             assert not fc.hastexts
 
         with ix.reader() as r:
             r.fieldcache("num")
+            assert r.fieldcache_loaded("num")
             r.unload_fieldcache("num")
+            assert not r.fieldcache_loaded("num")
+            assert r.fieldcache_available("num")
 
             fc = r.fieldcache("num")
             assert not fc.hastexts
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.