summaryrefslogtreecommitdiff
path: root/src/sequence/sequence.c
blob: 1c19f8384e7066baa4055c5d432bcd7167d7654c (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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2004, 2012 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#include "dbinc/db_page.h"
#include "dbinc/db_am.h"
#include "dbinc/lock.h"
#include "dbinc/txn.h"
#include "dbinc_auto/sequence_ext.h"

#ifdef HAVE_64BIT_TYPES
/*
 * Sequences must be architecture independent but they are stored as user
 * data in databases so the code here must handle the byte ordering.  We
 * store them in little-endian byte ordering.  If we are on a big-endian
 * machine we swap in and out when we read from the database. seq->seq_rp
 * always points to the record in native ordering.
 *
 * Version 1 always stored things in native format so if we detect this we
 * upgrade on the fly and write the record back at open time.
 */
#define	SEQ_SWAP(rp)							\
	do {								\
		M_32_SWAP((rp)->seq_version);				\
		M_32_SWAP((rp)->flags);					\
		M_64_SWAP((rp)->seq_value);				\
		M_64_SWAP((rp)->seq_max);				\
		M_64_SWAP((rp)->seq_min);				\
	} while (0)

#define	SEQ_SWAP_IN(env, seq) \
	do {								\
		if (!F_ISSET((env), ENV_LITTLEENDIAN)) {		\
			memcpy(&seq->seq_record, seq->seq_data.data,	\
			     sizeof(seq->seq_record));			\
			SEQ_SWAP(&seq->seq_record);			\
		}							\
	} while (0)

#define	SEQ_SWAP_OUT(env, seq) \
	do {								\
		if (!F_ISSET((env), ENV_LITTLEENDIAN)) {		\
			memcpy(seq->seq_data.data,			\
			     &seq->seq_record, sizeof(seq->seq_record));\
			SEQ_SWAP((DB_SEQ_RECORD*)seq->seq_data.data);	\
		}							\
	} while (0)

static int __seq_chk_cachesize __P((ENV *, int32_t, db_seq_t, db_seq_t));
static int __seq_close __P((DB_SEQUENCE *, u_int32_t));
static int __seq_close_pp __P((DB_SEQUENCE *, u_int32_t));
static int __seq_get
	       __P((DB_SEQUENCE *, DB_TXN *, int32_t,  db_seq_t *, u_int32_t));
static int __seq_get_cachesize __P((DB_SEQUENCE *, int32_t *));
static int __seq_get_db __P((DB_SEQUENCE *, DB **));
static int __seq_get_flags __P((DB_SEQUENCE *, u_int32_t *));
static int __seq_get_key __P((DB_SEQUENCE *, DBT *));
static int __seq_get_range __P((DB_SEQUENCE *, db_seq_t *, db_seq_t *));
static int __seq_initial_value __P((DB_SEQUENCE *, db_seq_t));
static int __seq_open_pp __P((DB_SEQUENCE *, DB_TXN *, DBT *, u_int32_t));
static int __seq_remove __P((DB_SEQUENCE *, DB_TXN *, u_int32_t));
static int __seq_set_cachesize __P((DB_SEQUENCE *, int32_t));
static int __seq_set_flags __P((DB_SEQUENCE *, u_int32_t));
static int __seq_set_range __P((DB_SEQUENCE *, db_seq_t, db_seq_t));
static int __seq_update
	__P((DB_SEQUENCE *, DB_THREAD_INFO *, DB_TXN *, int32_t, u_int32_t));

/*
 * db_sequence_create --
 *	DB_SEQUENCE constructor.
 *
 * EXTERN: int db_sequence_create __P((DB_SEQUENCE **, DB *, u_int32_t));
 */
int
db_sequence_create(seqp, dbp, flags)
	DB_SEQUENCE **seqp;
	DB *dbp;
	u_int32_t flags;
{
	DB_SEQUENCE *seq;
	ENV *env;
	int ret;

	env = dbp->env;

	DB_ILLEGAL_BEFORE_OPEN(dbp, "db_sequence_create");

	/* Check for invalid function flags. */
	switch (flags) {
	case 0:
		break;
	default:
		return (__db_ferr(env, "db_sequence_create", 0));
	}

	if (dbp->type == DB_HEAP) {
		__db_errx(env, DB_STR("4016",
		    "Heap databases may not be used with sequences."));
		return (EINVAL);

	}

	/* Allocate the sequence. */
	if ((ret = __os_calloc(env, 1, sizeof(*seq), &seq)) != 0)
		return (ret);

	seq->seq_dbp = dbp;
	seq->close = __seq_close_pp;
	seq->get = __seq_get;
	seq->get_cachesize = __seq_get_cachesize;
	seq->set_cachesize = __seq_set_cachesize;
	seq->get_db = __seq_get_db;
	seq->get_flags = __seq_get_flags;
	seq->get_key = __seq_get_key;
	seq->get_range = __seq_get_range;
	seq->initial_value = __seq_initial_value;
	seq->open = __seq_open_pp;
	seq->remove = __seq_remove;
	seq->set_flags = __seq_set_flags;
	seq->set_range = __seq_set_range;
	seq->stat = __seq_stat;
	seq->stat_print = __seq_stat_print;
	seq->seq_rp = &seq->seq_record;
	*seqp = seq;

	return (0);
}

/*
 * __seq_open --
 *	DB_SEQUENCE->open method.
 *
 */
static int
__seq_open_pp(seq, txn, keyp, flags)
	DB_SEQUENCE *seq;
	DB_TXN *txn;
	DBT *keyp;
	u_int32_t flags;
{
	DB *dbp;
	DB_SEQ_RECORD *rp;
	DB_THREAD_INFO *ip;
	ENV *env;
	u_int32_t tflags;
	int handle_check, txn_local, ret, t_ret;
#define	SEQ_OPEN_FLAGS	(DB_CREATE | DB_EXCL | DB_THREAD)

	dbp = seq->seq_dbp;
	env = dbp->env;
	txn_local = 0;

	STRIP_AUTO_COMMIT(flags);
	SEQ_ILLEGAL_AFTER_OPEN(seq, "DB_SEQUENCE->open");

	ENV_ENTER(env, ip);

	/* Check for replication block. */
	handle_check = IS_ENV_REPLICATED(env);
	if (handle_check &&
	    (ret = __db_rep_enter(dbp, 1, 0, IS_REAL_TXN(txn))) != 0) {
		handle_check = 0;
		goto err;
	}

	if ((ret = __db_fchk(env,
	    "DB_SEQUENCE->open", flags, SEQ_OPEN_FLAGS)) != 0)
		goto err;

	if (keyp->size == 0) {
		__db_errx(env, DB_STR("4001",
		    "Zero length sequence key specified"));
		ret = EINVAL;
		goto err;
	}

	if ((ret = __db_get_flags(dbp, &tflags)) != 0)
		goto err;

	/*
	 * We can let replication clients open sequences, but must
	 * check later that they do not update them.
	 */
	if (F_ISSET(dbp, DB_AM_RDONLY)) {
		ret = __db_rdonly(dbp->env, "DB_SEQUENCE->open");
		goto err;
	}
	if (FLD_ISSET(tflags, DB_DUP)) {
		__db_errx(env, DB_STR("4002",
	"Sequences not supported in databases configured for duplicate data"));
		ret = EINVAL;
		goto err;
	}

	if (LF_ISSET(DB_THREAD)) {
		if ((ret = __mutex_alloc(env,
		    MTX_SEQUENCE, DB_MUTEX_PROCESS_ONLY, &seq->mtx_seq)) != 0)
			goto err;
	}

	memset(&seq->seq_data, 0, sizeof(DBT));
	if (F_ISSET(env, ENV_LITTLEENDIAN)) {
		seq->seq_data.data = &seq->seq_record;
		seq->seq_data.flags = DB_DBT_USERMEM;
	} else {
		if ((ret = __os_umalloc(env,
		     sizeof(seq->seq_record), &seq->seq_data.data)) != 0)
			goto err;
		seq->seq_data.flags = DB_DBT_REALLOC;
	}

	seq->seq_data.ulen = seq->seq_data.size = sizeof(seq->seq_record);
	seq->seq_rp = &seq->seq_record;

	if ((ret = __dbt_usercopy(env, keyp)) != 0)
		goto err;

	memset(&seq->seq_key, 0, sizeof(DBT));
	if ((ret = __os_malloc(env, keyp->size, &seq->seq_key.data)) != 0)
		goto err;
	memcpy(seq->seq_key.data, keyp->data, keyp->size);
	seq->seq_key.size = seq->seq_key.ulen = keyp->size;
	seq->seq_key.flags = DB_DBT_USERMEM;

retry:	if ((ret = __db_get(dbp, ip,
	    txn, &seq->seq_key, &seq->seq_data, 0)) != 0) {
		if (ret == DB_BUFFER_SMALL &&
		    seq->seq_data.size > sizeof(seq->seq_record)) {
			seq->seq_data.flags = DB_DBT_REALLOC;
			seq->seq_data.data = NULL;
			goto retry;
		}
		if ((ret != DB_NOTFOUND && ret != DB_KEYEMPTY) ||
		    !LF_ISSET(DB_CREATE))
			goto err;
		if (IS_REP_CLIENT(env) &&
		    !F_ISSET(dbp, DB_AM_NOT_DURABLE)) {
			ret = __db_rdonly(env, "DB_SEQUENCE->open");
			goto err;
		}
		ret = 0;

		rp = &seq->seq_record;
		if (!F_ISSET(rp, DB_SEQ_RANGE_SET)) {
			rp->seq_max = INT64_MAX;
			rp->seq_min = INT64_MIN;
		}
		/* INC is the default. */
		if (!F_ISSET(rp, DB_SEQ_DEC))
			F_SET(rp, DB_SEQ_INC);

		rp->seq_version = DB_SEQUENCE_VERSION;

		if (rp->seq_value > rp->seq_max ||
		    rp->seq_value < rp->seq_min) {
			__db_errx(env, DB_STR("4003",
			    "Sequence value out of range"));
			ret = EINVAL;
			goto err;
		} else {
			SEQ_SWAP_OUT(env, seq);
			/* Create local transaction as necessary. */
			if (IS_DB_AUTO_COMMIT(dbp, txn)) {
				if ((ret =
				    __txn_begin(env, ip, NULL, &txn, 0)) != 0)
					goto err;
				txn_local = 1;
			}

			if ((ret = __db_put(dbp, ip, txn, &seq->seq_key,
			     &seq->seq_data, DB_NOOVERWRITE)) != 0) {
				__db_errx(env, DB_STR("4004",
				    "Sequence create failed"));
				goto err;
			}
		}
	} else if (LF_ISSET(DB_CREATE) && LF_ISSET(DB_EXCL)) {
		ret = EEXIST;
		goto err;
	} else if (seq->seq_data.size < sizeof(seq->seq_record)) {
		__db_errx(env, DB_STR("4005",
		    "Bad sequence record format"));
		ret = EINVAL;
		goto err;
	}

	if (F_ISSET(env, ENV_LITTLEENDIAN))
		seq->seq_rp = seq->seq_data.data;

	/*
	 * The first release was stored in native mode.
	 * Check the version number before swapping.
	 */
	rp = seq->seq_data.data;
	if (rp->seq_version == DB_SEQUENCE_OLDVER) {
oldver:		if (IS_REP_CLIENT(env) &&
		    !F_ISSET(dbp, DB_AM_NOT_DURABLE)) {
			ret = __db_rdonly(env, "DB_SEQUENCE->open");
			goto err;
		}
		rp->seq_version = DB_SEQUENCE_VERSION;
		if (!F_ISSET(env, ENV_LITTLEENDIAN)) {
			if (IS_DB_AUTO_COMMIT(dbp, txn)) {
				if ((ret =
				    __txn_begin(env, ip, NULL, &txn, 0)) != 0)
					goto err;
				txn_local = 1;
				goto retry;
			}
			memcpy(&seq->seq_record, rp, sizeof(seq->seq_record));
			SEQ_SWAP_OUT(env, seq);
		}
		if ((ret = __db_put(dbp,
		     ip, txn, &seq->seq_key, &seq->seq_data, 0)) != 0)
			goto err;
	}
	rp = seq->seq_rp;

	SEQ_SWAP_IN(env, seq);

	if (rp->seq_version != DB_SEQUENCE_VERSION) {
		/*
		 * The database may have moved from one type
		 * of machine to another, check here.
		 * If we moved from little-end to big-end then
		 * the swap above will make the version correct.
		 * If the move was from big to little
		 * then we need to swap to see if this
		 * is an old version.
		 */
		if (rp->seq_version == DB_SEQUENCE_OLDVER)
			goto oldver;
		M_32_SWAP(rp->seq_version);
		if (rp->seq_version == DB_SEQUENCE_OLDVER) {
			SEQ_SWAP(rp);
			goto oldver;
		}
		M_32_SWAP(rp->seq_version);
		__db_errx(env, DB_STR_A("4006",
		    "Unsupported sequence version: %d", "%d"),
		    rp->seq_version);
		goto err;
	}

	seq->seq_last_value = seq->seq_prev_value = rp->seq_value;
	if (F_ISSET(rp, DB_SEQ_INC))
		seq->seq_last_value--;
	else
		seq->seq_last_value++;

	/*
	 * It's an error to specify a cache larger than the range of sequences.
	 */
	if (seq->seq_cache_size != 0 && (ret = __seq_chk_cachesize(
	    env, seq->seq_cache_size, rp->seq_max, rp->seq_min)) != 0)
		goto err;

err:	if (txn_local &&
	    (t_ret = __db_txn_auto_resolve(env, txn, 0, ret)) && ret == 0)
		ret = t_ret;
	if (ret != 0) {
		__os_free(env, seq->seq_key.data);
		seq->seq_key.data = NULL;
	}
	/* Release replication block. */
	if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
		ret = t_ret;

	ENV_LEAVE(env, ip);
	__dbt_userfree(env, keyp, NULL, NULL);
	return (ret);
}

/*
 * __seq_get_cachesize --
 *	Accessor for value passed into DB_SEQUENCE->set_cachesize call.
 *
 */
static int
__seq_get_cachesize(seq, cachesize)
	DB_SEQUENCE *seq;
	int32_t *cachesize;
{
	SEQ_ILLEGAL_BEFORE_OPEN(seq, "DB_SEQUENCE->get_cachesize");

	*cachesize = seq->seq_cache_size;
	return (0);
}

/*
 * __seq_set_cachesize --
 *	DB_SEQUENCE->set_cachesize.
 *
 */
static int
__seq_set_cachesize(seq, cachesize)
	DB_SEQUENCE *seq;
	int32_t cachesize;
{
	ENV *env;
	int ret;

	env = seq->seq_dbp->env;

	if (cachesize < 0) {
		__db_errx(env, DB_STR("4007",
		    "Cache size must be >= 0"));
		return (EINVAL);
	}

	/*
	 * It's an error to specify a cache larger than the range of sequences.
	 */
	if (SEQ_IS_OPEN(seq) && (ret = __seq_chk_cachesize(env,
	    cachesize, seq->seq_rp->seq_max, seq->seq_rp->seq_min)) != 0)
		return (ret);

	seq->seq_cache_size = cachesize;
	return (0);
}

#define	SEQ_SET_FLAGS	(DB_SEQ_WRAP | DB_SEQ_INC | DB_SEQ_DEC)
/*
 * __seq_get_flags --
 *	Accessor for flags passed into DB_SEQUENCE->open call
 *
 */
static int
__seq_get_flags(seq, flagsp)
	DB_SEQUENCE *seq;
	u_int32_t *flagsp;
{
	SEQ_ILLEGAL_BEFORE_OPEN(seq, "DB_SEQUENCE->get_flags");

	*flagsp = F_ISSET(seq->seq_rp, SEQ_SET_FLAGS);
	return (0);
}

/*
 * __seq_set_flags --
 *	DB_SEQUENCE->set_flags.
 *
 */
static int
__seq_set_flags(seq, flags)
	DB_SEQUENCE *seq;
	u_int32_t flags;
{
	DB_SEQ_RECORD *rp;
	ENV *env;
	int ret;

	env = seq->seq_dbp->env;
	rp = seq->seq_rp;

	SEQ_ILLEGAL_AFTER_OPEN(seq, "DB_SEQUENCE->set_flags");

	if ((ret = __db_fchk(
	    env, "DB_SEQUENCE->set_flags", flags, SEQ_SET_FLAGS)) != 0)
		return (ret);
	if ((ret = __db_fcchk(env,
	     "DB_SEQUENCE->set_flags", flags, DB_SEQ_DEC, DB_SEQ_INC)) != 0)
		return (ret);

	if (LF_ISSET(DB_SEQ_DEC | DB_SEQ_INC))
		F_CLR(rp, DB_SEQ_DEC | DB_SEQ_INC);
	F_SET(rp, flags);

	return (0);
}

/*
 * __seq_initial_value --
 *	DB_SEQUENCE->initial_value.
 *
 */
static int
__seq_initial_value(seq, value)
	DB_SEQUENCE *seq;
	db_seq_t value;
{
	DB_SEQ_RECORD *rp;
	ENV *env;

	env = seq->seq_dbp->env;
	SEQ_ILLEGAL_AFTER_OPEN(seq, "DB_SEQUENCE->initial_value");

	rp = seq->seq_rp;
	if (F_ISSET(rp, DB_SEQ_RANGE_SET) &&
	     (value > rp->seq_max || value < rp->seq_min)) {
		__db_errx(env, DB_STR("4008",
		    "Sequence value out of range"));
		return (EINVAL);
	}

	rp->seq_value = value;

	return (0);
}

/*
 * __seq_get_range --
 *	Accessor for range passed into DB_SEQUENCE->set_range call
 *
 */
static int
__seq_get_range(seq, minp, maxp)
	DB_SEQUENCE *seq;
	db_seq_t *minp, *maxp;
{
	SEQ_ILLEGAL_BEFORE_OPEN(seq, "DB_SEQUENCE->get_range");

	*minp = seq->seq_rp->seq_min;
	*maxp = seq->seq_rp->seq_max;
	return (0);
}

/*
 * __seq_set_range --
 *	SEQUENCE->set_range.
 *
 */
static int
__seq_set_range(seq, min, max)
	DB_SEQUENCE *seq;
	db_seq_t min, max;
{
	DB_SEQ_RECORD *rp;
	ENV *env;

	env = seq->seq_dbp->env;
	SEQ_ILLEGAL_AFTER_OPEN(seq, "DB_SEQUENCE->set_range");

	rp = seq->seq_rp;
	if (min >= max) {
		__db_errx(env, DB_STR("4009",
    "Minimum sequence value must be less than maximum sequence value"));
		return (EINVAL);
	}

	rp->seq_min = min;
	rp->seq_max = max;
	F_SET(rp, DB_SEQ_RANGE_SET);

	return (0);
}

static int
__seq_update(seq, ip, txn, delta, flags)
	DB_SEQUENCE *seq;
	DB_THREAD_INFO *ip;
	DB_TXN *txn;
	int32_t delta;
	u_int32_t flags;
{
	DB *dbp;
	DBT *data, ldata;
	DB_SEQ_RECORD *rp;
	ENV *env;
	int32_t adjust;
	int ret, txn_local, need_mutex;

	dbp = seq->seq_dbp;
	env = dbp->env;
	need_mutex = 0;
	data = &seq->seq_data;

	/*
	 * Create a local transaction as necessary, check for consistent
	 * transaction usage, and, if we have no transaction but do have
	 * locking on, acquire a locker id for the handle lock acquisition.
	 */
	if (IS_DB_AUTO_COMMIT(dbp, txn)) {
		if ((ret = __txn_begin(env, ip, NULL, &txn, flags)) != 0)
			return (ret);
		txn_local = 1;
	} else
		txn_local = 0;

	/* Check for consistent transaction usage. */
	if ((ret = __db_check_txn(dbp, txn, DB_LOCK_INVALIDID, 0)) != 0)
		goto err;
	/*
	 * If we are in a global transaction avoid deadlocking on the mutex.
	 * The write lock on the data will prevent two updaters getting in
	 * at once.  Fetch the data then see if things are what we thought
	 * they were.
	 */
	if (txn_local == 0 && txn != NULL) {
		MUTEX_UNLOCK(env, seq->mtx_seq);
		need_mutex = 1;
		data = &ldata;
		data->data = NULL;
		data->flags = DB_DBT_REALLOC;
	}

retry:	if ((ret = __db_get(dbp, ip,
	    txn, &seq->seq_key, data, DB_RMW)) != 0) {
		if (ret == DB_BUFFER_SMALL &&
		    seq->seq_data.size > sizeof(seq->seq_record)) {
			data->flags = DB_DBT_REALLOC;
			data->data = NULL;
			goto retry;
		}
		goto err;
	}

	if (data->size < sizeof(seq->seq_record)) {
		__db_errx(env, DB_STR("4010",
		    "Bad sequence record format"));
		ret = EINVAL;
		goto err;
	}

	/* We have an exclusive lock on the data, see if we raced. */
	if (need_mutex) {
		MUTEX_LOCK(env, seq->mtx_seq);
		need_mutex = 0;
		rp = seq->seq_rp;
		/*
		 * Note that caching must be off if we have global
		 * transaction so the value we fetch from the database
		 * is the correct current value.
		 */
		if (data->size <= seq->seq_data.size) {
			memcpy(seq->seq_data.data, data->data, data->size);
			__os_ufree(env, data->data);
		} else {
			seq->seq_data.data = data->data;
			seq->seq_data.size = data->size;
		}
	}
	if (F_ISSET(env, ENV_LITTLEENDIAN))
		seq->seq_rp = seq->seq_data.data;
	SEQ_SWAP_IN(env, seq);
	rp = seq->seq_rp;

	if (F_ISSET(rp, DB_SEQ_WRAPPED))
		goto overflow;

	adjust = delta > seq->seq_cache_size ? delta : seq->seq_cache_size;

	/*
	 * Check whether this operation will cause the sequence to wrap.
	 *
	 * The sequence minimum and maximum values can be INT64_MIN and
	 * INT64_MAX, so we need to do the test carefully to cope with
	 * arithmetic overflow.  The first part of the test below checks
	 * whether we will hit the end of the 64-bit range.  The second part
	 * checks whether we hit the end of the sequence.
	 */
again:	if (F_ISSET(rp, DB_SEQ_INC)) {
		if (rp->seq_value + adjust - 1 < rp->seq_value ||
		     rp->seq_value + adjust - 1 > rp->seq_max) {
			/* Don't wrap just to fill the cache. */
			if (adjust > delta) {
				adjust = delta;
				goto again;
			}
			if (F_ISSET(rp, DB_SEQ_WRAP))
				rp->seq_value = rp->seq_min;
			else {
overflow:			__db_errx(env, DB_STR("4011",
				    "Sequence overflow"));
				ret = EINVAL;
				goto err;
			}
		}
		/* See if we are at the end of the 64 bit range. */
		if (!F_ISSET(rp, DB_SEQ_WRAP) &&
		    rp->seq_value + adjust < rp->seq_value)
			F_SET(rp, DB_SEQ_WRAPPED);
	} else {
		if ((rp->seq_value - adjust) + 1 > rp->seq_value ||
		   (rp->seq_value - adjust) + 1 < rp->seq_min) {
			/* Don't wrap just to fill the cache. */
			if (adjust > delta) {
				adjust = delta;
				goto again;
			}
			if (F_ISSET(rp, DB_SEQ_WRAP))
				rp->seq_value = rp->seq_max;
			else
				goto overflow;
		}
		/* See if we are at the end of the 64 bit range. */
		if (!F_ISSET(rp, DB_SEQ_WRAP) &&
		    rp->seq_value - adjust > rp->seq_value)
			F_SET(rp, DB_SEQ_WRAPPED);
		adjust = -adjust;
	}

	rp->seq_value += adjust;
	SEQ_SWAP_OUT(env, seq);
	ret = __db_put(dbp, ip, txn, &seq->seq_key, &seq->seq_data, 0);
	rp->seq_value -= adjust;
	if (ret != 0) {
		__db_errx(env, DB_STR("4012",
		    "Sequence update failed"));
		goto err;
	}
	seq->seq_last_value = rp->seq_value + adjust;
	if (F_ISSET(rp, DB_SEQ_INC))
		seq->seq_last_value--;
	else
		seq->seq_last_value++;

err:	if (need_mutex) {
		if (data->data != NULL)
			__os_ufree(env, data->data);
		MUTEX_LOCK(env, seq->mtx_seq);
	}
	return (txn_local ? __db_txn_auto_resolve(
	    env, txn, LF_ISSET(DB_TXN_NOSYNC), ret) : ret);
}

static int
__seq_get(seq, txn, delta, retp, flags)
	DB_SEQUENCE *seq;
	DB_TXN *txn;
	int32_t delta;
	db_seq_t *retp;
	u_int32_t flags;
{
	DB *dbp;
	DB_SEQ_RECORD *rp;
	DB_THREAD_INFO *ip;
	ENV *env;
	int handle_check, ret, t_ret;

	dbp = seq->seq_dbp;
	env = dbp->env;
	rp = seq->seq_rp;
	ret = 0;

	STRIP_AUTO_COMMIT(flags);
	SEQ_ILLEGAL_BEFORE_OPEN(seq, "DB_SEQUENCE->get");

	if (delta < 0 || (delta == 0 && !LF_ISSET(DB_CURRENT))) {
		__db_errx(env, "Sequence delta must be greater than 0");
		return (EINVAL);
	}

	if (seq->seq_cache_size != 0 && txn != NULL) {
		__db_errx(env,
	    "Sequence with non-zero cache may not specify transaction handle");
		return (EINVAL);
	}

	ENV_ENTER(env, ip);

	/* Check for replication block. */
	handle_check = IS_ENV_REPLICATED(env);
	if (handle_check &&
	    (ret = __db_rep_enter(dbp, 1, 0, IS_REAL_TXN(txn))) != 0)
		return (ret);

	MUTEX_LOCK(env, seq->mtx_seq);

	if (handle_check && IS_REP_CLIENT(env) &&
	    !F_ISSET(dbp, DB_AM_NOT_DURABLE)) {
		ret = __db_rdonly(env, "DB_SEQUENCE->get");
		goto err;
	}

	if (rp->seq_min + delta > rp->seq_max) {
		__db_errx(env, DB_STR("4013", "Sequence overflow"));
		ret = EINVAL;
		goto err;
	}

	if (LF_ISSET(DB_CURRENT)) {
		*retp = seq->seq_prev_value;
	} else if (F_ISSET(rp, DB_SEQ_INC)) {
		if (seq->seq_last_value + 1 - rp->seq_value < delta &&
		    (ret = __seq_update(seq, ip, txn, delta, flags)) != 0)
			goto err;

		rp = seq->seq_rp;
		*retp = rp->seq_value;
		seq->seq_prev_value = rp->seq_value;
		rp->seq_value += delta;
	} else {
		if ((rp->seq_value - seq->seq_last_value) + 1 < delta &&
		    (ret = __seq_update(seq, ip, txn, delta, flags)) != 0)
			goto err;

		rp = seq->seq_rp;
		*retp = rp->seq_value;
		seq->seq_prev_value = rp->seq_value;
		rp->seq_value -= delta;
	}

err:	MUTEX_UNLOCK(env, seq->mtx_seq);

	/* Release replication block. */
	if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
		ret = t_ret;

	ENV_LEAVE(env, ip);
	return (ret);
}

/*
 * __seq_get_db --
 *	Accessor for dbp passed into db_sequence_create call
 *
 */
static int
__seq_get_db(seq, dbpp)
	DB_SEQUENCE *seq;
	DB **dbpp;
{
	*dbpp = seq->seq_dbp;
	return (0);
}

/*
 * __seq_get_key --
 *	Accessor for key passed into DB_SEQUENCE->open call
 *
 */
static int
__seq_get_key(seq, key)
	DB_SEQUENCE *seq;
	DBT *key;
{
	SEQ_ILLEGAL_BEFORE_OPEN(seq, "DB_SEQUENCE->get_key");

	if (F_ISSET(key, DB_DBT_USERCOPY))
		return (__db_retcopy(seq->seq_dbp->env, key,
		    seq->seq_key.data, seq->seq_key.size, NULL, 0));

	key->data = seq->seq_key.data;
	key->size = key->ulen = seq->seq_key.size;
	key->flags = seq->seq_key.flags;
	return (0);
}

/*
 * __seq_close_pp --
 *	Close a sequence pre/post processing
 *
 */
static int
__seq_close_pp(seq, flags)
	DB_SEQUENCE *seq;
	u_int32_t flags;
{
	DB_THREAD_INFO *ip;
	int ret;

	ENV_ENTER(seq->seq_dbp->env, ip);
	ret = __seq_close(seq, flags);
	ENV_LEAVE(seq->seq_dbp->env, ip);

	return (ret);
}

/*
 * __seq_close --
 *	Close a sequence
 *
 */
static int
__seq_close(seq, flags)
	DB_SEQUENCE *seq;
	u_int32_t flags;
{
	ENV *env;
	int ret, t_ret;

	ret = 0;
	env = seq->seq_dbp->env;

	if (flags != 0)
		ret = __db_ferr(env, "DB_SEQUENCE->close", 0);

	if ((t_ret = __mutex_free(env, &seq->mtx_seq)) != 0 && ret == 0)
		ret = t_ret;

	if (seq->seq_key.data != NULL)
		__os_free(env, seq->seq_key.data);
	if (seq->seq_data.data != NULL &&
	    seq->seq_data.data != &seq->seq_record)
		__os_ufree(env, seq->seq_data.data);
	seq->seq_key.data = NULL;

	memset(seq, CLEAR_BYTE, sizeof(*seq));
	__os_free(env, seq);

	return (ret);
}

/*
 * __seq_remove --
 *	Remove a sequence from the database.
 */
static int
__seq_remove(seq, txn, flags)
	DB_SEQUENCE *seq;
	DB_TXN *txn;
	u_int32_t flags;
{
	DB *dbp;
	DB_THREAD_INFO *ip;
	ENV *env;
	int handle_check, ret, t_ret, txn_local;

	dbp = seq->seq_dbp;
	env = dbp->env;
	txn_local = 0;

	SEQ_ILLEGAL_BEFORE_OPEN(seq, "DB_SEQUENCE->remove");

	/*
	 * Flags can only be 0, unless the database has DB_AUTO_COMMIT enabled.
	 * Then DB_TXN_NOSYNC is allowed.
	 */
	if (flags != 0 &&
	    (flags != DB_TXN_NOSYNC || !IS_DB_AUTO_COMMIT(dbp, txn)))
		return (__db_ferr(env, "DB_SEQUENCE->remove illegal flag", 0));

	ENV_ENTER(env, ip);

	/* Check for replication block. */
	handle_check = IS_ENV_REPLICATED(env);
	if (handle_check &&
	    (ret = __db_rep_enter(dbp, 1, 0, IS_REAL_TXN(txn))) != 0) {
		handle_check = 0;
		goto err;
	}

	/*
	 * Create a local transaction as necessary, check for consistent
	 * transaction usage, and, if we have no transaction but do have
	 * locking on, acquire a locker id for the handle lock acquisition.
	 */
	if (IS_DB_AUTO_COMMIT(dbp, txn)) {
		if ((ret = __txn_begin(env, ip, NULL, &txn, flags)) != 0)
			return (ret);
		txn_local = 1;
	}

	/* Check for consistent transaction usage. */
	if ((ret = __db_check_txn(dbp, txn, DB_LOCK_INVALIDID, 0)) != 0)
		goto err;

	ret = __db_del(dbp, ip, txn, &seq->seq_key, 0);

	if ((t_ret = __seq_close(seq, 0)) != 0 && ret == 0)
		ret = t_ret;

	/* Release replication block. */
	if (handle_check && (t_ret = __env_db_rep_exit(env)) != 0 && ret == 0)
		ret = t_ret;
err:	if (txn_local && (t_ret =
	    __db_txn_auto_resolve(env, txn, 0, ret)) != 0 && ret == 0)
		ret = t_ret;

	ENV_LEAVE(env, ip);
	return (ret);
}

/*
 * __seq_chk_cachesize --
 *	Validate the cache size vs. the range.
 */
static int
__seq_chk_cachesize(env, cachesize, max, min)
	ENV *env;
	int32_t cachesize;
	db_seq_t max, min;
{
	/*
	 * It's an error to specify caches larger than the sequence range.
	 *
	 * The min and max of the range can be either positive or negative,
	 * the difference will fit in an unsigned variable of the same type.
	 * Assume a 2's complement machine, and simply subtract.
	 */
	if ((u_int32_t)cachesize > (u_int64_t)max - (u_int64_t)min) {
		__db_errx(env, DB_STR("4014",
    "Number of items to be cached is larger than the sequence range"));
		return (EINVAL);
	}
	return (0);
}

#else /* !HAVE_64BIT_TYPES */

int
db_sequence_create(seqp, dbp, flags)
	DB_SEQUENCE **seqp;
	DB *dbp;
	u_int32_t flags;
{
	COMPQUIET(seqp, NULL);
	COMPQUIET(flags, 0);
	__db_errx(dbp->env, DB_STR("4015",
	    "library build did not include support for sequences"));
	return (DB_OPNOTSUP);
}
#endif /* HAVE_64BIT_TYPES */