tensor_blob 0.4.0

Content-addressable blob storage with streaming and garbage collection
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
// SPDX-License-Identifier: MIT OR Apache-2.0
use std::time::{SystemTime, UNIX_EPOCH};

use tensor_store::{ScalarValue, TensorData, TensorStore, TensorValue};

use crate::{
    chunker::{Chunk, Chunker, StreamingHasher},
    error::{BlobError, Result},
    gc::increment_chunk_refs,
    metadata::PutOptions,
};

/// Internal state shared between `BlobWriter` and the store.
pub struct WriteState {
    pub artifact_id: String,
    pub filename: String,
    pub content_type: String,
    pub created_by: String,
    pub linked_to: Vec<String>,
    pub tags: Vec<String>,
    pub custom_metadata: std::collections::HashMap<String, String>,
    pub embedding: Option<(Vec<f32>, String)>,
}

/// Streaming writer for uploading artifacts.
pub struct BlobWriter {
    store: TensorStore,
    chunker: Chunker,
    state: WriteState,
    chunks: Vec<String>,
    total_size: usize,
    hasher: StreamingHasher,
    buffer: Vec<u8>,
}

impl BlobWriter {
    pub(crate) fn new(
        store: TensorStore,
        chunk_size: usize,
        artifact_id: String,
        filename: String,
        options: PutOptions,
        default_content_type: &str,
    ) -> Self {
        Self {
            store,
            chunker: Chunker::new(chunk_size),
            state: WriteState {
                artifact_id,
                filename,
                content_type: options
                    .content_type
                    .unwrap_or_else(|| default_content_type.to_string()),
                created_by: options.created_by.unwrap_or_default(),
                linked_to: options.linked_to,
                tags: options.tags,
                custom_metadata: options.metadata,
                embedding: options.embedding,
            },
            chunks: Vec::new(),
            total_size: 0,
            hasher: StreamingHasher::new(),
            buffer: Vec::new(),
        }
    }

    /// Write data to the artifact. Data is chunked and stored incrementally.
    ///
    /// # Errors
    ///
    /// Returns an error if chunk storage fails.
    #[allow(clippy::unused_async)]
    pub async fn write(&mut self, data: &[u8]) -> Result<()> {
        if data.is_empty() {
            return Ok(());
        }

        // Update the full-file hash
        self.hasher.update(data);
        self.total_size += data.len();

        // Add to buffer
        self.buffer.extend_from_slice(data);

        // Process complete chunks
        while self.buffer.len() >= self.chunker.chunk_size() {
            let chunk_data: Vec<u8> = self.buffer.drain(..self.chunker.chunk_size()).collect();
            let chunk = Chunk::new(chunk_data);
            self.store_chunk(chunk)?;
        }

        Ok(())
    }

    /// Store a chunk, handling deduplication.
    fn store_chunk(&mut self, chunk: Chunk) -> Result<()> {
        let chunk_key = chunk.key();

        // Check if chunk already exists (deduplication)
        if self.store.exists(&chunk_key) {
            // Increment reference count
            increment_chunk_refs(&self.store, &chunk_key)?;
        } else {
            // Store new chunk
            let mut tensor = TensorData::new();
            tensor.set(
                "_type",
                TensorValue::Scalar(ScalarValue::String("blob_chunk".to_string())),
            );
            tensor.set("_data", TensorValue::Scalar(ScalarValue::Bytes(chunk.data)));
            tensor.set(
                "_size",
                TensorValue::Scalar(ScalarValue::Int(i64::try_from(chunk.size).unwrap_or(0))),
            );
            tensor.set("_refs", TensorValue::Scalar(ScalarValue::Int(1)));
            tensor.set(
                "_created",
                TensorValue::Scalar(ScalarValue::Int(
                    i64::try_from(current_timestamp()).unwrap_or(0),
                )),
            );

            self.store.put(&chunk_key, tensor)?;
        }

        self.chunks.push(chunk_key);
        Ok(())
    }

