Skip to content

Commit

Permalink
Changed the way int/long values are stored so that they are not seria…
Browse files Browse the repository at this point in the history
…lised as xapian types. Instead, use 0 padded strings.
  • Loading branch information
notanumber committed Aug 28, 2009
1 parent 7c00117 commit 4ea8271
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
14 changes: 7 additions & 7 deletions tests/xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ def test_update(self):

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):
Expand All @@ -123,8 +123,8 @@ def test_remove(self):
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):
Expand Down Expand Up @@ -327,8 +327,8 @@ def test_boost(self):

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)')
Expand Down
22 changes: 16 additions & 6 deletions xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import re
import shutil
import sys
import warnings

from django.conf import settings
Expand Down Expand Up @@ -67,20 +68,27 @@ def __call__(self, begin, end):
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)


Expand Down Expand Up @@ -651,8 +659,10 @@ def _marshal_value(self, value):
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
Expand Down

0 comments on commit 4ea8271

Please sign in to comment.