dovecot-2.0: Created mail_index_map_lookup_seq_range() from code...
dovecot at dovecot.org
dovecot at dovecot.org
Thu May 21 00:32:22 EEST 2009
details: http://hg.dovecot.org/dovecot-2.0/rev/190669ac816c
changeset: 9333:190669ac816c
user: Timo Sirainen <tss at iki.fi>
date: Wed May 20 17:30:46 2009 -0400
description:
Created mail_index_map_lookup_seq_range() from code in mail-index-view.c.
diffstat:
3 files changed, 92 insertions(+), 80 deletions(-)
src/lib-index/mail-index-map.c | 85 ++++++++++++++++++++++++++++++++++++
src/lib-index/mail-index-private.h | 5 ++
src/lib-index/mail-index-view.c | 82 ----------------------------------
diffs (203 lines):
diff -r e55de8b34144 -r 190669ac816c src/lib-index/mail-index-map.c
--- a/src/lib-index/mail-index-map.c Wed May 20 17:28:53 2009 -0400
+++ b/src/lib-index/mail-index-map.c Wed May 20 17:30:46 2009 -0400
@@ -482,3 +482,88 @@ bool mail_index_map_get_ext_idx(struct m
*idx_r = *id;
return *idx_r != (uint32_t)-1;
}
+
+static uint32_t mail_index_bsearch_uid(struct mail_index_map *map,
+ uint32_t uid, uint32_t left_idx,
+ int nearest_side)
+{
+ const struct mail_index_record *rec_base, *rec;
+ uint32_t idx, right_idx, record_size;
+
+ i_assert(map->hdr.messages_count <= map->rec_map->records_count);
+
+ rec_base = map->rec_map->records;
+ record_size = map->hdr.record_size;
+
+ idx = left_idx;
+ right_idx = I_MIN(map->hdr.messages_count, uid);
+
+ while (left_idx < right_idx) {
+ idx = (left_idx + right_idx) / 2;
+
+ rec = CONST_PTR_OFFSET(rec_base, idx * record_size);
+ if (rec->uid < uid)
+ left_idx = idx+1;
+ else if (rec->uid > uid)
+ right_idx = idx;
+ else
+ break;
+ }
+ i_assert(idx < map->hdr.messages_count);
+
+ rec = CONST_PTR_OFFSET(rec_base, idx * record_size);
+ if (rec->uid != uid) {
+ if (nearest_side > 0) {
+ /* we want uid or larger */
+ return rec->uid > uid ? idx+1 :
+ (idx == map->hdr.messages_count-1 ? 0 : idx+2);
+ } else {
+ /* we want uid or smaller */
+ return rec->uid < uid ? idx + 1 : idx;
+ }
+ }
+
+ return idx+1;
+}
+
+void mail_index_map_lookup_seq_range(struct mail_index_map *map,
+ uint32_t first_uid, uint32_t last_uid,
+ uint32_t *first_seq_r,
+ uint32_t *last_seq_r)
+{
+ i_assert(first_uid > 0);
+ i_assert(first_uid <= last_uid);
+
+ if (map->hdr.messages_count == 0) {
+ *first_seq_r = *last_seq_r = 0;
+ return;
+ }
+
+ *first_seq_r = mail_index_bsearch_uid(map, first_uid, 0, 1);
+ if (*first_seq_r == 0 ||
+ MAIL_INDEX_MAP_IDX(map, *first_seq_r-1)->uid > last_uid) {
+ *first_seq_r = *last_seq_r = 0;
+ return;
+ }
+
+ if (last_uid >= map->hdr.next_uid-1) {
+ /* we want the last message */
+ last_uid = map->hdr.next_uid-1;
+ if (first_uid > last_uid) {
+ *first_seq_r = *last_seq_r = 0;
+ return;
+ }
+
+ *last_seq_r = map->hdr.messages_count;
+ return;
+ }
+
+ if (first_uid == last_uid)
+ *last_seq_r = *first_seq_r;
+ else {
+ /* optimization - binary lookup only from right side: */
+ *last_seq_r = mail_index_bsearch_uid(map, last_uid,
+ *first_seq_r - 1, -1);
+ }
+ i_assert(*last_seq_r >= *first_seq_r);
+}
diff -r e55de8b34144 -r 190669ac816c src/lib-index/mail-index-private.h
--- a/src/lib-index/mail-index-private.h Wed May 20 17:28:53 2009 -0400
+++ b/src/lib-index/mail-index-private.h Wed May 20 17:30:46 2009 -0400
@@ -313,6 +313,11 @@ const struct mail_index_ext *
const struct mail_index_ext *
mail_index_view_get_ext(struct mail_index_view *view, uint32_t ext_id);
+void mail_index_map_lookup_seq_range(struct mail_index_map *map,
+ uint32_t first_uid, uint32_t last_uid,
+ uint32_t *first_seq_r,
+ uint32_t *last_seq_r);
+
int mail_index_map_check_header(struct mail_index_map *map);
bool mail_index_check_header_compat(struct mail_index *index,
const struct mail_index_header *hdr,
diff -r e55de8b34144 -r 190669ac816c src/lib-index/mail-index-view.c
--- a/src/lib-index/mail-index-view.c Wed May 20 17:28:53 2009 -0400
+++ b/src/lib-index/mail-index-view.c Wed May 20 17:30:46 2009 -0400
@@ -212,90 +212,12 @@ static void view_lookup_uid(struct mail_
*uid_r = MAIL_INDEX_MAP_IDX(view->map, seq-1)->uid;
}
-static uint32_t mail_index_bsearch_uid(struct mail_index_view *view,
- uint32_t uid, uint32_t left_idx,
- int nearest_side)
-{
- const struct mail_index_record *rec_base, *rec;
- uint32_t idx, right_idx, record_size;
-
- i_assert(view->map->hdr.messages_count <=
- view->map->rec_map->records_count);
-
- rec_base = view->map->rec_map->records;
- record_size = view->map->hdr.record_size;
-
- idx = left_idx;
- right_idx = I_MIN(view->map->hdr.messages_count, uid);
-
- while (left_idx < right_idx) {
- idx = (left_idx + right_idx) / 2;
-
- rec = CONST_PTR_OFFSET(rec_base, idx * record_size);
- if (rec->uid < uid)
- left_idx = idx+1;
- else if (rec->uid > uid)
- right_idx = idx;
- else
- break;
- }
- i_assert(idx < view->map->hdr.messages_count);
-
- rec = CONST_PTR_OFFSET(rec_base, idx * record_size);
- if (rec->uid != uid) {
- if (nearest_side > 0) {
- /* we want uid or larger */
- return rec->uid > uid ? idx+1 :
- (idx == view->map->hdr.messages_count-1 ?
- 0 : idx+2);
- } else {
- /* we want uid or smaller */
- return rec->uid < uid ? idx + 1 : idx;
- }
- }
-
- return idx+1;
-}
-
static void view_lookup_seq_range(struct mail_index_view *view,
uint32_t first_uid, uint32_t last_uid,
uint32_t *first_seq_r, uint32_t *last_seq_r)
{
- i_assert(first_uid > 0);
- i_assert(first_uid <= last_uid);
-
- if (view->map->hdr.messages_count == 0) {
- *first_seq_r = *last_seq_r = 0;
- return;
- }
-
- *first_seq_r = mail_index_bsearch_uid(view, first_uid, 0, 1);
- if (*first_seq_r == 0 ||
- MAIL_INDEX_MAP_IDX(view->map, *first_seq_r-1)->uid > last_uid) {
- *first_seq_r = *last_seq_r = 0;
- return;
- }
-
- if (last_uid >= view->map->hdr.next_uid-1) {
- /* we want the last message */
- last_uid = view->map->hdr.next_uid-1;
- if (first_uid > last_uid) {
- *first_seq_r = *last_seq_r = 0;
- return;
- }
-
- *last_seq_r = view->map->hdr.messages_count;
- return;
- }
-
- if (first_uid == last_uid)
- *last_seq_r = *first_seq_r;
- else {
- /* optimization - binary lookup only from right side: */
- *last_seq_r = mail_index_bsearch_uid(view, last_uid,
- *first_seq_r - 1, -1);
- }
- i_assert(*last_seq_r >= *first_seq_r);
+ mail_index_map_lookup_seq_range(view->map, first_uid, last_uid,
+ first_seq_r, last_seq_r);
}
static void view_lookup_first(struct mail_index_view *view,
More information about the dovecot-cvs
mailing list