forked from SWI-Prolog/packages-semweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.c
332 lines (253 loc) · 6.97 KB
/
resource.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2002-2010, VU University Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "rdf_db.h"
#include "murmur.h"
static int
init_resource_hash(resource_db *rdb)
{ size_t bytes = sizeof(resource**)*INITIAL_RESOURCE_TABLE_SIZE;
resource **r = rdf_malloc(rdb->db, bytes);
int i, count = INITIAL_RESOURCE_TABLE_SIZE;
memset(r, 0, bytes);
for(i=0; i<MSB(count); i++)
rdb->hash.blocks[i] = r;
rdb->hash.bucket_count = count;
rdb->hash.bucket_count_epoch = count;
rdb->hash.count = 0;
return TRUE;
}
static void
free_resource_chain(rdf_db *db, resource *r)
{ resource *n;
for(; r; r=n)
{ n = r->next;
PL_unregister_atom(r->name);
rdf_free(db, r, sizeof(*r));
}
}
static void
free_resource_chains(rdf_db *db, resource **rl, int count)
{ int i;
for(i=0; i<count; i++)
free_resource_chain(db, rl[i]);
rdf_free(db, rl, sizeof(resource**)*count);
}
static void
erase_resource_hash(resource_db *rdb)
{ if ( rdb->hash.blocks[0] )
{ int i, count = INITIAL_RESOURCE_TABLE_SIZE;
free_resource_chains(rdb->db, rdb->hash.blocks[0], count);
for(i=MSB(count); i<MAX_RBLOCKS; i++)
{ resource **r = rdb->hash.blocks[i];
if ( r )
{ int size = BLOCKLEN(i);
r += size;
free_resource_chains(rdb->db, r, size);
} else
break;
}
}
memset(&rdb->hash, 0, sizeof(rdb->hash));
}
static int
resize_resource_table(resource_db *rdb)
{ int i = MSB(rdb->hash.bucket_count);
size_t bytes = sizeof(resource**)*rdb->hash.bucket_count;
resource **r = rdf_malloc(rdb->db, bytes);
memset(r, 0, bytes);
rdb->hash.blocks[i] = r-rdb->hash.bucket_count;
rdb->hash.bucket_count *= 2;
DEBUG(1, Sdprintf("Resized resource table to %ld\n",
(long)rdb->hash.bucket_count));
return TRUE;
}
int
init_resource_db(rdf_db *db, resource_db *rdb)
{ rdb->db = db;
init_resource_hash(rdb);
return TRUE;
}
void
erase_resources(resource_db *rdb)
{ erase_resource_hash(rdb);
}
typedef struct res_walker
{ resource_db *rdb; /* Resource DB */
atom_t name; /* Name of the resource */
size_t unbounded_hash; /* Atom's hash */
size_t bcount; /* current bucket count */
resource *current; /* current location */
} res_walker;
static void
init_res_walker(res_walker *rw, resource_db *rdb, atom_t name)
{ rw->rdb = rdb;
rw->name = name;
rw->unbounded_hash = atom_hash(name, MURMUR_SEED);
rw->bcount = rdb->hash.bucket_count_epoch;
rw->current = NULL;
}
static resource*
next_resource(res_walker *rw)
{ resource *r;
if ( rw->current )
{ r = rw->current;
rw->current = r->next;
} else if ( rw->bcount <= rw->rdb->hash.bucket_count )
{ do
{ int entry = rw->unbounded_hash % rw->bcount;
r = rw->rdb->hash.blocks[MSB(entry)][entry];
rw->bcount *= 2;
} while(!r && rw->bcount <= rw->rdb->hash.bucket_count );
if ( r )
rw->current = r->next;
} else
return NULL;
return r;
}
static resource *
existing_resource(resource_db *rdb, atom_t name)
{ res_walker rw;
resource *r;
init_res_walker(&rw, rdb, name);
while((r=next_resource(&rw)))
{ if ( r->name == name )
return r;
}
return NULL;
}
resource *
lookup_resource(resource_db *rdb, atom_t name)
{ resource *r, **rp;
int entry;
if ( (r=existing_resource(rdb, name)) )
return r;
LOCK_MISC(rdb->db);
if ( (r=existing_resource(rdb, name)) )
{ UNLOCK_MISC(rdb->db);
return r;
}
r = rdf_malloc(rdb->db, sizeof(*r));
memset(r, 0, sizeof(*r));
r->name = name;
PL_register_atom(name);
if ( rdb->hash.count > rdb->hash.bucket_count )
resize_resource_table(rdb);
entry = atom_hash(name, MURMUR_SEED) % rdb->hash.bucket_count;
rp = &rdb->hash.blocks[MSB(entry)][entry];
r->next = *rp;
*rp = r;
rdb->hash.count++;
UNLOCK_MISC(rdb->db);
return r;
}
resource *
register_resource(resource_db *rdb, atom_t name)
{ resource *r = lookup_resource(rdb, name);
assert(r);
ATOMIC_INC(&r->references);
return r;
}
resource *
unregister_resource(resource_db *rdb, atom_t name)
{ resource *r = existing_resource(rdb, name);
ATOMIC_DEC(&r->references);
return r;
}
/*******************************
* PROLOG *
*******************************/
typedef struct res_enum
{ resource_db *rdb;
resource *current;
int current_entry;
} res_enum;
static foreign_t
rdf_resource(term_t r, control_t h)
{ rdf_db *db = rdf_current_db();
res_enum *state;
switch( PL_foreign_control(h) )
{ case PL_FIRST_CALL:
{ atom_t name;
if ( PL_is_variable(r) )
{ state = PL_malloc_uncollectable(sizeof(*state));
state->rdb = &db->resources;
state->current = NULL;
state->current_entry = -1;
break;
} else if ( PL_get_atom_ex(r, &name) )
{ resource *r;
if ( (r=existing_resource(&db->resources, name)) &&
r->references > 0
)
return TRUE;
return FALSE;
}
return FALSE;
}
case PL_REDO:
state = PL_foreign_context_address(h);
break;
case PL_PRUNED:
state = PL_foreign_context_address(h);
rdf_free(db, state, sizeof(*state));
return TRUE;
default:
assert(0);
return FALSE;
}
for(;;)
{ int ce;
for ( ; state->current; state->current = state->current->next )
{ if ( state->current->references )
{ if ( !PL_unify_atom(r, state->current->name) )
{ PL_free(state);
return FALSE; /* error */
}
state->current = state->current->next;
PL_retry_address(state);
}
}
if ( (ce = ++state->current_entry) < state->rdb->hash.bucket_count )
{ state->current = state->rdb->hash.blocks[MSB(ce)][ce];
} else
{ PL_free(state);
return FALSE;
}
}
}
#ifdef O_DEBUG
#define RDF_LOOKUP_RESOURCE
static foreign_t
rdf_lookup_resource(term_t r)
{ rdf_db *db = rdf_current_db();
atom_t a;
if ( !PL_get_atom_ex(r, &a) )
return FALSE;
lookup_resource(&db->resources, a);
return TRUE;
}
#endif
#define NDET PL_FA_NONDETERMINISTIC
int
register_resource_predicates(void)
{ PL_register_foreign("rdf_resource", 1, rdf_resource, NDET);
#ifdef RDF_LOOKUP_RESOURCE
PL_register_foreign("rdf_lookup_resource", 1, rdf_lookup_resource, 0);
#endif
return TRUE;
}