sochdb 2.0.2

SochDB - LLM-optimized database with native vector search
Documentation
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// SPDX-License-Identifier: AGPL-3.0-or-later
// SochDB - LLM-Optimized Embedded Database
// Copyright (C) 2026 Sushanth Reddy Vanagala (https://github.com/sushanthpy)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

//! CRUD Operations (DML)
//!
//! Provides insert, update, delete, upsert, and find operations.

use crate::connection::SochConnection;
use crate::error::Result;
use crate::transaction::ClientTransaction;
use std::collections::HashMap;

use sochdb_core::soch::SochValue;

/// Insert result
#[derive(Debug, Clone)]
pub struct InsertResult {
    pub rows_inserted: usize,
    pub last_id: Option<u64>,
}

/// Update result  
#[derive(Debug, Clone)]
pub struct UpdateResult {
    pub rows_updated: usize,
}

/// Delete result
#[derive(Debug, Clone)]
pub struct DeleteResult {
    pub rows_deleted: usize,
}

/// Upsert result
#[derive(Debug, Clone)]
pub struct UpsertResult {
    pub rows_upserted: usize,
    pub rows_inserted: usize,
    pub rows_updated: usize,
}

/// Row builder for inserts
pub struct RowBuilder<'a> {
    conn: &'a SochConnection,
    table: String,
    rows: Vec<HashMap<String, SochValue>>,
    current: HashMap<String, SochValue>,
}

impl<'a> RowBuilder<'a> {
    /// Create new row builder
    pub fn new(conn: &'a SochConnection, table: &str) -> Self {
        Self {
            conn,
            table: table.to_string(),
            rows: Vec::new(),
            current: HashMap::new(),
        }
    }

    /// Set field value
    pub fn set(mut self, field: &str, value: impl Into<SochValue>) -> Self {
        self.current.insert(field.to_string(), value.into());
        self
    }

    /// Add current row and start new one
    pub fn row(mut self) -> Self {
        if !self.current.is_empty() {
            self.rows.push(std::mem::take(&mut self.current));
        }
        self
    }

    /// Execute insert
    pub fn execute(mut self) -> Result<InsertResult> {
        // Add final current row if not empty
        if !self.current.is_empty() {
            self.rows.push(std::mem::take(&mut self.current));
        }

        let rows_inserted = self.rows.len();
        let mut last_id = None;

        // Insert via TCH (in-memory columnar store)
        {
            let mut tch = self.conn.tch.write();
            for row in &self.rows {
                let id = tch.insert_row(&self.table, row);
                last_id = Some(id);
                
                // Persist to storage backend for durability
                // Key format: {table}:{row_id}
                // Value: bincode serialized row data
                if let Ok(value) = bincode::serialize(row) {
                    let key = format!("{}:{}", self.table, id);
                    let _ = self.conn.put(key.as_bytes().to_vec(), value);
                }
            }
        }

        Ok(InsertResult {
            rows_inserted,
            last_id,
        })
    }
}

/// Update builder
pub struct UpdateBuilder<'a> {
    conn: &'a SochConnection,
    table: String,
    updates: HashMap<String, SochValue>,
    where_clause: Option<crate::connection::WhereClause>,
}

// Re-export from connection
pub use crate::connection::{CompareOp, UpsertAction, WhereClause};

impl<'a> UpdateBuilder<'a> {
    /// Create new update builder
    pub fn new(conn: &'a SochConnection, table: &str) -> Self {
        Self {
            conn,
            table: table.to_string(),
            updates: HashMap::new(),
            where_clause: None,
        }
    }

    /// Set field to new value
    pub fn set(mut self, field: &str, value: impl Into<SochValue>) -> Self {
        self.updates.insert(field.to_string(), value.into());
        self
    }

    /// Add WHERE clause
    pub fn where_eq(mut self, field: &str, value: impl Into<SochValue>) -> Self {
        self.where_clause = Some(WhereClause::Simple {
            field: field.to_string(),
            op: CompareOp::Eq,
            value: value.into(),
        });
        self
    }

    /// Add WHERE clause with custom operator
    pub fn where_cond(mut self, field: &str, op: CompareOp, value: impl Into<SochValue>) -> Self {
        self.where_clause = Some(WhereClause::Simple {
            field: field.to_string(),
            op,
            value: value.into(),
        });
        self
    }

