如何使用散列函数在文件系统中存储〜400万个图像 [英] how to use hash function for storing ~4 million images in file system

查看:136
本文介绍了如何使用散列函数在文件系统中存储〜400万个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想存储大约100万的图像,这些图像可以调整到4种不同的类型,所以会有大约400万的图像。我应该如何使用像md5这样的散列函数来在目录结构中均匀和唯一地分发图像?

在下文中,我假设您的一百万个输入文件具有唯一的文件名。



例子也会把原来的和缩略图放在同一个目录下。这可以很容易地删除或找到文件。



首先,您需要一个方法将文件名映射到一个目录:

  // $ id =一个唯一的标识符(一个文件名)
//可以使这个id与原始的相同,
//以及任何缩略图。您的图像和变体将全部
//最终在同一个目录中。

// $ levels_deep =您想去的目录数量。
//想要更多关卡?使用一个更长的
输出的哈希方法,比如sha1(40个字符)。
$ b $函数getDir($ id,$ levels_deep = 32){
$ file_hash = md5($ id);
$ dirname = implode(/,str_split(
substr($ file_hash,0,$ levels_deep)
));
返回$ dirname;
}

接下来,您需要写出这些文件:



$ $ $ $ $ $ $ $ code $函数存储($ dirname,$文件名){
//这里的`true`标志将有`mkdir`递归创建目录。
if(!file_exists($ dirname)&!mkdir($ dirname,0777,true))
抛出新异常(Could not create directory。$ dirname);

return file_put_contents(
$ dirname。/。$ filename,
示例文件的内容。\\\

);



$ b



$ b pre> store(getDir(myfile.jpg,4),myfile.jpg);
store(getDir(myfile.jpg,4),myfile_large.jpg);
store(getDir(myfile.jpg,4),myfile_small.jpg);
store(getDir(myfile.jpg,4),myfile_thumb.jpg);
store(getDir(someOtherFile.jpg,4),someOtherFile.jpg);

这将在以下位置存储上述五个文件:

  /d/0/6/a/myfile_large.jpg 
/d/0/6/a/myfile_small.jpg
/ d / 0/6 / a / myfile_thumb.jpg
/d/0/6/a/myfile.jpg
/1/4/4/d/someOtherFile.jpg
$ b $ p
$ b

我没有看到md5位的随机性,但应该足够均匀分配。

I want to store ~1 million images which would be resized into 4 different kinds,so there would be ~4 million images.How should I use hash functions like md5 to evenly and uniquely distribute images in the directory structure?

解决方案

As others have noted, multiple file names can theoretically hash to the same value. That's easily solved by keeping the original filename, in addition to the hash.

In the following, I'm assuming that your one million input files have unique file names.

This example will also put the original and its thumbnails in the same directory. That will make it easy to remove or find files.

First of all, you'll want a method to map a file name to a directory:

// $id = A unique identifier (a filename)
//       It could be useful to make this id the same for the original, 
//       as well as any thumbnails. Your image and variants will all
//       then end up in the same directory.

// $levels_deep = The number of directories deep you want to go.
//                Want more levels? Use a hashing method with a longer
//                output, such as sha1 (40 characters).

function getDir($id, $levels_deep = 32) {
    $file_hash   = md5($id);
    $dirname     = implode("/", str_split(
        substr($file_hash, 0, $levels_deep)
    ));
    return $dirname;
}

Next, you need to write out the files:

function store($dirname, $filename) {
    // The `true` flag here will have `mkdir` create directories recursively.  
    if(!file_exists($dirname) && !mkdir($dirname, 0777, true))
        throw new Exception("Could not create directory " . $dirname);

    return file_put_contents(
        $dirname . "/" . $filename,
        "Contents of example file.\n"
    );
}

Example use:

store(getDir("myfile.jpg", 4), "myfile.jpg");
store(getDir("myfile.jpg", 4), "myfile_large.jpg");
store(getDir("myfile.jpg", 4), "myfile_small.jpg");
store(getDir("myfile.jpg", 4), "myfile_thumb.jpg");
store(getDir("someOtherFile.jpg", 4), "someOtherFile.jpg");

This will store the above mentioned five files at these locations:

/d/0/6/a/myfile_large.jpg
/d/0/6/a/myfile_small.jpg
/d/0/6/a/myfile_thumb.jpg
/d/0/6/a/myfile.jpg
/1/4/4/d/someOtherFile.jpg

I have not looked into the 'randomness' of md5 bits, but it ought to be distributed evenly enough.

这篇关于如何使用散列函数在文件系统中存储〜400万个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