summaryrefslogtreecommitdiffstats
path: root/src/dbutil.c
blob: 407593aa3bd5ebf127b9983839ef32e612942cca (plain)
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
/* $Id$ */

/***
  This file is part of syrep.

  syrep 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 2 of the License, or
  (at your option) any later version.
  
  syrep 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 GNU General Public
  License for more details.
  
  You should have received a copy of the GNU General Public License
  along with syrep; if not, write to the Free Software Foundation,
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***/

#include <string.h>
#include <assert.h>
#include <time.h>

#include "dbutil.h"
#include "util.h"

int get_meta_by_name_md(struct syrep_db_context *c, const struct syrep_name*name, const struct syrep_md *md, struct syrep_meta *meta) {
    int ret;
    struct syrep_id id;
    DBT key, data;

    assert(c && c->db_id_meta && name);

    memset(&id, 0, sizeof(id));
    memcpy(&id.name, name, sizeof(struct syrep_name));
    memcpy(&id.md, md, sizeof(struct syrep_md));
    
    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));

    key.data = &id;
    key.size = sizeof(struct syrep_id);
    
    if ((ret = c->db_id_meta->get(c->db_id_meta, NULL, &key, &data, 0))) {
        if (ret == DB_NOTFOUND)
            return 0;
        
        c->db_id_meta->err(c->db_id_meta, ret, "id_meta::get");
        return -1;
    }

    assert(data.data);
    
    if (meta)
        memcpy(meta, data.data, sizeof(struct syrep_meta));
    
    return 1;
}


int get_current_name_by_md(struct syrep_db_context *c, const struct syrep_md *md, struct syrep_name *name) {
    int ret, f;
    struct syrep_meta meta;
    DBT key, data;

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));
    key.data = (void*) md;
    key.size = sizeof(struct syrep_md);

    if ((ret = c->db_md_lastname->get(c->db_md_lastname, NULL, &key, &data, 0))) {
        if (ret == DB_NOTFOUND)
            return 0;
        
        c->db_md_lastname->err(c->db_md_lastname, ret, "md_lastname::get()");
        return -1;
    }

    assert(data.data);

    if ((f = get_meta_by_name_md(c, (struct syrep_name*) data.data, md, &meta)) < 0)
        return -1;

    if (!f) {
        fprintf(stderr, "Database inconsistency\n");
        return -1;
    }
        
    if (meta.last_seen != c->version)
        return 0;
    
    if (name)
        memcpy(name, data.data, sizeof(struct syrep_name));
    
    return 1;
}

int get_last_md_by_name(struct syrep_db_context *c, const struct syrep_name *name, struct syrep_md *md) {
    int ret;
    DBT key, data;

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));

    key.data = (void*) name;
    key.size = sizeof(struct syrep_name);

    if ((ret = c->db_name_lastmd->get(c->db_name_lastmd, NULL, &key, &data, 0))) {
        if (ret == DB_NOTFOUND) {
            return 0;
        }
        
        c->db_name_lastmd->err(c->db_name_lastmd, ret, "name_lastmd::get()");
        return -1;
    }


    assert(data.data);
    if (md)
        memcpy(md, data.data, sizeof(struct syrep_md));

    return 1;
}


int get_current_md_by_name(struct syrep_db_context *c, const struct syrep_name *name, struct syrep_md *md) {
    struct syrep_md lmd;
    struct syrep_meta meta;
    int f;

    if ((f = get_last_md_by_name(c, name, &lmd)) < 0)
        return -1;
    
    if (!f)
        return 0;

    if ((f = get_meta_by_name_md(c, name, &lmd, &meta)) < 0)
        return -1;

    if (!f) {
        fprintf(stderr, "Database inconsistency\n");
        return -1;
    }

    if (meta.last_seen != c->version)
        return 0;

    memcpy(md, &lmd, sizeof(struct syrep_md));
    return 1;
}


uint32_t get_version_timestamp(struct syrep_db_context *c, uint32_t v) {
    DBT key, data;
    struct syrep_version version;
    struct syrep_timestamp *timestamp;
    int ret;
    
    assert(c && c->db_version_timestamp);

    if (v > c->version)
        return time(NULL);

    if (v <= 0)
        v = 1;

    memset(&version, 0, sizeof(version));
    version.v = v;

    memset(&key, 0, sizeof(key));
    memset(&data, 0, sizeof(data));

    key.data = &version;
    key.size = sizeof(version);

    if ((ret = c->db_version_timestamp->get(c->db_version_timestamp, NULL, &key, &data, 0))) {
        c->db_version_timestamp->err(c->db_version_timestamp, ret, "version_timestamp::get");
        return (uint32_t) -1;
    }

    timestamp = (struct syrep_timestamp*) data.data;
    assert(timestamp);

    return timestamp->t;
}