summaryrefslogtreecommitdiff
path: root/src/mongo/db/storage/wiredtiger/wiredtiger_record_store.cpp
blob: 41f646f8c6e04c1a3063d25bab0510963ba19186 (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
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
// wiredtiger_record_store.cpp

/**
 *    Copyright (C) 2014 MongoDB Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *
 *    This program 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 Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the GNU Affero General Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kStorage

#include "mongo/platform/basic.h"

#include "mongo/db/storage/wiredtiger/wiredtiger_record_store.h"

#include <boost/scoped_ptr.hpp>
#include <boost/shared_array.hpp>
#include <wiredtiger.h>

#include "mongo/base/checked_cast.h"
#include "mongo/db/concurrency/write_conflict_exception.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/operation_context.h"
#include "mongo/db/storage/oplog_hack.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_global_options.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_kv_engine.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_session_cache.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_size_storer.h"
#include "mongo/db/storage/wiredtiger/wiredtiger_util.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/fail_point.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"
#include "mongo/util/scopeguard.h"

//#define RS_ITERATOR_TRACE(x) log() << "WTRS::Iterator " << x
#define RS_ITERATOR_TRACE(x)

namespace mongo {

    using boost::scoped_ptr;
    using std::string;

namespace {

    static const int kMinimumRecordStoreVersion = 1;
    static const int kCurrentRecordStoreVersion = 1; // New record stores use this by default.
    static const int kMaximumRecordStoreVersion = 1;
    BOOST_STATIC_ASSERT(kCurrentRecordStoreVersion >= kMinimumRecordStoreVersion);
    BOOST_STATIC_ASSERT(kCurrentRecordStoreVersion <= kMaximumRecordStoreVersion);

    bool shouldUseOplogHack(OperationContext* opCtx, const std::string& uri) {
        StatusWith<BSONObj> appMetadata = WiredTigerUtil::getApplicationMetadata(opCtx, uri);
        if (!appMetadata.isOK()) {
            return false;
        }

        return (appMetadata.getValue().getIntField("oplogKeyExtractionVersion") == 1);
    }

} // namespace

    MONGO_FP_DECLARE(WTWriteConflictException);

    const std::string kWiredTigerEngineName = "wiredTiger";

    const long long WiredTigerRecordStore::kCollectionScanOnCreationThreshold = 10000;

    StatusWith<std::string> WiredTigerRecordStore::parseOptionsField(const BSONObj options) {
        StringBuilder ss;
        BSONForEach(elem, options) {
            if (elem.fieldNameStringData() == "configString") {
                if (elem.type() != String) {
                    return StatusWith<std::string>(ErrorCodes::TypeMismatch, str::stream()
                                                   << "storageEngine.wiredTiger.configString "
                                                   << "must be a string. "
                                                   << "Not adding 'configString' value "
                                                   << elem << " to collection configuration");
                }
                ss << elem.valueStringData() << ',';
            }
            else {
                // Return error on first unrecognized field.
                return StatusWith<std::string>(ErrorCodes::InvalidOptions, str::stream()
                                               << '\'' << elem.fieldNameStringData() << '\''
                                               << " is not a supported option in "
                                               << "storageEngine.wiredTiger");
            }
        }
        return StatusWith<std::string>(ss.str());
    }

    // static
    StatusWith<std::string> WiredTigerRecordStore::generateCreateString(
        const StringData& ns,
        const CollectionOptions& options,
        const StringData& extraStrings) {

        // Separate out a prefix and suffix in the default string. User configuration will
        // override values in the prefix, but not values in the suffix.
        str::stream ss;
        ss << "type=file,";
        // Setting this larger than 10m can hurt latencies and throughput degradation if this
        // is the oplog.  See SERVER-16247
        ss << "memory_page_max=10m,";
        // Choose a higher split percent, since most usage is append only. Allow some space
        // for workloads where updates increase the size of documents.
        ss << "split_pct=90,";
        ss << "leaf_value_max=64MB,";
        ss << "checksum=on,";
        if (wiredTigerGlobalOptions.useCollectionPrefixCompression) {
            ss << "prefix_compression,";
        }

        ss << "block_compressor=" << wiredTigerGlobalOptions.collectionBlockCompressor << ",";

        ss << extraStrings << ",";

        StatusWith<std::string> customOptions =
            parseOptionsField(options.storageEngine.getObjectField(kWiredTigerEngineName));
        if (!customOptions.isOK())
            return customOptions;

        ss << customOptions.getValue();

        if ( NamespaceString::oplog(ns) ) {
            // force file for oplog
            ss << "type=file,";
            // Tune down to 10m.  See SERVER-16247
            ss << "memory_page_max=10m,";
        }

        // WARNING: No user-specified config can appear below this line. These options are required
        // for correct behavior of the server.

        ss << "key_format=q,value_format=u";

        // Record store metadata
        ss << ",app_metadata=(formatVersion=" << kCurrentRecordStoreVersion;
        if (NamespaceString::oplog(ns)) {
            ss << ",oplogKeyExtractionVersion=1";
        }
        ss << ")";

        return StatusWith<std::string>(ss);
    }

    WiredTigerRecordStore::WiredTigerRecordStore(OperationContext* ctx,
                                                 const StringData& ns,
                                                 const StringData& uri,
                                                 bool isCapped,
                                                 int64_t cappedMaxSize,
                                                 int64_t cappedMaxDocs,
                                                 CappedDocumentDeleteCallback* cappedDeleteCallback,
                                                 WiredTigerSizeStorer* sizeStorer)
    : RecordStore( ns ),
              _uri( uri.toString() ),
              _instanceId( WiredTigerSession::genCursorId() ),
              _isCapped( isCapped ),
              _isOplog( NamespaceString::oplog( ns ) ),
              _cappedMaxSize( cappedMaxSize ),
              _cappedMaxSizeSlack( std::min(cappedMaxSize/10, int64_t(16*1024*1024)) ),
              _cappedMaxDocs( cappedMaxDocs ),
              _cappedFirstRecord(),
              _cappedDeleteCallback( cappedDeleteCallback ),
              _cappedDeleteCheckCount(0),
              _useOplogHack(shouldUseOplogHack(ctx, _uri)),
              _sizeStorer( sizeStorer ),
              _sizeStorerCounter(0),
              _shuttingDown(false)
    {
        Status versionStatus = WiredTigerUtil::checkApplicationMetadataFormatVersion(
            ctx, uri, kMinimumRecordStoreVersion, kMaximumRecordStoreVersion);
        if (!versionStatus.isOK()) {
            fassertFailedWithStatusNoTrace(28548, versionStatus);
        }

        if (_isCapped) {
            invariant(_cappedMaxSize > 0);
            invariant(_cappedMaxDocs == -1 || _cappedMaxDocs > 0);
        }
        else {
            invariant(_cappedMaxSize == -1);
            invariant(_cappedMaxDocs == -1);
        }

        // Find the largest RecordId currently in use and estimate the number of records.
        scoped_ptr<RecordIterator> iterator( getIterator( ctx, RecordId(),
                                                          CollectionScanParams::BACKWARD ) );
        if ( iterator->isEOF() ) {
            _dataSize.store(0);
            _numRecords.store(0);
            // Need to start at 1 so we are always higher than RecordId::min()
            _nextIdNum.store( 1 );
            if ( sizeStorer )
                _sizeStorer->onCreate( this, 0, 0 );
        }
        else {
            RecordId maxLoc = iterator->curr();
            int64_t max = _makeKey( maxLoc );
            _oplog_highestSeen = maxLoc;
            _nextIdNum.store( 1 + max );

            if ( _sizeStorer ) {
                long long numRecords;
                long long dataSize;
                _sizeStorer->loadFromCache( uri, &numRecords, &dataSize );
                _numRecords.store( numRecords );
                _dataSize.store( dataSize );
                _sizeStorer->onCreate( this, numRecords, dataSize );
            }

            if (_sizeStorer == NULL || _numRecords.load() < kCollectionScanOnCreationThreshold) {
                LOG(1) << "doing scan of collection " << ns << " to get info";

                _numRecords.store(0);
                _dataSize.store(0);

                while( !iterator->isEOF() ) {
                    RecordId loc = iterator->getNext();
                    RecordData data = iterator->dataFor( loc );
                    _numRecords.fetchAndAdd(1);
                    _dataSize.fetchAndAdd(data.size());
                }

                if ( _sizeStorer ) {
                    _sizeStorer->storeToCache( _uri, _numRecords.load(), _dataSize.load() );
                }
            }

        }

        _hasBackgroundThread = WiredTigerKVEngine::initRsOplogBackgroundThread(ns);
    }

    WiredTigerRecordStore::~WiredTigerRecordStore() {
        {
            boost::timed_mutex::scoped_lock lk(_cappedDeleterMutex);
            _shuttingDown = true;
        }

        LOG(1) << "~WiredTigerRecordStore for: " << ns();
        if ( _sizeStorer ) {
            _sizeStorer->onDestroy( this );
        }
    }

    const char* WiredTigerRecordStore::name() const {
        return kWiredTigerEngineName.c_str();
    }

    bool WiredTigerRecordStore::inShutdown() const {
        boost::timed_mutex::scoped_lock lk(_cappedDeleterMutex);
        return _shuttingDown;
    }

    long long WiredTigerRecordStore::dataSize( OperationContext *txn ) const {
        return _dataSize.load();
    }

    long long WiredTigerRecordStore::numRecords( OperationContext *txn ) const {
        return _numRecords.load();
    }

    bool WiredTigerRecordStore::isCapped() const {
        return _isCapped;
    }

    int64_t WiredTigerRecordStore::cappedMaxDocs() const {
        invariant(_isCapped);
        return _cappedMaxDocs;
    }

    int64_t WiredTigerRecordStore::cappedMaxSize() const {
        invariant(_isCapped);
        return _cappedMaxSize;
    }

    int64_t WiredTigerRecordStore::storageSize( OperationContext* txn,
                                                BSONObjBuilder* extraInfo,
                                                int infoLevel ) const {
        WiredTigerSession* session = WiredTigerRecoveryUnit::get(txn)->getSession(txn);
        StatusWith<int64_t> result = WiredTigerUtil::getStatisticsValueAs<int64_t>(
            session->getSession(),
            "statistics:" + getURI(), "statistics=(size)", WT_STAT_DSRC_BLOCK_SIZE);
        uassertStatusOK(result.getStatus());

        int64_t size = result.getValue();

        if ( size == 0 && _isCapped ) {
            // Many things assume an empty capped collection still takes up space.
            return 1;
        }
        return size;
    }

    // Retrieve the value from a positioned cursor.
    RecordData WiredTigerRecordStore::_getData(const WiredTigerCursor& cursor) const {
        WT_ITEM value;
        int ret = cursor->get_value(cursor.get(), &value);
        invariantWTOK(ret);

        SharedBuffer data = SharedBuffer::allocate(value.size);
        memcpy( data.get(), value.data, value.size );
        return RecordData(data.moveFrom(), value.size);
    }

    RecordData WiredTigerRecordStore::dataFor(OperationContext* txn, const RecordId& loc) const {
        // ownership passes to the shared_array created below
        WiredTigerCursor curwrap( _uri, _instanceId, true, txn);
        WT_CURSOR *c = curwrap.get();
        invariant( c );
        c->set_key(c, _makeKey(loc));
        int ret = WT_OP_CHECK(c->search(c));
        massert(28556, "Didn't find RecordId in WiredTigerRecordStore", ret != WT_NOTFOUND);
        invariantWTOK(ret);
        return _getData(curwrap);
    }

    bool WiredTigerRecordStore::findRecord( OperationContext* txn,
                                            const RecordId& loc, RecordData* out ) const {
        WiredTigerCursor curwrap( _uri, _instanceId, true, txn);
        WT_CURSOR *c = curwrap.get();
        invariant( c );
        c->set_key(c, _makeKey(loc));
        int ret = WT_OP_CHECK(c->search(c));
        if (ret == WT_NOTFOUND) {
            return false;
        }
        invariantWTOK(ret);
        *out = _getData(curwrap);
        return true;
    }

    void WiredTigerRecordStore::deleteRecord( OperationContext* txn, const RecordId& loc ) {
        WiredTigerCursor cursor( _uri, _instanceId, true, txn );
        cursor.assertInActiveTxn();
        WT_CURSOR *c = cursor.get();
        c->set_key(c, _makeKey(loc));
        int ret = WT_OP_CHECK(c->search(c));
        invariantWTOK(ret);

        WT_ITEM old_value;
        ret = c->get_value(c, &old_value);
        invariantWTOK(ret);

        int old_length = old_value.size;

        ret = WT_OP_CHECK(c->remove(c));
        invariantWTOK(ret);

        _changeNumRecords(txn, -1);
        _increaseDataSize(txn, -old_length);
    }

    bool WiredTigerRecordStore::cappedAndNeedDelete() const {
        if (!_isCapped)
            return false;

        if (_dataSize.load() >= _cappedMaxSize)
            return true;

        if ((_cappedMaxDocs != -1) && (_numRecords.load() > _cappedMaxDocs))
            return true;

        return false;
    }

    int64_t WiredTigerRecordStore::cappedDeleteAsNeeded(OperationContext* txn,
                                                        const RecordId& justInserted) {

        // We only want to do the checks occasionally as they are expensive.
        // This variable isn't thread safe, but has loose semantics anyway.
        dassert( !_isOplog || _cappedMaxDocs == -1 );

        if (!cappedAndNeedDelete())
            return 0;

        // ensure only one thread at a time can do deletes, otherwise they'll conflict.
        boost::timed_mutex::scoped_lock lock(_cappedDeleterMutex, boost::defer_lock);

        if (_cappedMaxDocs != -1) {
            lock.lock(); // Max docs has to be exact, so have to check every time.
        }
        else if(_hasBackgroundThread) {
            // We are foreground, and there is a background thread,

            // Check if we need some back pressure.
            if ((_dataSize.load() - _cappedMaxSize) < _cappedMaxSizeSlack) {
                return 0;
            }

            // Back pressure needed!
            // We're not actually going to delete anything, but we're going to syncronize
            // on the deleter thread.
            // Don't wait forever: we're in a transaction, we could block eviction.
            (void)lock.timed_lock(boost::posix_time::millisec(200));
            return 0;
        }
        else {
            if (!lock.try_lock()) {
                // Someone else is deleting old records. Apply back-pressure if too far behind,
                // otherwise continue.
                if ((_dataSize.load() - _cappedMaxSize) < _cappedMaxSizeSlack)
                    return 0;

                // Don't wait forever: we're in a transaction, we could block eviction.
                if (!lock.timed_lock(boost::posix_time::millisec(200)))
                    return 0;

                // If we already waited, let someone else do cleanup unless we are significantly
                // over the limit.
                if ((_dataSize.load() - _cappedMaxSize) < (2 * _cappedMaxSizeSlack))
                    return 0;
            }
        }

        return cappedDeleteAsNeeded_inlock(txn, justInserted);
    }

    int64_t WiredTigerRecordStore::cappedDeleteAsNeeded_inlock(OperationContext* txn,
                                                               const RecordId& justInserted) {
        // we do this is a side transaction in case it aborts
        WiredTigerRecoveryUnit* realRecoveryUnit =
            checked_cast<WiredTigerRecoveryUnit*>( txn->releaseRecoveryUnit() );
        invariant( realRecoveryUnit );
        WiredTigerSessionCache* sc = realRecoveryUnit->getSessionCache();
        txn->setRecoveryUnit( new WiredTigerRecoveryUnit( sc ) );

        WiredTigerRecoveryUnit::get(txn)->markNoTicketRequired(); // realRecoveryUnit already has
        WT_SESSION* session = WiredTigerRecoveryUnit::get(txn)->getSession(txn)->getSession();

        int64_t dataSize = _dataSize.load();
        int64_t numRecords = _numRecords.load();

        int64_t sizeOverCap = (dataSize > _cappedMaxSize) ? dataSize - _cappedMaxSize : 0;
        int64_t sizeSaved = 0;
        int64_t docsOverCap = 0, docsRemoved = 0;
        if (_cappedMaxDocs != -1 && numRecords > _cappedMaxDocs)
            docsOverCap = numRecords - _cappedMaxDocs;

        try {
            WriteUnitOfWork wuow(txn);

            WiredTigerCursor curwrap( _uri, _instanceId, true, txn);
            WT_CURSOR *c = curwrap.get();
            RecordId newestOld;
            int64_t firstKey = 0;
            int first = 0, ret = 0;
            if (_cappedFirstRecord != RecordId()) {
                c->set_key(c, _makeKey(_cappedFirstRecord));
                if ((ret = WT_OP_CHECK(c->search(c))) != WT_NOTFOUND) {
                    invariantWTOK(ret);
                    invariantWTOK(c->get_key(c, &firstKey));
                    first = 1;
                }
            }

            while ((sizeSaved < sizeOverCap || docsRemoved < docsOverCap) &&
                   (docsRemoved < 20000)) {

                if (first)
                    first = 0;
                else if ((ret = WT_OP_CHECK(c->next(c))) == WT_NOTFOUND)
                    break;
                invariantWTOK(ret);

                int64_t key;
                ret = c->get_key(c, &key);
                invariantWTOK(ret);

                // don't go past the record we just inserted
                newestOld = _fromKey(key);
                if ( newestOld >= justInserted ) // TODO: use oldest uncommitted instead
                    break;

                if ( _shuttingDown )
                    break;

                WT_ITEM old_value;
                invariantWTOK(c->get_value(c, &old_value));

                ++docsRemoved;
                sizeSaved += old_value.size;

                if ( _cappedDeleteCallback ) {
                    uassertStatusOK(
                        _cappedDeleteCallback->aboutToDeleteCapped(
                            txn,
                            newestOld,
                            RecordData(static_cast<const char*>(old_value.data), old_value.size)));
                }
            }

            if (docsRemoved > 0) {
                // if we scanned to the end of the collection or past our insert, go back one
                if (ret == WT_NOTFOUND || newestOld >= justInserted) {
                    ret = WT_OP_CHECK(c->prev(c));
                }
                invariantWTOK(ret);

                WiredTigerCursor startWrap( _uri, _instanceId, true, txn);
                WT_CURSOR* start = NULL;
                if (firstKey != 0) {
                    start = startWrap.get();
                    start->set_key(start, firstKey);
                }

                ret = session->truncate(session, NULL, start, c, NULL);
                if (ret == ENOENT || ret == WT_NOTFOUND) {
                    // TODO we should remove this case once SERVER-17141 is resolved
                    log() << "Soft failure truncating capped collection. Will try again later.";
                    docsRemoved = 0;
                }
                else {
                    invariantWTOK(ret);
                    _changeNumRecords(txn, -docsRemoved);
                    _increaseDataSize(txn, -sizeSaved);
                    wuow.commit();

                    // Update the starting key
                    _cappedFirstRecord = newestOld;
                }
            }
        }
        catch ( const WriteConflictException& wce ) {
            delete txn->releaseRecoveryUnit();
            txn->setRecoveryUnit( realRecoveryUnit );
            log() << "got conflict truncating capped, ignoring";
            return 0;
        }
        catch ( ... ) {
            delete txn->releaseRecoveryUnit();
            txn->setRecoveryUnit( realRecoveryUnit );
            throw;
        }

        delete txn->releaseRecoveryUnit();
        txn->setRecoveryUnit( realRecoveryUnit );
        return docsRemoved;
    }

    StatusWith<RecordId> WiredTigerRecordStore::extractAndCheckLocForOplog(const char* data,
                                                                           int len) {
        return oploghack::extractKey(data, len);
    }

    StatusWith<RecordId> WiredTigerRecordStore::insertRecord( OperationContext* txn,
                                                              const char* data,
                                                              int len,
                                                              bool enforceQuota ) {
        if ( _isCapped && len > _cappedMaxSize ) {
            return StatusWith<RecordId>( ErrorCodes::BadValue,
                                         "object to insert exceeds cappedMaxSize" );
        }

        RecordId loc;
        if ( _useOplogHack ) {
            StatusWith<RecordId> status = extractAndCheckLocForOplog(data, len);
            if (!status.isOK())
                return status;
            loc = status.getValue();
            if ( loc > _oplog_highestSeen ) {
                boost::mutex::scoped_lock lk( _uncommittedDiskLocsMutex );
                if ( loc > _oplog_highestSeen ) {
                    _oplog_highestSeen = loc;
                }
            }
        }
        else if ( _isCapped ) {
            boost::mutex::scoped_lock lk( _uncommittedDiskLocsMutex );
            loc = _nextId();
            _addUncommitedDiskLoc_inlock( txn, loc );
        }
        else {
            loc = _nextId();
        }

        WiredTigerCursor curwrap( _uri, _instanceId, true, txn);
        curwrap.assertInActiveTxn();
        WT_CURSOR *c = curwrap.get();
        invariant( c );

        c->set_key(c, _makeKey(loc));
        WiredTigerItem value(data, len);
        c->set_value(c, value.Get());
        int ret = WT_OP_CHECK(c->insert(c));
        if (ret) {
            return StatusWith<RecordId>(wtRCToStatus(ret, "WiredTigerRecordStore::insertRecord"));
        }

        _changeNumRecords( txn, 1 );
        _increaseDataSize( txn, len );

        cappedDeleteAsNeeded(txn, loc);

        return StatusWith<RecordId>( loc );
    }

    void WiredTigerRecordStore::dealtWithCappedLoc( const RecordId& loc ) {
        boost::mutex::scoped_lock lk( _uncommittedDiskLocsMutex );
        SortedDiskLocs::iterator it = std::find(_uncommittedDiskLocs.begin(),
                                                _uncommittedDiskLocs.end(),
                                                loc);
        invariant(it != _uncommittedDiskLocs.end());
        _uncommittedDiskLocs.erase(it);
    }

    bool WiredTigerRecordStore::isCappedHidden( const RecordId& loc ) const {
        boost::mutex::scoped_lock lk( _uncommittedDiskLocsMutex );
        if (_uncommittedDiskLocs.empty()) {
            return false;
        }
        return _uncommittedDiskLocs.front() <= loc;
    }

    StatusWith<RecordId> WiredTigerRecordStore::insertRecord( OperationContext* txn,
                                                              const DocWriter* doc,
                                                              bool enforceQuota ) {
        const int len = doc->documentSize();

        boost::shared_array<char> buf( new char[len] );
        doc->writeDocument( buf.get() );

        return insertRecord( txn, buf.get(), len, enforceQuota );
    }

    StatusWith<RecordId> WiredTigerRecordStore::updateRecord( OperationContext* txn,
                                                              const RecordId& loc,
                                                              const char* data,
                                                              int len,
                                                              bool enforceQuota,
                                                              UpdateNotifier* notifier ) {
        WiredTigerCursor curwrap( _uri, _instanceId, true, txn);
        curwrap.assertInActiveTxn();
        WT_CURSOR *c = curwrap.get();
        invariant( c );
        c->set_key(c, _makeKey(loc));
        int ret = WT_OP_CHECK(c->search(c));
        invariantWTOK(ret);

        WT_ITEM old_value;
        ret = c->get_value(c, &old_value);
        invariantWTOK(ret);

        int old_length = old_value.size;

        c->set_key(c, _makeKey(loc));
        WiredTigerItem value(data, len);
        c->set_value(c, value.Get());
        ret = WT_OP_CHECK(c->insert(c));
        invariantWTOK(ret);

        _increaseDataSize(txn, len - old_length);

        cappedDeleteAsNeeded(txn, loc);

        return StatusWith<RecordId>( loc );
    }

    bool WiredTigerRecordStore::updateWithDamagesSupported() const {
        return false;
    }

    Status WiredTigerRecordStore::updateWithDamages( OperationContext* txn,
                                                     const RecordId& loc,
                                                     const RecordData& oldRec,
                                                     const char* damageSource,
                                                     const mutablebson::DamageVector& damages ) {
        invariant(false);
    }

    void WiredTigerRecordStore::_oplogSetStartHack( WiredTigerRecoveryUnit* wru ) const {
        boost::mutex::scoped_lock lk( _uncommittedDiskLocsMutex );
        if ( _uncommittedDiskLocs.empty() ) {
            wru->setOplogReadTill( _oplog_highestSeen );
        }
        else {
            wru->setOplogReadTill( _uncommittedDiskLocs.front() );
        }
    }

    RecordIterator* WiredTigerRecordStore::getIterator(
        OperationContext* txn,
        const RecordId& start,
        const CollectionScanParams::Direction& dir) const {

        if ( _isOplog && dir == CollectionScanParams::FORWARD ) {
            WiredTigerRecoveryUnit* wru = WiredTigerRecoveryUnit::get(txn);
            if ( !wru->inActiveTxn() || wru->getOplogReadTill().isNull() ) {
                // if we don't have a session, we have no snapshot, so we can update our view
                _oplogSetStartHack( wru );
            }
        }

        return new Iterator(*this, txn, start, dir, false);
    }


    std::vector<RecordIterator*> WiredTigerRecordStore::getManyIterators(
        OperationContext* txn ) const {

        // XXX do we want this to actually return a set of iterators?

        std::vector<RecordIterator*> iterators;
        iterators.push_back( new Iterator(*this, txn, RecordId(),
                                          CollectionScanParams::FORWARD, true) );

        return iterators;
    }

    Status WiredTigerRecordStore::truncate( OperationContext* txn ) {
        WiredTigerCursor startWrap( _uri, _instanceId, true, txn);
        WT_CURSOR* start = startWrap.get();
        int ret = WT_OP_CHECK(start->next(start));
        //Empty collections don't have anything to truncate.
        if (ret == WT_NOTFOUND) {
            return Status::OK();
        }
        invariantWTOK(ret);

        WT_SESSION* session = WiredTigerRecoveryUnit::get(txn)->getSession(txn)->getSession();
        invariantWTOK(WT_OP_CHECK(session->truncate(session, NULL, start, NULL, NULL)));
        _changeNumRecords(txn, -numRecords(txn));
        _increaseDataSize(txn, -dataSize(txn));

        return Status::OK();
    }

    Status WiredTigerRecordStore::compact( OperationContext* txn,
                                           RecordStoreCompactAdaptor* adaptor,
                                           const CompactOptions* options,
                                           CompactStats* stats ) {
        WiredTigerSessionCache* cache = WiredTigerRecoveryUnit::get(txn)->getSessionCache();
        WiredTigerSession* session = cache->getSession();
        WT_SESSION *s = session->getSession();
        int ret = s->compact(s, getURI().c_str(), "timeout=0");
        invariantWTOK(ret);
        cache->releaseSession(session);
        return Status::OK();
    }

    Status WiredTigerRecordStore::validate( OperationContext* txn,
                                            bool full,
                                            bool scanData,
                                            ValidateAdaptor* adaptor,
                                            ValidateResults* results,
                                            BSONObjBuilder* output ) {

        {
            int err = WiredTigerUtil::verifyTable(txn, _uri, &results->errors);
            if (err == EBUSY) {
                const char* msg = "verify() returned EBUSY. Not treating as invalid.";
                warning() << msg;
                results->errors.push_back(msg);
            }
            else if (err) {
                std::string msg = str::stream()
                    << "verify() returned " << wiredtiger_strerror(err) << ". "
                    << "This indicates structural damage. "
                    << "Not examining individual documents.";
                error() << msg;
                results->errors.push_back(msg);
                results->valid = false;
                return Status::OK();
            }
        }

        long long nrecords = 0;
        long long dataSizeTotal = 0;
        boost::scoped_ptr<RecordIterator> iter( getIterator( txn ) );
        results->valid = true;
        while( !iter->isEOF() ) {
            ++nrecords;
            if ( full && scanData ) {
                size_t dataSize;
                RecordId loc = iter->curr();
                RecordData data = dataFor( txn, loc );
                Status status = adaptor->validate( data, &dataSize );
                if ( !status.isOK() ) {
                    results->valid = false;
                    results->errors.push_back( str::stream() << loc << " is corrupted" );
                }
                dataSizeTotal += static_cast<long long>(dataSize);
            }
            iter->getNext();
        }

        if (_sizeStorer && full && scanData && results->valid) {
            if (nrecords != _numRecords.load() || dataSizeTotal != _dataSize.load()) {
                warning() << _uri << ": Existing record and data size counters ("
                          << _numRecords.load() << " records " << _dataSize.load() << " bytes) "
                          << "are inconsistent with full validation results ("
                          << nrecords << " records " << dataSizeTotal << " bytes). "
                          << "Updating counters with new values.";
            }

            _numRecords.store(nrecords);
            _dataSize.store(dataSizeTotal);

            long long oldNumRecords;
            long long oldDataSize;
            _sizeStorer->loadFromCache(_uri, &oldNumRecords, &oldDataSize);
            if (nrecords != oldNumRecords || dataSizeTotal != oldDataSize) {
                warning() << _uri << ": Existing data in size storer ("
                          << oldNumRecords << " records " << oldDataSize << " bytes) "
                          << "is inconsistent with full validation results ("
                          << _numRecords.load() << " records " << _dataSize.load() << " bytes). "
                          << "Updating size storer with new values.";
            }

            _sizeStorer->storeToCache(_uri, _numRecords.load(), _dataSize.load());
        }

        output->appendNumber( "nrecords", nrecords );
        return Status::OK();
    }

    void WiredTigerRecordStore::appendCustomStats( OperationContext* txn,
                                                   BSONObjBuilder* result,
                                                   double scale ) const {
        result->appendBool( "capped", _isCapped );
        if ( _isCapped ) {
            result->appendIntOrLL( "max", _cappedMaxDocs );
            result->appendIntOrLL( "maxSize", static_cast<long long>(_cappedMaxSize / scale) );
        }
        WiredTigerSession* session = WiredTigerRecoveryUnit::get(txn)->getSession(txn);
        WT_SESSION* s = session->getSession();
        BSONObjBuilder bob(result->subobjStart(kWiredTigerEngineName));
        {
            BSONObjBuilder metadata(bob.subobjStart("metadata"));
            Status status = WiredTigerUtil::getApplicationMetadata(txn, getURI(), &metadata);
            if (!status.isOK()) {
                metadata.append("error", "unable to retrieve metadata");
                metadata.append("code", static_cast<int>(status.code()));
                metadata.append("reason", status.reason());
            }
        }

        std::string type, sourceURI;
        WiredTigerUtil::fetchTypeAndSourceURI(txn, _uri, &type, &sourceURI);
        StatusWith<std::string> metadataResult = WiredTigerUtil::getMetadata(txn, sourceURI);
        StringData creationStringName("creationString");
        if (!metadataResult.isOK()) {
            BSONObjBuilder creationString(bob.subobjStart(creationStringName));
            creationString.append("error", "unable to retrieve creation config");
            creationString.append("code", static_cast<int>(metadataResult.getStatus().code()));
            creationString.append("reason", metadataResult.getStatus().reason());
        }
        else {
            bob.append("creationString", metadataResult.getValue());
            // Type can be "lsm" or "file"
            bob.append("type", type);
        }

        Status status = WiredTigerUtil::exportTableToBSON(s, "statistics:" + getURI(),
                                                          "statistics=(fast)", &bob);
        if (!status.isOK()) {
            bob.append("error", "unable to retrieve statistics");
            bob.append("code", static_cast<int>(status.code()));
            bob.append("reason", status.reason());
        }

    }

    Status WiredTigerRecordStore::oplogDiskLocRegister( OperationContext* txn,
                                                        const OpTime& opTime ) {
        StatusWith<RecordId> loc = oploghack::keyForOptime( opTime );
        if ( !loc.isOK() )
            return loc.getStatus();

        boost::mutex::scoped_lock lk( _uncommittedDiskLocsMutex );
        _addUncommitedDiskLoc_inlock( txn, loc.getValue() );
        return Status::OK();
    }

    class WiredTigerRecordStore::CappedInsertChange : public RecoveryUnit::Change {
    public:
        CappedInsertChange( WiredTigerRecordStore* rs, const RecordId& loc )
            : _rs( rs ), _loc( loc ) {
        }

        virtual void commit() {
            _rs->dealtWithCappedLoc( _loc );
        }

        virtual void rollback() {
            _rs->dealtWithCappedLoc( _loc );
        }

    private:
        WiredTigerRecordStore* _rs;
        RecordId _loc;
    };

    void WiredTigerRecordStore::_addUncommitedDiskLoc_inlock( OperationContext* txn,
                                                              const RecordId& loc ) {
        // todo: make this a dassert at some point
        invariant( _uncommittedDiskLocs.empty() ||
                   _uncommittedDiskLocs.back() < loc );
        _uncommittedDiskLocs.push_back( loc );
        txn->recoveryUnit()->registerChange( new CappedInsertChange( this, loc ) );
        _oplog_highestSeen = loc;
    }

    boost::optional<RecordId> WiredTigerRecordStore::oplogStartHack(
            OperationContext* txn,
            const RecordId& startingPosition) const {

        if (!_useOplogHack)
            return boost::none;

        {
            WiredTigerRecoveryUnit* wru = WiredTigerRecoveryUnit::get(txn);
            _oplogSetStartHack( wru );
        }

        WiredTigerCursor cursor(_uri, _instanceId, true, txn);
        WT_CURSOR* c = cursor.get();

        int cmp;
        c->set_key(c, _makeKey(startingPosition));
        int ret = WT_OP_CHECK(c->search_near(c, &cmp));
        if (ret == 0 && cmp > 0) ret = c->prev(c); // landed one higher than startingPosition
        if (ret == WT_NOTFOUND) return RecordId(); // nothing <= startingPosition
        invariantWTOK(ret);

        int64_t key;
        ret = c->get_key(c, &key);
        invariantWTOK(ret);
        return _fromKey(key);
    }

    void WiredTigerRecordStore::updateStatsAfterRepair(OperationContext* txn,
                                                       long long numRecords,
                                                       long long dataSize) {
        _numRecords.store(numRecords);
        _dataSize.store(dataSize);
        _sizeStorer->storeToCache(_uri, numRecords, dataSize);
    }

    RecordId WiredTigerRecordStore::_nextId() {
        invariant(!_useOplogHack);
        RecordId out = RecordId(_nextIdNum.fetchAndAdd(1));
        invariant(out.isNormal());
        return out;
    }

    WiredTigerRecoveryUnit* WiredTigerRecordStore::_getRecoveryUnit( OperationContext* txn ) {
        return checked_cast<WiredTigerRecoveryUnit*>( txn->recoveryUnit() );
    }

    class WiredTigerRecordStore::NumRecordsChange : public RecoveryUnit::Change {
    public:
        NumRecordsChange(WiredTigerRecordStore* rs, int64_t diff) :_rs(rs), _diff(diff) {}
        virtual void commit() {}
        virtual void rollback() {
            _rs->_numRecords.fetchAndAdd( -_diff );
        }

    private:
        WiredTigerRecordStore* _rs;
        int64_t _diff;
    };

    void WiredTigerRecordStore::_changeNumRecords( OperationContext* txn, int64_t diff ) {
        txn->recoveryUnit()->registerChange(new NumRecordsChange(this, diff));
        if ( diff > 0 ) {
            if ( _numRecords.fetchAndAdd( diff ) < diff )
                    _numRecords.store( diff );
        } else if ( _numRecords.fetchAndAdd( diff ) < 0 ) {
            _numRecords.store( 0 );
        }
    }

    class WiredTigerRecordStore::DataSizeChange : public RecoveryUnit::Change {
    public:
        DataSizeChange(WiredTigerRecordStore* rs, int amount) :_rs(rs), _amount(amount) {}
        virtual void commit() {}
        virtual void rollback() {
            _rs->_increaseDataSize( NULL, -_amount );
        }

    private:
        WiredTigerRecordStore* _rs;
        bool _amount;
    };

    void WiredTigerRecordStore::_increaseDataSize( OperationContext* txn, int amount ) {
        if ( txn )
            txn->recoveryUnit()->registerChange(new DataSizeChange(this, amount));

        if ( _dataSize.fetchAndAdd(amount) < 0 ) {
            if ( amount > 0 ) {
                _dataSize.store( amount );
            }
            else {
                _dataSize.store( 0 );
            }
        }

        if ( _sizeStorer && _sizeStorerCounter++ % 1000 == 0 ) {
            _sizeStorer->storeToCache( _uri, _numRecords.load(), _dataSize.load() );
        }
    }

    int64_t WiredTigerRecordStore::_makeKey( const RecordId& loc ) {
        return loc.repr();
    }
    RecordId WiredTigerRecordStore::_fromKey( int64_t key ) {
        return RecordId(key);
    }

    // --------

    WiredTigerRecordStore::Iterator::Iterator(
        const WiredTigerRecordStore& rs,
        OperationContext *txn,
        const RecordId& start,
        const CollectionScanParams::Direction& dir,
        bool forParallelCollectionScan)
        : _rs( rs ),
          _txn( txn ),
          _forward( dir == CollectionScanParams::FORWARD ),
          _forParallelCollectionScan( forParallelCollectionScan ),
          _cursor( new WiredTigerCursor( rs.getURI(), rs.instanceId(), true, txn ) ),
          _eof(false),
          _readUntilForOplog(WiredTigerRecoveryUnit::get(txn)->getOplogReadTill()) {
        RS_ITERATOR_TRACE("start");
        _locate(start, true);
    }

    WiredTigerRecordStore::Iterator::~Iterator() {
    }

    void WiredTigerRecordStore::Iterator::_locate(const RecordId &loc, bool exact) {
        RS_ITERATOR_TRACE("_locate " << loc);
        WT_CURSOR *c = _cursor->get();
        invariant( c );
        int ret;
        if (loc.isNull()) {
            ret = _forward ? c->next(c) : c->prev(c);
            _eof = (ret == WT_NOTFOUND);
            if (!_eof) invariantWTOK(ret);
            _loc = _curr();

            RS_ITERATOR_TRACE("_locate   null loc eof: " << _eof);
            return;
        }

        c->set_key(c, _makeKey(loc));
        if (exact) {
            ret = c->search(c);
        }
        else {
            // If loc doesn't exist, inexact matches should find the first existing record before
            // it, in the direction of the scan. Note that inexact callers will call _getNext()
            // after locate so they actually return the record *after* the one we seek to.
            int cmp;
            ret = c->search_near(c, &cmp);
            if ( ret == WT_NOTFOUND ) {
                _eof = true;
                _loc = RecordId();
                return;
            }
            invariantWTOK(ret);
            if (_forward) {
                // return >= loc
                if (cmp < 0)
                    ret = c->next(c);
            }
            else {
                // return <= loc
                if (cmp > 0)
                    ret = c->prev(c);
            }
        }
        if (ret != WT_NOTFOUND) invariantWTOK(ret);
        _eof = (ret == WT_NOTFOUND);
        _loc = _curr();
        RS_ITERATOR_TRACE("_locate   not null loc eof: " << _eof);
    }

    bool WiredTigerRecordStore::Iterator::isEOF() {
        RS_ITERATOR_TRACE( "isEOF " << _eof << " " << _lastLoc );
        return _eof;
    }

    // Allow const functions to use curr to find current location.
    RecordId WiredTigerRecordStore::Iterator::_curr() const {
        RS_ITERATOR_TRACE( "_curr" );
        if (_eof)
            return RecordId();

        WT_CURSOR *c = _cursor->get();
        dassert( c );
        int64_t key;
        int ret = c->get_key(c, &key);
        invariantWTOK(ret);
        return _fromKey(key);
    }

    RecordId WiredTigerRecordStore::Iterator::curr() {
        return _loc;
    }

    void WiredTigerRecordStore::Iterator::_getNext() {
        // Once you go EOF you never go back.
        if (_eof) return;

        RS_ITERATOR_TRACE("_getNext");
        WT_CURSOR *c = _cursor->get();
        int ret = _forward ? c->next(c) : c->prev(c);
        _eof = (ret == WT_NOTFOUND);
        RS_ITERATOR_TRACE("_getNext " << ret << " " << _eof );
        if ( !_eof ) {
            RS_ITERATOR_TRACE("_getNext " << ret << " " << _eof << " " << _curr() );
            invariantWTOK(ret);
            _loc = _curr();
            RS_ITERATOR_TRACE("_getNext " << ret << " " << _eof << " " << _loc );
            if ( _rs._isCapped ) {
                RecordId loc = _curr();
                if ( _readUntilForOplog.isNull() ) {
                    // this is the normal capped case
                    if ( _rs.isCappedHidden( loc ) ) {
                        _eof = true;
                    }
                }
                else {
                    // this is for oplogs
                    if ( loc > _readUntilForOplog ) {
                        _eof = true;
                    }
                    else if ( loc == _readUntilForOplog && _rs.isCappedHidden( loc ) ) {
                        // we allow if its been commited already
                        _eof = true;
                    }
                }
            }
        }

        if (_eof) {
            _loc = RecordId();
        }
    }

    RecordId WiredTigerRecordStore::Iterator::getNext() {
        RS_ITERATOR_TRACE( "getNext" );
        const RecordId toReturn = _loc;
        RS_ITERATOR_TRACE( "getNext toReturn: " << toReturn );
        _getNext();
        RS_ITERATOR_TRACE( " ----" );
        _lastLoc = toReturn;
        return toReturn;
    }

    void WiredTigerRecordStore::Iterator::invalidate( const RecordId& dl ) {
        // this should never be called
    }

    void WiredTigerRecordStore::Iterator::saveState() {
        RS_ITERATOR_TRACE("saveState");

        // the cursor and recoveryUnit are valid on restore
        // so we just record the recoveryUnit to make sure
        _savedRecoveryUnit = _txn->recoveryUnit();
        if ( !wt_keeptxnopen() ) {
            _cursor->reset();
        }

        if ( _forParallelCollectionScan ) {
            _cursor.reset( NULL );
        }
        _txn = NULL;
    }

    bool WiredTigerRecordStore::Iterator::restoreState( OperationContext *txn ) {

        // This is normally already the case, but sometimes we are given a new
        // OperationContext on restore - update the iterators context in that
        // case
        _txn = txn;

        // If we've hit EOF, then this iterator is done and need not be restored.
        if ( _eof ) {
            return true;
        }

        bool needRestore = false;

        if ( _forParallelCollectionScan ) {
            // parallel collection scan or something
            needRestore = true;
            _savedRecoveryUnit = txn->recoveryUnit();
            _cursor.reset( new WiredTigerCursor( _rs.getURI(), _rs.instanceId(), true, txn ) );
            _forParallelCollectionScan = false; // we only do this the first time
        }

        invariant( _savedRecoveryUnit == txn->recoveryUnit() );
        if ( needRestore || !wt_keeptxnopen() ) {
            // This will ensure an active session exists, so any restored cursors will bind to it
            invariant(WiredTigerRecoveryUnit::get(txn)->getSession(txn) == _cursor->getSession());

            RecordId saved = _lastLoc;
            _locate(_lastLoc, false);
            RS_ITERATOR_TRACE( "isEOF check " << _eof );
            if ( _eof ) {
                _lastLoc = RecordId();
            }
            else if ( _loc != saved ) {
                if (_rs._isCapped && _lastLoc != RecordId()) {
                    // Doc was deleted either by cappedDeleteAsNeeded() or cappedTruncateAfter().
                    // It is important that we error out in this case so that consumers don't
                    // silently get 'holes' when scanning capped collections. We don't make 
                    // this guarantee for normal collections so it is ok to skip ahead in that case.
                    _eof = true;
                    return false;
                }
                // lastLoc was either deleted or never set (yielded before first call to getNext()),
                // so bump ahead to the next record.
            }
            else {
                // we found where we left off!
                // now we advance to the next one
                RS_ITERATOR_TRACE( "isEOF found " << _curr() );
                _getNext();
            }
        }

        return true;
    }

    RecordData WiredTigerRecordStore::Iterator::dataFor( const RecordId& loc ) const {
        // Retrieve the data if the iterator is already positioned at loc, otherwise
        // open a new cursor and find the data to avoid upsetting the iterators
        // cursor position.
        if (loc == _loc) {
            dassert(loc == _curr());
            return _rs._getData(*_cursor);
        }
        else {
            return _rs.dataFor( _txn, loc );
        }
    }

    void WiredTigerRecordStore::temp_cappedTruncateAfter( OperationContext* txn,
                                                          RecordId end,
                                                          bool inclusive ) {
        WriteUnitOfWork wuow(txn);
        boost::scoped_ptr<RecordIterator> iter( getIterator( txn, end ) );
        while( !iter->isEOF() ) {
            RecordId loc = iter->getNext();
            if ( end < loc || ( inclusive && end == loc ) ) {
                deleteRecord( txn, loc );
            }
        }
        wuow.commit();
    }
}