forked from SWI-Prolog/packages-semweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshot.c
231 lines (179 loc) · 5 KB
/
snapshot.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
/* Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 2002-2012, University of Amsterdam
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "rdf_db.h"
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create a new snapshot, adding it as the _start_ of the DB's snapshot
list.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
snapshot *
new_snapshot(rdf_db *db)
{ query *q = open_query(db);
snapshot *ss = rdf_malloc(db, sizeof(*ss));
ss->rd_gen = q->rd_gen;
ss->tr_gen = q->tr_gen;
ss->db = db;
ss->symbol = 0;
simpleMutexLock(&db->locks.misc);
if ( db->snapshots.head )
{ ss->next = db->snapshots.head;
ss->prev = NULL;
db->snapshots.head->prev = ss;
db->snapshots.head = ss;
if ( ss->rd_gen < db->snapshots.keep )
db->snapshots.keep = ss->rd_gen;
} else
{ ss->next = ss->prev = NULL;
db->snapshots.head = db->snapshots.tail = ss;
db->snapshots.keep = ss->rd_gen;
}
simpleMutexUnlock(&db->locks.misc);
close_query(q);
return ss;
}
static void
unlink_snapshot(snapshot *ss)
{ rdf_db *db = ss->db;
if ( ss->next )
ss->next->prev = ss->prev;
if ( ss->prev )
ss->prev->next = ss->next;
if ( ss == db->snapshots.head )
db->snapshots.head = ss->next;
if ( ss == db->snapshots.tail )
db->snapshots.tail = ss->prev;
}
static void
update_keep_snapshot(snapshot *ss)
{ rdf_db *db = ss->db;
if ( ss->rd_gen == db->snapshots.keep )
{ gen_t oldest = GEN_MAX;
snapshot *s;
for(s=db->snapshots.head; s; s=s->next)
{ if ( s->rd_gen < oldest )
oldest = s->rd_gen;
}
db->snapshots.keep = oldest;
DEBUG(1, { char buf[64];
Sdprintf("Deleted oldest snapshot; set keep gen to %s\n",
gen_name(oldest, buf));
});
}
}
int
free_snapshot(snapshot *ss)
{ rdf_db *db = ss->db;
int rc;
simpleMutexLock(&db->locks.misc);
if ( (rc=(ss->symbol != 0)) )
{ unlink_snapshot(ss);
update_keep_snapshot(ss);
ss->symbol = 0;
}
simpleMutexUnlock(&db->locks.misc);
return rc;
}
void
erase_snapshots(rdf_db *db)
{ snapshot *ss;
simpleMutexLock(&db->locks.misc);
while( (ss=db->snapshots.head) )
{ unlink_snapshot(ss);
ss->symbol = 0;
}
db->snapshots.keep = GEN_MAX;
simpleMutexUnlock(&db->locks.misc);
}
static void
acquire_snapshot(atom_t symbol)
{ snapshot *ss = PL_blob_data(symbol, NULL, NULL);
ss->symbol = symbol;
}
static int
release_snapshot(atom_t symbol)
{ snapshot *ss = PL_blob_data(symbol, NULL, NULL);
free_snapshot(ss);
rdf_free(ss->db, ss, sizeof(*ss));
return TRUE;
}
static int
compare_snapshot(atom_t a, atom_t b)
{ snapshot *ssa = PL_blob_data(a, NULL, NULL);
snapshot *ssb = PL_blob_data(b, NULL, NULL);
return ( ssa->rd_gen > ssb->rd_gen ? 1 :
ssa->rd_gen < ssb->rd_gen ? -1 :
ssa->tr_gen > ssb->tr_gen ? 1 :
ssa->tr_gen < ssb->tr_gen ? -1 :
ssa > ssb ? 1 :
ssb < ssa ? -1 : 0
);
}
static int
write_snapshot(IOSTREAM *s, atom_t symbol, int flags)
{ snapshot *ss = PL_blob_data(symbol, NULL, NULL);
char buf[64];
if ( ss->tr_gen > GEN_TBASE )
{ char buf2[64];
Sfprintf(s, "<rdf-snapshot>(%s+%s)",
gen_name(ss->rd_gen, buf),
gen_name(ss->tr_gen, buf2));
} else
{ Sfprintf(s, "<rdf-snapshot>(%s)", gen_name(ss->rd_gen, buf));
}
return TRUE;
}
static PL_blob_t snap_blob =
{ PL_BLOB_MAGIC,
PL_BLOB_NOCOPY|PL_BLOB_UNIQUE,
"rdf_snapshot",
release_snapshot,
compare_snapshot,
write_snapshot,
acquire_snapshot
};
int
unify_snapshot(term_t t, snapshot *ss)
{ int rc = PL_unify_blob(t, ss, sizeof(*ss), &snap_blob);
if ( !rc )
free_snapshot(ss);
return rc;
}
int
get_snapshot(term_t t, snapshot **ssp)
{ PL_blob_t *type;
void *data;
if ( PL_get_blob(t, &data, NULL, &type) && type == &snap_blob)
{ snapshot *ss = data;
if ( ss->symbol )
{ *ssp = ss;
return TRUE;
}
return -1;
}
return FALSE;
}
/* snapshot_thread() is the id of the thread that created the snapshot
if the snapshot is created inside a modified transaction.
*/
int
snapshot_thread(snapshot *ss)
{ if ( ss->tr_gen > GEN_TBASE &&
((ss->tr_gen-GEN_TBASE)%GEN_TNEST) != 0 )
return (ss->tr_gen-GEN_TBASE)/GEN_TNEST;
return 0;
}