Skip to content

Commit c176e41

Browse files
authored
Merge pull request #8
Change return type of insert functions to Result<bool>
2 parents 143d444 + 0cf0dfa commit c176e41

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "feoxdb"
3-
version = "0.4.2"
3+
version = "0.5.0"
44
edition = "2021"
55
authors = ["Mehran Toosi <dev@mehran.dk>"]
66
description = "Iron-oxide fast embedded database - nanosecond-level key-value storage"

src/core/store/internal.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl FeoxStore {
1818
value: &[u8],
1919
timestamp: u64,
2020
ttl_expiry: u64,
21-
) -> Result<()> {
21+
) -> Result<bool> {
2222
let new_record = if ttl_expiry > 0 && self.enable_ttl {
2323
Arc::new(Record::new_with_timestamp_ttl(
2424
old_record.key.clone(),
@@ -84,7 +84,7 @@ impl FeoxStore {
8484
}
8585
}
8686

87-
Ok(())
87+
Ok(false)
8888
}
8989

9090
/// Update an existing record with a new value (Bytes version for zero-copy).
@@ -94,7 +94,7 @@ impl FeoxStore {
9494
value: Bytes,
9595
timestamp: u64,
9696
ttl_expiry: u64,
97-
) -> Result<()> {
97+
) -> Result<bool> {
9898
let new_record = if ttl_expiry > 0 && self.enable_ttl {
9999
Arc::new(Record::new_from_bytes_with_ttl(
100100
old_record.key.clone(),
@@ -160,7 +160,7 @@ impl FeoxStore {
160160
}
161161
}
162162

163-
Ok(())
163+
Ok(false)
164164
}
165165

166166
/// Get access to hash table (for TTL cleaner)

src/core/store/json_patch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ impl FeoxStore {
9595
let new_value = crate::utils::json_patch::apply_json_patch(&current_value, patch)?;
9696

9797
// Now update without holding any references
98-
self.insert_with_timestamp(key, &new_value, Some(timestamp))
98+
self.insert_with_timestamp(key, &new_value, Some(timestamp))?;
99+
Ok(())
99100
}
100101
}

src/core/store/operations.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl FeoxStore {
2323
///
2424
/// # Returns
2525
///
26-
/// Returns `Ok(())` if successful.
26+
/// Returns `Ok(true)` if a new key was inserted, `Ok(false)` if an existing key was updated.
2727
///
2828
/// # Errors
2929
///
@@ -47,7 +47,7 @@ impl FeoxStore {
4747
///
4848
/// * Memory mode: ~600ns
4949
/// * Persistent mode: ~800ns (buffered write)
50-
pub fn insert(&self, key: &[u8], value: &[u8]) -> Result<()> {
50+
pub fn insert(&self, key: &[u8], value: &[u8]) -> Result<bool> {
5151
self.insert_with_timestamp(key, value, None)
5252
}
5353

@@ -70,7 +70,7 @@ impl FeoxStore {
7070
key: &[u8],
7171
value: &[u8],
7272
timestamp: Option<u64>,
73-
) -> Result<()> {
73+
) -> Result<bool> {
7474
self.insert_with_timestamp_and_ttl_internal(key, value, timestamp, 0)
7575
}
7676

@@ -90,7 +90,7 @@ impl FeoxStore {
9090
///
9191
/// # Returns
9292
///
93-
/// Returns `Ok(())` if successful.
93+
/// Returns `Ok(true)` if a new key was inserted, `Ok(false)` if an existing key was updated.
9494
///
9595
/// # Errors
9696
///
@@ -116,7 +116,7 @@ impl FeoxStore {
116116
///
117117
/// * Memory mode: ~600ns (avoids value copy)
118118
/// * Persistent mode: ~800ns (buffered write, avoids value copy)
119-
pub fn insert_bytes(&self, key: &[u8], value: Bytes) -> Result<()> {
119+
pub fn insert_bytes(&self, key: &[u8], value: Bytes) -> Result<bool> {
120120
self.insert_bytes_with_timestamp(key, value, None)
121121
}
122122

@@ -139,7 +139,7 @@ impl FeoxStore {
139139
key: &[u8],
140140
value: Bytes,
141141
timestamp: Option<u64>,
142-
) -> Result<()> {
142+
) -> Result<bool> {
143143
self.insert_bytes_with_timestamp_and_ttl_internal(key, value, timestamp, 0)
144144
}
145145

@@ -149,7 +149,7 @@ impl FeoxStore {
149149
value: &[u8],
150150
timestamp: Option<u64>,
151151
ttl_expiry: u64,
152-
) -> Result<()> {
152+
) -> Result<bool> {
153153
let start = std::time::Instant::now();
154154
let timestamp = match timestamp {
155155
Some(0) | None => self.get_timestamp(),
@@ -217,7 +217,7 @@ impl FeoxStore {
217217
}
218218
}
219219

220-
Ok(())
220+
Ok(!is_update)
221221
}
222222

223223
/// Internal method to insert a Bytes value with timestamp and TTL (zero-copy)
@@ -227,7 +227,7 @@ impl FeoxStore {
227227
value: Bytes,
228228
timestamp: Option<u64>,
229229
ttl_seconds: u64,
230-
) -> Result<()> {
230+
) -> Result<bool> {
231231
let start = std::time::Instant::now();
232232
// Get timestamp before any operations
233233
let timestamp = match timestamp {
@@ -242,6 +242,7 @@ impl FeoxStore {
242242
}
243243

244244
// Check for existing record
245+
let is_update = self.hash_table.contains(key);
245246
let existing_record = self.hash_table.read(key, |_, v| v.clone());
246247
if let Some(existing_record) = existing_record {
247248
let existing_ts = existing_record.timestamp;
@@ -300,7 +301,7 @@ impl FeoxStore {
300301
.memory_usage
301302
.fetch_add(new_size, Ordering::AcqRel);
302303
self.stats
303-
.record_insert(start.elapsed().as_nanos() as u64, false);
304+
.record_insert(start.elapsed().as_nanos() as u64, is_update);
304305

305306
// Only do persistence if not in memory-only mode
306307
if !self.memory_only {
@@ -312,7 +313,7 @@ impl FeoxStore {
312313
}
313314
}
314315

315-
Ok(())
316+
Ok(!is_update)
316317
}
317318

318319
/// Retrieve a value by key.

src/core/store/ttl.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl FeoxStore {
3737
///
3838
/// * Memory mode: ~800ns
3939
/// * Persistent mode: ~1µs (buffered write)
40-
pub fn insert_with_ttl(&self, key: &[u8], value: &[u8], ttl_seconds: u64) -> Result<()> {
40+
pub fn insert_with_ttl(&self, key: &[u8], value: &[u8], ttl_seconds: u64) -> Result<bool> {
4141
if !self.enable_ttl {
4242
return Err(FeoxError::TtlNotEnabled);
4343
}
@@ -62,7 +62,7 @@ impl FeoxStore {
6262
value: &[u8],
6363
ttl_seconds: u64,
6464
timestamp: Option<u64>,
65-
) -> Result<()> {
65+
) -> Result<bool> {
6666
if !self.enable_ttl {
6767
return Err(FeoxError::TtlNotEnabled);
6868
}
@@ -114,7 +114,12 @@ impl FeoxStore {
114114
///
115115
/// * Memory mode: ~800ns (avoids value copy)
116116
/// * Persistent mode: ~1µs (buffered write, avoids value copy)
117-
pub fn insert_bytes_with_ttl(&self, key: &[u8], value: Bytes, ttl_seconds: u64) -> Result<()> {
117+
pub fn insert_bytes_with_ttl(
118+
&self,
119+
key: &[u8],
120+
value: Bytes,
121+
ttl_seconds: u64,
122+
) -> Result<bool> {
118123
if !self.enable_ttl {
119124
return Err(FeoxError::TtlNotEnabled);
120125
}
@@ -139,7 +144,7 @@ impl FeoxStore {
139144
value: Bytes,
140145
ttl_seconds: u64,
141146
timestamp: Option<u64>,
142-
) -> Result<()> {
147+
) -> Result<bool> {
143148
if !self.enable_ttl {
144149
return Err(FeoxError::TtlNotEnabled);
145150
}

0 commit comments

Comments
 (0)