Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implement rudimentary ascending sorting of numeric values.

  • Loading branch information...
commit 3a50d0626d04cbe0a473f91042c2abb8e5f94249 1 parent b5ae4a6
Albin Stjerna authored
Showing with 41 additions and 10 deletions.
  1. +16 −8 db/xapian_music.py
  2. +25 −2 test/test.py
View
24 db/xapian_music.py
@@ -17,10 +17,8 @@
'title' : 'XTITLE'}
# These numeric prefixes will also be used as data slots.
-NUMERIC_PREFIXES = {'year' : 'Y',
- 'mtime' : 'XMTIME',
- 'tracknumber' : 'XTRACKNR',
- 'rating' : 'XRATING'}
+NUMERIC_PREFIXES = ['year', 'mtime',
+ 'tracknumber', 'rating']
def index(datapath, dbpath):
# Create or open the database we're going to be writing to.
@@ -101,8 +99,11 @@ def parse_query(q):
# And parse the query
return queryparser.parse_query(q)
-def query(dbpath, querystring):
- "Query the database at path <dbpath> with the string <querystring>. Return iterator over maches. This is mostly for internal use, as it returns xapian match objects."
+def query(dbpath, querystring, order=None):
+ """Query the database at path <dbpath> with the string
+ <querystring>. Return iterator over maches. This is mostly for
+ internal use, as it returns xapian match objects. Optionally takes
+ the argument order with valid values None or any numeric term."""
# Open the database we're going to search.
db = xapian.Database(dbpath)
@@ -113,17 +114,24 @@ def query(dbpath, querystring):
enquire = xapian.Enquire(db)
enquire.set_query(query)
+ # Don't care about document ID order, just optimize.
+ enquire.set_docid_order(enquire.DONT_CARE)
+
+ if order in NUMERIC_PREFIXES:
+ slot_id = NUMERIC_PREFIXES.index(order)
+ enquire.set_sort_by_value_then_relevance(slot_id, False)
+
for match in enquire.get_mset(0, db.get_doccount()):
yield match
-def search(dbpath, querystring):
+def search(dbpath, querystring, order=None):
"Search the database at dbpath with querystring. Return list of matches."
return [({'id': match.docid,
'rank' : match.rank + 1,
'percent' : match.percent,
'data' : json.loads(unicode(match.document.get_data()))})
- for match in query(dbpath, querystring)]
+ for match in query(dbpath, querystring, order)]
def add_tag(dbpath, querystring, tag):
"Add the tag <tag> to all songs matching <querystring>."
View
27 test/test.py
@@ -272,11 +272,34 @@ def search_album(db):
with_index(search_album)
+def assert_ascending(values):
+ last_value = 0
+
+ for v in values:
+ assert v >= last_value
+ last_value = v
+ print last_value
+
def test_sort_mtime():
- assert False
+
+ def sort_mtime(db):
+ q = 'artist:VNV Nation'
+ songs = search(db, q, order="mtime")
+ mtimes = [s['data']['mtime'] for s in songs]
+ assert_ascending(mtimes)
+
+ with_index(sort_mtime)
def test_sort_year():
- assert False
+
+ def sort_year(db):
+ q = 'artist:VNV Nation'
+ songs = search(db, q, order="year")
+ years = [s['data']['year'] for s in songs]
+
+ assert_ascending(years)
+
+ with_index(sort_year)
def test_sort_length():
assert False
Please sign in to comment.
Something went wrong with that request. Please try again.