[Kimchi-devel] [PATCH] objectstore: support sorting by a key in get_list()
Mark Wu
wudxw at linux.vnet.ibm.com
Thu Apr 10 09:10:47 UTC 2014
On 04/10/2014 04:11 PM, zhshzhou at linux.vnet.ibm.com wrote:
> From: Zhou Zheng Sheng <zhshzhou at 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 at 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=?',
Reviewed-by: Mark Wu<wudxw at linux.vnet.ibm.com>
If we use different table for different type of objects, we can just
leverage the sort function of sqlite.
More information about the Kimchi-devel
mailing list