Add rolling statistics that are collected across 10 second intervals.
[metze/ctdb/wip.git] / libctdb / local_tdb.c
1 /*
2    libctdb local tdb access code
3
4    Copyright (C) Andrew Tridgell  2006
5    Copyright (C) Rusty Russell  2010
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <ctdb.h>
22 #include <tdb.h>
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <ctdb_protocol.h> // For struct ctdb_ltdb_header
28 #include "local_tdb.h"
29
30 /*
31   fetch a record from the ltdb, separating out the header information
32   and returning the body of the record.  The caller should free() the
33   header when done, rather than the (optional) data->dptr.
34 */
35 struct ctdb_ltdb_header *ctdb_local_fetch(struct tdb_context *tdb,
36                                           TDB_DATA key, TDB_DATA *data)
37 {
38         TDB_DATA rec;
39
40         rec = tdb_fetch(tdb, key);
41         if (rec.dsize < sizeof(struct ctdb_ltdb_header)) {
42                 free(rec.dptr);
43                 return NULL;
44         }
45
46         if (data) {
47                 data->dsize = rec.dsize - sizeof(struct ctdb_ltdb_header);
48                 data->dptr = rec.dptr + sizeof(struct ctdb_ltdb_header);
49         }
50         return (struct ctdb_ltdb_header *)rec.dptr;
51 }
52
53
54 /*
55   write a record to a normal database: 1 on success, 0 if noop, -1 on fail.
56   errno = EIO => tdb error.
57 */
58 int ctdb_local_store(struct tdb_context *tdb, TDB_DATA key,
59                      struct ctdb_ltdb_header *header, TDB_DATA data)
60 {
61         TDB_DATA rec;
62         int ret;
63         TDB_DATA old;
64
65         old = tdb_fetch(tdb, key);
66         if (old.dsize < sizeof(*header)) {
67                 errno = EIO;
68                 return -1;
69         }
70
71         /* Debugging check: we have lock and should not change hdr. */
72         if (memcmp(old.dptr, header, sizeof(*header)) != 0) {
73                 free(old.dptr);
74                 errno = EINVAL;
75                 return -1;
76         }
77
78         /* Optimize out the nothing-changed case. */
79         if (old.dsize == sizeof(*header) + data.dsize
80             && memcmp(old.dptr+sizeof(*header), data.dptr, data.dsize) == 0) {
81                 free(old.dptr);
82                 return 0;
83         }
84
85         rec.dsize = sizeof(*header) + data.dsize;
86         rec.dptr = malloc(rec.dsize);
87         if (!rec.dptr) {
88                 free(old.dptr);
89                 errno = ENOMEM;
90                 return -1;
91         }
92         memcpy(rec.dptr, header, sizeof(*header));
93         memcpy(rec.dptr + sizeof(*header), data.dptr, data.dsize);
94
95         ret = tdb_store(tdb, key, rec, TDB_REPLACE);
96         free(old.dptr);
97         free(rec.dptr);
98         if (ret != 0) {
99                 errno = EIO;
100                 return -1;
101         }
102         return 1;
103 }