Skip to content

Commit 965b3ff

Browse files
committed
Implement ListIndexes
1 parent 593a843 commit 965b3ff

11 files changed

+63
-5
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ set(tests_SOURCES
221221
src/util/tests.cpp
222222
src/server/session_test.cpp
223223
src/server/http_test.cpp
224+
src/server/grpc/service_test.cpp
224225
)
225226

226227
install(

src/index/multi_index.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,21 @@ void MultiIndex::setThreadPool(QThreadPool *threadPool) {
2626
m_threadPool = threadPool;
2727
}
2828

29-
bool MultiIndex::indexExists(const QString &name) {
29+
QStringList MultiIndex::listIndexes() {
3030
QMutexLocker locker(&m_mutex);
31+
QStringList indexes;
32+
if (checkIndex(ROOT_INDEX_NAME)) {
33+
indexes.push_back(ROOT_INDEX_NAME);
34+
}
35+
for (auto subDir: m_dir->listDirectories()) {
36+
if (checkIndex(subDir)) {
37+
indexes.push_back(subDir);
38+
}
39+
}
40+
return indexes;
41+
}
42+
43+
bool MultiIndex::checkIndex(const QString &name) {
3144
if (m_indexes.contains(name)) {
3245
return true;
3346
}
@@ -38,6 +51,11 @@ bool MultiIndex::indexExists(const QString &name) {
3851
return Index::exists(subDir);
3952
}
4053

54+
bool MultiIndex::indexExists(const QString &name) {
55+
QMutexLocker locker(&m_mutex);
56+
return checkIndex(name);
57+
}
58+
4159
QSharedPointer<Index> MultiIndex::getRootIndex(bool create) {
4260
return getIndex(ROOT_INDEX_NAME, create);
4361
}
@@ -59,6 +77,10 @@ QSharedPointer<Index> MultiIndex::getIndex(const QString &name, bool create) {
5977
return index;
6078
}
6179

80+
void MultiIndex::createRootIndex() {
81+
getRootIndex(true);
82+
}
83+
6284
void MultiIndex::createIndex(const QString &name) {
6385
if (name == ROOT_INDEX_NAME) {
6486
throw NotImplemented("Changing the legacy root index is not supported");

src/index/multi_index.h

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <QSharedPointer>
1010
#include <QThreadPool>
1111
#include <QString>
12+
#include <vector>
1213

1314
#include "index.h"
1415
#include "store/directory.h"
@@ -26,15 +27,21 @@ class MultiIndex {
2627

2728
QSharedPointer<Directory> dir() const { return m_dir; }
2829

30+
QStringList listIndexes();
31+
2932
bool indexExists(const QString &name);
3033
QSharedPointer<Index> getRootIndex(bool create = false);
3134
QSharedPointer<Index> getIndex(const QString &name, bool create = false);
35+
void createRootIndex();
3236
void createIndex(const QString &name);
3337
void deleteIndex(const QString &name);
3438

39+
3540
constexpr static const char* ROOT_INDEX_NAME = "_root";
3641

3742
private:
43+
bool checkIndex(const QString &name);
44+
3845
QMutex m_mutex;
3946
QSharedPointer<Directory> m_dir;
4047
QMap<QString, QSharedPointer<Index>> m_indexes;

src/server/grpc/proto/index.proto

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ package fpindex;
44

55
import "google/api/annotations.proto";
66

7-
message IndexInfo {
7+
message IndexStatus {
88
string name = 1;
9+
bool ready = 2;
910
}
1011

1112
message GetAttributeRequest {
@@ -67,15 +68,15 @@ message GetIndexRequest {
6768
};
6869

6970
message GetIndexResponse {
70-
IndexInfo index = 1;
71+
IndexStatus index = 1;
7172
};
7273

7374
message CreateIndexRequest {
7475
string index_name = 1;
7576
};
7677

7778
message CreateIndexResponse {
78-
IndexInfo index = 1;
79+
IndexStatus index = 1;
7980
};
8081

8182
message DeleteIndexRequest {
@@ -89,7 +90,7 @@ message ListIndexesRequest {
8990
};
9091

9192
message ListIndexesResponse {
92-
repeated IndexInfo indexes = 1;
93+
repeated IndexStatus indexes = 1;
9394
};
9495

9596
service Index {

src/server/grpc/service.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ static inline int remainingTime(std::chrono::system_clock::time_point deadline)
1212
return std::chrono::duration_cast<std::chrono::milliseconds>(deadline - std::chrono::system_clock::now()).count();
1313
}
1414

15+
grpc::Status IndexServiceImpl::ListIndexes(grpc::ServerContext* context, const fpindex::ListIndexesRequest* request,
16+
fpindex::ListIndexesResponse* response) {
17+
qDebug() << "[gRPC] ListIndexes";
18+
auto indexNames = m_indexes->listIndexes();
19+
for (const auto& indexName : indexNames) {
20+
auto status = response->add_indexes();
21+
status->set_name(indexName.toStdString());
22+
}
23+
return grpc::Status::OK;
24+
}
25+
1526
grpc::Status IndexServiceImpl::GetIndex(grpc::ServerContext* context, const fpindex::GetIndexRequest* request,
1627
fpindex::GetIndexResponse* response) {
1728
auto indexName = QString::fromStdString(request->index_name());

src/server/grpc/service.h

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ class IndexServiceImpl final : public fpindex::Index::Service {
1313
public:
1414
IndexServiceImpl(QSharedPointer<MultiIndex> indexes, QSharedPointer<Metrics> metrics);
1515

16+
virtual ::grpc::Status ListIndexes(::grpc::ServerContext* context, const fpindex::ListIndexesRequest* request,
17+
fpindex::ListIndexesResponse* response) override;
18+
1619
virtual ::grpc::Status GetIndex(::grpc::ServerContext* context, const fpindex::GetIndexRequest* request,
1720
fpindex::GetIndexResponse* response) override;
1821

src/store/directory.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Directory {
3030
virtual InputStream *openFile(const QString &name) = 0;
3131
virtual void renameFile(const QString &oldName, const QString &newName) = 0;
3232
virtual QStringList listFiles() = 0;
33+
virtual QStringList listDirectories() = 0;
3334
virtual bool fileExists(const QString &name);
3435

3536
virtual Directory *openDirectory(const QString &name) = 0;

src/store/fs_directory.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ QStringList FSDirectory::listFiles() {
7979
return dir.entryList(QStringList(), QDir::Files);
8080
}
8181

82+
QStringList FSDirectory::listDirectories() {
83+
QMutexLocker locker(&m_mutex);
84+
QDir dir(m_path);
85+
return dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
86+
}
87+
8288
bool FSDirectory::fileExists(const QString &name) {
8389
QMutexLocker locker(&m_mutex);
8490
return QFile::exists(filePath(name));

src/store/fs_directory.h

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class FSDirectory : public Directory {
3030
virtual InputStream *openFile(const QString &name);
3131
virtual void renameFile(const QString &oldName, const QString &newName);
3232
QStringList listFiles();
33+
QStringList listDirectories();
3334
bool fileExists(const QString &name);
3435
virtual void sync(const QStringList &names);
3536

src/store/ram_directory.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ SQLiteDatabase RAMDirectory::openDatabase(const QString &name) {
7676
auto fileName = QString("file:%1?mode=memory&cache=shared").arg(m_dbPrefix + name);
7777
return SQLiteDatabase(fileName);
7878
}
79+
80+
QStringList RAMDirectory::listDirectories() {
81+
return m_data->directories.keys();
82+
}

src/store/ram_directory.h

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class RAMDirectory : public Directory {
3636
virtual InputStream *openFile(const QString &name);
3737
virtual void renameFile(const QString &oldName, const QString &newName);
3838
QStringList listFiles();
39+
QStringList listDirectories();
3940
bool fileExists(const QString &name);
4041

4142
virtual SQLiteDatabase openDatabase(const QString &name) override;

0 commit comments

Comments
 (0)