Bladeren bron

Implements support for max-date

Yann Weber 7 jaren geleden
bovenliggende
commit
e3e636e298
2 gewijzigde bestanden met toevoegingen van 171 en 43 verwijderingen
  1. 16
    10
      src/include/ttail_search_files.h
  2. 155
    33
      src/ttail_search_files.c

+ 16
- 10
src/include/ttail_search_files.h Bestand weergeven

12
 typedef struct _ttail_search_file_s ttail_search_file_t;
12
 typedef struct _ttail_search_file_s ttail_search_file_t;
13
 
13
 
14
 
14
 
15
+struct _ttail_files_off_s
16
+{
17
+	size_t id;
18
+	off_t off;
19
+};
20
+
21
+typedef struct _ttail_files_off_s ttail_files_off_t;
22
+
15
 /*<! Private search session for logfiles */
23
 /*<! Private search session for logfiles */
16
 struct _ttail_search_file_s
24
 struct _ttail_search_file_s
17
 {
25
 {
39
 	/*<! buffer size for ttail_getiline() macro */
47
 	/*<! buffer size for ttail_getiline() macro */
40
 	size_t buf_sz;
48
 	size_t buf_sz;
41
 	
49
 	
42
-	/*<! Current file id */
43
-	size_t id;
44
-	/*<! Current file offset */
45
-	off_t off;
50
+	/*<! Closest offset to min date*/
51
+	ttail_files_off_t off_min;
52
+	/*<! Closest offset to max date*/
53
+	ttail_files_off_t off_max;
46
 };
54
 };
47
 
55
 
48
 #include "config.h"
56
 #include "config.h"
81
 /**@brief Binary search of the last logline with a date < tm
89
 /**@brief Binary search of the last logline with a date < tm
82
  *@param ttail ttail_t*
90
  *@param ttail ttail_t*
83
  *@param tm struct tm*
91
  *@param tm struct tm*
84
- *param id size_t* result file id
85
- *@param off off_t* set to -1 if none found
92
+ *param min short if 1 process min else process max
86
  *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
93
  *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
87
  *@return 0 if ok -1 if error 1 if empty result
94
  *@return 0 if ok -1 if error 1 if empty result
88
  */
95
  */
89
 int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
96
 int _ttail_search_files_binary_search(ttail_t*, const struct tm*,
90
-	const struct tm**);
97
+	const struct tm**, short);
91
 
98
 
92
 /**@brief Binary search of the last logline with a date < tm in a file
99
 /**@brief Binary search of the last logline with a date < tm in a file
93
  *@param ttail ttail_t*
100
  *@param ttail ttail_t*
94
  *@param tm struct tm*
101
  *@param tm struct tm*
95
- *param id size_t the file to search into
96
- *@param off off_t* set to -1 if none found
97
  *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
102
  *@param ftm struct tm** local variable of @ref _ttail_search_closest_files()
103
+ *param min short if 1 process min else process max
98
  *@return 0 if ok -1 if error 1 if empty result
104
  *@return 0 if ok -1 if error 1 if empty result
99
  */
105
  */
100
 int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
106
 int _ttail_search_file_binary_search(ttail_t*, const struct tm*,
101
-	const struct tm**);
107
+	const struct tm**, short);
102
 
108
 
103
 /**@brief Attempt to reopen a file
109
 /**@brief Attempt to reopen a file
104
  *@param ttail_t* ttail
110
  *@param ttail_t* ttail

+ 155
- 33
src/ttail_search_files.c Bestand weergeven

64
 		prev_found = 1;
64
 		prev_found = 1;
65
 		prev = i;
65
 		prev = i;
66
 	}
66
 	}
67
-	/* TODO begining binary search of date_min */
68
-	ret = _ttail_search_files_binary_search(t, tm, (const struct tm**)ftm);
67
+	if(t->flag & TTAIL_FLAG_DATE_MIN)
68
+	{
69
+		ret = _ttail_search_files_binary_search(t, &(t->date_min),
70
+			(const struct tm**)ftm, 1);
71
+		if(ret)
72
+		{
73
+			goto _ttail_search_closest_files_err;
74
+		}
75
+	}
76
+	if(t->flag & TTAIL_FLAG_DATE_MAX)
77
+	{
78
+		ret = _ttail_search_files_binary_search(t, &(t->date_max),
79
+			(const struct tm**)ftm, 0);
80
+		if(ret)
81
+		{
82
+			goto _ttail_search_closest_files_err;
83
+		}
84
+		t->session->file.off_max.off++;
85
+	}
69
 	for(i=0; i<t->logfile_sz; i++)
86
 	for(i=0; i<t->logfile_sz; i++)
