aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/tests/proton/documentdb/lid_space_compaction/lid_space_common.cpp
blob: 44e663a559b09f663ba74d3cc255fdbea9dbbe0c (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "lid_space_common.h"
#include <vespa/searchcore/proton/test/dummy_document_sub_db.h>

using vespalib::make_string_short::fmt;
using proton::test::DummyDocumentSubDb;

MyScanIterator::MyScanIterator(const MyHandler & handler, const LidVector &lids)
    : _handler(handler),
      _lids(lids),
      _itr(_lids.begin()),
      _validItr(true)
{}
MyScanIterator::~MyScanIterator() = default;

bool
MyScanIterator::valid() const {
    return _validItr;
}

search::DocumentMetaData MyScanIterator::next(uint32_t compactLidLimit) {
    if (_itr != _lids.begin()) {
        ++_itr;
    }
    for (; _itr != _lids.end() && (*_itr) <= compactLidLimit; ++_itr) {}
    if (_itr != _lids.end()) {
        uint32_t lid = *_itr;
        if (lid > compactLidLimit) {
            return _handler.getMetaData(lid);
        }
    } else {
        _validItr = false;
    }
    return search::DocumentMetaData();
}

document::BucketId
MyHandler::createBucketId(uint32_t lid) const {
    return _bucketIdEqualLid ? document::BucketId(lid) : BUCKET_ID_1;
}

void
MyHandler::clearMoveDoneContexts() {
    _moveDoneContexts.clear();
}

void
MyHandler::run_remove_ops(bool remove_batch) {
    // This ensures to max out the threshold time in the operation rate tracker.
    if (remove_batch) {
        _op_listener->notify_remove_batch();
        _op_listener->notify_remove_batch();
        _op_listener->notify_remove_batch();
    } else {
        _op_listener->notify_remove();
        _op_listener->notify_remove();
        _op_listener->notify_remove();
    }
}

void
MyHandler::stop_remove_ops(bool remove_batch) const {
    if (remove_batch) {
        _rm_listener->get_remove_batch_tracker().reset(vespalib::steady_clock::now());
    } else {
        _rm_listener->get_remove_tracker().reset(vespalib::steady_clock::now());
    }
}

vespalib::string
MyHandler::getName() const {
    return "myhandler";
}

void
MyHandler::set_operation_listener(documentmetastore::OperationListener::SP op_listener) {
    auto* rm_listener = dynamic_cast<RemoveOperationsRateTracker*>(op_listener.get());
    assert(rm_listener != nullptr);
    _op_listener = std::move(op_listener);
    _rm_listener = rm_listener;
}

LidUsageStats
MyHandler::getLidStatus() const {
    assert(_handleMoveCnt < _stats.size());
    return _stats[_handleMoveCnt];
}

IDocumentScanIterator::UP
MyHandler::getIterator() const {
    assert(_iteratorCnt < _lids.size());
    return std::make_unique<MyScanIterator>(*this, _lids[_iteratorCnt++]);
}

search::DocumentMetaData
MyHandler::getMetaData(uint32_t lid) const {
    if (lid < _docs.size()) {
        return _docs[lid].first;
    }
    return search::DocumentMetaData();
}

MoveOperation::UP
MyHandler::createMoveOperation(const search::DocumentMetaData &document, uint32_t moveToLid) const {
    assert(document.lid > moveToLid);
    _moveFromLid = document.lid;
    const auto & entry = _docs[document.lid];
    auto op = std::make_unique<MoveOperation>(entry.first.bucketId, storage::spi::Timestamp(entry.first.timestamp), entry.second,
                                              DbDocumentId(document.lid), 0);
    op->setTargetLid(moveToLid);
    return op;
}

void
MyHandler::handleMove(const MoveOperation & op, IDestructorCallback::SP moveDoneCtx) {
    ++_handleMoveCnt;
    _moveToLid = op.getTargetDbdId().getLid();
    if (_storeMoveDoneContexts) {
        _moveDoneContexts.push_back(std::move(moveDoneCtx));
    }
}

void
MyHandler::handleCompactLidSpace(const CompactLidSpaceOperation &op, std::shared_ptr<IDestructorCallback>) {
    _wantedLidLimit = op.getLidLimit();
}

MyHandler::MyHandler(bool storeMoveDoneContexts, bool bucketIdEqualLid)
    : _builder(),
      _stats(),
      _moveFromLid(0),
      _moveToLid(0),
      _handleMoveCnt(0),
      _wantedLidLimit(0),
      _iteratorCnt(0),
      _storeMoveDoneContexts(storeMoveDoneContexts),
      _bucketIdEqualLid(bucketIdEqualLid),
      _moveDoneContexts(),
      _op_listener(),
      _rm_listener(),
      _docs()
{
    for (uint32_t i(0); i < 10; i++) {
        auto doc = _builder.make_document(fmt("%s%d", DOC_ID.c_str(), i));
        _docs.emplace_back(DocumentMetaData(i, TIMESTAMP_1, createBucketId(i), doc->getId().getGlobalId()), std::move(doc));
    }
}

MyHandler::~MyHandler() = default;

void
MyStorer::appendOperation(const FeedOperation &op, DoneCallback) {
    if (op.getType() == FeedOperation::MOVE) {
        ++ _moveCnt;
    } else if (op.getType() == FeedOperation::COMPACT_LID_SPACE) {
        ++_compactCnt;
    }
}

IOperationStorer::CommitResult
MyStorer::startCommit(DoneCallback) {
    return CommitResult();
}

MyDocumentStore::MyDocumentStore()
    : _readDoc(),
      _readLid(0)
{}

MyDocumentStore::~MyDocumentStore() = default;

document::Document::UP
MyDocumentStore::read(search::DocumentIdT lid, const document::DocumentTypeRepo &) const {
    _readLid = lid;
    return Document::UP(_readDoc->clone());
}

MyDocumentRetriever::MyDocumentRetriever(std::shared_ptr<const DocumentTypeRepo> repo_in, const MyDocumentStore& store_in) noexcept
    : repo(std::move(repo_in)),
      store(store_in)
{}

MyDocumentRetriever::~MyDocumentRetriever() = default;

const document::DocumentTypeRepo&
MyDocumentRetriever::getDocumentTypeRepo() const {
    return *repo;
}

void
MyDocumentRetriever::getBucketMetaData(const storage::spi::Bucket&, DocumentMetaData::Vector&) const {
    abort();
}

DocumentMetaData
MyDocumentRetriever::getDocumentMetaData(const DocumentId&) const {
    abort();
}

Document::UP
MyDocumentRetriever::getFullDocument(DocumentIdT lid) const {
    return store.read(lid, *repo);
}

CachedSelect::SP
MyDocumentRetriever::parseSelect(const vespalib::string&) const {
    abort();
}

MySubDb::MySubDb(std::shared_ptr<bucketdb::BucketDBOwner> bucket_db, const MyDocumentStore& store, const std::shared_ptr<const DocumentTypeRepo> & repo)
    : sub_db(std::make_unique<DummyDocumentSubDb>(std::move(bucket_db), SUBDB_ID)),
      maintenance_sub_db(sub_db->getName(), sub_db->getSubDbId(), sub_db->getDocumentMetaStoreContext().getSP(),
                         std::make_shared<MyDocumentRetriever>(repo, store),
                         std::make_shared<MyFeedView>(repo),
                         &_pendingLidsForCommit)
{
}

MySubDb::~MySubDb() = default;