-
Notifications
You must be signed in to change notification settings - Fork 7
/
obsvTpl.c
212 lines (180 loc) · 5.57 KB
/
obsvTpl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/***************************************************************************
* Copyright (C) 2019 by John D. Robertson *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#define _GNU_SOURCE
#include <assert.h>
#include "ez_libdb.h"
#include "ez_libc.h"
#include "ez_libz.h"
#include "obsvTpl.h"
/* Make sure we don't have padding at the end of this struct */
#pragma pack(push,2)
struct obsv {
z_off_t pos; /* position in the file */
uint16_t len; /* length of string to retrieve */
};
#pragma pack(pop)
ObsvTpl*
ObsvTpl_constructor(ObsvTpl *self, LOGFILE *lf, const char *addr)
/*****************************************************************
* prepare for use.
*/
{
memset(self, 0, sizeof(*self));
strncpy(self->addr, addr, sizeof(self->addr)-1);
self->lf= lf;
DS_constructor(&self->stack, sizeof(struct obsv), 10000);
return self;
}
int
ObsvTpl_sinit(ObsvTpl *self, LOGFILE *lf, const char *addr)
/********************************************************
* Initialize or reset a static instance
*/
{
if(self->lf) {
memset(self->addr, 0, sizeof(self->addr));
strncpy(self->addr, addr, sizeof(self->addr)-1);
self->lf= lf;
DS_reset(&self->stack);
} else {
ObsvTpl_constructor(self, lf, addr);
}
return 0;
}
void*
ObsvTpl_destructor(ObsvTpl *self)
/*****************************************************************
* Free resources
*/
{
DS_destructor(&self->stack);
return self;
}
int
ObsvTpl_addObsv(ObsvTpl *self, z_off_t pos, unsigned len)
/*****************************************************************
* Add an observation to this report object.
*/
{
struct obsv obs;
obs.pos= pos;
obs.len= len;
DS_push(&self->stack, &obs);
return 0;
}
int
ObsvTpl_db_put(ObsvTpl *self, DB *db)
/*****************************************************************
* Write our contents into the database.
*/
{
int rtn= -1;
DBT key= {
.data= self->addr,
.size= strlen(self->addr)
},
data= {
.data= DS_ptr(&self->stack),
.size= DS_numItems(&self->stack) * sizeof(struct obsv)
};
/* Write data to database */
ez_db_put(db, NULL, &key, &data, 0);
rtn= 0;
abort:
return rtn;
}
int
ObsvTpl_db_get(ObsvTpl *self, DB *db)
/********************************************************
* Fetch our content from a database.
*/
{
int rtn= -1;
DBT key= {
.data= self->addr,
.size= strlen(self->addr)
},
data;
memset(&data, 0, sizeof(data));
/* Read from the database */
int rc= ez_db_get(db, NULL, &key, &data, 0);
if(DB_NOTFOUND == rc) return rc;
/* Now put all obsv's in place */
DS_reset(&self->stack);
/* Compute number of items we fetched */
unsigned nItems= data.size / sizeof(struct obsv);
/* Load them into the stack */
for(unsigned i= 0; i < nItems; ++i) {
struct obsv *src= (struct obsv*)data.data + i;
DS_push(&self->stack, src);
}
rtn= 0;
abort:
return rtn;
}
static int obsv_print(struct obsv *self, FILE *fh)
/********************************************************
* Print observation to fh
*/
{
ez_fprintf(fh, "\tpos= %lu, len= %hu\n", (long unsigned)self->pos, self->len);
return 0;
}
int
ObsvTpl_print(ObsvTpl *self, FILE *fh)
/********************************************************
* Print a human readable representation of *self.
*/
{
ez_fprintf(fh, "ObsvTpl %s {\n", self->addr);
DS_visitAllEntries(&self->stack, (int(*)(void*,void*))obsv_print, fh);
ez_fprintf(fh, "}\n");
return 0;
}
struct infoTuple {
gzFile fh;
AddrRPT *ar;
LOGFILE *lf;
};
static int
obsv_load_AddrRPT(struct obsv *self, struct infoTuple *it)
/********************************************************
* load specific observation into it->ar.
*/
{
static char lbuf[1024];
assert(self->len < sizeof(lbuf));
unsigned len= MIN(self->len, sizeof(lbuf)-1);
/* Put null terminator in place */
lbuf[len]= '\0';
ez_gzseek(it->fh, self->pos, SEEK_SET);
ez_gzread(it->fh, lbuf, len);
AddrRPT_addLine(it->ar, it->lf, lbuf);
return 0;
}
int
ObsvTpl_put_AddrRPT(ObsvTpl *self, gzFile fh, AddrRPT *ar)
/********************************************************
* Place contents of self into ar.
*/
{
struct infoTuple it= {.fh= fh, .ar= ar, .lf= self->lf};
DS_visitAllEntries(&self->stack, (int(*)(void*,void*))obsv_load_AddrRPT, &it);
return 0;
}