Skip to content

Commit

Permalink
Changed the getQec function and the respective makeTestIndex to take …
Browse files Browse the repository at this point in the history
…in the wordsFileContent and docsFileContent as pair contentsOfWordsFileAndDocsFile
  • Loading branch information
Flixtastic committed Dec 5, 2024
1 parent c62a7e6 commit 89f0b27
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 37 deletions.
19 changes: 11 additions & 8 deletions test/engine/TextIndexScanForWordTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,12 @@ std::string secondDocText =
std::string docsFileContent = h::createDocsFileLine(4, firstDocText) +
h::createDocsFileLine(7, secondDocText);

std::pair<std::string, std::string> contentsOfWordsFileAndDocsFile = {
wordsFileContent, docsFileContent};

TEST(TextIndexScanForWord, WordScanPrefix) {
auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent,
docsFileContent);
auto qec = getQec(kg, true, true, true, 16_B, true, true,
contentsOfWordsFileAndDocsFile);

TextIndexScanForWord s1{qec, Variable{"?text1"}, "test*"};
TextIndexScanForWord s2{qec, Variable{"?text2"}, "test*"};
Expand Down Expand Up @@ -171,8 +174,8 @@ TEST(TextIndexScanForWord, WordScanPrefix) {
}

TEST(TextIndexScanForWord, WordScanBasic) {
auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent,
docsFileContent);
auto qec = getQec(kg, true, true, true, 16_B, true, true,
contentsOfWordsFileAndDocsFile);

TextIndexScanForWord s1{qec, Variable{"?text1"}, "test"};

Expand Down Expand Up @@ -210,8 +213,8 @@ TEST(TextIndexScanForWord, WordScanBasic) {
}

TEST(TextIndexScanForWord, CacheKey) {
auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent,
docsFileContent);
auto qec = getQec(kg, true, true, true, 16_B, true, true,
contentsOfWordsFileAndDocsFile);

TextIndexScanForWord s1{qec, Variable{"?text1"}, "test*"};
TextIndexScanForWord s2{qec, Variable{"?text2"}, "test*"};
Expand All @@ -234,8 +237,8 @@ TEST(TextIndexScanForWord, CacheKey) {
}

