aboutsummaryrefslogtreecommitdiffstats
path: root/libibex/diskarray.c
blob: 996ec78818f2a92d9366e27ecf5e25d79c61609b (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2000 Ximian, Inc.
 *
 * Authors: Michael Zucchi <notzed@ximian.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 * 
 * This library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with the Gnome Library; see the file COPYING.LIB.  If not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* a disk based array storage class */


#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <glib.h>

#include "block.h"
#include "index.h"

#define d(x)
/*#define DEBUG*/

static struct _IBEXStore *disk_create(struct _memcache *bc);
static int disk_sync(struct _IBEXStore *store);
static int disk_close(struct _IBEXStore *store);

static blockid_t disk_add(struct _IBEXStore *store, blockid_t head, nameid_t data);
static blockid_t disk_add_list(struct _IBEXStore *store, blockid_t head, GArray *data);
static blockid_t disk_remove(struct _IBEXStore *store, blockid_t head, nameid_t data);
static void disk_free(struct _IBEXStore *store, blockid_t head);

static gboolean disk_find(struct _IBEXStore *store, blockid_t head, nameid_t data);
static GArray *disk_get(struct _IBEXStore *store, blockid_t head);

struct _IBEXStoreClass ibex_diskarray_class = {
    disk_create, disk_sync, disk_close,
    disk_add, disk_add_list,
    disk_remove, disk_free,
    disk_find, disk_get
};

static struct _IBEXStore *disk_create(struct _memcache *bc)
{
    struct _IBEXStore *store;

    store = g_malloc0(sizeof(*store));
    store->klass = &ibex_diskarray_class;
    store->blocks = bc;

    return store;
}

static int disk_sync(struct _IBEXStore *store)
{
    /* no cache, nop */
    return 0;
}

static int disk_close(struct _IBEXStore *store)
{
    g_free(store);
    return 0;
}

static blockid_t
disk_add(struct _IBEXStore *store, blockid_t head, nameid_t data)
{
    struct _block *block;
    struct _block *newblock;
    blockid_t new;

    if (head == 0) {
        head = ibex_block_get(store->blocks);
    }
    block = ibex_block_read(store->blocks, head);

    d(printf("adding record %d to block %d (next = %d)\n", data, head, block->next));

    if (block->used < sizeof(block->bl_data)/sizeof(block->bl_data[0])) {
        d(printf("adding record into block %d  %d\n", head, data));
        block->bl_data[block->used] = data;
        block->used++;
        ibex_block_dirty(block);
        return head;
    } else {
        new = ibex_block_get(store->blocks);
        newblock = ibex_block_read(store->blocks, new);
        newblock->next = head;
        newblock->bl_data[0] = data;
        newblock->used = 1;
        d(printf("adding record into new %d  %d, next =%d\n", new, data, newblock->next));
        ibex_block_dirty(newblock);
        return new;
    }
}

static blockid_t
disk_add_list(struct _IBEXStore *store, blockid_t head, GArray *data)
{
    struct _block *block;
    struct _block *newblock;
    blockid_t new;
    int copied = 0;
    int left, space, tocopy;

    if (head == 0) {
        head = ibex_block_get(store->blocks);
    }
    block = ibex_block_read(store->blocks, head);

    while (copied < data->len) {
        left = data->len - copied;
        space = sizeof(block->bl_data)/sizeof(block->bl_data[0]) - block->used;
        if (space) {
            tocopy = MIN(left, space);
            memcpy(block->bl_data+block->used, &g_array_index(data, blockid_t, copied), tocopy*sizeof(blockid_t));
            block->used += tocopy;
            ibex_block_dirty(block);
        } else {
            new = ibex_block_get(store->blocks);
            newblock = ibex_block_read(store->blocks, new);
            newblock->next = head;
            tocopy = MIN(left, sizeof(block->bl_data)/sizeof(block->bl_data[0]));
            memcpy(newblock->bl_data, &g_array_index(data, blockid_t, copied), tocopy*sizeof(blockid_t));
            newblock->used = tocopy;
            block = newblock;
            head = new;
            ibex_block_dirty(newblock);
        }
        copied += tocopy;
    }
    return head;
}

static blockid_t
disk_remove(struct _IBEXStore *store, blockid_t head, nameid_t data)
{
    blockid_t node = head;

    d(printf("removing %d from %d\n", data, head));
    while (node) {
        struct _block *block = ibex_block_read(store->blocks, node);
        int i;

        for (i=0;i<block->used;i++) {
            if (block->bl_data[i] == data) {
                struct _block *start = ibex_block_read(store->blocks, head);

                start->used--;
                block->bl_data[i] = start->bl_data[start->used];
                if (start->used == 0) {
                    struct _root *root = (struct _root *)ibex_block_read(store->blocks, 0);
                    blockid_t new;

                    d(printf("dropping block %d, new head = %d\n", head, start->next));
                    new = start->next;
                    start->next = root->free;
                    root->free = head;
                    head = new;
                    ibex_block_dirty((struct _block *)root);
                }
                ibex_block_dirty(block);
                ibex_block_dirty(start);
                return head;
            }
        }
        node = block->next;
    }
    return head;
}

static void disk_free(struct _IBEXStore *store, blockid_t head)
{
    blockid_t next;
    struct _block *block;

    while (head) {
        block = ibex_block_read(store->blocks, head);
        next = block->next;
        ibex_block_free(store->blocks, head);
        head = next;
    }
}

static gboolean
disk_find(struct _IBEXStore *store, blockid_t head, nameid_t data)
{
    blockid_t node = head;

    d(printf("finding %d from %d\n", data, head));
    while (node) {
        struct _block *block = ibex_block_read(store->blocks, node);
        int i;

        for (i=0;i<block->used;i++) {
            if (block->bl_data[i] == data) {
                return TRUE;
            }
        }
        node = block->next;
    }
    return FALSE;
}

static GArray *
disk_get(struct _IBEXStore *store, blockid_t head)
{
    GArray *result = g_array_new(0, 0, sizeof(nameid_t));

    while (head) {
        struct _block *block = ibex_block_read(store->blocks, head);

        d(printf("getting data from block %d\n", head));

        g_array_append_vals(result, block->bl_data, block->used);
        head = block->next;
        d(printf("next = %d\n", head));
    }
    return result;
}

void
ibex_diskarray_dump(struct _memcache *blocks, blockid_t head)
{
    blockid_t node = head;

    printf("dumping list %d\n", node);
    while (node) {
        struct _block *block = ibex_block_read(blocks, node);
        int i;

        printf(" block %d used %d\n ", node, block->used);
        for (i=0;i<block->used;i++) {
            printf(" %d", block->bl_data[i]);
        }
        printf("\n");
        node = block->next;
    }
}