    /// Finalize the artifact and return its ID.
    ///
    /// # Errors
    ///
    /// Returns an error if metadata storage fails.
    #[allow(clippy::unused_async)]
    pub async fn finish(mut self) -> Result<String> {
        // Flush remaining buffer
        if !self.buffer.is_empty() {
            let chunk = Chunk::new(std::mem::take(&mut self.buffer));
            self.store_chunk(chunk)?;
        }

        let content_type_for_idx = self.state.content_type.clone();
        let linked_to_for_idx = self.state.linked_to.clone();
        let tags_for_idx = self.state.tags.clone();

        let checksum = self.hasher.finalize();
        let tensor = build_metadata_tensor(
            &mut self.state,
            &mut self.chunks,
            self.total_size,
            self.chunker.chunk_size(),
            &checksum,
        );

        let meta_key = format!("_blob:meta:{}", self.state.artifact_id);
        self.store.put(&meta_key, tensor)?;

        Self::write_secondary_indexes(
            &self.store,
            &self.state.artifact_id,
            &content_type_for_idx,
            &linked_to_for_idx,
            &tags_for_idx,
        )?;

        Ok(self.state.artifact_id)
    }

    /// Write secondary index entries for content type, links, and tags.
    fn write_secondary_indexes(
        store: &TensorStore,
        artifact_id: &str,
        content_type: &str,
        linked_to: &[String],
        tags: &[String],
    ) -> Result<()> {
        if !content_type.is_empty() {
            let idx_key = format!("_blob:idx:ct:{content_type}:{artifact_id}");
            store.put(&idx_key, TensorData::new())?;
        }

        for entity in linked_to {
            let idx_key = format!("_blob:idx:link:{entity}:{artifact_id}");
            store.put(&idx_key, TensorData::new())?;
        }

        for tag in tags {
            let idx_key = format!("_blob:idx:tag:{tag}:{artifact_id}");
            store.put(&idx_key, TensorData::new())?;
        }

        Ok(())
    }

    /// Get the current total size written.
    #[must_use]
    pub const fn bytes_written(&self) -> usize {
        self.total_size
    }

    /// Get the number of chunks written so far.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)]
    pub fn chunks_written(&self) -> usize {
        self.chunks.len()
    }
}

/// Build the metadata tensor from the writer's accumulated state.
///
/// This is a free function rather than a method because `finish()` partially
/// moves `self.hasher` before calling this, which prevents borrowing `self`.
fn build_metadata_tensor(
    state: &mut WriteState,
    chunks: &mut Vec<String>,
    total_size: usize,
    chunk_size: usize,
    checksum: &str,
) -> TensorData {
    let now = current_timestamp();
    let mut tensor = TensorData::new();

    tensor.set(
        "_type",
        TensorValue::Scalar(ScalarValue::String("blob_artifact".to_string())),
    );
    tensor.set(
        "_id",
        TensorValue::Scalar(ScalarValue::String(state.artifact_id.clone())),
    );
    tensor.set(
        "_filename",
        TensorValue::Scalar(ScalarValue::String(std::mem::take(&mut state.filename))),
    );
    tensor.set(
        "_content_type",
        TensorValue::Scalar(ScalarValue::String(std::mem::take(&mut state.content_type))),
    );
    tensor.set(
        "_size",
        TensorValue::Scalar(ScalarValue::Int(i64::try_from(total_size).unwrap_or(0))),
    );
    tensor.set(
        "_checksum",
        TensorValue::Scalar(ScalarValue::String(checksum.to_string())),
    );
    tensor.set(
        "_chunk_size",
        TensorValue::Scalar(ScalarValue::Int(i64::try_from(chunk_size).unwrap_or(0))),
    );
    tensor.set(
        "_chunk_count",
        TensorValue::Scalar(ScalarValue::Int(i64::try_from(chunks.len()).unwrap_or(0))),
    );
    tensor.set("_chunks", TensorValue::Pointers(std::mem::take(chunks)));
    tensor.set(
        "_created",
        TensorValue::Scalar(ScalarValue::Int(i64::try_from(now).unwrap_or(0))),
    );
    tensor.set(
        "_modified",
        TensorValue::Scalar(ScalarValue::Int(i64::try_from(now).unwrap_or(0))),
    );
    tensor.set(
        "_created_by",
        TensorValue::Scalar(ScalarValue::String(std::mem::take(&mut state.created_by))),
    );

    let linked_to = std::mem::take(&mut state.linked_to);
    if !linked_to.is_empty() {
        tensor.set("_linked_to", TensorValue::Pointers(linked_to));
    }

    let tags = std::mem::take(&mut state.tags);
    if !tags.is_empty() {
        tensor.set(
            "_tags",
            TensorValue::Pointers(tags.into_iter().map(|t| format!("tag:{t}")).collect()),
        );
    }

    for (key, value) in std::mem::take(&mut state.custom_metadata) {
        tensor.set(
            format!("_meta:{key}"),
            TensorValue::Scalar(ScalarValue::String(value)),
        );
    }

    if let Some((embedding, model)) = state.embedding.take() {
        use tensor_store::SparseVector;
        let storage = if should_use_sparse(&embedding) {
            TensorValue::Sparse(SparseVector::from_dense(&embedding))
        } else {
            TensorValue::Vector(embedding)
        };
        tensor.set("_embedding", storage);
        tensor.set(
            "_embedded_model",
            TensorValue::Scalar(ScalarValue::String(model)),
        );
    }

    tensor
}

