aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/apps/vespa-attribute-inspect/vespa-attribute-inspect.cpp
blob: 75fb1f9139ed4dd3dea4acc8ac0f4277182a5f70 (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
// Copyright Yahoo. 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/searchcommon/attribute/config.h>
#include <vespa/vespalib/data/fileheader.h>
#include <fstream>

#include <vespa/fastlib/io/bufferedfile.h>
#include <vespa/vespalib/util/signalhandler.h>
#include <unistd.h>

namespace search {

using AttributePtr = AttributeVector::SP;

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

public:
    int main(int argc, char **argv);
};

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->isIntegerType()) {
        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->isFloatingPointType()) {
        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->isStringType()) {
        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(int argc, char **argv)
{
    bool doPrintContent = false;
    bool doApplyUpdate = false;
    bool doSave = false;
    bool doFastSearch = false;

    int opt;
    bool optError = false;
    while ((opt = getopt(argc, argv, "pasf:")) != -1) {
        switch (opt) {
        case 'p':
            doPrintContent = true;
            break;
        case 'a':
            doApplyUpdate = true;
            break;
        case 'f':
            if (strcmp(optarg, "search") == 0) {
                doFastSearch = true;
            } else {
                std::cerr << "Expected 'search' or 'aggregate', got '" <<
                    optarg << "'" << std::endl;
                optError = true;
            }
            break;
        case 's':
            doSave = true;
            break;
        default:
            optError = true;
            break;
        }
    }

    if (argc != (optind + 1) || optError) {
        usage();
        return -1;
    }

    vespalib::string fileName(argv[optind]);
    vespalib::FileHeader fh;
    {
        vespalib::string datFileName(fileName + ".dat");
        Fast_BufferedFile file;
        file.ReadOpenExisting(datFileName.c_str());
        (void) fh.readFile(file);
    }
    attribute::BasicType bt(fh.getTag("datatype").asString());
    attribute::CollectionType ct(fh.getTag("collectiontype").asString());
    attribute::Config c(bt, ct);
    c.setFastSearch(doFastSearch);
    AttributePtr ptr = AttributeFactory::createAttribute(fileName, c);
    vespalib::Timer timer;
    load(ptr);
    std::cout << "load time: " << vespalib::to_s(timer.elapsed()) << " seconds " << std::endl;

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

    if (doApplyUpdate) {
        timer = vespalib::Timer();
        applyUpdate(ptr);
        std::cout << "update time: " << vespalib::to_s(timer.elapsed()) << " 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 = vespalib::Timer();
        ptr->save(saveFile);
        std::cout << "save time: " << vespalib::to_s(timer.elapsed()) << " seconds " << std::endl;
    }

    return 0;
}

}

int main(int argc, char ** argv) {
    vespalib::SignalHandler::PIPE.ignore();
    search::LoadAttribute myApp;
    return myApp.main(argc, argv);
}