From 7c0728230f65679e7c1b8039bb9dbf8c828ac9d2 Mon Sep 17 00:00:00 2001 From: Marius Vollmer Date: Thu, 16 May 2013 09:59:57 +0300 Subject: [PATCH] journal: try to predict the index when looking up an entry by seqnum --- src/journal/journal-file.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index 38499a6..71bfe59 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -1767,6 +1767,24 @@ int journal_file_move_to_entry_by_seqnum( Object **ret, uint64_t *offset) { + /* Most of the time, the entry with sqenum S is found at index + * S-1, so we try that first. This is faster and might even + * allow us to survive a corrupted journal file that would + * otherwise trip up the binary search. + */ + if (seqnum >= 1 && seqnum <= f->header->n_entries) { + Object *o; + if (generic_array_get(f, + le64toh(f->header->entry_array_offset), + seqnum-1, + &o, offset) >= 0 && + le64toh(o->entry.seqnum) == seqnum) { + if (ret) + *ret = o; + return 1; + } + } + return generic_array_bisect(f, le64toh(f->header->entry_array_offset), le64toh(f->header->n_entries), -- 1.8.1.4