Mercurial > lbo > hg > sstable
changeset 19:ba64383e5593
Implement helper functions to quickly read from/write to files.
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Wed, 23 Nov 2016 20:12:20 +0100 |
parents | 186231a15135 |
children | ae10f3dadcea |
files | TODO src/table_builder.rs src/table_reader.rs |
diffstat | 3 files changed, 24 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/TODO Wed Nov 23 13:22:47 2016 +0000 +++ b/TODO Wed Nov 23 20:12:20 2016 +0100 @@ -1,3 +0,0 @@ -* Implement per-block compression using `snap` (http://burntsushi.net/rustdoc/snap/) -* Implement helpers to easily open files for reading/writing -* Write blocks directly to disk while writing? (Avoiding high memory consumption)
--- a/src/table_builder.rs Wed Nov 23 13:22:47 2016 +0000 +++ b/src/table_builder.rs Wed Nov 23 20:12:20 2016 +0100 @@ -3,7 +3,9 @@ use options::{CompressionType, BuildOptions}; use iterator::{Comparator, StandardComparator}; -use std::io::Write; +use std::io::{Result, Write}; +use std::fs::{File, OpenOptions}; +use std::path::Path; use std::cmp::Ordering; use crc::crc32; @@ -107,6 +109,15 @@ } } +impl TableBuilder<StandardComparator, File> { + /// Open/create a file for writing a table. + /// This will truncate the file, if it exists. + pub fn new_to_file(file: &Path) -> Result<TableBuilder<StandardComparator, File>> { + let f = try!(OpenOptions::new().create(true).write(true).truncate(true).open(file)); + Ok(TableBuilder::new(f, BuildOptions::default(), StandardComparator)) + } +} + impl<C: Comparator, Dst: Write> TableBuilder<C, Dst> { /// Create a new TableBuilder. pub fn new(dst: Dst, opt: BuildOptions, cmp: C) -> TableBuilder<C, Dst> {
--- a/src/table_reader.rs Wed Nov 23 13:22:47 2016 +0000 +++ b/src/table_reader.rs Wed Nov 23 20:12:20 2016 +0100 @@ -10,6 +10,8 @@ use std::cmp::Ordering; use std::io::{Error, ErrorKind, Read, Seek, SeekFrom, Result}; +use std::fs::{File, OpenOptions}; +use std::path::Path; /// Reads the table footer. fn read_footer<R: Read + Seek>(f: &mut R, size: usize) -> Result<Footer> { @@ -53,6 +55,16 @@ } } +impl Table<File, StandardComparator> { + /// Directly open a file for reading. + pub fn new_from_file(file: &Path) -> Result<Table<File, StandardComparator>> { + let f = try!(OpenOptions::new().read(true).open(file)); + let len = try!(f.metadata()).len() as usize; + + Table::new(f, len, ReadOptions::default(), StandardComparator) + } +} + impl<R: Read + Seek, C: Comparator> Table<R, C> { /// Open a table for reading. Note: The comparator must be the same that was chosen when /// building the table.