forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread.c
225 lines (186 loc) · 6.02 KB
/
read.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
Copyright 2015 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stddef.h>
#include <build/db.h>
#include <epochlib.h>
#include <ctrace.h>
#include <net.h>
#include "bdb_int.h"
#include "locks.h"
#include "genid.h"
#include "bdb_api.h"
static int bdb_fetch_next_genids_int(bdb_state_type *bdb_state, int ixnum,
int ixlen, unsigned char *ix,
unsigned long long *genids, int numgenids,
int *num_genids_gotten, int *bdberr)
{
DBT dbt_key, dbt_data;
int rc;
DBC *dbcp = NULL;
unsigned long long found_genid;
int outrc;
int cursor_flags;
int num;
int i;
unsigned char buf[512];
outrc = 0;
cursor_flags = 0;
cursor_flags |= DB_DIRTY_READ;
*num_genids_gotten = 0;
num = 0;
memset(&dbt_key, 0, sizeof(dbt_key));
memset(&dbt_data, 0, sizeof(dbt_data));
memcpy(buf, ix, ixlen);
dbt_key.data = buf;
dbt_key.size = ixlen;
dbt_key.ulen = 512;
dbt_data.data = &found_genid;
dbt_data.size = sizeof(unsigned long long);
dbt_data.ulen = sizeof(unsigned long long);
dbt_key.flags |= DB_DBT_USERMEM;
dbt_data.flags |= DB_DBT_USERMEM;
rc = bdb_state->dbp_ix[ixnum]->cursor(bdb_state->dbp_ix[ixnum], 0, &dbcp,
cursor_flags);
if (rc != 0) {
return -2;
}
rc = dbcp->c_get(dbcp, &dbt_key, &dbt_data, DB_SET_RANGE);
if (rc != 0) {
dbcp->c_close(dbcp);
return -1;
}
memcpy(genids + 0, dbt_data.data, sizeof(unsigned long long));
num++;
for (i = 1; i < numgenids; i++) {
rc = dbcp->c_get(dbcp, &dbt_key, &dbt_data, DB_NEXT);
if (rc != 0) {
dbcp->c_close(dbcp);
outrc = 0;
return 0;
}
memcpy(genids + num, dbt_data.data, sizeof(unsigned long long));
num++;
}
dbcp->c_close(dbcp);
outrc = 0;
if (outrc == 0) {
*num_genids_gotten = num;
}
return outrc;
}
int bdb_fetch_next_genids(bdb_state_type *bdb_state, int ixnum, int ixlen,
unsigned char *ix, unsigned long long *genids,
int numgenids, int *num_genids_gotten, int *bdberr)
{
int rc;
BDB_READLOCK("bdb_fetch_next_genids");
rc = bdb_fetch_next_genids_int(bdb_state, ixnum, ixlen, ix, genids,
numgenids, num_genids_gotten, bdberr);
BDB_RELLOCK();
return rc;
}
/* finds the oldest or newest genid, if direction is 0 it finds the oldest, if
* it is anything else it finds the newest. */
static int bdb_find_edge_genid_int(bdb_state_type *bdb_state, tran_type *tran,
int direction, int stripe, void *rec,
int *reclen, int maxlen,
unsigned long long *genid, uint8_t *ver,
int *bdberr)
{
DBT dbt_key = {0}, dbt_data = {0};
DBC *cur;
int rc, ixrc;
rc = bdb_state->dbp_data[0][stripe]->cursor(
bdb_state->dbp_data[0][stripe], tran ? tran->tid : NULL, &cur, 0);
if (rc) {
if (rc == DB_LOCK_DEADLOCK) {
*bdberr = BDBERR_DEADLOCK;
return -1;
} else {
*bdberr = rc;
return -1;
}
}
dbt_key.data = genid;
dbt_key.ulen = sizeof(unsigned long long);
dbt_key.flags = DB_DBT_USERMEM;
dbt_data.data = rec;
dbt_data.ulen = maxlen;
dbt_data.flags = DB_DBT_USERMEM;
ixrc = bdb_cget_unpack(bdb_state, cur, &dbt_key, &dbt_data, ver,
(direction) ? DB_LAST : DB_FIRST);
if (ixrc == 0)
*reclen = dbt_data.size;
rc = cur->c_close(cur);
if (rc) {
if (rc == DB_LOCK_DEADLOCK) {
*bdberr = BDBERR_DEADLOCK;
return -1;
} else {
*bdberr = rc;
return -1;
}
}
if (ixrc == 0)
return 0;
else if (ixrc == DB_LOCK_DEADLOCK) {
*bdberr = BDBERR_DEADLOCK;
return -1;
} else if (ixrc == DB_NOTFOUND)
return 1;
else {
*bdberr = ixrc;
return -1;
}
}
int bdb_find_oldest_genid(bdb_state_type *bdb_state, tran_type *tran,
int stripe, void *rec, int *reclen, int maxlen,
unsigned long long *genid, uint8_t *ver, int *bdberr)
{
int rc;
BDB_READLOCK("bdb_find_oldest_genid");
*bdberr = BDBERR_NOERROR;
rc = bdb_find_edge_genid_int(bdb_state, tran, 0 /*direction*/, stripe, rec,
reclen, maxlen, genid, ver, bdberr);
BDB_RELLOCK();
return rc;
}
int bdb_find_newest_genid(bdb_state_type *bdb_state, tran_type *tran,
int stripe, void *rec, int *reclen, int maxlen,
unsigned long long *genid, uint8_t *ver, int *bdberr)
{
int rc;
BDB_READLOCK("bdb_find_newest_genid");
*bdberr = BDBERR_NOERROR;
rc = bdb_find_edge_genid_int(bdb_state, tran, 1 /*direction*/, stripe, rec,
reclen, maxlen, genid, ver, bdberr);
BDB_RELLOCK();
return rc;
}