fn compact(
        &mut self,
        branches: usize,
        config: StoreConfig,
        store_version: u32,
    ) -> Result<bool> {
        // Determine how many base layer leafs there are (and in bytes).
        let leafs = get_merkle_tree_leafs(self.len, branches)?;
        let data_width = leafs * self.elem_len;

        // Calculate how large the cache should be (based on the
        // config.rows_to_discard param).
        let cache_size =
            get_merkle_tree_cache_size(leafs, branches, config.rows_to_discard)? * self.elem_len;

        // The file cannot be compacted if the specified configuration
        // requires either 1) nothing to be cached, or 2) everything
        // to be cached.  For #1, create a data store of leafs and do
        // not use that store as backing for the MT.  For #2, avoid
        // calling this method.  To resolve, provide a sane
        // configuration.
        ensure!(
            cache_size < self.len * self.elem_len && cache_size != 0,
            "Cannot compact with this configuration"
        );

        let v1 = store_version == StoreConfigDataVersion::One as u32;
        let start: u64 = if v1 { data_width as u64 } else { 0 };

        // Calculate cache start and updated size with repect to the
        // data size.
        let cache_start = self.store_size - cache_size;

        // Seek the reader to the start of the cached data.
        let mut reader = OpenOptions::new()
            .read(true)
            .open(StoreConfig::data_path(&config.path, &config.id))?;
        reader.seek(SeekFrom::Start(cache_start as u64))?;

        // Make sure the store file is opened for read/write.
        self.file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(StoreConfig::data_path(&config.path, &config.id))?;

        // Seek the writer.
        self.file.seek(SeekFrom::Start(start))?;

        // Copy the data from the cached region to the writer.
        let written = copy(&mut reader, &mut self.file)?;
        ensure!(written == cache_size as u64, "Failed to copy all data");
        if v1 {
            // Truncate the data on-disk to be the base layer data
            // followed by the cached data.
            self.file.set_len((data_width + cache_size) as u64)?;
            // Adjust our length for internal consistency.
            self.len = (data_width + cache_size) / self.elem_len;
        } else {
            // Truncate the data on-disk to be only the cached data.
            self.file.set_len(cache_size as u64)?;

            // Adjust our length to be the cached elements only for
            // internal consistency.
            self.len = cache_size / self.elem_len;
        }

        // Sync and sanity check that we match on disk (this can be
        // removed if needed).
        self.sync()?;
        let metadata = self.file.metadata()?;
        let store_size = metadata.len() as usize;
        ensure!(
            self.len * self.elem_len == store_size,
            "Inconsistent metadata detected"
        );

        Ok(true)
    }
// Specifically, this method truncates an existing DiskStore and
// formats the data in such a way that is compatible with future
// access using LevelCacheStore::new_from_disk.
Specifically, this method truncates an existing DiskStore and
formats the data in such a way that is compatible with future
access using LevelCacheStore::new_from_disk.
Specifically this method truncates an existing DiskStore and formats the data in such a way that is compatible with future access using LevelCacheStore :: new_from_disk .
fn compact(
        &mut self,
        branches: usize,
        config: StoreConfig,
        store_version: u32,
    ) -> Result<bool> {
        // Determine how many base layer leafs there are (and in bytes).
        let leafs = get_merkle_tree_leafs(self.len, branches)?;
        let data_width = leafs * self.elem_len;
        // Calculate how large the cache should be (based on the
        // config.rows_to_discard param).
        let cache_size =
            get_merkle_tree_cache_size(leafs, branches, config.rows_to_discard)? * self.elem_len;
        // The file cannot be compacted if the specified configuration
        // requires either 1) nothing to be cached, or 2) everything
        // to be cached.  For #1, create a data store of leafs and do
        // not use that store as backing for the MT.  For #2, avoid
        // calling this method.  To resolve, provide a sane
        // configuration.
        ensure!(
            cache_size < self.len * self.elem_len && cache_size != 0,
            "Cannot compact with this configuration"
        );
        let v1 = store_version == StoreConfigDataVersion::One as u32;
        let start: u64 = if v1 { data_width as u64 } else { 0 };
        // Calculate cache start and updated size with repect to the
        // data size.
        let cache_start = self.store_size - cache_size;
        // Seek the reader to the start of the cached data.
        let mut reader = OpenOptions::new()
            .read(true)
            .open(StoreConfig::data_path(&config.path, &config.id))?;
        reader.seek(SeekFrom::Start(cache_start as u64))?;
        // Make sure the store file is opened for read/write.
        self.file = OpenOptions::new()
            .read(true)
            .write(true)
            .open(StoreConfig::data_path(&config.path, &config.id))?;
        // Seek the writer.
        self.file.seek(SeekFrom::Start(start))?;
        // Copy the data from the cached region to the writer.
        let written = copy(&mut reader, &mut self.file)?;
        ensure!(written == cache_size as u64, "Failed to copy all data");
        if v1 {
            // Truncate the data on-disk to be the base layer data
            // followed by the cached data.
            self.file.set_len((data_width + cache_size) as u64)?;
            // Adjust our length for internal consistency.
            self.len = (data_width + cache_size) / self.elem_len;
        } else {
            // Truncate the data on-disk to be only the cached data.
            self.file.set_len(cache_size as u64)?;
            // Adjust our length to be the cached elements only for
            // internal consistency.
            self.len = cache_size / self.elem_len;
        }
        // Sync and sanity check that we match on disk (this can be
        // removed if needed).
        self.sync()?;
        let metadata = self.file.metadata()?;
        let store_size = metadata.len() as usize;
        ensure!(
            self.len * self.elem_len == store_size,
            "Inconsistent metadata detected"
        );
        Ok(true)
    }