    /// Execute update
    /// 
    /// Note: Updates are persisted to TCH in-memory store. 
    /// The MutationResult contains affected row IDs for storage-level operations.
    pub fn execute(self) -> Result<UpdateResult> {
        let mut tch = self.conn.tch.write();
        let mutation_result = tch.update_rows(&self.table, &self.updates, self.where_clause.as_ref());

        // TODO: Wire mutation_result.affected_row_ids to storage backend for WAL/index/CDC
        Ok(UpdateResult { rows_updated: mutation_result.affected_count })
    }
}

/// Delete builder
pub struct DeleteBuilder<'a> {
    conn: &'a SochConnection,
    table: String,
    where_clause: Option<WhereClause>,
}

impl<'a> DeleteBuilder<'a> {
    /// Create new delete builder
    pub fn new(conn: &'a SochConnection, table: &str) -> Self {
        Self {
            conn,
            table: table.to_string(),
            where_clause: None,
        }
    }

    /// Add WHERE clause
    pub fn where_eq(mut self, field: &str, value: impl Into<SochValue>) -> Self {
        self.where_clause = Some(WhereClause::Simple {
            field: field.to_string(),
            op: CompareOp::Eq,
            value: value.into(),
        });
        self
    }

    /// Add WHERE clause with custom operator
    pub fn where_cond(mut self, field: &str, op: CompareOp, value: impl Into<SochValue>) -> Self {
        self.where_clause = Some(WhereClause::Simple {
            field: field.to_string(),
            op,
            value: value.into(),
        });
        self
    }

    /// Execute delete
    /// 
    /// Note: Deletes are persisted to TCH in-memory store.
    /// The MutationResult contains affected row IDs for storage-level operations.
    pub fn execute(self) -> Result<DeleteResult> {
        let mut tch = self.conn.tch.write();
        let mutation_result = tch.delete_rows(&self.table, self.where_clause.as_ref());

        // TODO: Wire mutation_result.affected_row_ids to storage backend for WAL/index/CDC
        Ok(DeleteResult { rows_deleted: mutation_result.affected_count })
    }
}

/// Find (SELECT) builder
pub struct FindBuilder<'a> {
    conn: &'a SochConnection,
    table: String,
    columns: Vec<String>,
    where_clause: Option<WhereClause>,
    order_by: Option<(String, bool)>,
    limit: Option<usize>,
    offset: Option<usize>,
}

impl<'a> FindBuilder<'a> {
    /// Create new find builder
    pub fn new(conn: &'a SochConnection, table: &str) -> Self {
        Self {
            conn,
            table: table.to_string(),
            columns: Vec::new(),
            where_clause: None,
            order_by: None,
            limit: None,
            offset: None,
        }
    }

    /// Select specific columns
    pub fn select(mut self, columns: &[&str]) -> Self {
        self.columns = columns.iter().map(|s| s.to_string()).collect();
        self
    }

    /// Add WHERE clause
    pub fn where_eq(mut self, field: &str, value: impl Into<SochValue>) -> Self {
        self.where_clause = Some(WhereClause::Simple {
            field: field.to_string(),
            op: CompareOp::Eq,
            value: value.into(),
        });
        self
    }

    /// Add WHERE clause with custom operator
    pub fn where_cond(mut self, field: &str, op: CompareOp, value: impl Into<SochValue>) -> Self {
        self.where_clause = Some(WhereClause::Simple {
            field: field.to_string(),
            op,
            value: value.into(),
        });
        self
    }

    /// Order by column
    pub fn order_by(mut self, column: &str, ascending: bool) -> Self {
        self.order_by = Some((column.to_string(), ascending));
        self
    }

    /// Limit results
    pub fn limit(mut self, n: usize) -> Self {
        self.limit = Some(n);
        self
    }

    /// Offset results
    pub fn offset(mut self, n: usize) -> Self {
        self.offset = Some(n);
        self
    }

    /// Execute find and return rows
    pub fn execute(self) -> Result<Vec<HashMap<String, SochValue>>> {
        let tch = self.conn.tch.read();
        let mut cursor = tch.select(
            &self.table,
            &self.columns,
            self.where_clause.as_ref(),
            self.order_by.as_ref(),
            self.limit,
            self.offset,
        );
        let mut rows = Vec::new();
        while let Some(row) = cursor.next() {
            rows.push(row);
        }
        Ok(rows)
    }

