Changed the way int/long values are stored so that they are not serialised as xapian types. Instead, use 0 padded strings.

This commit is contained in:
David Sauve 2009-08-28 10:42:58 -04:00
parent 7c00117043
commit 4ea8271fb3
2 changed files with 23 additions and 13 deletions

View file

@ -111,9 +111,9 @@ class XapianSearchBackendTestCase(TestCase):
self.assertEqual(len(self.xapian_search('')), 3)
self.assertEqual([dict(doc) for doc in self.xapian_search('')], [
{'flag': u't', 'name': u'david1', 'text': u'Indexed!\n1', 'pub_date': u'20090224000000', 'value': '\xa9', 'id': u'tests.mockmodel.1', 'slug': 'http://example.com/1', 'popularity': '\xca\x84'},
{'flag': u'f', 'name': u'david2', 'text': u'Indexed!\n2', 'pub_date': u'20090223000000', 'value': '\xad', 'id': u'tests.mockmodel.2', 'slug': 'http://example.com/2', 'popularity': '\xb4`'},
{'flag': u't', 'name': u'david3', 'text': u'Indexed!\n3', 'pub_date': u'20090222000000', 'value': '\xaf\x80', 'id': u'tests.mockmodel.3', 'slug': 'http://example.com/3', 'popularity': '\xcb\x98'}
{'flag': u't', 'name': u'david1', 'text': u'Indexed!\n1', 'pub_date': u'20090224000000', 'value': '000000000005', 'id': u'tests.mockmodel.1', 'slug': 'http://example.com/1', 'popularity': '\xca\x84'},
{'flag': u'f', 'name': u'david2', 'text': u'Indexed!\n2', 'pub_date': u'20090223000000', 'value': '000000000010', 'id': u'tests.mockmodel.2', 'slug': 'http://example.com/2', 'popularity': '\xb4`'},
{'flag': u't', 'name': u'david3', 'text': u'Indexed!\n3', 'pub_date': u'20090222000000', 'value': '000000000015', 'id': u'tests.mockmodel.3', 'slug': 'http://example.com/3', 'popularity': '\xcb\x98'}
])
def test_remove(self):
@ -123,8 +123,8 @@ class XapianSearchBackendTestCase(TestCase):
self.sb.remove(self.sample_objs[0])
self.assertEqual(len(self.xapian_search('')), 2)
self.assertEqual([dict(doc) for doc in self.xapian_search('')], [
{'flag': u'f', 'name': u'david2', 'text': u'Indexed!\n2', 'pub_date': u'20090223000000', 'value': '\xad', 'id': u'tests.mockmodel.2', 'slug': 'http://example.com/2', 'popularity': '\xb4`'},
{'flag': u't', 'name': u'david3', 'text': u'Indexed!\n3', 'pub_date': u'20090222000000', 'value': '\xaf\x80', 'id': u'tests.mockmodel.3', 'slug': 'http://example.com/3', 'popularity': '\xcb\x98'}
{'flag': u'f', 'name': u'david2', 'text': u'Indexed!\n2', 'pub_date': u'20090223000000', 'value': '000000000010', 'id': u'tests.mockmodel.2', 'slug': 'http://example.com/2', 'popularity': '\xb4`'},
{'flag': u't', 'name': u'david3', 'text': u'Indexed!\n3', 'pub_date': u'20090222000000', 'value': '000000000015', 'id': u'tests.mockmodel.3', 'slug': 'http://example.com/3', 'popularity': '\xcb\x98'}
])
def test_clear(self):
@ -327,8 +327,8 @@ class XapianSearchBackendTestCase(TestCase):
def test__marshal_value(self):
self.assertEqual(self.sb._marshal_value('abc'), u'abc')
self.assertEqual(self.sb._marshal_value(1), '\xa0')
self.assertEqual(self.sb._marshal_value(2653), '\xd1.\x80')
self.assertEqual(self.sb._marshal_value(1), '000000000001')
self.assertEqual(self.sb._marshal_value(2653), '000000002653')
self.assertEqual(self.sb._marshal_value(25.5), '\xb2`')
self.assertEqual(self.sb._marshal_value([1, 2, 3]), u'[1, 2, 3]')
self.assertEqual(self.sb._marshal_value((1, 2, 3)), u'(1, 2, 3)')

View file

@ -19,6 +19,7 @@ import cPickle as pickle
import os
import re
import shutil
import sys
import warnings
from django.conf import settings
@ -67,20 +68,27 @@ class XHValueRangeProcessor(xapian.ValueRangeProcessor):
if not begin:
if field_dict['type'] == 'text':
begin = u'a' # TODO: A better way of getting a min text value?
elif field_dict['type'] == 'long' or field_dict['type'] == 'float':
elif field_dict['type'] == 'long':
begin = -sys.maxint - 1
elif field_dict['type'] == 'float':
begin = float('-inf')
elif field_dict['type'] == 'date' or field_dict['type'] == 'datetime':
begin = u'00010101000000'
elif end == '*':
if field_dict['type'] == 'text':
end = u'z' * 100 # TODO: A better way of getting a max text value?
elif field_dict['type'] == 'long' or field_dict['type'] == 'float':
elif field_dict['type'] == 'long':
end = sys.maxint
elif field_dict['type'] == 'float':
end = float('inf')
elif field_dict['type'] == 'date' or field_dict['type'] == 'datetime':
end = u'99990101000000'
if field_dict['type'] == 'long' or field_dict['type'] == 'float':
begin = xapian.sortable_serialise(float(begin))
end = xapian.sortable_serialise(float(end))
if field_dict['type'] == 'float':
begin = self.sb._marshal_value(float(begin))
end = self.sb._marshal_value(float(end))
elif field_dict['type'] == 'long':
begin = self.sb._marshal_value(long(begin))
end = self.sb._marshal_value(long(end))
return field_dict['column'], str(begin), str(end)
@ -651,8 +659,10 @@ class SearchBackend(BaseSearchBackend):
value = u't'
else:
value = u'f'
elif isinstance(value, (int, long, float)):
elif isinstance(value, float):
value = xapian.sortable_serialise(value)
elif isinstance(value, (int, long)):
value = u'%012d' % value
else:
value = force_unicode(value)
return value