TEST(TextIndexScanForWord, KnownEmpty) {
auto qec = getQec(kg, true, true, true, 16_B, true, true, wordsFileContent,
docsFileContent);
auto qec = getQec(kg, true, true, true, 16_B, true, true,
contentsOfWordsFileAndDocsFile);

TextIndexScanForWord s1{qec, Variable{"?text1"}, "nonExistentWord*"};
ASSERT_TRUE(s1.knownEmptyResult());
Expand Down
49 changes: 24 additions & 25 deletions test/util/IndexTestHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ Index makeTestIndex(const std::string& indexBasename,
[[maybe_unused]] bool usePrefixCompression,
ad_utility::MemorySize blocksizePermutations,
bool createTextIndex, bool addWordsFromLiterals,
std::optional<std::string> wordsFileContent,
std::optional<std::string> docsFileContent) {
std::optional<std::pair<std::string, std::string>>
contentsOfWordsFileAndDocsFile) {
// Ignore the (irrelevant) log output of the index building and loading during
// these tests.
static std::ostringstream ignoreLogStream;
Expand Down Expand Up @@ -188,15 +188,15 @@ Index makeTestIndex(const std::string& indexBasename,
std::nullopt};
index.createFromFiles({spec});
if (createTextIndex) {
if (wordsFileContent.has_value() && docsFileContent.has_value()) {
if (contentsOfWordsFileAndDocsFile.has_value()) {
// Create and write to words- and docsfile to later build a full text
// index from them
ad_utility::File wordsFile(indexBasename + ".wordsfile", "w");
ad_utility::File docsFile(indexBasename + ".docsfile", "w");
wordsFile.write(wordsFileContent.value().c_str(),
wordsFileContent.value().size());
docsFile.write(docsFileContent.value().c_str(),
docsFileContent.value().size());
wordsFile.write(contentsOfWordsFileAndDocsFile.value().first.c_str(),
contentsOfWordsFileAndDocsFile.value().first.size());
docsFile.write(contentsOfWordsFileAndDocsFile.value().second.c_str(),
contentsOfWordsFileAndDocsFile.value().second.size());
wordsFile.close();
docsFile.close();
index.setKbName(indexBasename);
Expand Down Expand Up @@ -246,8 +246,8 @@ QueryExecutionContext* getQec(std::optional<std::string> turtleInput,
bool usePrefixCompression,
ad_utility::MemorySize blocksizePermutations,
bool createTextIndex, bool addWordsFromLiterals,
std::optional<std::string> wordsFileContent,
std::optional<std::string> docsFileContent) {
std::optional<std::pair<std::string, std::string>>
contentsOfWordsFileAndDocsFile) {
// Similar to `absl::Cleanup`. Calls the `callback_` in the destructor, but
// the callback is stored as a `std::function`, which allows to store
// different types of callbacks in the same wrapper type.
Expand Down Expand Up @@ -294,22 +294,21 @@ QueryExecutionContext* getQec(std::optional<std::string> turtleInput,
std::string testIndexBasename =
"_staticGlobalTestIndex" + std::to_string(contextMap.size());
contextMap.emplace(
key,
Context{TypeErasedCleanup{[testIndexBasename]() {
for (const std::string& indexFilename :
getAllIndexFilenames(testIndexBasename)) {
// Don't log when a file can't be deleted,
// because the logging might already be
// destroyed.
ad_utility::deleteFile(indexFilename, false);
}
}},
std::make_unique<Index>(makeTestIndex(
testIndexBasename, turtleInput, loadAllPermutations,
usePatterns, usePrefixCompression, blocksizePermutations,
createTextIndex, addWordsFromLiterals, wordsFileContent,
docsFileContent)),
std::make_unique<QueryResultCache>()});
key, Context{TypeErasedCleanup{[testIndexBasename]() {
for (const std::string& indexFilename :
getAllIndexFilenames(testIndexBasename)) {
// Don't log when a file can't be deleted,
// because the logging might already be
// destroyed.
ad_utility::deleteFile(indexFilename, false);
}
}},
std::make_unique<Index>(makeTestIndex(
testIndexBasename, turtleInput, loadAllPermutations,
usePatterns, usePrefixCompression,
blocksizePermutations, createTextIndex,
addWordsFromLiterals, contentsOfWordsFileAndDocsFile)),
std::make_unique<QueryResultCache>()});
}
auto* qec = contextMap.at(key).qec_.get();
qec->getIndex().getImpl().setGlobalIndexAndComparatorOnlyForTesting();
Expand Down
8 changes: 4 additions & 4 deletions test/util/IndexTestHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ Index makeTestIndex(const std::string& indexBasename,
ad_utility::MemorySize blocksizePermutations = 16_B,
bool createTextIndex = false,
bool addWordsFromLiterals = true,
std::optional<std::string> wordsFileContent = std::nullopt,
std::optional<std::string> docsFileContent = std::nullopt);
std::optional<std::pair<std::string, std::string>>
contentsOfWordsFileAndDocsfile = std::nullopt);

// Return a static `QueryExecutionContext` that refers to an index that was
// build using `makeTestIndex` (see above). The index (most notably its
Expand All @@ -59,8 +59,8 @@ QueryExecutionContext* getQec(
bool usePrefixCompression = true,
ad_utility::MemorySize blocksizePermutations = 16_B,
bool createTextIndex = false, bool addWordsFromLiterals = true,
std::optional<std::string> wordsFileContent = std::nullopt,
std::optional<std::string> docsFileContent = std::nullopt);
std::optional<std::pair<std::string, std::string>>
contentsOfWordsFileAndDocsfile = std::nullopt);

// Return a lambda that takes a string and converts it into an ID by looking
// it up in the vocabulary of `index`. An `AD_CONTRACT_CHECK` will fail if the
Expand Down

0 comments on commit 89f0b27

Please sign in to comment.