Skip to content

Commit

Permalink
[clangd] Allow consuming limited number of items
Browse files Browse the repository at this point in the history
This patch modifies `consume` function to allow retrieval of limited
number of symbols. This is the "cheap" implementation of top-level
limiting iterator. In the future we would like to have a complete limit
iterator implementation to insert it into the query subtrees, but in the
meantime this version would be enough for a fully-functional
proof-of-concept Dex implementation.

Reviewers: ioeric, ilya-biryukov

Reviewed by: ioeric

Differential Revision: https://reviews.llvm.org/D50500

llvm-svn: 339426
  • Loading branch information
kirillbobyrev committed Aug 10, 2018
1 parent 130b00b commit 0a75766
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
5 changes: 3 additions & 2 deletions clang-tools-extra/clangd/index/dex/Iterator.cpp
Expand Up @@ -218,9 +218,10 @@ class OrIterator : public Iterator {

} // end namespace

std::vector<DocID> consume(Iterator &It) {
std::vector<DocID> consume(Iterator &It, size_t Limit) {
std::vector<DocID> Result;
for (; !It.reachedEnd(); It.advance())
for (size_t Retrieved = 0; !It.reachedEnd() && Retrieved < Limit;
It.advance(), ++Retrieved)
Result.push_back(It.peek());
return Result;
}
Expand Down
7 changes: 4 additions & 3 deletions clang-tools-extra/clangd/index/dex/Iterator.h
Expand Up @@ -101,9 +101,10 @@ class Iterator {
virtual llvm::raw_ostream &dump(llvm::raw_ostream &OS) const = 0;
};

/// Exhausts given iterator and returns all processed DocIDs. The result
/// contains sorted DocumentIDs.
std::vector<DocID> consume(Iterator &It);
/// Advances the iterator until it is either exhausted or the number of
/// requested items is reached. The result contains sorted DocumentIDs.
std::vector<DocID> consume(Iterator &It,
size_t Limit = std::numeric_limits<size_t>::max());

/// Returns a document iterator over given PostingList.
std::unique_ptr<Iterator> create(PostingListRef Documents);
Expand Down
21 changes: 21 additions & 0 deletions clang-tools-extra/unittests/clangd/DexIndexTests.cpp
Expand Up @@ -240,6 +240,27 @@ TEST(DexIndexIterators, StringRepresentation) {
"(& (& [1, 3, 5, 8, 9] [1, 5, 7, 9]) (| [0, 5] [0, 1, 5] []))");
}

TEST(DexIndexIterators, Limit) {
const PostingList L0 = {4, 7, 8, 20, 42, 100};
const PostingList L1 = {1, 3, 5, 8, 9};
const PostingList L2 = {1, 5, 7, 9};
const PostingList L3 = {0, 5};
const PostingList L4 = {0, 1, 5};
const PostingList L5;

auto DocIterator = create(L0);
EXPECT_THAT(consume(*DocIterator, 42), ElementsAre(4, 7, 8, 20, 42, 100));

DocIterator = create(L0);
EXPECT_THAT(consume(*DocIterator), ElementsAre(4, 7, 8, 20, 42, 100));

DocIterator = create(L0);
EXPECT_THAT(consume(*DocIterator, 3), ElementsAre(4, 7, 8));

DocIterator = create(L0);
EXPECT_THAT(consume(*DocIterator, 0), ElementsAre());
}

testing::Matcher<std::vector<Token>>
trigramsAre(std::initializer_list<std::string> Trigrams) {
std::vector<Token> Tokens;
Expand Down

0 comments on commit 0a75766

Please sign in to comment.