Commits

Matt Chaput  committed c05f35b

Added signed varints to StructFile. Minor docstring fixups.

  • Participants
  • Parent commits 74327d4

Comments (0)

Files changed (4)

File src/whoosh/filedb/structfile.py

                            pack_long, pack_float,
                            unpack_sbyte, unpack_ushort, unpack_int,
                            unpack_uint, unpack_long, unpack_float, IS_LITTLE)
-from whoosh.util import varint, read_varint, float_to_byte, byte_to_float
+from whoosh.util import (varint, read_varint, signed_varint,
+                         decode_signed_varint, float_to_byte, byte_to_float)
 
 
 _SIZEMAP = dict((typecode, calcsize(typecode)) for typecode in "bBiIhHqQf")
         self.seek(l, 1)
 
     def write_varint(self, i):
-        """Writes a variable-length integer to the wrapped file.
+        """Writes a variable-length unsigned integer to the wrapped file.
         """
         self.file.write(varint(i))
 
+    def write_svarint(self, i):
+        """Writes a variable-length signed integer to the wrapped file.
+        """
+        self.file.write(signed_varint(i))
+
     def read_varint(self):
-        """Reads a variable-length encoded integer from the wrapped file.
+        """Reads a variable-length encoded unsigned integer from the wrapped file.
         """
         return read_varint(self.file.read)
 
+    def read_svarint(self):
+        """Reads a variable-length encoded signed integer from the wrapped file.
+        """
+        return decode_signed_varint(read_varint(self.file.read))
+
     def write_byte(self, n):
         """Writes a single byte to the wrapped file, shortcut for
         ``file.write(chr(n))``.

File src/whoosh/spans.py

     You can use the ``phrase()`` class method to create a tree of SpanNear
     queries to match a list of terms::
     
-        q = spans.SpanNear.phrase("text", ["whoosh", "search", "library"], slop=2)
+        q = spans.SpanNear.phrase("text", [u"whoosh", u"search", u"library"], slop=2)
     """
     
     def __init__(self, a, b, slop=1, ordered=True, mindist=1):
     def phrase(cls, fieldname, words, slop=1, ordered=True):
         """Returns a tree of SpanNear queries to match a list of terms.
         
+        This class method is a convenience for constructing a phrase query
+        using a binary tree of SpanNear queries.
+        
+        >>> SpanNear.phrase("f", [u"a", u"b", u"c", u"d"])
+        SpanNear(SpanNear(Term("f", u"a"), Term("f", u"b")), SpanNear(Term("f", u"c"), Term("f", u"d")))
+        
         :param fieldname: the name of the field to search in.
-        :param words: a sequence of tokens to search for.
+        :param words: a sequence of token texts to search for.
         :param slop: the number of positions within which the terms must
             occur. Default is 1, meaning the terms must occur right next
             to each other.

File src/whoosh/util.py

         shift += 7
     return i
 
+
+def signed_varint(i):
+    """Zig-zag encodes a signed integer into a varint.
+    """
+    
+    if i >= 0:
+        return varint(i << 1)
+    return varint((i << 1) ^ (~0))
+
+def decode_signed_varint(i):
+    """Zig-zag decodes an integer value.
+    """
+    
+    if not i & 1:
+        return i >> 1
+    return (i >> 1) ^ (~0)
+
+
 def read_varint(readfn):
     """
     Reads a variable-length encoded integer.

File src/whoosh/writing.py

     """Convenience wrapper that batches up calls to ``add_document()``,
     ``update_document()``, and/or ``delete_document()``, and commits them
     whenever a maximum amount of time passes or a maximum number of batched
-    changes accumulates.
+    changes accumulate.
     
     In scenarios where you are continuously adding single documents very
     rapidly (for example a web application where lots of users are adding