liboxen/core/v_latest/workspaces/data_frames/
rows.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
use polars::frame::DataFrame;

use polars::prelude::NamedFrom;
use polars::prelude::PlSmallStr;
use polars::series::Series;
use rocksdb::DB;
use serde_json::Value;

use crate::constants::DIFF_STATUS_COL;
use crate::core::db;
use crate::core::v_latest::index::CommitMerkleTree;
use crate::model::merkle_tree::node::EMerkleTreeNode;
use crate::opts::DFOpts;

use crate::core::db::data_frames::{df_db, rows};
use crate::core::df::tabular;
use crate::core::v_latest::{rm, workspaces};
use crate::error::OxenError;
use crate::model::data_frame::update_result::UpdateResult;
use crate::model::diff::DiffResult;
use crate::model::staged_row_status::StagedRowStatus;
use crate::model::{Commit, LocalRepository, Workspace};
use crate::repositories;
use crate::util;
use crate::view::JsonDataFrameView;

use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;

pub fn add(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    data: &serde_json::Value,
) -> Result<DataFrame, OxenError> {
    let path = path.as_ref();
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, path);
    let row_changes_path = repositories::workspaces::data_frames::row_changes_path(workspace, path);

    log::debug!(
        "add_row() path: {:?} got db_path: {:?}",
        row_changes_path,
        db_path
    );
    let conn = df_db::get_connection(db_path)?;

    let df = tabular::parse_json_to_df(data)?;
    log::debug!("add() df: {:?}", df);

    let mut result = rows::append_row(&conn, &df)?;

    let oxen_id_col = result
        .column("_oxen_id")
        .expect("Column _oxen_id not found");

    let last_idx = oxen_id_col.len() - 1;
    let last_value = oxen_id_col.get(last_idx)?;

    let row_id = last_value.to_string().trim_matches('"').to_string();

    let row = JsonDataFrameView::json_from_df(&mut result);

    rows::record_row_change(&row_changes_path, row_id, "added".to_owned(), row, None)?;

    workspaces::files::track_modified_data_frame(workspace, path)?;

    Ok(result)
}

pub fn restore(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    row_id: impl AsRef<str>,
) -> Result<DataFrame, OxenError> {
    let row_id = row_id.as_ref();
    let restored_row = restore_row_in_db(workspace, path.as_ref(), row_id)?;
    let diff = repositories::workspaces::data_frames::full_diff(workspace, path.as_ref())?;
    if let DiffResult::Tabular(diff) = diff {
        if !diff.has_changes() {
            log::debug!("no changes, deleting file from staged db");
            // Restored to original state == delete file from staged db
            // TODO: Implement this
            rm::remove_staged_recursively(
                &workspace.workspace_repo,
                &HashSet::from([path.as_ref().to_path_buf()]),
            )?;

            // loop over parents and delete from staged db
            let mut current_path = path.as_ref().to_path_buf();
            while let Some(parent) = current_path.parent() {
                rm::remove_staged_recursively(
                    &workspace.workspace_repo,
                    &HashSet::from([parent.to_path_buf()]),
                )?;
                current_path = parent.to_path_buf();
            }
        }
    }

    Ok(restored_row)
}

pub fn delete(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    row_id: &str,
) -> Result<DataFrame, OxenError> {
    log::debug!("delete() row_id: {row_id} from path: {:?}", path.as_ref());
    let path = path.as_ref();
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, path);
    let row_changes_path = repositories::workspaces::data_frames::row_changes_path(workspace, path);

    let mut deleted_row = {
        let conn = df_db::get_connection(db_path)?;
        rows::delete_row(&conn, row_id)?
    };
    log::debug!("delete() deleted_row: {:?}", deleted_row);

    let row = JsonDataFrameView::json_from_df(&mut deleted_row);

    rows::record_row_change(
        &row_changes_path,
        row_id.to_owned(),
        "deleted".to_owned(),
        row,
        None,
    )?;

    // TODO: Better way of tracking when a file is restored to its original state without diffing
    //       this could be really slow
    let diff = repositories::workspaces::data_frames::full_diff(workspace, path)?;

    if let DiffResult::Tabular(diff) = diff {
        if !diff.has_changes() {
            log::debug!("no changes, deleting file from staged db {:?}", path);
            // Restored to original state == delete file from staged db
            rm::remove_staged_recursively(
                &workspace.workspace_repo,
                &HashSet::from([path.to_path_buf()]),
            )?;
        } else {
            log::debug!("there are still changes, not deleting file from staged db");
            log::debug!("diff: {:?}", diff);
            // We track that the file has been modified
            log::debug!("rows::delete() tracking file to staged db: {:?}", path);
            workspaces::files::track_modified_data_frame(workspace, path)?;
        }
    }
    Ok(deleted_row)
}

pub fn update(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    row_id: &str,
    data: &serde_json::Value,
) -> Result<DataFrame, OxenError> {
    let path = path.as_ref();
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, path);
    let conn = df_db::get_connection(db_path)?;
    let row_changes_path = repositories::workspaces::data_frames::row_changes_path(workspace, path);

    let mut df = tabular::parse_json_to_df(data)?;

    let mut row = repositories::workspaces::data_frames::rows::get_by_id(workspace, path, row_id)?;

    let mut result = rows::modify_row(&conn, &mut df, row_id)?;

    let row_before = JsonDataFrameView::json_from_df(&mut row);

    let row_after = JsonDataFrameView::json_from_df(&mut result);

    rows::record_row_change(
        &row_changes_path,
        row_id.to_owned(),
        "updated".to_owned(),
        row_before,
        Some(row_after),
    )?;

    let diff = repositories::workspaces::data_frames::full_diff(workspace, path)?;
    log::debug!("update() diff: {:?}", diff);
    if let DiffResult::Tabular(diff) = diff {
        if !diff.has_changes() {
            rm::remove_staged_recursively(
                &workspace.workspace_repo,
                &HashSet::from([path.to_path_buf()]),
            )?;
        } else {
            workspaces::files::track_modified_data_frame(workspace, path)?;
        }
    }

    Ok(result)
}

