Update id generation methods

This commit is contained in:
syuilo 2019-04-14 01:08:26 +09:00
parent b247be80cc
commit e64912545a
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
6 changed files with 30 additions and 72 deletions

View file

@ -127,19 +127,11 @@ drive:
# change it according to your preferences. # change it according to your preferences.
# Available methods: # Available methods:
# aid1 ... Use AID for ID generation (with random 1 char) # aid ... Use AID for ID generation
# aid2 ... Use AID for ID generation (with random 2 chars)
# aid3 ... Use AID for ID generation (with random 3 chars)
# aid4 ... Use AID for ID generation (with random 4 chars)
# ulid ... Use ulid for ID generation # ulid ... Use ulid for ID generation
# objectid ... This is left for backward compatibility. # objectid ... This is left for backward compatibility.
# AID(n) is the original ID generation method. # AID is the original ID generation method.
# The trailing n represents the number of random characters that
# will be suffixed.
# The larger n is the safer. If n is small, the possibility of
# collision at the same time increases, but there are also
# advantages such as shortening of the URL.
# ULID: Universally Unique Lexicographically Sortable Identifier. # ULID: Universally Unique Lexicographically Sortable Identifier.
# for more details: https://github.com/ulid/spec # for more details: https://github.com/ulid/spec
@ -148,7 +140,7 @@ drive:
# ObjectID is the method used in previous versions of Misskey. # ObjectID is the method used in previous versions of Misskey.
# * Choose this if you are migrating from a previous Misskey. # * Choose this if you are migrating from a previous Misskey.
id: 'aid2' id: 'aid'
# ┌─────────────────────┐ # ┌─────────────────────┐
#───┘ Other configuration └───────────────────────────────────── #───┘ Other configuration └─────────────────────────────────────

View file

@ -1,26 +0,0 @@
// AID
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さnの[ランダムな文字列]
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
const TIME2000 = 946684800000;
function getTime(time: number) {
time = time - TIME2000;
if (time < 0) time = 0;
return time.toString(36);
}
function getRandom(length: number) {
let str = '';
for (let i = 0; i < length; i++) {
str += CHARS[Math.floor(Math.random() * CHARS.length)];
}
return str;
}
export function genAid(date: Date, rand: number): string {
return getTime(date.getTime()).padStart(8, CHARS[0]) + getRandom(rand);
}

View file

@ -1,26 +0,0 @@
// AID(Cheep)
// 長さ6の[2000年1月1日からの経過秒をbase36でエンコードしたもの] + 長さ3の[ランダムな文字列]
const CHARS = '0123456789abcdefghijklmnopqrstuvwxyz';
const TIME2000 = 946684800000;
function getTime(time: number) {
time = time - TIME2000;
if (time < 0) time = 0;
time = Math.floor(time / 1000);
return time.toString(36);
}
function getRandom() {
let str = '';
for (let i = 0; i < 3; i++) {
str += CHARS[Math.floor(Math.random() * CHARS.length)];
}
return str;
}
export function genAidc(date: Date): string {
return getTime(date.getTime()).padStart(6, CHARS[0]) + getRandom();
}

View file

@ -1,7 +1,6 @@
import { ulid } from 'ulid'; import { ulid } from 'ulid';
import { genAid } from './aid'; import { genAid } from './id/aid';
import { genAidc } from './aidc'; import { genObjectId } from './id/object-id';
import { genObjectId } from './object-id';
import config from '../config'; import config from '../config';
const metohd = config.id.toLowerCase(); const metohd = config.id.toLowerCase();
@ -10,11 +9,7 @@ export function genId(date?: Date): string {
if (!date || (date > new Date())) date = new Date(); if (!date || (date > new Date())) date = new Date();
switch (metohd) { switch (metohd) {
case 'aidc': return genAidc(date); case 'aid': return genAid(date);
case 'aid1': return genAid(date, 1);
case 'aid2': return genAid(date, 2);
case 'aid3': return genAid(date, 3);
case 'aid4': return genAid(date, 4);
case 'ulid': return ulid(date.getTime()); case 'ulid': return ulid(date.getTime());
case 'objectid': return genObjectId(date); case 'objectid': return genObjectId(date);
default: throw 'unknown id generation method'; default: throw 'unknown id generation method';

23
src/misc/id/aid.ts Normal file
View file

@ -0,0 +1,23 @@
// AID
// 長さ8の[2000年1月1日からの経過ミリ秒をbase36でエンコードしたもの] + 長さ2の[ノイズ文字列]
import * as cluster from 'cluster';
const TIME2000 = 946684800000;
let counter = process.pid + (cluster.isMaster ? 0 : cluster.worker.id);
function getTime(time: number) {
time = time - TIME2000;
if (time < 0) time = 0;
return time.toString(36).padStart(8, '0');
}
function getRandom() {
return counter.toString(36).padStart(length, '0').substr(2);
}
export function genAid(date: Date): string {
counter++;
return getTime(date.getTime()) + getRandom();
}

View file

@ -8,7 +8,7 @@ function getTime(time: number) {
time = Math.floor(time / 1000); time = Math.floor(time / 1000);
return time.toString(16); return time.toString(16).padStart(8, CHARS[0]);
} }
function getRandom() { function getRandom() {