Skip to content

Commit 42cad35

Browse files
committed
Merge branch 'main' into v2
2 parents d6a68c5 + f4f5a0a commit 42cad35

6 files changed

+55
-19
lines changed

src/index/index_reader.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ std::vector<SearchResult> IndexReader::search(const std::vector<uint32_t> &terms
7171
const SegmentInfo& s = segments.at(i);
7272
SegmentSearcher searcher(s.index(), segmentDataReader(s), s.lastKey());
7373
segmentHits.clear();
74-
searcher.search(sortedTerms.data(), sortedTerms.size(), segmentHits);
74+
searcher.search(sortedTerms, segmentHits);
7575
auto segmentId = s.id();
7676
for (auto hit : segmentHits) {
7777
auto docId = hit.first;

src/index/index_reader_test.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2011 Lukas Lalinsky
2+
// Distributed under the MIT license, see the LICENSE file for details.
3+
4+
#include <gtest/gtest.h>
5+
#include "util/test_utils.h"
6+
#include "store/ram_directory.h"
7+
#include "store/input_stream.h"
8+
#include "store/output_stream.h"
9+
#include "top_hits_collector.h"
10+
#include "index.h"
11+
#include "index_writer.h"
12+
#include "index_reader.h"
13+
14+
using namespace Acoustid;
15+
16+
TEST(IndexReaderTest, Search)
17+
{
18+
DirectorySharedPtr dir(new RAMDirectory());
19+
IndexSharedPtr index(new Index(dir, true));
20+
21+
std::vector<uint32_t> fp1 = { 7, 9, 12 };
22+
std::vector<uint32_t> fp2 = { 7, 9, 11 };
23+
24+
{
25+
auto writer = index->openWriter();
26+
writer->addDocument(1, fp1.data(), fp1.size());
27+
writer->commit();
28+
writer->addDocument(2, fp2.data(), fp2.size());
29+
writer->commit();
30+
}
31+
32+
{
33+
IndexReader reader(index);
34+
auto results = reader.search(fp1);
35+
ASSERT_EQ(2, results.size());
36+
ASSERT_EQ(1, results.at(0).docId());
37+
ASSERT_EQ(3, results.at(0).score());
38+
ASSERT_EQ(2, results.at(1).docId());
39+
ASSERT_EQ(2, results.at(1).score());
40+
}
41+
}
42+

src/index/search_result.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,7 @@ class SearchResult {
2929
inline void sortSearchResults(std::vector<SearchResult> &results)
3030
{
3131
std::sort(results.begin(), results.end(), [](const SearchResult &a, const SearchResult &b) {
32-
if (a.score() > b.score()) {
33-
return true;
34-
} else if (a.score() < b.score()) {
35-
return false;
36-
} else {
37-
return a.docId() < b.docId();
38-
}
32+
return a.score() > b.score() || (a.score() == b.score() && a.docId() < b.docId());
3933
});
4034
}
4135

src/index/segment_searcher.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ SegmentSearcher::~SegmentSearcher()
1616
{
1717
}
1818

19-
void SegmentSearcher::search(uint32_t *fingerprint, size_t length, std::unordered_map<uint32_t, int> &hits)
19+
void SegmentSearcher::search(const std::vector<uint32_t> &hashes, std::unordered_map<uint32_t, int> &hits)
2020
{
2121
size_t i = 0, block = 0, lastBlock = SIZE_MAX;
22-
while (i < length) {
22+
while (i < hashes.size()) {
2323
if (block > lastBlock || lastBlock == SIZE_MAX) {
2424
size_t localFirstBlock, localLastBlock;
25-
if (fingerprint[i] > m_lastKey) {
25+
if (hashes[i] > m_lastKey) {
2626
// All following items are larger than the last segment's key.
2727
return;
2828
}
29-
if (m_index->search(fingerprint[i], &localFirstBlock, &localLastBlock)) {
29+
if (m_index->search(hashes[i], &localFirstBlock, &localLastBlock)) {
3030
if (block > localLastBlock) {
3131
// We already searched this block and the fingerprint item was not found.
3232
i++;
@@ -47,18 +47,18 @@ void SegmentSearcher::search(uint32_t *fingerprint, size_t length, std::unordere
4747
std::unique_ptr<BlockDataIterator> blockData(m_dataReader->readBlock(block, firstKey));
4848
while (blockData->next()) {
4949
uint32_t key = blockData->key();
50-
if (key >= fingerprint[i]) {
51-
while (key > fingerprint[i]) {
50+
if (key >= hashes[i]) {
51+
while (key > hashes[i]) {
5252
i++;
53-
if (i >= length) {
53+
if (i >= hashes.size()) {
5454
return;
5555
}
56-
else if (lastKey < fingerprint[i]) {
56+
else if (lastKey < hashes[i]) {
5757
// There are no longer any items in this block that we could match.
5858
goto nextBlock;
5959
}
6060
}
61-
if (key == fingerprint[i]) {
61+
if (key == hashes[i]) {
6262
auto docId = blockData->value();
6363
hits[docId]++;
6464
}

src/index/segment_searcher.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class SegmentSearcher
1717
SegmentSearcher(SegmentIndexSharedPtr index, SegmentDataReader *dataReader, uint32_t lastKey = UINT32_MAX);
1818
virtual ~SegmentSearcher();
1919

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

2222
private:
2323
SegmentIndexSharedPtr m_index;

src/server/session.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include <QMutex>
88
#include <QSharedPointer>
99
#include <memory>
10+
#include "index/search_result.h"
1011

1112
namespace Acoustid {
1213

1314
class Index;
1415
class IndexWriter;
15-
class SearchResult;
1616
class OpBatch;
1717

1818
namespace Server {

0 commit comments

Comments
 (0)