1个不稳定版本
0.1.0 | 2021年10月30日 |
---|
#29 in #companion
在vid_dup_finder中使用
27KB
334 代码行
video_hash_filesystem_cache
vid_dup_finder crate使用的视频哈希的缓存
lib.rs
:
一个包含缓存的实用库,用于vid_dup_finder_lib::VideoHash。此库定义了结构体[VideoHashFilesystemCache],它将视频的哈希缓存到磁盘。
缓存存储底层文件的最后修改时间,并在此更改时自动更新。
示例(使用单个文件)
use video_hash_filesystem_cache::*;
use vid_dup_finder_lib::*;
// Create a cache on disk which will save itself to disk after every 100 changes
let cache = VideoHashFilesystemCache::new(100, cache_file_path).expect("failed to create cache");
// Now create a video hash by calling get_update on the cache.
let video_hash : VideoHash = match cache.fetch_update(&vid_file_path) {
Ok(Some(Ok(hash))) => hash, // A hash was successfully created/fetched
Ok(None) => panic!(), // None is returned when vid_file_path is removed from the filesystem
Ok(Some(Err(_e))) => panic!(), // Ok(Some(Err())) is returned when an error occurs while creating a VideoHash
Err(cache_error) => panic!(), //"All other Io errors")
};
// Subsequent calls will fetch the hash from the cache instead of creating it from the filesystem.
// The cache must be saved to disk at the end of execution,
// otherwise changes since the last save will be lost.
cache.save().unwrap()
一次性缓存多个视频
结构体crate::FileProjection用于一次性更新多个文件。当使用一组起始路径创建时,它可以传递给缓存以更新这些起始路径的所有子文件。
示例(缓存整个目录)
use video_hash_filesystem_cache::*;
use vid_dup_finder_lib::*;
// Create a cache on disk which will save itself to disk after every 100 changes
let cache = VideoHashFilesystemCache::new(100, cache_file_path).expect("failed to create cache");
// Create the projection representing two directories of video files.
// the second argument is a list of directories/paths to be ignored
let mut projection = FileProjection::new(&video_dirs, &excl_dirs, &excl_exts).unwrap();
let project_errs = projection.project_using_fs().unwrap();
// Update the cache using the projection. a list of individual loading errors will be returned.
let cache_update_errs = cache.update_using_fs(&projection).unwrap();
// Now all videos under videos_dir_1 and videos_dir_2 will be cached.
// They can be retrieved from the cache without touching the filesystem using
// VideoHashFilesystemCache::fetch
let video_hash : VideoHash = cache.fetch(&vid_file_path).unwrap();
// The cache must be saved to disk at the end of execution,
// otherwise changes since the last save will be lost.
cache.save().unwrap()
依赖项
~21–30MB
~246K SLoC