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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "bucketprocessor.h"
#include <vespa/document/fieldset/fieldsets.h>
#include <vespa/vespalib/stllike/asciistream.h>
#include <cassert>
namespace storage {
namespace {
class IteratorGuard
{
private:
spi::PersistenceProvider& _spi;
spi::IteratorId _iteratorId;
spi::Context& _context;
public:
IteratorGuard(spi::PersistenceProvider& spi,
spi::IteratorId iteratorId,
spi::Context& context)
: _spi(spi),
_iteratorId(iteratorId),
_context(context)
{ }
~IteratorGuard()
{
assert(_iteratorId != 0);
_spi.destroyIterator(_iteratorId, _context);
}
spi::IteratorId getIteratorId() const { return _iteratorId; }
spi::PersistenceProvider& getPersistenceProvider() const { return _spi; }
};
}
void
BucketProcessor::iterateAll(spi::PersistenceProvider& provider,
const spi::Bucket& bucket,
const std::string& documentSelection,
EntryProcessor& processor,
spi::IncludedVersions versions,
spi::Context& context)
{
spi::Selection sel
= spi::Selection(spi::DocumentSelection(documentSelection));
spi::CreateIteratorResult createIterResult(provider.createIterator(
bucket,
document::HeaderFields(),
sel,
versions,
context));
if (createIterResult.getErrorCode() != spi::Result::ErrorType::NONE) {
vespalib::asciistream ss;
ss << "Failed to create iterator: "
<< createIterResult.getErrorMessage();
throw std::runtime_error(ss.str());
}
spi::IteratorId iteratorId(createIterResult.getIteratorId());
IteratorGuard iteratorGuard(provider, iteratorId, context);
while (true) {
spi::IterateResult result(
provider.iterate(iteratorId, UINT64_MAX, context));
if (result.getErrorCode() != spi::Result::ErrorType::NONE) {
vespalib::asciistream ss;
ss << "Failed: " << result.getErrorMessage();
throw std::runtime_error(ss.str());
}
for (size_t i = 0; i < result.getEntries().size(); ++i) {
processor.process(*result.getEntries()[i]);
}
if (result.isCompleted()) {
break;
}
}
}
}
|