fn compact ( & mut self , branches : usize , config : StoreConfig , store_version : u32 , ) -> Result < bool > { // Determine how many base layer leafs there are (and in bytes). let leafs = get_merkle_tree_leafs ( self . len , branches ) ? ; let data_width = leafs * self . elem_len ; // Calculate how large the cache should be (based on the // config.rows_to_discard param). let cache_size = get_merkle_tree_cache_size ( leafs , branches , config . rows_to_discard ) ? * self . elem_len ; // The file cannot be compacted if the specified configuration // requires either 1) nothing to be cached, or 2) everything // to be cached.  For #1, create a data store of leafs and do // not use that store as backing for the MT.  For #2, avoid // calling this method.  To resolve, provide a sane // configuration. ensure ! ( cache_size self len self elem_len cache_size 0 " " ) ; let v1 = store_version == StoreConfigDataVersion :: One as u32 ; let start : u64 = if v1 { data_width as u64 } else { 0 } ; // Calculate cache start and updated size with repect to the // data size. let cache_start = self . store_size - cache_size ; // Seek the reader to the start of the cached data. let mut reader = OpenOptions :: new ( ) . read ( true ) . open ( StoreConfig :: data_path ( & config . path , & config . id ) ) ? ; reader . seek ( SeekFrom :: Start ( cache_start as u64 ) ) ? ; // Make sure the store file is opened for read/write. self . file = OpenOptions :: new ( ) . read ( true ) . write ( true ) . open ( StoreConfig :: data_path ( & config . path , & config . id ) ) ? ; // Seek the writer. self . file . seek ( SeekFrom :: Start ( start ) ) ? ; // Copy the data from the cached region to the writer. let written = copy ( & mut reader , & mut self . file ) ? ; ensure ! ( written cache_size as u64 " " ) ; if v1 { // Truncate the data on-disk to be the base layer data // followed by the cached data. self . file . set_len ( ( data_width + cache_size ) as u64 ) ? ; // Adjust our length for internal consistency. self . len = ( data_width + cache_size ) / self . elem_len ; } else { // Truncate the data on-disk to be only the cached data. self . file . set_len ( cache_size as u64 ) ? ; // Adjust our length to be the cached elements only for // internal consistency. self . len = cache_size / self . elem_len ; } // Sync and sanity check that we match on disk (this can be // removed if needed). self . sync ( ) ? ; let metadata = self . file . metadata ( ) ? ; let store_size = metadata . len ( ) as usize ; ensure ! ( self len self elem_len store_size " " ) ; Ok ( true ) }
Specifically, this method truncates an existing DiskStore and
formats the data in such a way that is compatible with future
access using LevelCacheStore::new_from_disk.
Specifically this method truncates an existing DiskStore and formats the data in such a way that is compatible with future access using LevelCacheStore :: new_from_disk .
&mut self
branches
usize
config
StoreConfig
store_version
u32