    /// Execute and return first row only
    pub fn first(mut self) -> Result<Option<HashMap<String, SochValue>>> {
        self.limit = Some(1);
        let rows = self.execute()?;
        Ok(rows.into_iter().next())
    }

    /// Execute and return all rows
    pub fn all(self) -> Result<Vec<HashMap<String, SochValue>>> {
        self.execute()
    }
}

/// Upsert builder
pub struct UpsertBuilder<'a> {
    conn: &'a SochConnection,
    table: String,
    conflict_key: String,
    rows: Vec<HashMap<String, SochValue>>,
    current: HashMap<String, SochValue>,
}

impl<'a> UpsertBuilder<'a> {
    /// Create new upsert builder
    pub fn new(conn: &'a SochConnection, table: &str, conflict_key: &str) -> Self {
        Self {
            conn,
            table: table.to_string(),
            conflict_key: conflict_key.to_string(),
            rows: Vec::new(),
            current: HashMap::new(),
        }
    }

    /// Set field value
    pub fn set(mut self, field: &str, value: impl Into<SochValue>) -> Self {
        self.current.insert(field.to_string(), value.into());
        self
    }

    /// Add current row and start new one
    pub fn row(mut self) -> Self {
        if !self.current.is_empty() {
            self.rows.push(std::mem::take(&mut self.current));
        }
        self
    }

    /// Execute upsert
    pub fn execute(mut self) -> Result<UpsertResult> {
        // Add final current row if not empty
        if !self.current.is_empty() {
            self.rows.push(std::mem::take(&mut self.current));
        }

        let mut rows_inserted = 0;
        let mut rows_updated = 0;

        {
            let mut tch = self.conn.tch.write();
            for row in self.rows {
                match tch.upsert_row(&self.table, &self.conflict_key, &row) {
                    UpsertAction::Inserted => rows_inserted += 1,
                    UpsertAction::Updated => rows_updated += 1,
                }
            }
        }

        Ok(UpsertResult {
            rows_upserted: rows_inserted + rows_updated,
            rows_inserted,
            rows_updated,
        })
    }
}

/// CRUD methods on connection
impl SochConnection {
    /// Start an INSERT operation
    pub fn insert_into<'a>(&'a self, table: &str) -> RowBuilder<'a> {
        RowBuilder::new(self, table)
    }

    /// Start an UPDATE operation
    pub fn update<'a>(&'a self, table: &str) -> UpdateBuilder<'a> {
        UpdateBuilder::new(self, table)
    }

    /// Start a DELETE operation
    pub fn delete_from<'a>(&'a self, table: &str) -> DeleteBuilder<'a> {
        DeleteBuilder::new(self, table)
    }

    /// Start a SELECT operation
    pub fn find<'a>(&'a self, table: &str) -> FindBuilder<'a> {
        FindBuilder::new(self, table)
    }

    /// Start an UPSERT operation
    pub fn upsert<'a>(&'a self, table: &str, conflict_key: &str) -> UpsertBuilder<'a> {
        UpsertBuilder::new(self, table, conflict_key)
    }

    /// Quick insert single row
    pub fn insert_one(
        &self,
        table: &str,
        values: HashMap<String, SochValue>,
    ) -> Result<InsertResult> {
        let mut builder = RowBuilder::new(self, table);
        for (k, v) in values {
            builder = builder.set(&k, v);
        }
        builder.execute()
    }

    /// Quick find by ID
    pub fn find_by_id(
        &self,
        table: &str,
        id_field: &str,
        id: impl Into<SochValue>,
    ) -> Result<Option<HashMap<String, SochValue>>> {
        self.find(table).where_eq(id_field, id).first()
    }

    /// Quick delete by ID
    pub fn delete_by_id(
        &self,
        table: &str,
        id_field: &str,
        id: impl Into<SochValue>,
    ) -> Result<DeleteResult> {
        self.delete_from(table).where_eq(id_field, id).execute()
    }

    /// Count rows in table
    pub fn count(&self, table: &str) -> Result<u64> {
        let tch = self.tch.read();
        Ok(tch.count_rows(table))
    }

    /// Check if row exists
    pub fn exists(&self, table: &str, field: &str, value: impl Into<SochValue>) -> Result<bool> {
        let row = self.find(table).where_eq(field, value).first()?;
        Ok(row.is_some())
    }
}

