mysql - 在一个多人共享的文件夹内, 创建文件夹/文件, 名字重复加数字后缀有没有好的算法
伊谢尔伦
伊谢尔伦 2017-04-17 16:46:40
0
4
509
伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(4)
刘奇

Using Redis’s Bitmap is very suitable to solve this problem, taking “photos” as an example

  1. "Photo" creates a bitmap for the key. The bit with a value of 1 represents that it has been used (no need to create it, just query it directly)

  2. Find the index of the leftmost bit with 0: bitpos "photo" 0

  3. The index found is the smallest numerical space

Note: If there are multiple servers and multi-threads concurrently, you can consider using the distributed lock implemented by Redis, or use the watch command
The pseudo code is as follows

if ('OK'.equals(redis.setnx("照片.lock")) {
    pos = redis.bitpos("照片", 0);
    redis.setbit("照片", pos, 1);
    return pos;
}
PHPzhong

The hash table uses the file name as the key. Since it generates an ordered list of [1..MaxNum], MaxNum takes a number that you think is reasonable. Synchronize hash tables.
Every time you create a file, check the hash table to see if there is the key, and if so, use the first value of list.pop.
Every time you delete a file, push the numbers in the file() back to the list
The hash table controls the size. Excessive data can be stored in the file system [database, etc.]. Every time you query the hash and cannot find the data, you need to go to the file system. Find the call. Similar to the memory paging cache mechanism.

伊谢尔伦
import os

def make_dir(filename,i=1):
    fullname = '%s (%s)' % (filename,i)

    if not os.path.exists(filename):
        os.mkdir(filename)
        return filename

    if os.path.exists(fullname):
        i += 1
        return make_dir(filename,i)
    else:
        os.mkdir(fullname)
        return fullname

for i in range(3):
    print(make_dir('照片'))
伊谢尔伦

The simplest idea is to maintain two sets of data
Pseudo code

removeNameArray = {};
NameArrayLen = 0;
if(removeNameArray.length>0){
    name = removeNameArray.pop();
}else{
    name = name+(NameArrayLen++);
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template