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
|
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009-2014 Alan Wright. All rights reserved.
// Distributable under the terms of either the Apache License (Version 2.0)
// or the GNU Lesser General Public License.
/////////////////////////////////////////////////////////////////////////////
#include "LuceneInc.h"
#include "MMapDirectory.h"
#include "_MMapDirectory.h"
#include "SimpleFSDirectory.h"
#include "_SimpleFSDirectory.h"
#include "MiscUtils.h"
#include "FileUtils.h"
#include "StringUtils.h"
namespace Lucene {
MMapDirectory::MMapDirectory(const String& path, const LockFactoryPtr& lockFactory) : FSDirectory(path, lockFactory) {
}
MMapDirectory::~MMapDirectory() {
}
IndexInputPtr MMapDirectory::openInput(const String& name, int32_t bufferSize) {
ensureOpen();
return newLucene<MMapIndexInput>(FileUtils::joinPath(directory, name));
}
IndexOutputPtr MMapDirectory::createOutput(const String& name) {
initOutput(name);
return newLucene<SimpleFSIndexOutput>(FileUtils::joinPath(directory, name));
}
MMapIndexInput::MMapIndexInput(const String& path) {
_length = path.empty() ? 0 : (int32_t)FileUtils::fileLength(path);
bufferPosition = 0;
if (!path.empty()) {
try {
file.open(boost::filesystem::path(path), _length);
} catch (...) {
boost::throw_exception(FileNotFoundException(path));
}
}
isClone = false;
}
MMapIndexInput::~MMapIndexInput() {
}
uint8_t MMapIndexInput::readByte() {
try {
return file.data()[bufferPosition++];
} catch (...) {
boost::throw_exception(IOException(L"Read past EOF"));
return 0;
}
}
void MMapIndexInput::readBytes(uint8_t* b, int32_t offset, int32_t length) {
try {
MiscUtils::arrayCopy(file.data(), bufferPosition, b, offset, length);
bufferPosition += length;
} catch (...) {
boost::throw_exception(IOException(L"Read past EOF"));
}
}
int64_t MMapIndexInput::getFilePointer() {
return bufferPosition;
}
void MMapIndexInput::seek(int64_t pos) {
bufferPosition = (int32_t)pos;
}
int64_t MMapIndexInput::length() {
return (int64_t)_length;
}
void MMapIndexInput::close() {
if (isClone || !file.is_open()) {
return;
}
_length = 0;
bufferPosition = 0;
file.close();
}
LuceneObjectPtr MMapIndexInput::clone(const LuceneObjectPtr& other) {
if (!file.is_open()) {
boost::throw_exception(AlreadyClosedException(L"MMapIndexInput already closed"));
}
LuceneObjectPtr clone = IndexInput::clone(other ? other : newLucene<MMapIndexInput>());
MMapIndexInputPtr cloneIndexInput(boost::dynamic_pointer_cast<MMapIndexInput>(clone));
cloneIndexInput->_length = _length;
cloneIndexInput->file = file;
cloneIndexInput->bufferPosition = bufferPosition;
cloneIndexInput->isClone = true;
return cloneIndexInput;
}
}
|