70
 	{
87
 	{
71
 		if(ftm) { free(ftm[i]); }
88
 		if(ftm) { free(ftm[i]); }
97
 	int fd;
114
 	int fd;
98
 	char buf[8192];
115
 	char buf[8192];
99
 	int r;
116
 	int r;
100
-
101
-	for(i=t->session->file.id; i<t->logfile_sz; i++)
117
+	off_t cur_off;
118
+	
119
+	/* if no date_min t->session->file.id == 0 */
120
+	for(i=t->session->file.off_min.id; i<t->logfile_sz; i++)
102
 	{
121
 	{
103
 		if(!t->logfile[i])
122
 		if(!t->logfile[i])
104
 		{
123
 		{
105
 			continue;
124
 			continue;
106
 		}
125
 		}
107
 		fd = fileno(t->logfile[i]);
126
 		fd = fileno(t->logfile[i]);
108
-		if(i==t->session->file.id)
127
+		if(t->flag & TTAIL_FLAG_DATE_MIN &&
128
+			i==t->session->file.off_min.id)
109
 		{
129
 		{
110
-			lseek(fd, t->session->file.off, SEEK_SET);
111
-		} else {
112
-			lseek(fd, 0, SEEK_SET);
130
+			if(t->flag & TTAIL_FLAG_DATE_MAX)
131
+			{
132
+				fseek(t->logfile[i],
133
+					t->session->file.off_min.off,
134
+					SEEK_SET);
135
+			}
136
+			else
137
+			{
138
+				lseek(fd, t->session->file.off_min.off,
139
+					SEEK_SET);
140
+			}
113
 		}
141
 		}
114
-		while(1)
142
+		else
115
 		{
143
 		{
116
-			r = read(fd, buf, sizeof(buf));
117
-			if(r == -1)
144
+			if(t->flag & TTAIL_FLAG_DATE_MAX)
118
 			{
145
 			{
119
-				perror("unable to read file");
120
-				return;
146
+				fseek(t->logfile[i], 0, SEEK_SET);
121
 			}
147
 			}
122
-			else if(r == 0)
148
+			else
123
 			{
149
 			{
124
-				break;
150
+				lseek(fd, 0, SEEK_SET);
125
 			}
151
 			}
126
-			r = write(out, buf, r);
127
-			if(r == -1 || r == 0)
152
+		}
153
+		cur_off = 0;
154
+		while(1)
155
+		{
156
+			if(t->flag & TTAIL_FLAG_DATE_MAX)
128
 			{
157
 			{
129
-				perror("Unable to write result");
130
-				return;
158
+				if(i >= t->session->file.off_max.id &&
159
+					cur_off >= t->session->file.off_max.off)
160
+				{
161
+					return;
162
+				}
163
+				if((r = ttail_getline(t, i)) < 0)
164
+				{
165
+					break;
166
+				}
167
+				cur_off += r;
168
+				write(out, ttail_getline_buf(t),
169
+					strlen(ttail_getline_buf(t)));
170
+			}
171
+			else
172
+			{
173
+				r = read(fd, buf, sizeof(buf));
174
+				if(r == -1)
175
+				{
176
+					perror("unable to read file");
177
+					return;
178
+				}
179
+				else if(r == 0)
180
+				{
181
+					break;
182
+				}
183
+				cur_off += r;
184
+				r = write(out, buf, r);
185
+				if(r == -1 || r == 0)
186
+				{
187
+					perror("Unable to write result");
188
+					return;
189
+				}
131
 			}
190
 			}
132
 		}
191
 		}
133
 	}
192
 	}
134
 }
193
 }
135
 
194
 
136
 int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
195
 int _ttail_search_files_binary_search(ttail_t* t, const struct tm* in,
137
-	const struct tm** ftm)
196
+	const struct tm** ftm, short min)
138
 {
197
 {
139
-	int cmin, cmax;
198
+	int cmin, cmax, ret;
140
 	size_t valid;
199
 	size_t valid;
141
-	off_t *off;
200
+	int cmpres;
201
+	off_t tmpoff;
202
+	ttail_files_off_t *files_off;
142
 	size_t *id;
203
 	size_t *id;
143
-	off = &(t->session->file.off);
144
-	id = &(t->session->file.id);
204
+	off_t *off;
205
+
206
+	files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
207
+	id = &(files_off->id),
208
+	off = &(files_off->off);
209
+
145
 	*id = *off = 0;
210
 	*id = *off = 0;
146
 	valid = 0;
211
 	valid = 0;
147
 	if(ttail_tm_cmp(&(ftm[0][0]), in) > 0)
212
 	if(ttail_tm_cmp(&(ftm[0][0]), in) > 0)
185
 			cmax > 0)
250
 			cmax > 0)
