From 2b14f8b279734cebecb3ac532ee912c3b2f5726c Mon Sep 17 00:00:00 2001
Message-Id: <2b14f8b279734cebecb3ac532ee912c3b2f5726c.1278225681.git.andresambrois@gmail.com>
In-Reply-To: <cover.1278225681.git.andresambrois@gmail.com>
References: <cover.1278225681.git.andresambrois@gmail.com>
From: =?UTF-8?q?Andr=C3=A9s=20Ambrois?= <andresambrois@gmail.com>
Date: Fri, 2 Jul 2010 03:34:23 -0300
Subject: [PATCH v2 1/7] Add filesize property to the index.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Don't fail indexing if filesize is missing.

Signed-off-by: Andrés Ambrois <andresambrois@gmail.com>
---
 src/carquinyol/datastore.py  |   12 ++++++++++++
 src/carquinyol/indexstore.py |   14 ++++++++++++++
 2 files changed, 26 insertions(+), 0 deletions(-)

diff --git a/src/carquinyol/datastore.py b/src/carquinyol/datastore.py
index a556869..93ad419 100644
--- a/src/carquinyol/datastore.py
+++ b/src/carquinyol/datastore.py
@@ -157,6 +157,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,
@@ -193,6 +199,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 8a69334..1b32a01 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:
+                logging.debug('Invalid value for filesize property: %s',
+                              properties['filesize'])
 
         self.set_document(document)
 
@@ -284,6 +294,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)
 
-- 
1.7.0.4

