summaryrefslogtreecommitdiff
path: root/ext/SDBM_File/pair.c
blob: c12ad334e66bc8453db3d7ad1761cb323fa09447 (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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
 * sdbm - ndbm work-alike hashed database library
 * based on Per-Aake Larson's Dynamic Hashing algorithms. BIT 18 (1978).
 * author: oz@nexus.yorku.ca
 * status: public domain.
 *
 * page-level routines
 */

#include "config.h"
#ifdef __CYGWIN__
# define EXTCONST extern const
#else
# include "EXTERN.h"
#endif
#include "sdbm.h"
#include "tune.h"
#include "pair.h"

#define exhash(item)    sdbm_hash((item).dptr, (item).dsize)

/* 
 * forward 
 */
static int seepair(char *, int, const char *, int);

/*
 * page format:
 *      +------------------------------+
 * ino  | n | keyoff | datoff | keyoff |
 *      +------------+--------+--------+
 *      | datoff | - - - ---->         |
 *      +--------+---------------------+
 *      |        F R E E A R E A       |
 *      +--------------+---------------+
 *      |  <---- - - - | data          |
 *      +--------+-----+----+----------+
 *      |  key   | data     | key      |
 *      +--------+----------+----------+
 *
 * calculating the offsets for free area:  if the number
 * of entries (ino[0]) is zero, the offset to the END of
 * the free area is the block size. Otherwise, it is the
 * nth (ino[ino[0]]) entry's offset.
 */

int
fitpair(char *pag, int need)
{
        int n;
        int off;
        int free;
        short *ino = (short *) pag;

        off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
        free = off - (n + 1) * sizeof(short);
        need += 2 * sizeof(short);

        debug(("free %d need %d\n", free, need));

        return need <= free;
}

void
putpair(char *pag, datum key, datum val)
{
        int n;
        int off;
        short *ino = (short *) pag;

        off = ((n = ino[0]) > 0) ? ino[n] : PBLKSIZ;
/*
 * enter the key first
 */
        off -= key.dsize;
        (void) memcpy(pag + off, key.dptr, key.dsize);
        ino[n + 1] = off;
/*
 * now the data
 */
        off -= val.dsize;
        (void) memcpy(pag + off, val.dptr, val.dsize);
        ino[n + 2] = off;
/*
 * adjust item count
 */
        ino[0] += 2;
}

datum
getpair(char *pag, datum key)
{
        int i;
        int n;
        datum val;
        short *ino = (short *) pag;

        if ((n = ino[0]) == 0)
                return nullitem;

        if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
                return nullitem;

        val.dptr = pag + ino[i + 1];
        val.dsize = ino[i] - ino[i + 1];
        return val;
}

int
exipair(char *pag, datum key)
{
        short *ino = (short *) pag;

        if (ino[0] == 0)
                return 0;

        return (seepair(pag, ino[0], key.dptr, key.dsize) != 0);
}

#ifdef SEEDUPS
int
duppair(char *pag, datum key)
{
        short *ino = (short *) pag;
        return ino[0] > 0 && seepair(pag, ino[0], key.dptr, key.dsize) > 0;
}
#endif

datum
getnkey(char *pag, int num)
{
        datum key;
        int off;
        short *ino = (short *) pag;

        num = num * 2 - 1;
        if (ino[0] == 0 || num > ino[0])
                return nullitem;

        off = (num > 1) ? ino[num - 1] : PBLKSIZ;

        key.dptr = pag + ino[num];
        key.dsize = off - ino[num];

        return key;
}

int
delpair(char *pag, datum key)
{
        int n;
        int i;
        short *ino = (short *) pag;

        if ((n = ino[0]) == 0)
                return 0;

        if ((i = seepair(pag, n, key.dptr, key.dsize)) == 0)
                return 0;
/*
 * found the key. if it is the last entry
 * [i.e. i == n - 1] we just adjust the entry count.
 * hard case: move all data down onto the deleted pair,
 * shift offsets onto deleted offsets, and adjust them.
 * [note: 0 < i < n]
 */
        if (i < n - 1) {
                int m;
                char *dst = pag + (i == 1 ? PBLKSIZ : ino[i - 1]);
                char *src = pag + ino[i + 1];
                int   zoo = dst - src;

                debug(("free-up %d ", zoo));
/*
 * shift data/keys down
 */
                m = ino[i + 1] - ino[n];
#ifdef DUFF
#define MOVB    *--dst = *--src

                if (m > 0) {
                        int loop = (m + 8 - 1) >> 3;

                        switch (m & (8 - 1)) {
                        case 0: do {
                                MOVB; /* FALLTHROUGH */ case 7: MOVB; /* FALLTHROUGH */
                        case 6: MOVB; /* FALLTHROUGH */ case 5: MOVB; /* FALLTHROUGH */
                        case 4: MOVB; /* FALLTHROUGH */ case 3: MOVB; /* FALLTHROUGH */
                        case 2: MOVB; /* FALLTHROUGH */ case 1: MOVB;
                                } while (--loop);
                        }
                }
#else
                dst -= m;
                src -= m;
                memmove(dst, src, m);
#endif
/*
 * adjust offset index up
 */
                while (i < n - 1) {
                        ino[i] = ino[i + 2] + zoo;
                        i++;
                }
        }
        ino[0] -= 2;
        return 1;
}

/*
 * search for the key in the page.
 * return offset index in the range 0 < i < n.
 * return 0 if not found.
 */
static int
seepair(char *pag, int n, const char *key, int siz)
{
        int i;
        int off = PBLKSIZ;
        short *ino = (short *) pag;

        for (i = 1; i < n; i += 2) {
                if (siz == off - ino[i] &&
                    memEQ(key, pag + ino[i], siz))
                        return i;
                off = ino[i + 1];
        }
        return 0;
}

void
splpage(char *pag, char *New, long int sbit)
{
        datum key;
        datum val;

        int n;
        int off = PBLKSIZ;
        char cur[PBLKSIZ];
        short *ino = (short *) cur;

        (void) memcpy(cur, pag, PBLKSIZ);
        (void) memset(pag, 0, PBLKSIZ);
        (void) memset(New, 0, PBLKSIZ);

        n = ino[0];
        for (ino++; n > 0; ino += 2) {
                key.dptr = cur + ino[0]; 
                key.dsize = off - ino[0];
                val.dptr = cur + ino[1];
                val.dsize = ino[0] - ino[1];
/*
 * select the page pointer (by looking at sbit) and insert
 */
                (void) putpair((exhash(key) & sbit) ? New : pag, key, val);

                off = ino[1];
                n -= 2;
        }

        debug(("%d split %d/%d\n", ((short *) cur)[0] / 2, 
               ((short *) New)[0] / 2,
               ((short *) pag)[0] / 2));
}

/*
 * check page sanity: 
 * number of entries should be something
 * reasonable, and all offsets in the index should be in order.
 * this could be made more rigorous.
 */
/*
  Layout of a page is:
  Top of block:
  number of keys/values (short)
  Array of (number of keys/values) offsets, alternating between key offsets
  and value offsets (shorts)
  End of block:
   - value/key data, last key ends at end of block (bytes)

  So:
    N key0off val0off key1off val1off ... val1 key1 val0 key0

  Be careful to note N is the number of offsets, *not* the number of keys.
 */
int
chkpage(char *pag)
{
        int n;
        int off;
        short *ino = (short *) pag;

        if ((n = ino[0]) < 0 || n > (int)(PBLKSIZ / sizeof(short)))
                return 0;

        if (n > 0) {
                off = PBLKSIZ;
                for (ino++; n > 0; ino += 2) {
                        if (ino[0] > off || ino[1] > off ||
                            ino[1] > ino[0] || ino[1] <= 0)
                                return 0;
                        off = ino[1];
                        n -= 2;
                }
                /* there must be an even number of offsets */
                if (n != 0)
                    return 0;
                /* check the key/value offsets don't overlap the key/value data */
                if ((char *)ino > pag + off)
                    return 0;
        }
        return 1;
}