186
 		{
251
 		{
187
 			/* somewhere in current file */
252
 			/* somewhere in current file */
188
-			break;
253
+			ret = _ttail_search_file_binary_search(t, in, ftm, min);
254
+			if(ret)
255
+			{
256
+				*id = 0;
257
+				return ret;
258
+			}
259
+			/* searching for equivalent */
260
+			tmpoff = *off;
261
+			while(1)
262
+			{
263
+				if(fseek(t->logfile[*id], tmpoff, SEEK_SET) < 0)
264
+				{
265
+					return -1;
266
+				}
267
+				tmpoff = min?
268
+					_ttail_file_start_line(t->logfile[*id]):
269
+					_ttail_file_next_line(t->logfile[*id]);
270
+				if(tmpoff < 0)
271
+				{
272
+					break;
273
+				}
274
+				cmpres=0;
275
+				ret = _ttail_file_cur_cmp(t, *id, in, &cmpres);
276
+				if((min && cmpres < 0) || (!min && cmpres > 0))
277
+				{
278
+					break;
279
+				}
280
+				*off = tmpoff;
281
+				if(min)
282
+				{
283
+					tmpoff--;
284
+				}
285
+			}
286
+			return 0;
189
 		}
287
 		}
190
 		(*id)++;
288
 		(*id)++;
191
 	}
289
 	}
199
 }
297
 }
200
 
298
 
201
 inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
299
 inline int _ttail_search_file_binary_search(ttail_t* t, const struct tm* in,
202
-	const struct tm** ftm)
300
+	const struct tm** ftm, short min)
203
 {
301
 {
204
-	off_t cur, sz, d, prev;
302
+	off_t cur, sz, d, prev, tmp;
205
 	int ret, cmpres;
303
 	int ret, cmpres;
206
-	off_t *off;
304
+	ttail_files_off_t *files_off;
207
 	size_t id;
305
 	size_t id;
208
-	off = &(t->session->file.off);
209
-	id = t->session->file.id;
306
+	off_t *off;
307
+
308
+	files_off = min?&(t->session->file.off_min):&(t->session->file.off_max);
309
+	id = files_off->id,
310
+	off = &(files_off->off);
210
 	
311
 	
211
 	sz = t->session->file.file_sz[id];
312
 	sz = t->session->file.file_sz[id];
212
 	d = cur = sz / 2;
313
 	d = cur = sz / 2;
214
 	cmpres = 0;
315
 	cmpres = 0;
215
 	while(1)
316
 	while(1)
216
 	{
317
 	{
217
-		cur = _ttail_file_next_line(t->logfile[id]);
318
+		if(fseek(t->logfile[id], cur, SEEK_SET) < 0)
319
+		{
320
+			perror("Unable to move in the file");
321
+			return -1;
322
+		}
323
+		if(cur > prev)
324
+		{
325
+			cur = _ttail_file_next_line(t->logfile[id]);
326
+		}
327
+		else
328
+		{
329
+			cur = _ttail_file_start_line(t->logfile[id]);
330
+		}
218
 		if(cur < -1)
331
 		if(cur < -1)
219
 		{
332
 		{
220
 			cur = _ttail_file_start_line(t->logfile[id]);
333
 			cur = _ttail_file_start_line(t->logfile[id]);
241
 		}
354
 		}
242
 		else if(cmpres < 0)
355
 		else if(cmpres < 0)
243
 		{
356
 		{
357
+			tmp = _ttail_file_next_line(t->logfile[id]);
244
 			ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
358
 			ret = _ttail_file_cur_cmp(t, id, in, &cmpres);
245
 			if(cmpres >=0)
359
 			if(cmpres >=0)
246
 			{
360
 			{
247
-				*off = cur;
361
+				*off = tmp;
248
 				break;
362
 				break;
249
 			}
363
 			}
250
 			d/=2;
364
 			d/=2;
255
 		{
369
 		{
256
 			d/=2;
370
 			d/=2;
257
 			cur -= d;
371
 			cur -= d;
372
+			if(cur < 0)
373
+			{
374
+				cur = 0;
375
+			}
258
 		}
376
 		}
259
 	}
377
 	}
260
 	return 0;
378
 	return 0;
616
 {
734
 {
617
 	int ret;
735
 	int ret;
618
 	struct tm ctm;
736
 	struct tm ctm;
737
+	if(ttail_getline(t, id) < 0)
738
+	{
739
+		return -1;
740
+	}
619
 	ret = ttail_logline2date(t, ttail_getline_buf(t), &ctm);
741
 	ret = ttail_logline2date(t, ttail_getline_buf(t), &ctm);
620
-	if(ret < 0)
742
+	if(ret)
621
 	{
743
 	{
622
 		return -1;
744
 		return -1;
623
 	}
745
 	}

Loading…
Annuleren
Opslaan