/// Streaming reader for downloading artifacts.
pub struct BlobReader {
    store: TensorStore,
    chunks: Vec<String>,
    current_chunk: usize,
    current_data: Option<Vec<u8>>,
    current_offset: usize,
    total_size: usize,
    bytes_read: usize,
    checksum: String,
}

impl BlobReader {
    /// Creates a new reader for the specified artifact.
    ///
    /// # Errors
    ///
    /// Returns an error if the artifact is not found.
    pub(crate) fn new(store: TensorStore, artifact_id: &str) -> Result<Self> {
        let meta_key = format!("_blob:meta:{artifact_id}");
        let tensor = store
            .get(&meta_key)
            .map_err(|_| BlobError::NotFound(artifact_id.to_string()))?;

        let chunks = get_pointers(&tensor, "_chunks")
            .ok_or_else(|| BlobError::NotFound(format!("chunks for {artifact_id}")))?;
        let total_size = usize::try_from(get_int(&tensor, "_size").unwrap_or(0)).unwrap_or(0);
        let checksum = get_string(&tensor, "_checksum").unwrap_or_default();

        Ok(Self {
            store,
            chunks,
            current_chunk: 0,
            current_data: None,
            current_offset: 0,
            total_size,
            bytes_read: 0,
            checksum,
        })
    }

    /// Read the next chunk. Returns None when all chunks have been read.
    ///
    /// # Errors
    ///
    /// Returns an error if a chunk is missing.
    #[allow(clippy::unused_async)]
    pub async fn next_chunk(&mut self) -> Result<Option<Vec<u8>>> {
        if self.current_chunk >= self.chunks.len() {
            return Ok(None);
        }

        let chunk_key = &self.chunks[self.current_chunk];
        let tensor = self
            .store
            .get(chunk_key)
            .map_err(|_| BlobError::ChunkMissing(chunk_key.clone()))?;

        let data = get_bytes(&tensor, "_data")
            .ok_or_else(|| BlobError::ChunkMissing(chunk_key.clone()))?;

        self.current_chunk += 1;
        self.bytes_read += data.len();

        Ok(Some(data))
    }

    /// Read all remaining data into a single buffer.
    ///
    /// # Errors
    ///
    /// Returns an error if a chunk is missing.
    pub async fn read_all(&mut self) -> Result<Vec<u8>> {
        let mut result = Vec::with_capacity(self.total_size);

        while let Some(chunk) = self.next_chunk().await? {
            result.extend(chunk);
        }

        Ok(result)
    }

