@@ -160,6 +160,32 @@ static xid_t max(xid_t a, xid_t b) {
160
160
return a > b ?a :b ;
161
161
}
162
162
163
+ static void gen_snapshot (Snapshot * s ) {
164
+ s -> times_sent = 0 ;
165
+ s -> nactive = 0 ;
166
+ s -> xmin = MAX_XID ;
167
+ s -> xmax = MIN_XID ;
168
+ int i ;
169
+ for (i = 0 ;i < transactions_count ;i ++ ) {
170
+ Transaction * t = transactions + i ;
171
+ if (t -> xid < s -> xmin ) {
172
+ s -> xmin = t -> xid ;
173
+ }
174
+ if (t -> xid >=s -> xmax ) {
175
+ s -> xmax = t -> xid + 1 ;
176
+ }
177
+ s -> active [s -> nactive ++ ]= t -> xid ;
178
+ }
179
+ if (s -> nactive > 0 ) {
180
+ assert (s -> xmin < MAX_XID );
181
+ assert (s -> xmax > MIN_XID );
182
+ assert (s -> xmin <=s -> xmax );
183
+ snapshot_sort (s );
184
+ }else {
185
+ s -> xmin = s -> xmax = 0 ;
186
+ }
187
+ }
188
+
163
189
static char * onreserve (void * stream ,void * clientdata ,cmd_t * cmd ) {
164
190
CHECK (
165
191
cmd -> argc == 2 ,
@@ -195,49 +221,29 @@ static char *onreserve(void *stream, void *clientdata, cmd_t *cmd) {
195
221
minxid ,maxxid
196
222
);
197
223
198
- char response [1 + 16 + 16 + 1 ];
199
- sprintf (response ,"+%016llx%016llx" ,minxid ,maxxid );
200
- return strdup (response );
201
- }
224
+ char head [1 + 16 + 16 + 1 ];
225
+ sprintf (head ,"+%016llx%016llx" ,minxid ,maxxid );
202
226
203
- static void gen_snapshot ( Transaction * t ) {
204
- t -> snapshots_count += 1 ;
205
- Snapshot * s = transaction_latest_snapshot ( t );
227
+ Snapshot s ;
228
+ gen_snapshot ( & s ) ;
229
+ char * snapser = snapshot_serialize ( & s );
206
230
207
- s -> times_sent = 0 ;
208
- s -> nactive = 0 ;
209
- s -> xmin = MAX_XID ;
210
- s -> xmax = MIN_XID ;
211
- int i ;
212
- for (i = 0 ;i < transactions_count ;i ++ ) {
213
- Transaction * t = transactions + i ;
214
- if (t -> xid < s -> xmin ) {
215
- s -> xmin = t -> xid ;
216
- }
217
- if (t -> xid >=s -> xmax ) {
218
- s -> xmax = t -> xid + 1 ;
219
- }
220
- s -> active [s -> nactive ++ ]= t -> xid ;
221
- }
222
- assert (s -> xmin < MAX_XID );
223
- assert (s -> xmax > MIN_XID );
224
- assert (s -> xmin <=s -> xmax );
225
- snapshot_sort (s );
231
+ return destructive_concat (strdup (head ),snapser );
226
232
}
227
233
228
234
static xid_t get_global_xmin () {
229
235
int i ,j ;
230
- xid_t xmin = MAX_XID ;
236
+ xid_t xmin = INVALID_XID ;
231
237
Transaction * t ;
232
238
for (i = 0 ;i < transactions_count ;i ++ ) {
233
239
t = transactions + i ;
234
- j = t -> snapshots_count > MAX_SNAPSHOTS_PER_TRANS ?MAX_SNAPSHOTS_PER_TRANS :t -> snapshots_count ;
235
- while (-- j >=0 ) {
236
- Snapshot * s = transaction_snapshot (t ,j );
237
- if (s -> xmin < xmin ) {
238
- xmin = s -> xmin ;
239
- }
240
- // minor TODO: Use 'times_sent' to generate a bit greater xmin?
240
+ j = t -> snapshots_count > MAX_SNAPSHOTS_PER_TRANS ?MAX_SNAPSHOTS_PER_TRANS :t -> snapshots_count ;
241
+ while (-- j >=0 ) {
242
+ Snapshot * s = transaction_snapshot (t ,j );
243
+ if (( xmin == INVALID_XID ) || (s -> xmin < xmin ) ) {
244
+ xmin = s -> xmin ;
245
+ }
246
+ // minor TODO: Use 'times_sent' to generate a bit greater xmin?
241
247
}
242
248
}
243
249
return xmin ;
@@ -293,7 +299,7 @@ static char *onbegin(void *stream, void *clientdata, cmd_t *cmd) {
293
299
294
300
transactions_count ++ ;
295
301
296
- gen_snapshot (t );
302
+ gen_snapshot (transaction_next_snapshot ( t ) );
297
303
// will wrap around if exceeded max snapshots
298
304
Snapshot * snap = transaction_latest_snapshot (t );
299
305
char * snapser = snapshot_serialize (snap );
@@ -442,7 +448,7 @@ static char *onsnapshot(void *stream, void *clientdata, cmd_t *cmd) {
442
448
443
449
if (CLIENT_SNAPSENT (clientdata )== t -> snapshots_count ) {
444
450
// a fresh snapshot is needed
445
- gen_snapshot (t );
451
+ gen_snapshot (transaction_next_snapshot ( t ) );
446
452
}
447
453
448
454
char head [1 + 16 + 1 ];