12
12
#include " knowhere/index/index.h"
13
13
14
14
#include " fmt/format.h"
15
+ #include " folly/futures/Future.h"
16
+ #include " knowhere/comp/thread_pool.h"
15
17
#include " knowhere/comp/time_recorder.h"
16
18
#include " knowhere/dataset.h"
17
19
#include " knowhere/expected.h"
@@ -34,6 +36,40 @@ LoadConfig(BaseConfig* cfg, const Json& json, knowhere::PARAM_TYPE param_type, c
34
36
return Config::Load (*cfg, json_, param_type, msg);
35
37
}
36
38
39
+ #ifdef KNOWHERE_WITH_CARDINAL
40
+ template <typename T>
41
+ inline const std::shared_ptr<Interrupt>
42
+ Index<T>::BuildAsync(const DataSetPtr dataset, const Json& json, const std::chrono::seconds timeout) {
43
+ auto pool = ThreadPool::GetGlobalBuildThreadPool ();
44
+ auto interrupt = std::make_shared<Interrupt>(timeout);
45
+ interrupt->Set (pool->push ([this , dataset, &json, &interrupt]() {
46
+ auto cfg = this ->node ->CreateConfig ();
47
+ RETURN_IF_ERROR (LoadConfig (cfg.get (), json, knowhere::TRAIN, " Build" ));
48
+
49
+ #if defined(NOT_COMPILE_FOR_SWIG) && !defined(KNOWHERE_WITH_LIGHT)
50
+ TimeRecorder rc (" BuildAsync index " , 2 );
51
+ auto res = this ->node ->BuildAsync (dataset, std::move (cfg), interrupt.get ());
52
+ auto time = rc.ElapseFromBegin (" done" );
53
+ time *= 0.000001 ; // convert to s
54
+ knowhere_build_latency.Observe (time );
55
+ #else
56
+ auto res = this ->node ->BuildAsync (dataset, std::move (cfg), Interrupt.get ());
57
+ #endif
58
+ return res;
59
+ }));
60
+ return interrupt;
61
+ }
62
+ #else
63
+ template <typename T>
64
+ inline const std::shared_ptr<Interrupt>
65
+ Index<T>::BuildAsync(const DataSetPtr dataset, const Json& json) {
66
+ auto pool = ThreadPool::GetGlobalBuildThreadPool ();
67
+ auto interrupt = std::make_shared<Interrupt>();
68
+ interrupt->Set (pool->push ([this , &dataset, &json]() { return this ->Build (dataset, json); }));
69
+ return interrupt;
70
+ }
71
+ #endif
72
+
37
73
template <typename T>
38
74
inline Status
39
75
Index<T>::Build(const DataSetPtr dataset, const Json& json) {
0 commit comments