Skip to content

Commit 4a192cf

Browse files
committed
fix notebook and notebook manager
1 parent 2ba85be commit 4a192cf

File tree

11 files changed

+270
-286
lines changed

11 files changed

+270
-286
lines changed

cli/notebook_cmd.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ int NotebookCommand::getProps(const ParsedArgs &args) {
208208
}
209209

210210
char *properties = nullptr;
211-
err = vxcore_notebook_get_properties(ctx, id.c_str(), &properties);
211+
err = vxcore_notebook_get_config(ctx, id.c_str(), &properties);
212212

213213
if (err != VXCORE_OK) {
214214
std::cerr << "Error: {" << id << "} " << vxcore_error_message(err) << std::endl;
@@ -250,7 +250,7 @@ int NotebookCommand::setProps(const ParsedArgs &args) {
250250
return 1;
251251
}
252252

253-
err = vxcore_notebook_set_properties(ctx, id.c_str(), propertiesJson.c_str());
253+
err = vxcore_notebook_update_config(ctx, id.c_str(), propertiesJson.c_str());
254254

255255
if (err != VXCORE_OK) {
256256
std::cerr << "Error: " << vxcore_error_message(err) << std::endl;

include/vxcore/vxcore.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ VXCORE_API VxCoreError vxcore_context_get_session_config(VxCoreContextHandle con
3535
char **out_json);
3636

3737
VXCORE_API VxCoreError vxcore_notebook_create(VxCoreContextHandle context, const char *path,
38-
const char *properties_json, VxCoreNotebookType type,
38+
const char *config_json, VxCoreNotebookType type,
3939
char **out_notebook_id);
4040

4141
VXCORE_API VxCoreError vxcore_notebook_open(VxCoreContextHandle context, const char *path,
@@ -45,13 +45,12 @@ VXCORE_API VxCoreError vxcore_notebook_close(VxCoreContextHandle context, const
4545

4646
VXCORE_API VxCoreError vxcore_notebook_list(VxCoreContextHandle context, char **out_notebooks_json);
4747

48-
VXCORE_API VxCoreError vxcore_notebook_get_properties(VxCoreContextHandle context,
49-
const char *notebook_id,
50-
char **out_properties_json);
48+
VXCORE_API VxCoreError vxcore_notebook_get_config(VxCoreContextHandle context,
49+
const char *notebook_id, char **out_config_json);
5150

52-
VXCORE_API VxCoreError vxcore_notebook_set_properties(VxCoreContextHandle context,
53-
const char *notebook_id,
54-
const char *properties_json);
51+
VXCORE_API VxCoreError vxcore_notebook_update_config(VxCoreContextHandle context,
52+
const char *notebook_id,
53+
const char *config_json);
5554

5655
VXCORE_API VxCoreError vxcore_folder_create(VxCoreContextHandle context, const char *notebook_id,
5756
const char *parent_path, const char *folder_name,

src/api/vxcore_api.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ VXCORE_API VxCoreError vxcore_context_create(const char *config_json,
7373
return err;
7474
}
7575

76-
ctx->notebook_manager = std::make_unique<vxcore::NotebookManager>(
77-
ctx->config_manager->GetLocalDataPath(), &ctx->config_manager->GetSessionConfig());
76+
ctx->notebook_manager =
77+
std::make_unique<vxcore::NotebookManager>(&ctx->config_manager->GetSessionConfig());
7878

7979
ctx->notebook_manager->SetSessionConfigUpdater([ctx]() {
8080
if (ctx && ctx->config_manager) {

src/api/vxcore_notebook_api.cpp

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "vxcore/vxcore.h"
88

99
VXCORE_API VxCoreError vxcore_notebook_create(VxCoreContextHandle context, const char *path,
10-
const char *properties_json, VxCoreNotebookType type,
10+
const char *config_json, VxCoreNotebookType type,
1111
char **out_notebook_id) {
1212
if (!context || !path || !out_notebook_id) {
1313
return VXCORE_ERR_NULL_POINTER;
@@ -19,11 +19,11 @@ VXCORE_API VxCoreError vxcore_notebook_create(VxCoreContextHandle context, const
1919
vxcore::NotebookType notebook_type =
2020
(type == VXCORE_NOTEBOOK_RAW) ? vxcore::NotebookType::Raw : vxcore::NotebookType::Bundled;
2121

22-
std::string properties_str = properties_json ? properties_json : "";
22+
std::string config_str = config_json ? config_json : "";
2323
std::string notebook_id;
2424

2525
VxCoreError err =
26-
ctx->notebook_manager->CreateNotebook(path, notebook_type, properties_str, notebook_id);
26+
ctx->notebook_manager->CreateNotebook(path, notebook_type, config_str, notebook_id);
2727

2828
if (err != VXCORE_OK) {
2929
ctx->last_error = "Failed to create notebook";
@@ -122,55 +122,54 @@ VXCORE_API VxCoreError vxcore_notebook_list(VxCoreContextHandle context,
122122
}
123123
}
124124

125-
VXCORE_API VxCoreError vxcore_notebook_get_properties(VxCoreContextHandle context,
126-
const char *notebook_id,
127-
char **out_properties_json) {
128-
if (!context || !notebook_id || !out_properties_json) {
125+
VXCORE_API VxCoreError vxcore_notebook_get_config(VxCoreContextHandle context,
126+
const char *notebook_id, char **out_config_json) {
127+
if (!context || !notebook_id || !out_config_json) {
129128
return VXCORE_ERR_NULL_POINTER;
130129
}
131130

132131
auto *ctx = reinterpret_cast<vxcore::VxCoreContext *>(context);
133132

134133
try {
135-
std::string properties_json;
136-
VxCoreError err = ctx->notebook_manager->GetNotebookProperties(notebook_id, properties_json);
134+
std::string config_json;
135+
VxCoreError err = ctx->notebook_manager->GetNotebookConfig(notebook_id, config_json);
137136

138137
if (err != VXCORE_OK) {
139-
ctx->last_error = "Failed to get notebook properties";
138+
ctx->last_error = "Failed to get notebook config";
140139
return err;
141140
}
142141

143-
char *json_copy = vxcore_strdup(properties_json.c_str());
142+
char *json_copy = vxcore_strdup(config_json.c_str());
144143
if (!json_copy) {
145144
return VXCORE_ERR_OUT_OF_MEMORY;
146145
}
147146

148-
*out_properties_json = json_copy;
147+
*out_config_json = json_copy;
149148
return VXCORE_OK;
150149
} catch (...) {
151-
ctx->last_error = "Unknown error getting notebook properties";
150+
ctx->last_error = "Unknown error getting notebook config";
152151
return VXCORE_ERR_UNKNOWN;
153152
}
154153
}
155154

156-
VXCORE_API VxCoreError vxcore_notebook_set_properties(VxCoreContextHandle context,
157-
const char *notebook_id,
158-
const char *properties_json) {
159-
if (!context || !notebook_id || !properties_json) {
155+
VXCORE_API VxCoreError vxcore_notebook_update_config(VxCoreContextHandle context,
156+
const char *notebook_id,
157+
const char *config_json) {
158+
if (!context || !notebook_id || !config_json) {
160159
return VXCORE_ERR_NULL_POINTER;
161160
}
162161

163162
auto *ctx = reinterpret_cast<vxcore::VxCoreContext *>(context);
164163

165164
try {
166-
VxCoreError err = ctx->notebook_manager->SetNotebookProperties(notebook_id, properties_json);
165+
VxCoreError err = ctx->notebook_manager->UpdateNotebookConfig(notebook_id, config_json);
167166

168167
if (err != VXCORE_OK) {
169-
ctx->last_error = "Failed to set notebook properties";
168+
ctx->last_error = "Failed to set notebook config";
170169
}
171170
return err;
172171
} catch (...) {
173-
ctx->last_error = "Unknown error setting notebook properties";
172+
ctx->last_error = "Unknown error setting notebook config";
174173
return VXCORE_ERR_UNKNOWN;
175174
}
176175
}

src/core/config_manager.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ bool ConfigManager::test_mode_ = false;
1111

1212
ConfigManager::ConfigManager() : config_(), session_config_() {
1313
if (test_mode_) {
14-
auto temp_path = std::filesystem::temp_directory_path() / "vxcore_test";
14+
std::filesystem::path temp_path(GetDataPathInTestMode());
15+
16+
std::error_code ec;
17+
std::filesystem::remove_all(temp_path, ec);
18+
if (ec) {
19+
VXCORE_LOG_WARN("Failed to clear test data path: %s", ec.message().c_str());
20+
}
21+
1522
app_data_path_ = temp_path;
1623
local_data_path_ = temp_path;
1724
} else {
@@ -26,6 +33,10 @@ ConfigManager::ConfigManager() : config_(), session_config_() {
2633
}
2734
}
2835

36+
std::string ConfigManager::GetDataPathInTestMode() {
37+
return (std::filesystem::temp_directory_path() / "vxcore_test").string();
38+
}
39+
2940
VxCoreError ConfigManager::LoadConfigs() {
3041
VXCORE_LOG_INFO("Loading configs");
3142

src/core/config_manager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ConfigManager {
3535
VxCoreError EnsureDataFolders();
3636
VxCoreError CheckAndMigrateVersion();
3737

38+
static std::string GetDataPathInTestMode();
39+
3840
VxCoreConfig config_;
3941
VxCoreSessionConfig session_config_;
4042
std::filesystem::path app_data_path_;

src/core/notebook.cpp

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <filesystem>
77
#include <fstream>
88

9+
#include "utils/logger.h"
910
#include "utils/utils.h"
1011

1112
namespace vxcore {
@@ -84,15 +85,63 @@ nlohmann::json NotebookRecord::ToJson() const {
8485
return json;
8586
}
8687

87-
Notebook::Notebook(const std::string &root_folder, NotebookType type, const NotebookConfig &config)
88-
: root_folder_(root_folder), type_(type), config_(config) {
88+
Notebook::Notebook(const std::string &root_folder, NotebookType type)
89+
: root_folder_(root_folder), type_(type) {}
90+
91+
VxCoreError Notebook::CreateBundledNotebook(const std::string &root_folder,
92+
const NotebookConfig *overridden_config,
93+
std::unique_ptr<Notebook> &out_notebook) {
94+
out_notebook.reset(new Notebook(root_folder, NotebookType::Bundled));
95+
if (overridden_config) {
96+
out_notebook->config_ = *overridden_config;
97+
}
98+
out_notebook->EnsureId();
99+
auto error = out_notebook->UpdateConfig(out_notebook->config_);
100+
if (error != VXCORE_OK) {
101+
VXCORE_LOG_ERROR("Failed to save bundled notebook config: root=%s, error=%d",
102+
root_folder.c_str(), error);
103+
out_notebook.reset();
104+
return error;
105+
}
106+
return VXCORE_OK;
107+
}
108+
109+
VxCoreError Notebook::CreateRawNotebook(const std::string &root_folder,
110+
const NotebookConfig &config,
111+
std::unique_ptr<Notebook> &out_notebook) {
112+
out_notebook.reset();
113+
std::filesystem::path rootPath(root_folder);
114+
if (!std::filesystem::exists(rootPath)) {
115+
VXCORE_LOG_ERROR("Failed to create raw notebook: root=%s, error=%d", root_folder.c_str(),
116+
VXCORE_ERR_NOT_FOUND);
117+
return VXCORE_ERR_NOT_FOUND;
118+
}
119+
120+
out_notebook.reset(new Notebook(root_folder, NotebookType::Raw));
121+
out_notebook->config_ = config;
122+
out_notebook->EnsureId();
123+
return VXCORE_OK;
124+
}
125+
126+
VxCoreError Notebook::FromBundledNotebook(const std::string &root_folder,
127+
std::unique_ptr<Notebook> &out_notebook) {
128+
out_notebook.reset(new Notebook(root_folder, NotebookType::Bundled));
129+
auto err = out_notebook->LoadConfig();
130+
if (err != VXCORE_OK) {
131+
VXCORE_LOG_ERROR("Failed to load bundled notebook config: root=%s, error=%d",
132+
root_folder.c_str(), err);
133+
out_notebook.reset();
134+
return err;
135+
}
136+
return VXCORE_OK;
137+
}
138+
139+
void Notebook::EnsureId() {
89140
if (config_.id.empty()) {
90141
config_.id = GenerateUUID();
91142
}
92143
}
93144

94-
void Notebook::SetConfig(const NotebookConfig &config) { config_ = config; }
95-
96145
std::string Notebook::GetDbPath(const std::string &local_data_folder) const {
97146
std::filesystem::path dbPath(local_data_folder);
98147
dbPath /= "notebooks";
@@ -126,7 +175,11 @@ VxCoreError Notebook::LoadConfig() {
126175
try {
127176
nlohmann::json json;
128177
file >> json;
129-
config_ = NotebookConfig::FromJson(json);
178+
auto config = NotebookConfig::FromJson(json);
179+
if (config.id.empty()) {
180+
return VXCORE_ERR_INVALID_STATE;
181+
}
182+
config_ = config;
130183
return VXCORE_OK;
131184
} catch (const nlohmann::json::exception &) {
132185
return VXCORE_ERR_JSON_PARSE;
@@ -135,7 +188,11 @@ VxCoreError Notebook::LoadConfig() {
135188
}
136189
}
137190

138-
VxCoreError Notebook::SaveConfig() {
191+
VxCoreError Notebook::UpdateConfig(const NotebookConfig &config) {
192+
assert(config_.id == config_.id);
193+
194+
config_ = config;
195+
139196
if (type_ == NotebookType::Raw) {
140197
return VXCORE_OK;
141198
}

src/core/notebook.h

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,36 @@ struct NotebookRecord {
3939

4040
class Notebook {
4141
public:
42-
Notebook(const std::string &root_folder, NotebookType type, const NotebookConfig &config);
43-
4442
const std::string &GetId() const { return config_.id; }
4543
const std::string &GetRootFolder() const { return root_folder_; }
4644
NotebookType GetType() const { return type_; }
45+
std::string GetTypeStr() const { return type_ == NotebookType::Raw ? "raw" : "bundled"; }
4746
const NotebookConfig &GetConfig() const { return config_; }
4847

49-
void SetConfig(const NotebookConfig &config);
48+
VxCoreError UpdateConfig(const NotebookConfig &config);
5049

5150
std::string GetDbPath(const std::string &local_data_folder) const;
5251
std::string GetMetadataFolder() const;
5352
std::string GetConfigPath() const;
5453

55-
VxCoreError LoadConfig();
56-
VxCoreError SaveConfig();
54+
static VxCoreError CreateBundledNotebook(const std::string &root_folder,
55+
const NotebookConfig *overridden_config,
56+
std::unique_ptr<Notebook> &out_notebook);
57+
58+
static VxCoreError CreateRawNotebook(const std::string &root_folder, const NotebookConfig &config,
59+
std::unique_ptr<Notebook> &out_notebook);
60+
61+
static VxCoreError FromBundledNotebook(const std::string &root_folder,
62+
std::unique_ptr<Notebook> &out_notebook);
63+
64+
protected:
65+
Notebook(const std::string &root_folder, NotebookType type);
5766

5867
private:
68+
VxCoreError LoadConfig();
69+
70+
void EnsureId();
71+
5972
std::string root_folder_;
6073
NotebookType type_;
6174
NotebookConfig config_;

0 commit comments

Comments
 (0)