Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clangd] Introduce PostingList interface
This patch abstracts `PostingList` interface and reuses existing implementation. It will be used later to test different `PostingList` representations. No functionality change is introduced, this patch is mostly refactoring so that the following patches could focus on functionality while not being too hard to review. Reviewed By: sammccall, ioeric Differential Revision: https://reviews.llvm.org/D51982 llvm-svn: 342155
- Loading branch information
1 parent
f29b36c
commit 249c586
Showing
8 changed files
with
216 additions
and
161 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
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
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,84 @@ | ||
//===--- PostingList.cpp - Symbol identifiers storage interface -----------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "PostingList.h" | ||
#include "Iterator.h" | ||
|
||
namespace clang { | ||
namespace clangd { | ||
namespace dex { | ||
|
||
namespace { | ||
|
||
/// Implements Iterator over std::vector<DocID>. This is the most basic | ||
/// iterator and is simply a wrapper around | ||
/// std::vector<DocID>::const_iterator. | ||
class PlainIterator : public Iterator { | ||
public: | ||
explicit PlainIterator(llvm::ArrayRef<DocID> Documents) | ||
: Documents(Documents), Index(std::begin(Documents)) {} | ||
|
||
bool reachedEnd() const override { return Index == std::end(Documents); } | ||
|
||
/// Advances cursor to the next item. | ||
void advance() override { | ||
assert(!reachedEnd() && | ||
"Posting List iterator can't advance() at the end."); | ||
++Index; | ||
} | ||
|
||
/// Applies binary search to advance cursor to the next item with DocID | ||
/// equal or higher than the given one. | ||
void advanceTo(DocID ID) override { | ||
assert(!reachedEnd() && | ||
"Posting List iterator can't advance() at the end."); | ||
// If current ID is beyond requested one, iterator is already in the right | ||
// state. | ||
if (peek() < ID) | ||
Index = std::lower_bound(Index, std::end(Documents), ID); | ||
} | ||
|
||
DocID peek() const override { | ||
assert(!reachedEnd() && | ||
"Posting List iterator can't peek() at the end."); | ||
return *Index; | ||
} | ||
|
||
float consume() override { | ||
assert(!reachedEnd() && | ||
"Posting List iterator can't consume() at the end."); | ||
return DEFAULT_BOOST_SCORE; | ||
} | ||
|
||
size_t estimateSize() const override { return Documents.size(); } | ||
|
||
private: | ||
llvm::raw_ostream &dump(llvm::raw_ostream &OS) const override { | ||
OS << '['; | ||
if (Index != std::end(Documents)) | ||
OS << *Index; | ||
else | ||
OS << "END"; | ||
OS << ']'; | ||
return OS; | ||
} | ||
|
||
llvm::ArrayRef<DocID> Documents; | ||
llvm::ArrayRef<DocID>::const_iterator Index; | ||
}; | ||
|
||
} // namespace | ||
|
||
std::unique_ptr<Iterator> PostingList::iterator() const { | ||
return llvm::make_unique<PlainIterator>(Documents); | ||
} | ||
|
||
} // namespace dex | ||
} // namespace clangd | ||
} // namespace clang |
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,52 @@ | ||
//===--- PostingList.h - Symbol identifiers storage interface --*- C++ -*-===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This defines posting list interface: a storage for identifiers of symbols | ||
// which can be characterized by a specific feature (such as fuzzy-find trigram, | ||
// scope, type or any other Search Token). Posting lists can be traversed in | ||
// order using an iterator and are values for inverted index, which maps search | ||
// tokens to corresponding posting lists. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_DEX_POSTINGLIST_H | ||
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_DEX_POSTINGLIST_H | ||
|
||
#include "Iterator.h" | ||
#include "llvm/ADT/ArrayRef.h" | ||
#include <cstdint> | ||
#include <vector> | ||
|
||
namespace clang { | ||
namespace clangd { | ||
namespace dex { | ||
|
||
class Iterator; | ||
|
||
/// PostingList is the storage of DocIDs which can be inserted to the Query | ||
/// Tree as a leaf by constructing Iterator over the PostingList object. | ||
// FIXME(kbobyrev): Use VByte algorithm to compress underlying data. | ||
class PostingList { | ||
public: | ||
explicit PostingList(const std::vector<DocID> &&Documents) | ||
: Documents(std::move(Documents)) {} | ||
|
||
std::unique_ptr<Iterator> iterator() const; | ||
|
||
size_t bytes() const { return Documents.size() * sizeof(DocID); } | ||
|
||
private: | ||
const std::vector<DocID> Documents; | ||
}; | ||
|
||
} // namespace dex | ||
} // namespace clangd | ||
} // namespace clang | ||
|
||
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_DEX_POSTINGLIST_H |
Oops, something went wrong.