pub fn batch_update(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    data: &Value,
) -> Result<Vec<UpdateResult>, OxenError> {
    let path = path.as_ref();

    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, path);
    let conn = df_db::get_connection(db_path)?;

    let Some(array) = data.as_array() else {
        return Err(OxenError::basic_str("Data is not an array"));
    };

    let mut keys: Vec<String> = Vec::new();

    let row_map: HashMap<String, DataFrame> = array
        .iter()
        .map(|obj| {
            let row_id = obj
                .get("row_id")
                .and_then(Value::as_str)
                .ok_or_else(|| OxenError::basic_str("Missing row_id"))?
                .to_owned();

            keys.push(row_id.clone());

            let df = tabular::parse_json_to_df(
                obj.get("value")
                    .ok_or_else(|| OxenError::basic_str("Missing value"))?,
            )?;
            Ok((row_id, df))
        })
        .collect::<Result<_, OxenError>>()?;

    rows::modify_rows(&conn, row_map)?;

    let results: Vec<UpdateResult> = keys
        .iter()
        .map(|key| UpdateResult::Success(key.to_owned(), None))
        .collect();

    Ok(results)
}

pub fn prepare_modified_or_removed_row(
    repo: &LocalRepository,
    commit: &Commit,
    path: impl AsRef<Path>,
    row_df: &DataFrame,
) -> Result<DataFrame, OxenError> {
    let row_idx = repositories::workspaces::data_frames::rows::get_row_idx(row_df)?
        .ok_or_else(|| OxenError::basic_str("Row index not found"))?;
    let row_idx_og = (row_idx - 1) as i64;

    let commit_merkle_tree = CommitMerkleTree::from_path(repo, commit, &path, true)?;
    let file_node = match commit_merkle_tree.root.node {
        EMerkleTreeNode::File(file_node) => file_node,
        _ => return Err(OxenError::basic_str("File node not found")),
    };

    log::debug!(
        "prepare_modified_or_removed_row() commit_merkle_tree: {:?}",
        &commit_merkle_tree.root.hash.to_string()
    );

    // let scan_rows = 10000 as usize;
    let committed_df_path = util::fs::version_path_from_node(
        repo,
        commit_merkle_tree.root.hash.to_string(),
        path.as_ref(),
    );

    log::debug!(
        "prepare_modified_or_removed_row() committed_df_path: {:?}",
        committed_df_path
    );

    // TODONOW should not be using all rows - just need to parse delim
    let lazy_df = tabular::read_df_with_extension(
        committed_df_path,
        file_node.extension(),
        &DFOpts::empty(),
    )?;

    // Get the row by index
    let mut row = lazy_df.slice(row_idx_og, 1_usize);

    // Added rows will error here on out of index, but we caught them earlier..
    // let mut row = row.collect()?;

    row.with_column(Series::new(
        PlSmallStr::from_str(DIFF_STATUS_COL),
        vec![StagedRowStatus::Unchanged.to_string()],
    ))?;

    Ok(row)
}

pub fn restore_row_in_db(
    workspace: &Workspace,
    path: impl AsRef<Path>,
    row_id: impl AsRef<str>,
) -> Result<DataFrame, OxenError> {
    let row_id = row_id.as_ref();
    let db_path = repositories::workspaces::data_frames::duckdb_path(workspace, path.as_ref());
    let conn = df_db::get_connection(db_path)?;
    let opts = db::key_val::opts::default();
    let column_changes_path =
        repositories::workspaces::data_frames::column_changes_path(workspace, path.as_ref());
    let db = DB::open(&opts, dunce::simplified(&column_changes_path))?;

    // Get the row by id
    let row =
        repositories::workspaces::data_frames::rows::get_by_id(workspace, path.as_ref(), row_id)?;

    if row.height() == 0 {
        return Err(OxenError::resource_not_found(row_id));
    };

    let row_status = repositories::workspaces::data_frames::rows::get_row_status(&row)?
        .ok_or_else(|| OxenError::basic_str("Row status not found"))?;
    let result_row = match row_status {
        StagedRowStatus::Added => {
            // Row is added, just delete it
            log::debug!("restore_row() row is added, deleting");
            rows::revert_row_changes(&db, row_id.to_owned())?;
            rows::delete_row(&conn, row_id)?
        }
        StagedRowStatus::Modified | StagedRowStatus::Removed => {
            // Row is modified, just delete it
            log::debug!("restore_row() row is modified, deleting");
            let mut insert_row = prepare_modified_or_removed_row(
                &workspace.base_repo,
                &workspace.commit,
                path.as_ref(),
                &row,
            )?;
            log::debug!("restore_row() insert_row: {:?}", insert_row);
            rows::revert_row_changes(&db, row_id.to_owned())?;
            log::debug!("restore_row() after revert");
            rows::modify_row(&conn, &mut insert_row, row_id)?
        }
        StagedRowStatus::Unchanged => {
            // Row is unchanged, just return it
            row
        }
    };

    log::debug!("we're returning this row: {:?}", result_row);

    Ok(result_row)
}