aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/apps/vespa-attribute-inspect/vespa-attribute-inspect.cpp
blob: 189074aa5d3ba7ae1f3aa24339bf54a187ebf485 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <iostream>
#include <vespa/searchlib/attribute/attribute.h>
#include <vespa/searchlib/attribute/attributeguard.h>
#include <vespa/searchlib/attribute/attributefactory.h>
#include <vespa/vespalib/data/fileheader.h>
#include <fstream>

#include <vespa/searchlib/attribute/attributevector.hpp>
#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/fastos/app.h>

namespace search {

typedef AttributeVector::SP AttributePtr;

class LoadAttribute : public FastOS_Application
{
private:
    void load(const AttributePtr & ptr);
    void applyUpdate(const AttributePtr & ptr);
    void printContent(const AttributePtr & ptr, std::ostream & os);
    void usage();

public:
    int Main() override;
};

void
LoadAttribute::load(const AttributePtr & ptr)
{
    std::cout << "loading attribute: " << ptr->getBaseFileName() << std::endl;
    ptr->load();
    std::cout << "attribute successfully loaded"  << std::endl;
}

void
LoadAttribute::applyUpdate(const AttributePtr & ptr)
{
    std::cout << "applyUpdate" << std::endl;
    if (ptr->getClass().inherits(IntegerAttribute::classId)) {
        IntegerAttribute * a = static_cast<IntegerAttribute *>(ptr.get());
        if (ptr->hasMultiValue()) {
            a->append(0, 123456789, 1);
        } else {
            a->update(0, 123456789);
        }
        a->commit();
    } else if (ptr->getClass().inherits(FloatingPointAttribute::classId)) {
        FloatingPointAttribute * a = static_cast<FloatingPointAttribute *>(ptr.get());
        if (ptr->hasMultiValue()) {
            a->append(0, 123456789.5f, 1);
        } else {
            a->update(0, 123456789);
        }
        a->commit();
    } else if (ptr->getClass().inherits(StringAttribute::classId)) {
        StringAttribute * a = static_cast<StringAttribute *>(ptr.get());
        if (ptr->hasMultiValue()) {
            a->append(0, "non-existing string value", 1);
        } else {
            a->update(0, "non-existing string value");
        }
        a->commit();
    }
}

void
LoadAttribute::printContent(const AttributePtr & ptr, std::ostream & os)
{
    uint32_t sz = ptr->getMaxValueCount();
    if (ptr->hasWeightedSetType()) {
        AttributeVector::WeightedString * buf = new AttributeVector::WeightedString[sz];
        for (uint32_t doc = 0; doc < ptr->getNumDocs(); ++doc) {
            uint32_t valueCount = ptr->get(doc, buf, sz);
            assert(valueCount <= sz);
            os << "doc " << doc << ": valueCount(" << valueCount << ")" << std::endl;
            for (uint32_t i = 0; i < valueCount; ++i) {
                os << "    " << i << ": " << "[" << buf[i].getValue() << ", " << buf[i].getWeight() << "]" << std::endl;
            }
        }
        delete [] buf;
    } else {
        vespalib::string *buf = new vespalib::string[ptr->getMaxValueCount()];
        for (uint32_t doc = 0; doc < ptr->getNumDocs(); ++doc) {
            uint32_t valueCount = ptr->get(doc, buf, sz);
            assert(valueCount <= sz);
            os << "doc " << doc << ": valueCount(" << valueCount << ")" << std::endl;
            for (uint32_t i = 0; i < valueCount; ++i) {
                os << "    " << i << ": " << "[" << buf[i] << "]" << std::endl;
            }
        }
        delete [] buf;
    }
}

void
LoadAttribute::usage()
{
    std::cout << "usage: vespa-attribute-inspect [-p (print content to <attribute>.out)]" << std::endl;
    std::cout << "                     [-a (apply a single update)]" << std::endl;
    std::cout << "                     [-s (save attribute to <attribute>.save.dat)]" << std::endl;
    std::cout << "                     <attribute>" << std::endl;
}

int
LoadAttribute::Main()
{
    bool doPrintContent = false;
    bool doApplyUpdate = false;
    bool doSave = false;
    bool doFastSearch = false;
    bool doEnableEnumeratedSave = false;
    bool doHuge = false;

    int idx = 1;
    char opt;
    const char * arg;
    bool optError = false;
    while ((opt = GetOpt("pasf:eh", arg, idx)) != -1) {
        switch (opt) {
        case 'p':
            doPrintContent = true;
            break;
        case 'a':
            doApplyUpdate = true;
            break;
        case 'e':
            doEnableEnumeratedSave = true;
            break;
        case 'h':
            doHuge = true;
            break;
        case 'f':
            if (strcmp(arg, "search") == 0) {
                doFastSearch = true;
            } else {
                std::cerr << "Expected 'search' or 'aggregate', got '" <<
                    arg << "'" << std::endl;
                optError = true;
            }
            break;
        case 's':
            doSave = true;
            break;
        default:
            optError = true;
            break;
        }
    }

    if (_argc != (idx + 1) || optError) {
        usage();
        return -1;
    }

    vespalib::string fileName(_argv[idx]);
    vespalib::FileHeader fh;
    do {
        vespalib::string datFileName(fileName + ".dat");
        Fast_BufferedFile file;
        file.ReadOpenExisting(datFileName.c_str());
        (void) fh.readFile(file);
    } while (0);
    attribute::BasicType bt(fh.getTag("datatype").asString());
    attribute::CollectionType ct(fh.getTag("collectiontype").asString());
    attribute::Config c(bt, ct);
    c.setFastSearch(doFastSearch);
    c.setHuge(doHuge);
    AttributePtr ptr = AttributeFactory::createAttribute(fileName, c);
    if (doEnableEnumeratedSave) {
        ptr->enableEnumeratedSave();
    }
    FastOS_Time timer;
    timer.SetNow();
    load(ptr);
    std::cout << "load time: " << timer.MilliSecsToNow() / 1000 << " seconds " << std::endl;

    std::cout << "numDocs: " << ptr->getNumDocs() << std::endl;

    if (doApplyUpdate) {
        timer.SetNow();
        applyUpdate(ptr);
        std::cout << "update time: " << timer.MilliSecsToNow() / 1000 << " seconds " << std::endl;
    }

    if (doPrintContent) {
        vespalib::string outFile(fileName + ".out");
        std::ofstream of(outFile.c_str());
        if (of.fail()) {
            std::cout << "failed opening: " << fileName << ".out" << std::endl;
        }
        std::cout << "printContent" << std::endl;
        printContent(ptr, of);
        of.close();
    }

    if (doSave) {
        vespalib::string saveFile = fileName + ".save";
        std::cout << "saving attribute: " << saveFile << std::endl;
        timer.SetNow();
        ptr->saveAs(saveFile);
        std::cout << "save time: " << timer.MilliSecsToNow() / 1000 << " seconds " << std::endl;
    }

    return 0;
}

}

int main(int argc, char ** argv)
{
    search::LoadAttribute myApp;
    return myApp.Entry(argc, argv);
}