chiark / gitweb /
journal: simplify set_location()
[elogind.git] / src / journal / sd-journal.c
index 028060d348c80d46caa22f84edbdae37d109d794..5a00cb0c9475699565124432f94706e7ce48ece4 100644 (file)
@@ -87,7 +87,7 @@ static void detach_location(sd_journal *j) {
         j->current_field = 0;
 
         ORDERED_HASHMAP_FOREACH(f, j->files, i)
-                f->current_offset = 0;
+                journal_file_reset_location(f);
 }
 
 static void reset_location(sd_journal *j) {
@@ -114,20 +114,19 @@ static void init_location(Location *l, LocationType type, JournalFile *f, Object
         l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
 }
 
-static void set_location(sd_journal *j, LocationType type, JournalFile *f, Object *o,
-                         direction_t direction, uint64_t offset) {
+static void set_location(sd_journal *j, JournalFile *f, Object *o) {
         assert(j);
-        assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
         assert(f);
         assert(o);
 
-        init_location(&j->current_location, type, f, o);
+        init_location(&j->current_location, LOCATION_DISCRETE, f, o);
 
         j->current_file = f;
         j->current_field = 0;
 
-        f->last_direction = direction;
-        f->current_offset = offset;
+        /* Let f know its candidate entry was picked. */
+        assert(f->location_type == LOCATION_SEEK);
+        f->location_type = LOCATION_DISCRETE;
 }
 
 static int match_is_valid(const void *data, size_t size) {
@@ -497,47 +496,6 @@ static int compare_entry_order(JournalFile *af, Object *_ao,
         return 0;
 }
 
-static bool whole_file_precedes_location(JournalFile *f, Location *l, direction_t direction) {
-        assert(f);
-        assert(l);
-
-        if (l->type != LOCATION_DISCRETE && l->type != LOCATION_SEEK)
-                return false;
-
-        if (l->seqnum_set && sd_id128_equal(l->seqnum_id, f->header->seqnum_id))
-                return direction == DIRECTION_DOWN ?
-                        l->seqnum > le64toh(f->header->tail_entry_seqnum) :
-                        l->seqnum < le64toh(f->header->head_entry_seqnum);
-
-        if (l->realtime_set)
-                return direction == DIRECTION_DOWN ?
-                        l->realtime > le64toh(f->header->tail_entry_realtime) :
-                        l->realtime < le64toh(f->header->head_entry_realtime);
-
-        return false;
-}
-
-static bool file_may_have_preceding_entry(JournalFile *f, JournalFile *of, uint64_t op, direction_t direction) {
-        Object *o;
-        int r;
-
-        assert(f);
-        assert(of);
-
-        r = journal_file_move_to_object(of, OBJECT_ENTRY, op, &o);
-        if (r < 0)
-                return true;
-
-        if (sd_id128_equal(f->header->seqnum_id, of->header->seqnum_id))
-                return direction == DIRECTION_DOWN ?
-                        le64toh(o->entry.seqnum) >= le64toh(f->header->head_entry_seqnum) :
-                        le64toh(o->entry.seqnum) <= le64toh(f->header->tail_entry_seqnum);
-
-        return direction == DIRECTION_DOWN ?
-                le64toh(o->entry.realtime) >= le64toh(f->header->head_entry_realtime) :
-                le64toh(o->entry.realtime) <= le64toh(f->header->tail_entry_realtime);
-}
-
 _pure_ static int compare_with_location(JournalFile *af, Object *ao, Location *l) {
         uint64_t a;
 
@@ -896,6 +854,8 @@ static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direc
                         found = true;
 
                 if (found) {
+                        journal_file_save_location(f, direction, c, cp);
+
                         if (ret)
                                 *ret = c;
                         if (offset)
@@ -923,19 +883,15 @@ static int real_journal_next(sd_journal *j, direction_t direction) {
         ORDERED_HASHMAP_FOREACH(f, j->files, i) {
                 bool found;
 
-                if (whole_file_precedes_location(f, &j->current_location, direction))
-                        continue;
-
-                if (new_file && !file_may_have_preceding_entry(f, new_file, new_offset, direction))
-                        continue;
-
                 r = next_beyond_location(j, f, direction, &o, &p);
                 if (r < 0) {
                         log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
                         remove_file_real(j, f);
                         continue;
-                } else if (r == 0)
+                } else if (r == 0) {
+                        f->location_type = LOCATION_TAIL;
                         continue;
+                }
 
                 if (!new_file)
                         found = true;
@@ -960,7 +916,7 @@ static int real_journal_next(sd_journal *j, direction_t direction) {
         if (r < 0)
                 return r;
 
-        set_location(j, LOCATION_DISCRETE, new_file, o, direction, new_offset);
+        set_location(j, new_file, o);
 
         return 1;
 }
@@ -2588,10 +2544,10 @@ _public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
                         continue;
                 }
 
-                /* We do not use the type context here, but 0 instead,
-                 * so that we can look at this data object at the same
+                /* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
+                 * instead, so that we can look at this data object at the same
                  * time as one on another file */
-                r = journal_file_move_to_object(j->unique_file, 0, j->unique_offset, &o);
+                r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
                 if (r < 0)
                         return r;