From 9222c127fc7c5af62a64dd5cf0fa46c42783d990 Mon Sep 17 00:00:00 2001
From: edef <edef@unfathomable.blue>
Date: Mon, 25 Apr 2022 00:18:39 +0000
Subject: ripple/fossil: implement incremental blob reading

Change-Id: I69ae53824e149133aa6bb61dda201f972c840b1f
---
 ripple/fossil/src/lib.rs | 33 ++++++++++++++++++++++++++++-----
 1 file changed, 28 insertions(+), 5 deletions(-)

(limited to 'ripple/fossil')

diff --git a/ripple/fossil/src/lib.rs b/ripple/fossil/src/lib.rs
index 4c747e8..ed55138 100644
--- a/ripple/fossil/src/lib.rs
+++ b/ripple/fossil/src/lib.rs
@@ -157,6 +157,12 @@ impl Store {
 	}
 
 	pub fn read_blob(&self, ident: Digest) -> Vec<u8> {
+		let mut buffer = Vec::new();
+		self.open_blob(ident).read_to_end(&mut buffer).unwrap();
+		buffer
+	}
+
+	pub fn open_blob(&self, ident: Digest) -> Blob {
 		let buf = self
 			.db
 			.get(&*ident.as_bytes())
@@ -169,7 +175,7 @@ impl Store {
 			bao_inline,
 		} = store::Blob::decode(&*buf).unwrap();
 
-		let mut blob = bao::decode::Decoder::new_outboard(
+		Blob(bao::decode::Decoder::new_outboard(
 			RawBlob {
 				store: self,
 				slice: Slice { offset, length },
@@ -177,12 +183,29 @@ impl Store {
 			},
 			io::Cursor::new(bao_inline),
 			&ident,
-		);
+		))
+	}
+}
 
-		let mut buffer = Vec::new();
-		blob.read_to_end(&mut buffer).unwrap();
+pub struct Blob<'a>(bao::decode::Decoder<RawBlob<'a>, std::io::Cursor<Vec<u8>>>);
 
-		buffer
+impl io::Read for Blob<'_> {
+	fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
+		self.0.read(buf)
+	}
+}
+
+impl io::Seek for Blob<'_> {
+	fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
+		self.0.seek(pos)
+	}
+
+	fn rewind(&mut self) -> io::Result<()> {
+		self.0.rewind()
+	}
+
+	fn stream_position(&mut self) -> io::Result<u64> {
+		self.0.stream_position()
 	}
 }
 
-- 
cgit 1.4.1