aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/visiting/countvisitor.cpp
blob: b8b415402d6c3d42705ad0f2367d8ae1ccfd7574 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "countvisitor.h"
#include <vespa/persistence/spi/docentry.h>
#include <vespa/document/fieldvalue/document.h>
#include <vespa/documentapi/messagebus/messages/visitor.h>
#include <vespa/vespalib/util/stringfmt.h>

#include <vespa/log/log.h>
LOG_SETUP(".visitor.instance.countvisitor");

namespace storage {

CountVisitor::CountVisitor(StorageComponent& component,
                           const vdslib::Parameters& params)
    : Visitor(component),
      _doScheme(params.get("scheme") == "true"),
      _doNamespace(params.get("namespace") == "true"),
      _doUser(params.get("user") == "true"),
      _doGroup(params.get("group") == "true")
{
}

void
CountVisitor::handleDocuments(const document::BucketId& /*bucketId*/,
                              DocEntryList& entries,
                              HitCounter& hitCounter)
{
    for (size_t i = 0; i < entries.size(); ++i) {
        const spi::DocEntry& entry(*entries[i]);
        if (!entry.isRemove()) {
            const document::Document* doc = entry.getDocument();

            if (doc) {
                const document::IdString& idString = doc->getId().getScheme();
                hitCounter.addHit(doc->getId(), 0);

                if (_doNamespace) {
                    _namespaceCount[idString.getNamespace()]++;
                }

                if (_doUser && idString.hasNumber()) {
                    _userCount[idString.getNumber()]++;
                }

                if (_doGroup && idString.hasGroup()) {
                    _groupCount[idString.getGroup()]++;
                }

                if (_doScheme) {
                    _schemeCount["id"]++;
                }

            }
        }
    }
}

void CountVisitor::completedVisiting(HitCounter&) {
    documentapi::MapVisitorMessage* cmd(new documentapi::MapVisitorMessage());

    for (std::map<std::string, int>::iterator iter = _schemeCount.begin();
         iter != _schemeCount.end();
         iter++) {
        cmd->getData().set(vespalib::make_string("scheme.%s", iter->first.c_str()), iter->second);
    }

    for (NamespaceCountMap::const_iterator iter = _namespaceCount.begin();
         iter != _namespaceCount.end();
         iter++) {
        cmd->getData().set(vespalib::make_string("namespace.%s", iter->first.c_str()), iter->second);
    }

    for (GroupCountMap::const_iterator iter = _groupCount.begin();
         iter != _groupCount.end();
         iter++) {
        cmd->getData().set(vespalib::make_string("group.%s", iter->first.c_str()), iter->second);
    }

    for (std::map<uint64_t, int>::iterator iter = _userCount.begin();
         iter != _userCount.end();
         iter++) {
        cmd->getData().set(vespalib::make_string("user.%" PRIu64, iter->first), iter->second);
    }

    sendMessage(documentapi::DocumentMessage::UP(cmd));
}

}