Reviewed-by: Daniel Barboza <danielhb(a)linux.vnet.ibm.com>
On 04/10/2014 05:11 AM, zhshzhou(a)linux.vnet.ibm.com wrote:
From: Zhou Zheng Sheng <zhshzhou(a)linux.vnet.ibm.com>
ObjectStoreSession.get_list() just returns an unsorted id list. When the
caller wants to sort the list according to a key, it has to implement
the sorting code each time. This patch implement the sorting in
objectstore to make the code re-usable.
Signed-off-by: Zhou Zheng Sheng <zhshzhou(a)linux.vnet.ibm.com>
---
src/kimchi/objectstore.py | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)
diff --git a/src/kimchi/objectstore.py b/src/kimchi/objectstore.py
index 153b548..d355c58 100644
--- a/src/kimchi/objectstore.py
+++ b/src/kimchi/objectstore.py
@@ -38,11 +38,19 @@ class ObjectStoreSession(object):
self.conn = conn
self.conn.text_factory = lambda x: unicode(x, "utf-8",
"ignore")
- def get_list(self, obj_type):
+ def _get_list(self, obj_type):
c = self.conn.cursor()
res = c.execute('SELECT id FROM objects WHERE type=?', (obj_type,))
return [x[0] for x in res]
+ def get_list(self, obj_type, sort_key=None):
+ ids = self._get_list(obj_type)
+ if sort_key is None:
+ return ids
+ objects = [(ident, self.get(obj_type, ident)) for ident in ids]
+ objects.sort(key=lambda (_, obj): obj[sort_key])
+ return [ident for ident, _ in objects]
+
def get(self, obj_type, ident):
c = self.conn.cursor()
res = c.execute('SELECT json FROM objects WHERE type=? AND id=?',