MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / convert_to_strict

Method convert_to_strict

nodedb/src/data/executor/handlers/convert.rs:78–165  ·  view source on GitHub ↗

Convert to strict mode: re-encode each document as a Binary Tuple.

(
        &mut self,
        task: &ExecutionTask,
        tid: u64,
        collection: &str,
        schema_json: &str,
    )

Source from the content-addressed store, hash-verified

76
77 /// Convert to strict mode: re-encode each document as a Binary Tuple.
78 fn convert_to_strict(
79 &mut self,
80 task: &ExecutionTask,
81 tid: u64,
82 collection: &str,
83 schema_json: &str,
84 ) -> Response {
85 // Parse the target schema from JSON column definitions.
86 let columns: Vec<ColumnDef> = match sonic_rs::from_str(schema_json) {
87 Ok(c) => c,
88 Err(e) => {
89 return self.response_error(
90 task,
91 ErrorCode::Internal {
92 detail: format!("invalid schema JSON: {e}"),
93 },
94 );
95 }
96 };
97
98 if columns.is_empty() {
99 return self.response_error(
100 task,
101 ErrorCode::Internal {
102 detail: "schema must have at least one column".into(),
103 },
104 );
105 }
106
107 let schema = StrictSchema {
108 columns,
109 version: 1,
110 dropped_columns: Vec::new(),
111 bitemporal: false,
112 };
113
114 // Scan all existing documents.
115 let docs = match self.sparse.scan_documents(tid, collection, usize::MAX) {
116 Ok(d) => d,
117 Err(e) => {
118 return self.response_error(
119 task,
120 ErrorCode::Internal {
121 detail: format!("scan failed: {e}"),
122 },
123 );
124 }
125 };
126
127 // Re-encode each document as a Binary Tuple.
128 let mut converted = 0u64;
129 let mut errors = 0u64;
130
131 for (doc_id, doc_bytes) in &docs {
132 match super::super::strict_format::bytes_to_binary_tuple(doc_bytes, &schema) {
133 Ok(tuple_bytes) => {
134 if let Err(e) = self.sparse.put(tid, collection, doc_id, &tuple_bytes) {
135 tracing::warn!(doc_id, error = %e, "failed to write converted doc");

Callers 1

Calls 8

bytes_to_binary_tupleFunction · 0.85
encode_jsonFunction · 0.85
response_errorMethod · 0.80
scan_documentsMethod · 0.80
response_with_payloadMethod · 0.80
to_stringMethod · 0.80
is_emptyMethod · 0.45
putMethod · 0.45

Tested by

no test coverage detected