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
//! Embedded file-based JSON database engine using memory-mapped files.
//!
//! # sqjson
//!
//! `sqjson` is a lightweight, embedded, file-based key-value database written in Rust.
//! It stores structured data as JSON values and uses memory-mapped I/O for efficient read/write access.
//!
//! Inspired by SQLite, but minimal and JSON-native, with an emphasis on simplicity and portability.
//!
//! ## Features
//!
//! - Embedded, single-file storage (`.db` file)
//! - Efficient memory-mapped I/O for fast reads/writes
//! - JSON value storage using `serde_json`
//! - String-keyed key-value API: `put`, `get`, `delete`, `flush`
//! - Field-level access: `get_field(key, field)`
//! - Secondary indexes for fast field-based queries: `query(field, value)`
//! - Range queries for numeric or string fields: `range_query(field, min, max)`
//! - Update specific field without replacing the full record: `update_field(key, field, value)`
//! - Text search on string fields: `search_contains(field, substring)`
//! - Filter records using a custom predicate: `filter(|val| ...)`
//! - Pagination for queries: `query_page(field, value, limit, offset)`
//! - Export query results to a JSON file: `export_query(field, value, path)`
//! - Export full database: `export_to_file(path)`
//! - Compact database to reclaim space (like SQLite VACUUM): `compact()`
//! - Atomic transactions: `begin_transaction()`, `commit()`, `rollback()`, `in_transaction()`
//! - Show all stored records
//!
//! ## Examples
//!
//! ### Open or Create a Database
//!
//! ```rust
//! use sqjson::{YourDb, DbError};
//! use serde_json::json;
//!
//! fn main() -> Result<(), DbError> {
//! let mut db = YourDb::open("jsondb.db")?;
//!
//! // Insert JSON records
//! db.put("user:1", &json!({ "name": "Alice", "age": 30, "city": "NY" }))?;
//! db.put("user:2", &json!({ "name": "Bob", "age": 25, "city": "LA" }))?;
//!
//! // Flush to disk
//! db.flush()?;
//! Ok(())
//! }
//! ```
//!
//! ### Query by Field
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # fn run() -> Result<(), DbError> {
//! let db = YourDb::open("jsondb.db")?;
//!
//! let users_age_30 = db.query("age", 30)?;
//! println!("Users with age 30: {:?}", users_age_30);
//!
//! let users_city_ny = db.query("city", "NY")?;
//! println!("Users in NY: {:?}", users_city_ny);
//! # Ok(()) }
//! ```
//!
//! ### Range Query
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # use serde_json::json;
//! # fn run() -> Result<(), DbError> {
//! let db = YourDb::open("jsondb.db")?;
//! let age_range = db.range_query("age", json!(20), json!(30))?;
//! println!("Users age 20–30: {:?}", age_range);
//! # Ok(()) }
//! ```
//!
//! ### Update a Single Field
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # use serde_json::json;
//! # fn run() -> Result<(), DbError> {
//! let mut db = YourDb::open("jsondb.db")?;
//! db.update_field("user:2", "city", json!("SF"))?;
//! # Ok(()) }
//! ```
//!
//! ### Search by Substring
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # fn run() -> Result<(), DbError> {
//! let db = YourDb::open("jsondb.db")?;
//! let results = db.search_contains("name", "li")?;
//! println!("Users with 'li' in name: {:?}", results);
//! # Ok(()) }
//! ```
//!
//! ### Export
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # fn run() -> Result<(), DbError> {
//! let mut db = YourDb::open("jsondb.db")?;
//! db.export_to_file("backup.json")?;
//! # Ok(()) }
//! ```
//!
//! ### Transactions
//!
//! Group several writes so they either all land or none do. Reads inside the transaction
//! see its own uncommitted changes; `rollback()` discards them entirely.
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # use serde_json::json;
//! # fn run() -> Result<(), DbError> {
//! let mut db = YourDb::open("jsondb.db")?;
//!
//! db.begin_transaction()?;
//! db.put("user:1", &json!({ "name": "Ali" }))?;
//! db.put("user:2", &json!({ "name": "Ahmed" }))?;
//!
//! // Reads inside the transaction observe the staged writes.
//! assert_eq!(db.get("user:1")?, Some(json!({ "name": "Ali" })));
//!
//! if db.in_transaction() {
//! db.commit()?; // or db.rollback()? to discard everything above
//! }
//! # Ok(()) }
//! ```
//!
//! Nested transactions are not supported: a second `begin_transaction()` returns an error.
//! `flush()` and `compact()` are refused while a transaction is active, since both would
//! make uncommitted state durable.
//!
//! ### Compact Database
//!
//! ```rust
//! # use sqjson::{YourDb, DbError};
//! # fn run() -> Result<(), DbError> {
//! let mut db = YourDb::open("jsondb.db")?;
//! db.compact()?; // Reclaim space after deletes
//! # Ok(()) }
//! ```
pub use YourDb;
pub use DbError;