summaryrefslogtreecommitdiffstats
path: root/searchsummary/src/vespa/searchsummary/docsummary/resultclass.cpp
blob: f52cc62af921dd13510fca2334c3413f5ac6a77c (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "resultclass.h"
#include "resultconfig.h"
#include <vespa/vespalib/stllike/hashtable.hpp>
#include <cassert>
#include <zlib.h>

namespace search::docsummary {

ResultClass::ResultClass(const char *name, uint32_t id, util::StringEnum & fieldEnum)
    : _name(name),
      _classID(id),
      _entries(),
      _nameMap(),
      _fieldEnum(fieldEnum),
      _enumMap(),
      _dynInfo(NULL),
      _omit_summary_features(false)
{ }


ResultClass::~ResultClass() = default;

int
ResultClass::GetIndexFromName(const char* name) const
{
    NameIdMap::const_iterator found(_nameMap.find(name));
    return (found != _nameMap.end()) ? found->second : -1;
}

bool
ResultClass::AddConfigEntry(const char *name, ResType type)
{
    if (_nameMap.find(name) != _nameMap.end())
        return false;

    _nameMap[name] = _entries.size();
    ResConfigEntry e;
    e._type      = type;
    e._bindname  = name;
    e._enumValue = _fieldEnum.Add(name);
    assert(e._enumValue >= 0);
    _entries.push_back(e);
    return true;
}


void
ResultClass::CreateEnumMap()
{
    _enumMap.resize(_fieldEnum.GetNumEntries());

    for (uint32_t i(0), m(_enumMap.size()); i < m; i++) {
        _enumMap[i] = -1;
    }
    for (uint32_t i(0); i < _entries.size(); i++) {
        _enumMap[_entries[i]._enumValue] = i;
    }
}


bool
ResEntry::_extract_field(search::RawBuf *target) const
{
    bool rc = true;
    target->reset();

    if (ResultConfig::IsVariableSize(_type)) {
        if (_is_compressed()) { // COMPRESSED

            uint32_t len = _get_length();
            uint32_t realLen = 0;

            if (len >= sizeof(uint32_t))
                realLen = _get_real_length();
            else
                rc = false;

            if (realLen > 0) {
                uLongf rlen = realLen;
                char *fillPos = target->GetWritableFillPos(realLen + 1 < 32000 ?
                                                           32000 : realLen + 1);
                if ((uncompress((Bytef *)fillPos, &rlen,
                                (const Bytef *)(_get_compressed()),
                                len - sizeof(realLen)) == Z_OK) &&
                    rlen == realLen) {
                    fillPos[realLen] = '\0';
                    target->Fill(realLen);
                } else {
                    rc = false;
                }
            }
        } else {                     // UNCOMPRESSED
            uint32_t len = _len;
            if (len + 1 < 32000)
                target->preAlloc(32000);
            else
                target->preAlloc(len + 1);
            char *fillPos = target->GetWritableFillPos(len + 1 < 32000 ?
                                                       32000 : len + 1);
            memcpy(fillPos, _pt, len);
            fillPos[len] = '\0';
            target->Fill(len);
        }
    }
    return rc;
}

}