    /// Read into a buffer, returning number of bytes read.
    ///
    /// # Errors
    ///
    /// Returns an error if a chunk is missing.
    ///
    /// # Panics
    ///
    /// This method will not panic under normal conditions. The internal unwrap
    /// is guarded by the preceding chunk load logic.
    pub async fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
        // Load chunk if needed
        if self.current_data.is_none()
            || self.current_offset >= self.current_data.as_ref().map_or(0, Vec::len)
        {
            match self.next_chunk().await? {
                Some(data) => {
                    self.current_data = Some(data);
                    self.current_offset = 0;
                },
                None => return Ok(0),
            }
        }

        let data = self.current_data.as_ref().unwrap();
        let remaining = &data[self.current_offset..];
        let to_copy = remaining.len().min(buf.len());

        buf[..to_copy].copy_from_slice(&remaining[..to_copy]);
        self.current_offset += to_copy;

        Ok(to_copy)
    }

    /// Verify the artifact checksum.
    ///
    /// # Errors
    ///
    /// Returns an error if a chunk is missing.
    pub async fn verify(&mut self) -> Result<bool> {
        let mut hasher = StreamingHasher::new();

        // Reset to start
        self.current_chunk = 0;
        self.bytes_read = 0;

        while let Some(chunk) = self.next_chunk().await? {
            hasher.update(&chunk);
        }

        let actual = hasher.finalize();
        Ok(actual == self.checksum)
    }

    /// Get the expected checksum.
    #[must_use]
    pub fn checksum(&self) -> &str {
        &self.checksum
    }

    /// Get total artifact size.
    #[must_use]
    pub const fn total_size(&self) -> usize {
        self.total_size
    }

    /// Get bytes read so far.
    #[must_use]
    pub const fn bytes_read(&self) -> usize {
        self.bytes_read
    }

    /// Get the number of chunks.
    #[must_use]
    #[allow(clippy::missing_const_for_fn)]
    pub fn chunk_count(&self) -> usize {
        self.chunks.len()
    }
}

// Helper functions

fn current_timestamp() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

pub fn get_int(tensor: &TensorData, field: &str) -> Option<i64> {
    match tensor.get(field) {
        Some(TensorValue::Scalar(ScalarValue::Int(i))) => Some(*i),
        _ => None,
    }
}

pub fn get_string(tensor: &TensorData, field: &str) -> Option<String> {
    match tensor.get(field) {
        Some(TensorValue::Scalar(ScalarValue::String(s))) => Some(s.clone()),
        _ => None,
    }
}

pub fn get_bytes(tensor: &TensorData, field: &str) -> Option<Vec<u8>> {
    match tensor.get(field) {
        Some(TensorValue::Scalar(ScalarValue::Bytes(b))) => Some(b.clone()),
        _ => None,
    }
}

pub fn get_pointers(tensor: &TensorData, field: &str) -> Option<Vec<String>> {
    match tensor.get(field) {
        Some(TensorValue::Pointers(p)) => Some(p.clone()),
        _ => None,
    }
}

#[cfg(feature = "vector")]
pub fn get_vector(tensor: &TensorData, field: &str) -> Option<Vec<f32>> {
    match tensor.get(field) {
        Some(TensorValue::Vector(v)) => Some(v.clone()),
        Some(TensorValue::Sparse(s)) => Some(s.to_dense()),
        _ => None,
    }
}

