-
Notifications
You must be signed in to change notification settings - Fork 3
/
tuple.c
248 lines (199 loc) · 4.93 KB
/
tuple.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
Copyright 2008-2011 Ostap Cherkashin
Copyright 2008-2011 Julius Chrobak
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 "config.h"
#include "system.h"
#include "memory.h"
#include "string.h"
#include "number.h"
#include "head.h"
#include "value.h"
#include "tuple.h"
static void init(Tuple *t)
{
void *mem = t + 1;
t->v.size = mem;
t->v.off = mem + t->v.len * sizeof(int);
}
extern Tuple *tuple_new(Value vals[], int len)
{
int size = 0;
for (int i = 0; i < len; ++i)
size += vals[i].size;
int total = size + sizeof(Tuple) + 2 * len * sizeof(int);
Tuple *res = mem_alloc(total);
res->size = total;
res->v.len = len;
init(res);
int off = total - size;
void *mem = (void*) res + off;
for (int i = 0; i < len; ++i) {
res->v.off[i] = off;
res->v.size[i] = val_bin_enc(mem, vals[i]);
off += res->v.size[i];
mem += res->v.size[i];
}
return res;
}
extern void tuple_free(Tuple *t)
{
mem_free(t);
}
extern Tuple *tuple_cpy(Tuple *t)
{
Tuple *res = mem_alloc(t->size);
mem_cpy(res, t, t->size);
init(res);
return res;
}
extern Tuple *tuple_dec(void *mem, int *len)
{
*len = int_dec(mem);
Tuple *res = mem_alloc(*len);
mem_cpy(res, mem, *len);
init(res);
return res;
}
extern int tuple_enc(Tuple *t, void *buf)
{
mem_cpy(buf, t, t->size);
return t->size;
}
extern Value tuple_attr(Tuple *t, int pos)
{
void *mem = t;
Value res = {.size = t->v.size[pos], .data = mem + t->v.off[pos]};
return res;
}
extern Tuple *tuple_reord(Tuple *t, int pos[], int len)
{
Value res[len];
for (int i = 0; i < len; ++i)
res[i] = tuple_attr(t, pos[i]);
return tuple_new(res, len);
}
extern Tuple *tuple_join(Tuple *l, Tuple *r, int lpos[], int rpos[], int len)
{
Value res[len];
for (int i = 0; i < len; ++i)
res[i] = rpos[i] < 0 ? tuple_attr(l, lpos[i]) : tuple_attr(r, rpos[i]);
return tuple_new(res, len);
}
extern int tuple_cmp(Tuple *l, Tuple *r, int lpos[], int rpos[], int len)
{
int res = 0;
for (int i = 0; i < len && !res; ++i)
res = val_cmp(tuple_attr(l, lpos[i]), tuple_attr(r, rpos[i]));
return res;
}
extern TBuf *tbuf_new()
{
TBuf *res = mem_alloc(sizeof(TBuf));
res->pos = res->len = res->size = 0;
res->buf = NULL;
return res;
}
extern void tbuf_add(TBuf *b, Tuple *t)
{
if (b->len >= b->size) {
b->size += 128;
b->buf = mem_realloc(b->buf, sizeof(Tuple*) * b->size);
}
b->buf[b->len] = t;
b->len++;
}
extern void tbuf_reset(TBuf *b)
{
b->pos = 0;
}
extern Tuple *tbuf_next(TBuf *b)
{
return (b->pos < b->len) ? b->buf[b->pos++] : NULL;
}
extern void tbuf_clean(TBuf *b)
{
tbuf_reset(b);
Tuple *t;
while ((t = tbuf_next(b)) != NULL)
tuple_free(t);
}
extern void tbuf_free(TBuf *b)
{
if (b->buf != NULL)
mem_free(b->buf);
mem_free(b);
}
extern TBuf *tbuf_read(IO *io)
{
TBuf *b = tbuf_new();
char data_buf[MAX_BLOCK];
for (;;) {
int size = -1;
if (sys_readn(io, &size, sizeof(size)) != sizeof(size))
goto failure;
if (size == 0)
goto success;
char *p = data_buf;
if (sys_readn(io, p, size) != size)
goto failure;
while (p - data_buf < size) {
int len = 0;
Tuple *t = tuple_dec(p, &len);
tbuf_add(b, t);
p += len;
}
}
failure:
if (b != NULL) {
tbuf_clean(b);
tbuf_free(b);
b = NULL;
}
success:
return b;
}
extern int tbuf_write(TBuf *b, IO *io)
{
char data_buf[MAX_BLOCK];
char *p = data_buf;
int used = 0, count = 0;
Tuple *t;
tbuf_reset(b);
while ((t = tbuf_next(b)) != NULL) {
used = p - data_buf;
if (MAX_BLOCK - used < t->size) {
if (sys_write(io, &used, sizeof(used)) < 0 ||
sys_write(io, data_buf, used) < 0)
goto failure;
p = data_buf;
}
p += tuple_enc(t, p);
tuple_free(t);
count++;
}
used = p - data_buf;
if (sys_write(io, &used, sizeof(used)) < 0)
goto failure;
if (used > 0) {
if (sys_write(io, data_buf, used) < 0)
goto failure;
used = 0;
if (sys_write(io, &used, sizeof(used)) < 0)
goto failure;
}
return count;
failure:
while ((t = tbuf_next(b)) != NULL)
tuple_free(t);
return -1;
}