/// CRUD methods on transaction
impl<'a> ClientTransaction<'a> {
    /// Insert row in transaction
    pub fn insert(&mut self, table: &str, values: HashMap<String, SochValue>) -> Result<u64> {
        let mut tch = self.conn.tch.write();
        let id = tch.insert_row(table, &values);
        Ok(id)
    }

    /// Update rows in transaction
    pub fn update_where(
        &mut self,
        table: &str,
        updates: HashMap<String, SochValue>,
        field: &str,
        value: impl Into<SochValue>,
    ) -> Result<usize> {
        let mut tch = self.conn.tch.write();
        let where_clause = WhereClause::Simple {
            field: field.to_string(),
            op: CompareOp::Eq,
            value: value.into(),
        };
        let mutation_result = tch.update_rows(table, &updates, Some(&where_clause));
        // TODO: Wire mutation_result.affected_row_ids to transaction WAL
        Ok(mutation_result.affected_count)
    }

    /// Delete rows in transaction
    pub fn delete_where(
        &mut self,
        table: &str,
        field: &str,
        value: impl Into<SochValue>,
    ) -> Result<usize> {
        let mut tch = self.conn.tch.write();
        let where_clause = WhereClause::Simple {
            field: field.to_string(),
            op: CompareOp::Eq,
            value: value.into(),
        };
        let mutation_result = tch.delete_rows(table, Some(&where_clause));
        // TODO: Wire mutation_result.affected_row_ids to transaction WAL
        Ok(mutation_result.affected_count)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_insert_builder() {
        let conn = SochConnection::open("./test").unwrap();

        let result = conn
            .insert_into("users")
            .set("id", SochValue::Int(1))
            .set("name", SochValue::Text("Alice".to_string()))
            .execute()
            .unwrap();

        assert_eq!(result.rows_inserted, 1);
    }

    #[test]
    fn test_multi_row_insert() {
        let conn = SochConnection::open("./test").unwrap();

        let result = conn
            .insert_into("users")
            .set("id", SochValue::Int(1))
            .set("name", SochValue::Text("Alice".to_string()))
            .row()
            .set("id", SochValue::Int(2))
            .set("name", SochValue::Text("Bob".to_string()))
            .execute()
            .unwrap();

        assert_eq!(result.rows_inserted, 2);
    }

    #[test]
    fn test_update_builder() {
        let conn = SochConnection::open("./test").unwrap();

        let result = conn
            .update("users")
            .set("name", SochValue::Text("New Name".to_string()))
            .where_eq("id", SochValue::Int(1))
            .execute()
            .unwrap();

        // May be 0 if no matching rows
        // rows_updated is u64, so this is just to verify the operation completed
        let _ = result.rows_updated; // Verify field exists
    }

    #[test]
    fn test_delete_builder() {
        let conn = SochConnection::open("./test").unwrap();

        let result = conn
            .delete_from("users")
            .where_eq("id", SochValue::Int(999))
            .execute()
            .unwrap();

        // rows_deleted is u64, so this is just to verify the operation completed
        let _ = result.rows_deleted; // Verify field exists
    }

    #[test]
    fn test_find_builder() {
        let conn = SochConnection::open("./test").unwrap();

        let result = conn
            .find("users")
            .select(&["id", "name"])
            .where_eq("active", SochValue::Bool(true))
            .order_by("name", true)
            .limit(10)
            .execute();

        // Should compile and return result
        assert!(result.is_ok());
    }

    #[test]
    fn test_upsert() {
        let conn = SochConnection::open("./test").unwrap();

        let result = conn
            .upsert("users", "id")
            .set("id", SochValue::Int(1))
            .set("name", SochValue::Text("Alice".to_string()))
            .execute()
            .unwrap();

        assert!(result.rows_upserted >= 1);
    }

    #[test]
    fn test_convenience_methods() {
        let conn = SochConnection::open("./test").unwrap();

        // Quick find
        let _user = conn.find_by_id("users", "id", SochValue::Int(1));

        // Exists check
        let _exists = conn.exists(
            "users",
            "email",
            SochValue::Text("test@test.com".to_string()),
        );

        // Count
        let _count = conn.count("users");
    }
}