aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/docstore/randreaders.cpp
blob: 337ae1f95d1ccd4ac8120d46a98fe3869075307b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "randreaders.h"
#include "summaryexceptions.h"
#include <vespa/vespalib/data/databuffer.h>
#include <vespa/fastos/file.h>

#include <vespa/log/log.h>
LOG_SETUP(".search.docstore.randreaders");

namespace search {

DirectIORandRead::DirectIORandRead(const vespalib::string & fileName)
    : _file(std::make_unique<FastOS_File>(fileName.c_str())),
      _alignment(1),
      _granularity(1),
      _maxChunkSize(0x100000)
{
    _file->EnableDirectIO();
    if (_file->OpenReadOnly()) {
        if (!_file->GetDirectIORestrictions(_alignment, _granularity, _maxChunkSize)) {
            LOG(debug, "Direct IO setup failed for file %s due to %s",
                       _file->GetFileName(), _file->getLastErrorString().c_str());
        }
    } else {
        throw SummaryException("Failed opening data file", *_file, VESPA_STRLOC);
    }
}

FileRandRead::FSP
DirectIORandRead::read(size_t offset, vespalib::DataBuffer & buffer, size_t sz)
{
    size_t padBefore(0);
    size_t padAfter(0);
    bool directio = _file->DirectIOPadding(offset, sz, padBefore, padAfter);
    buffer.clear();
    buffer.ensureFree(padBefore + sz + padAfter + _alignment - 1);
    if (directio) {
        size_t unAligned = (-reinterpret_cast<size_t>(buffer.getFree()) & (_alignment - 1));
        buffer.moveFreeToData(unAligned);
        buffer.moveDataToDead(unAligned);
    }
    // XXX needs to use pread or file-position-mutex
    _file->ReadBuf(buffer.getFree(), padBefore + sz + padAfter, offset - padBefore);
    buffer.moveFreeToData(padBefore + sz);
    buffer.moveDataToDead(padBefore);
    return FSP();
}


int64_t
DirectIORandRead::getSize() const {
    return _file->getSize();
}


MMapRandRead::MMapRandRead(const vespalib::string & fileName, int mmapFlags, int fadviseOptions)
    : _file(std::make_unique<FastOS_File>(fileName.c_str()))
{
    _file->enableMemoryMap(mmapFlags);
    _file->setFAdviseOptions(fadviseOptions);
    if ( ! _file->OpenReadOnly()) {
        throw SummaryException("Failed opening data file", *_file, VESPA_STRLOC);
    }
}


NormalRandRead::NormalRandRead(const vespalib::string & fileName)
    : _file(std::make_unique<FastOS_File>(fileName.c_str()))
{
    if ( ! _file->OpenReadOnly()) {
        throw SummaryException("Failed opening data file", *_file, VESPA_STRLOC);
    }
}

FileRandRead::FSP
MMapRandRead::read(size_t offset, vespalib::DataBuffer & buffer, size_t sz)
{
    const char *data = static_cast<const char *>(_file->MemoryMapPtr(offset));
    assert(data != nullptr);
    assert(_file->MemoryMapPtr(offset+sz-1) != nullptr);
    vespalib::DataBuffer(data, sz).swap(buffer);
    return FSP();
}

int64_t
MMapRandRead::getSize() const {
    return _file->getSize();
}

const void *
MMapRandRead::getMapping() {
    return _file->MemoryMapPtr(0);
}

MMapRandReadDynamic::MMapRandReadDynamic(const vespalib::string &fileName, int mmapFlags, int fadviseOptions)
    : _fileName(fileName),
      _holder(),
      _mmapFlags(mmapFlags),
      _fadviseOptions(fadviseOptions),
      _lock()
{
    remap(0);
}

void
MMapRandReadDynamic::remap(size_t sz)
{
    std::lock_guard guard(_lock);
    if ((sz > 0) && _holder.hasValue() && contains(*_holder.get(), sz)) {
        return;
    }
    std::unique_ptr<FastOS_File> file(new FastOS_File(_fileName.c_str()));
    file->enableMemoryMap(_mmapFlags);
    file->setFAdviseOptions(_fadviseOptions);
    if (file->OpenReadOnly()) {
        _holder.set(file.release());
        _holder.latch();
    } else {
        throw SummaryException("Failed opening data file", *file, VESPA_STRLOC);
    }
}

FileRandRead::FSP
MMapRandReadDynamic::read(size_t offset, vespalib::DataBuffer & buffer, size_t sz)
{
    FSP file(_holder.get());
    size_t end = offset + sz;
    const char * data(static_cast<const char *>(file->MemoryMapPtr(offset)));
    if ((data == nullptr) || !contains(*file, end)) {
        // Must check that both start and end of file is mapped in.
        remap(end);
        file = _holder.get();
        data = static_cast<const char *>(file->MemoryMapPtr(offset));
        assert(data != nullptr);
        assert(contains(*file, end));
    }
    vespalib::DataBuffer(data, sz).swap(buffer);
    return file;
}

bool
MMapRandReadDynamic::contains(const FastOS_FileInterface & file, size_t sz) {
    return (sz == 0) || (file.MemoryMapPtr(sz - 1) != nullptr);
}


int64_t
MMapRandReadDynamic::getSize() const {
    return _holder.get()->getSize();
}

FileRandRead::FSP
NormalRandRead::read(size_t offset, vespalib::DataBuffer & buffer, size_t sz)
{
    buffer.clear();
    buffer.ensureFree(sz);
    _file->ReadBuf(buffer.getFree(), sz, offset);
    buffer.moveFreeToData(sz);
    return FSP();
}

int64_t
NormalRandRead::getSize() const
{
    return _file->getSize();
}

}