Implements support for max-date

This commit is contained in:
Yann Weber 2017-03-18 13:36:00 +01:00
commit e3e636e298
2 changed files with 172 additions and 44 deletions

View file

@ -12,6 +12,14 @@
typedef struct _ttail_search_file_s ttail_search_file_t;
struct _ttail_files_off_s
{
size_t id;
off_t off;
};
typedef struct _ttail_files_off_s ttail_files_off_t;
/*<! Private search session for logfiles */
struct _ttail_search_file_s
{
@ -39,10 +47,10 @@ struct _ttail_search_file_s
/*<! buffer size for ttail_getiline() macro */
size_t buf_sz;
/*<! Current file id */
size_t id;
/*<! Current file offset */
off_t off;
/*<! Closest offset to min date*/
ttail_files_off_t off_min;
/*<! Closest offset to max date*/
ttail_files_off_t off_max;
};
#include "config.h"
@ -81,24 +89,22 @@ int _ttail_search_closest_files_set_fsizes(ttail_t*);
/**@brief Binary search of the last logline with a date < tm
*@param ttail ttail_t*
*@param tm struct tm*
*param id size_t* result file id
*@param off off_t* set to -1 if none found
*param min short if 1 process min else process max
*@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
*@return 0 if ok -1 if error 1 if empty result
*/
int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
const struct tm**);
const struct tm**, short);
/**@brief Binary search of the last logline with a date < tm in a file
*@param ttail ttail_t*
*@param tm struct tm*
*param id size_t the file to search into
*@param off off_t* set to -1 if none found
*@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
*param min short if 1 process min else process max
*@return 0 if ok -1 if error 1 if empty result
*/
int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
const struct tm**);
const struct tm**, short);
/**@brief Attempt to reopen a file
*@param ttail_t* ttail

View file

@ -64,8 +64,25 @@ File sorting not implemented yet\n");
prev_found = 1;
prev = i;
}
/* TODO begining binary search of date_min */
ret = _ttail_search_files_binary_search(t, tm, (const struct tm**)ftm);
if(t->flag & TTAIL_FLAG_DATE_MIN)
{
ret = _ttail_search_files_binary_search(t, &(t->date_min),
(const struct tm**)ftm, 1);
if(ret)
{
goto _ttail_search_closest_files_err;
}
}
if(t->flag & TTAIL_FLAG_DATE_MAX)
{
ret = _ttail_search_files_binary_search(t, &(t->date_max),
(const struct tm**)ftm, 0);
if(ret)
{
goto _ttail_search_closest_files_err;
}
t->session->file.off_max.off++;
}
for(i=0; i<t->logfile_sz; i++)
{
if(ftm) { free(ftm[i]); }
@ -97,51 +114,99 @@ void _ttail_search_print_files(ttail_t* t, int out)
int fd;
char buf[8192];
int r;
for(i=t->session->file.id; i<t->logfile_sz; i++)
off_t cur_off;
/* if no date_min t->session->file.id == 0 */
for(i=t->session->file.off_min.id; i<t->logfile_sz; i++)
{
if(!t->logfile[i])
{
continue;
}
fd = fileno(t->logfile[i]);
if(i==t->session->file.id)
if(t->flag & TTAIL_FLAG_DATE_MIN &&
i==t->session->file.off_min.id)
{
lseek(fd, t->session->file.off, SEEK_SET);
} else {
lseek(fd, 0, SEEK_SET);
if(t->flag & TTAIL_FLAG_DATE_MAX)
{
fseek(t->logfile[i],
t->session->file.off_min.off,
SEEK_SET);
}
else
{
lseek(fd, t->session->file.off_min.off,
SEEK_SET);
}
}
else
{
if(t->flag & TTAIL_FLAG_DATE_MAX)
{
fseek(t->logfile[i], 0, SEEK_SET);
}
else
{
lseek(fd, 0, SEEK_SET);
}
}
cur_off = 0;
while(1)
{
r = read(fd, buf, sizeof(buf));
if(r == -1)
if(t->flag & TTAIL_FLAG_DATE_MAX)
{
perror("unable to read file");
return;
if(i >= t->session->file.off_max.id &&
cur_off >= t->session->file.off_max.off)
{
return;
}
if((r = ttail_getline(t, i)) < 0)
{
break;
}
cur_off += r;
write(out, ttail_getline_buf(t),
strlen(ttail_getline_buf(t)));
}
else if(r == 0)
else
{
break;
}
r = write(out, buf, r);
if(r == -1 || r == 0)
{
perror("Unable to write result");
return;
r = read(fd, buf, sizeof(buf));
if(r == -1)
{
perror("unable to read file");
return;
}
else if(r == 0)
{
break;
}
cur_off += r;
r = write(out, buf, r);
if(r == -1 || r == 0)
{
perror("Unable to write result");
return;
}
}
}
}
}
int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
const struct tm** ftm)
const struct tm** ftm, short min)
{
int cmin, cmax;
int cmin, cmax, ret;
size_t valid;
off_t *off;
int cmpres;
off_t tmpoff;
ttail_files_off_t *files_off;
size_t *id;
off = &(t->session->file.off);
id = &(t->session->file.id);
off_t *off;
files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
id = &(files_off->id),
off = &(files_off->off);
*id = *off = 0;
valid = 0;
if(ttail_tm_cmp(&(ftm[0][0]), in) > 0)
@ -185,7 +250,40 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
cmax > 0)
{
/* somewhere in current file */
break;
ret = _ttail_search_file_binary_search(t, in, ftm, min);
if(ret)
{
*id = 0;
return ret;
}
/* searching for equivalent */
tmpoff = *off;
while(1)
{
if(fseek(t->logfile[*id], tmpoff, SEEK_SET) < 0)
{
return -1;
}
tmpoff = min?
_ttail_file_start_line(t->logfile[*id]):
_ttail_file_next_line(t->logfile[*id]);
if(tmpoff < 0)
{
break;
}
cmpres=0;
ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
if((min && cmpres < 0) || (!min && cmpres > 0))
{
break;
}
*off = tmpoff;
if(min)
{
tmpoff--;
}
}
return 0;
}
(*id)++;
}
@ -199,14 +297,17 @@ int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
}
inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
const struct tm** ftm)
const struct tm** ftm, short min)
{
off_t cur, sz, d, prev;
off_t cur, sz, d, prev, tmp;
int ret, cmpres;
off_t *off;
ttail_files_off_t *files_off;
size_t id;
off = &(t->session->file.off);
id = t->session->file.id;
off_t *off;
files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
id = files_off->id,
off = &(files_off->off);
sz = t->session->file.file_sz[id];
d = cur = sz / 2;
@ -214,7 +315,19 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
cmpres = 0;
while(1)
{
cur = _ttail_file_next_line(t->logfile[id]);
if(fseek(t->logfile[id], cur, SEEK_SET) < 0)
{
perror("Unable to move in the file");
return -1;
}
if(cur > prev)
{
cur = _ttail_file_next_line(t->logfile[id]);
}
else
{
cur = _ttail_file_start_line(t->logfile[id]);
}
if(cur < -1)
{
cur = _ttail_file_start_line(t->logfile[id]);
@ -241,10 +354,11 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
}
else if(cmpres < 0)
{
tmp = _ttail_file_next_line(t->logfile[id]);
ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
if(cmpres >=0)
{
*off = cur;
*off = tmp;
break;
}
d/=2;
@ -255,6 +369,10 @@ inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
{
d/=2;
cur -= d;
if(cur < 0)
{
cur = 0;
}
}
}
return 0;
@ -616,8 +734,12 @@ inline int _ttail_file_cur_cmp(ttail_t* t, size_t id, const struct tm* tm ,
{
int ret;
struct tm ctm;
if(ttail_getline(t, id) < 0)
{
return -1;
}
ret = ttail_logline2date(t, ttail_getline_buf(t), &ctm);
if(ret < 0)
if(ret)
{
return -1;
}