From 089ba387eafec10734ce3023665a163c531b82b4 Mon Sep 17 00:00:00 2001 From: Andrés Ambrois Date: Tue, 24 Aug 2010 03:54:59 +0000 Subject: Add filesize property to the index. Don't fail indexing if filesize is missing. --- diff --git a/src/carquinyol/datastore.py b/src/carquinyol/datastore.py index 82a6207..44df5a2 100644 --- a/src/carquinyol/datastore.py +++ b/src/carquinyol/datastore.py @@ -159,6 +159,12 @@ class DataStore(dbus.service.Object): if not props.get('timestamp', ''): props['timestamp'] = int(time.time()) + if os.path.exists(file_path): + stat = os.stat(file_path) + props['filesize'] = stat.st_size + else: + props['filesize'] = 0 + self._metadata_store.store(uid, props) self._index_store.store(uid, props) self._file_store.store(uid, file_path, transfer_ownership, @@ -195,6 +201,12 @@ class DataStore(dbus.service.Object): if not props.get('timestamp', ''): props['timestamp'] = int(time.time()) + if os.path.exists(file_path): + stat = os.stat(file_path) + props['filesize'] = stat.st_size + else: + props['filesize'] = 0 + self._metadata_store.store(uid, props) self._index_store.store(uid, props) diff --git a/src/carquinyol/indexstore.py b/src/carquinyol/indexstore.py index 62b843b..b659db3 100644 --- a/src/carquinyol/indexstore.py +++ b/src/carquinyol/indexstore.py @@ -28,6 +28,8 @@ from carquinyol.layoutmanager import MAX_QUERY_LIMIT _VALUE_UID = 0 _VALUE_TIMESTAMP = 1 _VALUE_TITLE = 2 +# 3 reserved for version support +_VALUE_FILESIZE = 4 _PREFIX_NONE = 'N' _PREFIX_FULL_VALUE = 'F' @@ -57,6 +59,7 @@ _QUERY_TERM_MAP = { _QUERY_VALUE_MAP = { 'timestamp': {'number': _VALUE_TIMESTAMP, 'type': float}, + 'filesize': {'number': _VALUE_FILESIZE, 'type': int}, } @@ -66,6 +69,13 @@ class TermGenerator (xapian.TermGenerator): document.add_value(_VALUE_TIMESTAMP, xapian.sortable_serialise(float(properties['timestamp']))) document.add_value(_VALUE_TITLE, properties.get('title', '').strip()) + if 'filesize' in properties: + try: + document.add_value(_VALUE_FILESIZE, + xapian.sortable_serialise(int(properties['filesize']))) + except (ValueError, TypeError): + logging.debug('Invalid value for filesize property: %s', + properties['filesize']) self.set_document(document) @@ -286,6 +296,10 @@ class IndexStore(object): enquire.set_sort_by_value(_VALUE_TITLE, True) elif order_by == '-title': enquire.set_sort_by_value(_VALUE_TITLE, False) + elif order_by == '+filesize': + enquire.set_sort_by_value(_VALUE_FILESIZE, True) + elif order_by == '-filesize': + enquire.set_sort_by_value(_VALUE_FILESIZE, False) else: logging.warning('Unsupported property for sorting: %s', order_by) -- cgit v0.9.1