-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,796 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// keyvi - A key value store. | ||
// | ||
// Copyright 2015 Hendrik Muhs<hendrik.muhs@gmail.com> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
/* | ||
* index_reader.h | ||
* | ||
* Created on: Jan 11, 2017 | ||
* Author: hendrik | ||
*/ | ||
|
||
#ifndef KEYVI_INDEX_INDEX_READER_H_ | ||
#define KEYVI_INDEX_INDEX_READER_H_ | ||
|
||
#include <string> | ||
|
||
#include "index/internal/base_index_reader.h" | ||
#include "index/internal/index_reader_worker.h" | ||
|
||
// #define ENABLE_TRACING | ||
#include "dictionary/util/trace.h" | ||
|
||
namespace keyvi { | ||
namespace index { | ||
|
||
class IndexReader final : public internal::BaseIndexReader<internal::IndexReaderWorker> { | ||
public: | ||
explicit IndexReader(const std::string index_directory, size_t refresh_interval = 1 /*, optional external logger*/) | ||
: worker_(index_directory, refresh_interval), BaseIndexReader(worker_) { | ||
worker_.StartWorkerThread(); | ||
} | ||
|
||
~IndexReader() { worker_.StopWorkerThread(); } | ||
|
||
void Reload() { worker_.Reload(); } | ||
|
||
private: | ||
internal::IndexReaderWorker worker_; | ||
}; | ||
} /* namespace index */ | ||
} /* namespace keyvi */ | ||
|
||
#endif // KEYVI_INDEX_INDEX_READER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// | ||
// keyvi - A key value store. | ||
// | ||
// Copyright 2015 Hendrik Muhs<hendrik.muhs@gmail.com> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
/* | ||
* index_writer.h | ||
* | ||
* Created on: Jan 11, 2017 | ||
* Author: hendrik | ||
*/ | ||
|
||
#ifndef KEYVI_INDEX_INDEX_WRITER_H_ | ||
#define KEYVI_INDEX_INDEX_WRITER_H_ | ||
|
||
#include <algorithm> | ||
#include <atomic> | ||
#include <chrono> //NOLINT | ||
#include <condition_variable> //NOLINT | ||
#include <ctime> | ||
#include <string> | ||
#include <thread> //NOLINT | ||
#include <vector> | ||
|
||
#include <boost/filesystem.hpp> | ||
#include <boost/filesystem/operations.hpp> | ||
#include <boost/interprocess/sync/file_lock.hpp> | ||
#include <boost/property_tree/json_parser.hpp> | ||
#include <boost/property_tree/ptree.hpp> | ||
|
||
#include "dictionary/dictionary.h" | ||
#include "dictionary/dictionary_compiler.h" | ||
#include "dictionary/dictionary_types.h" | ||
#include "dictionary/fsa/internal/serialization_utils.h" | ||
#include "dictionary/match.h" | ||
#include "index/internal/base_index_reader.h" | ||
#include "index/internal/index_writer_worker.h" | ||
|
||
#define ENABLE_TRACING | ||
#include "dictionary/util/trace.h" | ||
|
||
namespace keyvi { | ||
namespace index { | ||
|
||
class IndexWriter final : public internal::BaseIndexReader<internal::IndexWriterWorker> { | ||
public: | ||
explicit IndexWriter(const std::string& index_directory) | ||
: index_finalizer_(index_directory), BaseIndexReader(index_finalizer_) { | ||
index_directory_ = index_directory; | ||
|
||
index_toc_file_ = index_directory_; | ||
index_toc_file_ /= "index.toc"; | ||
|
||
// lock the index (filesystem lock) | ||
boost::filesystem::path index_lock_file = index_directory_; | ||
|
||
// create dir if it does not exist yet | ||
boost::filesystem::create_directories(index_directory_); | ||
|
||
index_lock_file /= "index.lock"; | ||
|
||
TRACE("locking index %s", index_lock_file.string().c_str()); | ||
|
||
std::ofstream o(index_lock_file.string(), std::ios_base::app); | ||
|
||
index_lock_ = boost::interprocess::file_lock(index_lock_file.string().c_str()); | ||
index_lock_.lock(); | ||
} | ||
|
||
~IndexWriter() { | ||
// todo: happens to early, move into own class, destruct after worker is destructed | ||
TRACE("Unlock Index"); | ||
try { | ||
index_lock_.unlock(); | ||
} catch (std::exception& e) { | ||
TRACE("exception %s", e.what()); | ||
} | ||
} | ||
|
||
void Set(const std::string& key, const std::string& value) { index_finalizer_.Add(key, value); } | ||
|
||
void Delete(const std::string& key) { index_finalizer_.Delete(key); } | ||
|
||
void Flush(bool async = true) { | ||
TRACE("Flush (manually)"); | ||
index_finalizer_.Flush(async); | ||
} | ||
|
||
private: | ||
boost::filesystem::path index_directory_; | ||
boost::filesystem::path index_toc_file_; | ||
boost::interprocess::file_lock index_lock_; | ||
internal::IndexWriterWorker index_finalizer_; | ||
}; | ||
|
||
} /* namespace index */ | ||
} /* namespace keyvi */ | ||
|
||
#endif // KEYVI_INDEX_INDEX_WRITER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// keyvi - A key value store. | ||
// | ||
// Copyright 2015 Hendrik Muhs<hendrik.muhs@gmail.com> | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
/* | ||
* base_index_reader.h | ||
* | ||
* Created on: Nov 1, 2017 | ||
* Author: hendrik | ||
*/ | ||
|
||
#ifndef KEYVI_INDEX_INTERNAL_BASE_INDEX_READER_H_ | ||
#define KEYVI_INDEX_INTERNAL_BASE_INDEX_READER_H_ | ||
|
||
#include <string> | ||
|
||
#include "dictionary/match.h" | ||
#include "index/internal/segment.h" | ||
|
||
namespace keyvi { | ||
namespace index { | ||
namespace internal { | ||
|
||
template <class SegmentHolderT> | ||
class BaseIndexReader { | ||
public: | ||
explicit BaseIndexReader(const SegmentHolderT& segementHolder) : holder_(segementHolder) {} | ||
|
||
dictionary::Match operator[](const std::string& key) const { | ||
dictionary::Match m; | ||
segments_t segments = holder_.Segments(); | ||
|
||
for (auto it = segments->crbegin(); it != segments->crend(); ++it) { | ||
m = (*it)->GetDictionary()->operator[](key); | ||
if (!m.IsEmpty()) { | ||
return m; | ||
} | ||
} | ||
|
||
return m; | ||
} | ||
|
||
bool Contains(const std::string& key) const { | ||
segments_t segments = holder_.Segments(); | ||
for (auto it = segments->crbegin(); it != segments->crend(); it++) { | ||
if ((*it)->GetDictionary()->Contains(key)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private: | ||
const SegmentHolderT& holder_; | ||
}; | ||
|
||
} /* namespace internal */ | ||
} /* namespace index */ | ||
} /* namespace keyvi */ | ||
|
||
#endif // KEYVI_INDEX_INTERNAL_BASE_INDEX_READER_H_ |
Oops, something went wrong.