|
| 1 | +/** |
| 2 | + * Copyright (C) 2018-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the Server Side Public License, version 1, |
| 6 | + * as published by MongoDB, Inc. |
| 7 | + * |
| 8 | + * This program is distributed in the hope that it will be useful, |
| 9 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | + * Server Side Public License for more details. |
| 12 | + * |
| 13 | + * You should have received a copy of the Server Side Public License |
| 14 | + * along with this program. If not, see |
| 15 | + * <http://www.mongodb.com/licensing/server-side-public-license>. |
| 16 | + * |
| 17 | + * As a special exception, the copyright holders give permission to link the |
| 18 | + * code of portions of this program with the OpenSSL library under certain |
| 19 | + * conditions as described in each individual source file and distribute |
| 20 | + * linked combinations including the program with the OpenSSL library. You |
| 21 | + * must comply with the Server Side Public License in all respects for |
| 22 | + * all of the code used other than as permitted herein. If you modify file(s) |
| 23 | + * with this exception, you may extend this exception to your version of the |
| 24 | + * file(s), but you are not obligated to do so. If you do not wish to do so, |
| 25 | + * delete this exception statement from your version. If you delete this |
| 26 | + * exception statement from all source files in the program, then also delete |
| 27 | + * it in the license file. |
| 28 | + */ |
| 29 | + |
| 30 | +#include "mongo/platform/basic.h" |
| 31 | + |
| 32 | +#include "mongo/db/storage/record_store_test_harness.h" |
| 33 | + |
| 34 | + |
| 35 | +#include "mongo/db/record_id.h" |
| 36 | +#include "mongo/db/storage/record_data.h" |
| 37 | +#include "mongo/db/storage/record_store.h" |
| 38 | +#include "mongo/unittest/unittest.h" |
| 39 | + |
| 40 | + |
| 41 | +namespace mongo { |
| 42 | +namespace { |
| 43 | + |
| 44 | +using std::string; |
| 45 | +using std::stringstream; |
| 46 | +using std::unique_ptr; |
| 47 | + |
| 48 | +// Insert a record in a store with capped max docs 1, and try to delete it by inserting another. |
| 49 | +TEST(RecordStoreTestHarness, CappedDeleteRecord) { |
| 50 | + const auto harness(newRecordStoreHarnessHelper()); |
| 51 | + if (!harness->supportsDocLocking()) |
| 52 | + return; |
| 53 | + auto rs(harness->newCappedRecordStore(RecordStoreHarnessHelper::kDefaultCapedSizeBytes, |
| 54 | + /*cappedMaxDocs*/ 1)); |
| 55 | + |
| 56 | + { |
| 57 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 58 | + ASSERT_EQUALS(0, rs->numRecords(opCtx.get())); |
| 59 | + } |
| 60 | + |
| 61 | + string data = "my record"; |
| 62 | + RecordId loc1, loc2; |
| 63 | + { |
| 64 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 65 | + WriteUnitOfWork uow(opCtx.get()); |
| 66 | + StatusWith<RecordId> res = |
| 67 | + rs->insertRecord(opCtx.get(), data.c_str(), data.size() + 1, Timestamp()); |
| 68 | + ASSERT_OK(res.getStatus()); |
| 69 | + loc1 = res.getValue(); |
| 70 | + uow.commit(); |
| 71 | + } |
| 72 | + |
| 73 | + { |
| 74 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 75 | + ASSERT_EQUALS(1, rs->numRecords(opCtx.get())); |
| 76 | + } |
| 77 | + |
| 78 | + { |
| 79 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 80 | + WriteUnitOfWork uow(opCtx.get()); |
| 81 | + StatusWith<RecordId> res = |
| 82 | + rs->insertRecord(opCtx.get(), data.c_str(), data.size() + 1, Timestamp()); |
| 83 | + ASSERT_OK(res.getStatus()); |
| 84 | + loc2 = res.getValue(); |
| 85 | + ASSERT_GT(loc2, loc1); |
| 86 | + uow.commit(); |
| 87 | + } |
| 88 | + |
| 89 | + { |
| 90 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 91 | + ASSERT_EQUALS(1, rs->numRecords(opCtx.get())); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// Insert multiple records at once, requiring multiple deletes. |
| 96 | +TEST(RecordStoreTestHarness, DeleteMultipleRecords) { |
| 97 | + const auto harness(newRecordStoreHarnessHelper()); |
| 98 | + if (!harness->supportsDocLocking()) |
| 99 | + return; |
| 100 | + const int cappedMaxDocs = 10; |
| 101 | + auto rs(harness->newCappedRecordStore(RecordStoreHarnessHelper::kDefaultCapedSizeBytes, |
| 102 | + cappedMaxDocs)); |
| 103 | + |
| 104 | + const int nToInsertFirst = cappedMaxDocs / 2; |
| 105 | + const int nToInsertSecond = cappedMaxDocs; |
| 106 | + RecordId lastLoc = RecordId(); |
| 107 | + |
| 108 | + // First insert some records that fit without exceeding the cap. |
| 109 | + { |
| 110 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 111 | + WriteUnitOfWork uow(opCtx.get()); |
| 112 | + for (int i = 0; i < nToInsertFirst; i++) { |
| 113 | + stringstream ss; |
| 114 | + ss << "record " << i; |
| 115 | + string data = ss.str(); |
| 116 | + |
| 117 | + StatusWith<RecordId> res = |
| 118 | + rs->insertRecord(opCtx.get(), data.c_str(), data.size() + 1, Timestamp()); |
| 119 | + ASSERT_OK(res.getStatus()); |
| 120 | + RecordId loc = res.getValue(); |
| 121 | + ASSERT_GT(loc, lastLoc); |
| 122 | + lastLoc = loc; |
| 123 | + } |
| 124 | + uow.commit(); |
| 125 | + } |
| 126 | + |
| 127 | + { |
| 128 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 129 | + ASSERT_EQUALS(nToInsertFirst, rs->numRecords(opCtx.get())); |
| 130 | + } |
| 131 | + |
| 132 | + // Then insert the largest batch possible (number of docs equal to the cap), causing deletes. |
| 133 | + { |
| 134 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 135 | + WriteUnitOfWork uow(opCtx.get()); |
| 136 | + for (int i = nToInsertFirst; i < nToInsertFirst + nToInsertSecond; i++) { |
| 137 | + stringstream ss; |
| 138 | + ss << "record " << i; |
| 139 | + string data = ss.str(); |
| 140 | + |
| 141 | + StatusWith<RecordId> res = |
| 142 | + rs->insertRecord(opCtx.get(), data.c_str(), data.size() + 1, Timestamp()); |
| 143 | + ASSERT_OK(res.getStatus()); |
| 144 | + RecordId loc = res.getValue(); |
| 145 | + ASSERT_GT(loc, lastLoc); |
| 146 | + lastLoc = loc; |
| 147 | + } |
| 148 | + uow.commit(); |
| 149 | + } |
| 150 | + |
| 151 | + { |
| 152 | + ServiceContext::UniqueOperationContext opCtx(harness->newOperationContext()); |
| 153 | + ASSERT_EQUALS(cappedMaxDocs, rs->numRecords(opCtx.get())); |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +} // namespace |
| 158 | +} // namespace mongo |
0 commit comments