[grilo] Add utility for services needing page_size + page_number
- From: Juan A. Suarez Romero <jasuarez src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [grilo] Add utility for services needing page_size + page_number
- Date: Wed, 23 Jun 2010 17:52:56 +0000 (UTC)
commit 323b139d8bef1c05b68ce9667750ad82519e22c5
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date: Thu Jun 17 16:55:26 2010 +0200
Add utility for services needing page_size + page_number
Grilo relies in offset + count to make pagination.
But there are services like Jamendo that needs the page_number and size of
page.
For these cases, a new utility function helps user to compute page_number and
offset inside the page where she needs to start looking for data. Also it
returns an "optimal" value for page size. This optimal value is the minimum
page size needed to retrieve the data in just one query to backend.
src/Makefile.am | 5 ++-
src/grilo.h | 1 +
src/grl-util.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/grl-util.h | 43 ++++++++++++++++++++++++++++
4 files changed, 132 insertions(+), 2 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 7a7564a..67bc2d8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -23,7 +23,7 @@ lib GRL_NAME@_la_SOURCES = \
grl-plugin-registry.c \
grl-metadata-key.c grl-metadata-key-priv.h \
grl-metadata-source.c grl-metadata-source-priv.h \
- grl-media-source.c \
+ grl-media-source.c grl-util.c \
grl-multiple.c \
grl-log.c \
grilo.c
@@ -51,7 +51,8 @@ lib GRL_NAME@inc_HEADERS = \
grl-metadata-source.h \
grl-media-source.h \
grl-log.h \
- grl-multiple.h
+ grl-multiple.h \
+ grl-util.h
data_h_headers = \
data/grl-data.h \
diff --git a/src/grilo.h b/src/grilo.h
index 5991197..552310f 100644
--- a/src/grilo.h
+++ b/src/grilo.h
@@ -48,6 +48,7 @@
#include <grl-media-box.h>
#include <grl-config.h>
#include <grl-multiple.h>
+#include <grl-util.h>
#undef _GRILO_H_INSIDE_
diff --git a/src/grl-util.c b/src/grl-util.c
new file mode 100644
index 0000000..7807813
--- /dev/null
+++ b/src/grl-util.c
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2010 Igalia S.L.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#include "grl-util.h"
+
+/**
+ * grl_paging_translate:
+ * @skip: number of elements to skip
+ * @count: number of elements to retrieve
+ * @max_page_size: maximum value for page size
+ * @page_size: optimal page size
+ * @page_number: page which contain the first element to retrieve (starting at 1)
+ * @internal_offset: in the @page_number, offset where first element can be found (starting at 0)
+ *
+ * Grilo browsing implements a paging mechanism through @skip and @count values.
+ *
+ * But there are some services (like Jamendo or Flickr) where paging is done
+ * through a page number and page size: user request all elements in a page,
+ * specifying in most cases what is the page size.
+ *
+ * This function is a helper for this task, computing from @skip and @count what
+ * is the optimal value of page size (limited by @max_page_size), which page
+ * should the user request, and where requested data start inside the page.
+ *
+ * By optimal we mean that it computes those values so only one page is required
+ * to satisfy the data, using the smallest page size. If user is limiting page
+ * size, then more requests to services might be needed. But still page size
+ * will be an optimal value.
+ **/
+void grl_paging_translate (guint skip,
+ guint count,
+ guint max_page_size,
+ guint *page_size,
+ guint *page_number,
+ guint *internal_offset)
+{
+ gulong _page_size;
+ gulong last_element;
+
+ if (skip < count) {
+ _page_size = skip + count;
+ if (max_page_size > 0) {
+ _page_size = CLAMP (_page_size, 0, max_page_size);
+ }
+ } else {
+ _page_size = count;
+ last_element = skip + count - 1;
+ while (skip/_page_size != last_element/_page_size &&
+ (max_page_size == 0 || _page_size < max_page_size)) {
+ _page_size++;
+ }
+ }
+ _page_size = CLAMP (_page_size, 0, G_MAXUINT);
+
+ if (page_size) {
+ *page_size = _page_size;
+ }
+
+ if (page_number) {
+ *page_number = skip/_page_size + 1;
+ }
+
+ if (internal_offset) {
+ *internal_offset = skip%_page_size;
+ }
+}
diff --git a/src/grl-util.h b/src/grl-util.h
new file mode 100644
index 0000000..a3f81ee
--- /dev/null
+++ b/src/grl-util.h
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2010 Igalia S.L.
+ *
+ * Contact: Iago Toral Quiroga <itoral igalia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+ */
+
+#if !defined (_GRILO_H_INSIDE_) && !defined (GRILO_COMPILATION)
+#error "Only <grilo.h> can be included directly."
+#endif
+
+#ifndef _GRL_UTIL_H_
+#define _GRL_UTIL_H_
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void grl_paging_translate (guint skip,
+ guint count,
+ guint max_page_size,
+ guint *page_size,
+ guint *page_number,
+ guint *internal_offset);
+
+G_END_DECLS
+
+#endif /* _GRL_UTIL_H_ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]