Skip to content

Commit

Permalink
Merge branch 'main' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
lalinsky committed Feb 25, 2024
2 parents d6a68c5 + f4f5a0a commit 42cad35
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/index/index_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ std::vector<SearchResult> IndexReader::search(const std::vector<uint32_t> &terms
const SegmentInfo& s = segments.at(i);
SegmentSearcher searcher(s.index(), segmentDataReader(s), s.lastKey());
segmentHits.clear();
searcher.search(sortedTerms.data(), sortedTerms.size(), segmentHits);
searcher.search(sortedTerms, segmentHits);
auto segmentId = s.id();
for (auto hit : segmentHits) {
auto docId = hit.first;
Expand Down
42 changes: 42 additions & 0 deletions src/index/index_reader_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (C) 2011 Lukas Lalinsky
// Distributed under the MIT license, see the LICENSE file for details.

#include <gtest/gtest.h>
#include "util/test_utils.h"
#include "store/ram_directory.h"
#include "store/input_stream.h"
#include "store/output_stream.h"
#include "top_hits_collector.h"
#include "index.h"
#include "index_writer.h"
#include "index_reader.h"

using namespace Acoustid;

TEST(IndexReaderTest, Search)
{
DirectorySharedPtr dir(new RAMDirectory());
IndexSharedPtr index(new Index(dir, true));

std::vector<uint32_t> fp1 = { 7, 9, 12 };
std::vector<uint32_t> fp2 = { 7, 9, 11 };

{
auto writer = index->openWriter();
writer->addDocument(1, fp1.data(), fp1.size());
writer->commit();
writer->addDocument(2, fp2.data(), fp2.size());
writer->commit();
}

{
IndexReader reader(index);
auto results = reader.search(fp1);
ASSERT_EQ(2, results.size());
ASSERT_EQ(1, results.at(0).docId());
ASSERT_EQ(3, results.at(0).score());
ASSERT_EQ(2, results.at(1).docId());
ASSERT_EQ(2, results.at(1).score());
}
}

8 changes: 1 addition & 7 deletions src/index/search_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,7 @@ class SearchResult {
inline void sortSearchResults(std::vector<SearchResult> &results)
{
std::sort(results.begin(), results.end(), [](const SearchResult &a, const SearchResult &b) {
if (a.score() > b.score()) {
return true;
} else if (a.score() < b.score()) {
return false;
} else {
return a.docId() < b.docId();
}
return a.score() > b.score() || (a.score() == b.score() && a.docId() < b.docId());
});
}

Expand Down
18 changes: 9 additions & 9 deletions src/index/segment_searcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ SegmentSearcher::~SegmentSearcher()
{
}

void SegmentSearcher::search(uint32_t *fingerprint, size_t length, std::unordered_map<uint32_t, int> &hits)
void SegmentSearcher::search(const std::vector<uint32_t> &hashes, std::unordered_map<uint32_t, int> &hits)
{
size_t i = 0, block = 0, lastBlock = SIZE_MAX;
while (i < length) {
while (i < hashes.size()) {
if (block > lastBlock || lastBlock == SIZE_MAX) {
size_t localFirstBlock, localLastBlock;
if (fingerprint[i] > m_lastKey) {
if (hashes[i] > m_lastKey) {
// All following items are larger than the last segment's key.
return;
}
if (m_index->search(fingerprint[i], &localFirstBlock, &localLastBlock)) {
if (m_index->search(hashes[i], &localFirstBlock, &localLastBlock)) {
if (block > localLastBlock) {
// We already searched this block and the fingerprint item was not found.
i++;
Expand All @@ -47,18 +47,18 @@ void SegmentSearcher::search(uint32_t *fingerprint, size_t length, std::unordere
std::unique_ptr<BlockDataIterator> blockData(m_dataReader->readBlock(block, firstKey));
while (blockData->next()) {
uint32_t key = blockData->key();
if (key >= fingerprint[i]) {
while (key > fingerprint[i]) {
if (key >= hashes[i]) {
while (key > hashes[i]) {
i++;
if (i >= length) {
if (i >= hashes.size()) {
return;
}
else if (lastKey < fingerprint[i]) {
else if (lastKey < hashes[i]) {
// There are no longer any items in this block that we could match.
goto nextBlock;
}
}
if (key == fingerprint[i]) {
if (key == hashes[i]) {
auto docId = blockData->value();
hits[docId]++;
}
Expand Down
2 changes: 1 addition & 1 deletion src/index/segment_searcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SegmentSearcher
SegmentSearcher(SegmentIndexSharedPtr index, SegmentDataReader *dataReader, uint32_t lastKey = UINT32_MAX);
virtual ~SegmentSearcher();

void search(uint32_t *fingerprint, size_t length, std::unordered_map<uint32_t, int> &hits);
void search(const std::vector<uint32_t> &hashes, std::unordered_map<uint32_t, int> &hits);

private:
SegmentIndexSharedPtr m_index;
Expand Down
2 changes: 1 addition & 1 deletion src/server/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
#include <QMutex>
#include <QSharedPointer>
#include <memory>
#include "index/search_result.h"

namespace Acoustid {

class Index;
class IndexWriter;
class SearchResult;
class OpBatch;

namespace Server {
Expand Down

0 comments on commit 42cad35

Please sign in to comment.