/// Check if a vector should use sparse storage (50% threshold).
pub fn should_use_sparse(vector: &[f32]) -> bool {
    if vector.is_empty() {
        return false;
    }
    let nnz = vector.iter().filter(|&&v| v.abs() > 1e-6).count();
    // For 0.5 threshold: sparse if nnz <= len/2, i.e., nnz*2 <= len
    nnz * 2 <= vector.len()
}

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

    fn create_test_store() -> TensorStore {
        TensorStore::new()
    }

    #[tokio::test]
    async fn test_blob_writer_small_file() {
        let store = create_test_store();
        let mut writer = BlobWriter::new(
            store.clone(),
            1024,
            "test-artifact".to_string(),
            "test.txt".to_string(),
            PutOptions::default(),
            "text/plain",
        );

        writer.write(b"hello world").await.unwrap();
        let artifact_id = writer.finish().await.unwrap();

        assert_eq!(artifact_id, "test-artifact");

        // Verify metadata was stored
        let meta_key = format!("_blob:meta:{artifact_id}");
        let tensor = store.get(&meta_key).unwrap();
        assert_eq!(
            get_string(&tensor, "_filename"),
            Some("test.txt".to_string())
        );
        assert_eq!(get_int(&tensor, "_size"), Some(11));
        assert_eq!(get_int(&tensor, "_chunk_count"), Some(1));
    }

    #[tokio::test]
    async fn test_blob_writer_multi_chunk() {
        let store = create_test_store();
        let chunk_size = 10;
        let mut writer = BlobWriter::new(
            store.clone(),
            chunk_size,
            "multi-chunk".to_string(),
            "data.bin".to_string(),
            PutOptions::default(),
            "application/octet-stream",
        );

        // Write 25 bytes = 3 chunks (10 + 10 + 5)
        writer.write(&[0u8; 25]).await.unwrap();
        let artifact_id = writer.finish().await.unwrap();

        let meta_key = format!("_blob:meta:{artifact_id}");
        let tensor = store.get(&meta_key).unwrap();
        assert_eq!(get_int(&tensor, "_chunk_count"), Some(3));
        assert_eq!(get_int(&tensor, "_size"), Some(25));
    }

    #[tokio::test]
    async fn test_blob_writer_incremental_write() {
        let store = create_test_store();
        let mut writer = BlobWriter::new(
            store.clone(),
            10,
            "incremental".to_string(),
            "data.bin".to_string(),
            PutOptions::default(),
            "application/octet-stream",
        );

        // Write in small increments
        writer.write(&[1, 2, 3]).await.unwrap();
        writer.write(&[4, 5, 6]).await.unwrap();
        writer.write(&[7, 8, 9, 10, 11, 12]).await.unwrap();

        let artifact_id = writer.finish().await.unwrap();

        let meta_key = format!("_blob:meta:{artifact_id}");
        let tensor = store.get(&meta_key).unwrap();
        assert_eq!(get_int(&tensor, "_size"), Some(12));
    }

    #[tokio::test]
    async fn test_blob_writer_with_options() {
        let store = create_test_store();
        let options = PutOptions::new()
            .with_content_type("application/pdf")
            .with_created_by("user:alice")
            .with_link("task:123")
            .with_tag("quarterly")
            .with_meta("author", "Alice");

        let mut writer = BlobWriter::new(
            store.clone(),
            1024,
            "with-options".to_string(),
            "report.pdf".to_string(),
            options,
            "application/octet-stream",
        );

        writer.write(b"PDF content").await.unwrap();
        let artifact_id = writer.finish().await.unwrap();

        let meta_key = format!("_blob:meta:{artifact_id}");
        let tensor = store.get(&meta_key).unwrap();
        assert_eq!(
            get_string(&tensor, "_content_type"),
            Some("application/pdf".to_string())
        );
        assert_eq!(
            get_string(&tensor, "_created_by"),
            Some("user:alice".to_string())
        );
        assert_eq!(
            get_string(&tensor, "_meta:author"),
            Some("Alice".to_string())
        );
    }

    #[tokio::test]
    async fn test_blob_reader_small_file() {
        let store = create_test_store();

        // First write
        let mut writer = BlobWriter::new(
            store.clone(),
            1024,
            "read-test".to_string(),
            "test.txt".to_string(),
            PutOptions::default(),
            "text/plain",
        );
        writer.write(b"hello world").await.unwrap();
        writer.finish().await.unwrap();

        // Then read
        let mut reader = BlobReader::new(store, "read-test").unwrap();
        let data = reader.read_all().await.unwrap();

        assert_eq!(data, b"hello world");
        assert_eq!(reader.bytes_read(), 11);
    }

    #[tokio::test]
    async fn test_blob_reader_multi_chunk() {
        let store = create_test_store();
        let data = vec![42u8; 25];

        // Write
        let mut writer = BlobWriter::new(
            store.clone(),
            10,
            "multi-read".to_string(),
            "data.bin".to_string(),
            PutOptions::default(),
            "application/octet-stream",
        );
        writer.write(&data).await.unwrap();
        writer.finish().await.unwrap();

        // Read
        let mut reader = BlobReader::new(store, "multi-read").unwrap();
        let result = reader.read_all().await.unwrap();

        assert_eq!(result, data);
        assert_eq!(reader.chunk_count(), 3);
    }

    #[tokio::test]
    async fn test_blob_reader_chunk_by_chunk() {
        let store = create_test_store();

        // Write 30 bytes in 10-byte chunks
        let mut writer = BlobWriter::new(
            store.clone(),
            10,
            "chunk-read".to_string(),
            "data.bin".to_string(),
            PutOptions::default(),
            "application/octet-stream",
        );
        writer.write(&[1u8; 30]).await.unwrap();
        writer.finish().await.unwrap();

        // Read chunk by chunk
        let mut reader = BlobReader::new(store, "chunk-read").unwrap();
        let chunk1 = reader.next_chunk().await.unwrap().unwrap();
        let chunk2 = reader.next_chunk().await.unwrap().unwrap();
        let chunk3 = reader.next_chunk().await.unwrap().unwrap();
        let chunk4 = reader.next_chunk().await.unwrap();

        assert_eq!(chunk1.len(), 10);
        assert_eq!(chunk2.len(), 10);
        assert_eq!(chunk3.len(), 10);
        assert!(chunk4.is_none());
    }

    #[tokio::test]
    async fn test_blob_reader_verify() {
        let store = create_test_store();
        let data = b"verification test data";

        // Write
        let mut writer = BlobWriter::new(
            store.clone(),
            1024,
            "verify-test".to_string(),
            "test.txt".to_string(),
            PutOptions::default(),
            "text/plain",
        );
        writer.write(data).await.unwrap();
        writer.finish().await.unwrap();

        // Verify
        let mut reader = BlobReader::new(store, "verify-test").unwrap();
        let valid = reader.verify().await.unwrap();
        assert!(valid);
    }

    #[tokio::test]
    async fn test_blob_reader_not_found() {
        let store = create_test_store();
        let result = BlobReader::new(store, "nonexistent");
        assert!(matches!(result, Err(BlobError::NotFound(_))));
    }

    #[tokio::test]
    async fn test_deduplication() {
        let store = create_test_store();
        let data = vec![42u8; 10];

        // Write same data twice
        let mut writer1 = BlobWriter::new(
            store.clone(),
            10,
            "dedup-1".to_string(),
            "file1.bin".to_string(),
            PutOptions::default(),
            "application/octet-stream",
        );
        writer1.write(&data).await.unwrap();
        writer1.finish().await.unwrap();

        let mut writer2 = BlobWriter::new(
            store.clone(),
            10,
            "dedup-2".to_string(),
            "file2.bin".to_string(),
            PutOptions::default(),
            "application/octet-stream",
        );
        writer2.write(&data).await.unwrap();
        writer2.finish().await.unwrap();

        // Count chunks - should only be 1 due to deduplication
        let chunk_count = store.scan("_blob:chunk:").len();
        assert_eq!(chunk_count, 1);

        // But the chunk should have ref count of 2
        let chunks = store.scan("_blob:chunk:");
        let chunk = store.get(&chunks[0]).unwrap();
        assert_eq!(get_int(&chunk, "_refs"), Some(2));
    }
}