From e64912545a61b89ecff5f300d2a36f7f9cfa1fdd Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 14 Apr 2019 01:08:26 +0900 Subject: [PATCH] Update id generation methods --- .config/example.yml | 14 +++----------- src/misc/aid.ts | 26 -------------------------- src/misc/aidc.ts | 26 -------------------------- src/misc/gen-id.ts | 11 +++-------- src/misc/id/aid.ts | 23 +++++++++++++++++++++++ src/misc/{ => id}/object-id.ts | 2 +- 6 files changed, 30 insertions(+), 72 deletions(-) delete mode 100644 src/misc/aid.ts delete mode 100644 src/misc/aidc.ts create mode 100644 src/misc/id/aid.ts rename src/misc/{ => id}/object-id.ts (89%) diff --git a/.config/example.yml b/.config/example.yml index 24985fd28..32ad16562 100644 --- a/.config/example.yml +++ b/.config/example.yml @@ -127,19 +127,11 @@ drive: # change it according to your preferences. # Available methods: -# aid1 ... Use AID for ID generation (with random 1 char) -# 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) +# aid ... Use AID for ID generation # ulid ... Use ulid for ID generation # objectid ... This is left for backward compatibility. -# AID(n) 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. +# AID is the original ID generation method. # ULID: Universally Unique Lexicographically Sortable Identifier. # for more details: https://github.com/ulid/spec @@ -148,7 +140,7 @@ drive: # ObjectID is the method used in previous versions of Misskey. # * Choose this if you are migrating from a previous Misskey. -id: 'aid2' +id: 'aid' # ┌─────────────────────┐ #───┘ Other configuration └───────────────────────────────────── diff --git a/src/misc/aid.ts b/src/misc/aid.ts deleted file mode 100644 index aba53ed5f..000000000 --- a/src/misc/aid.ts +++ /dev/null @@ -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); -} diff --git a/src/misc/aidc.ts b/src/misc/aidc.ts deleted file mode 100644 index 75168ac30..000000000 --- a/src/misc/aidc.ts +++ /dev/null @@ -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(); -} diff --git a/src/misc/gen-id.ts b/src/misc/gen-id.ts index fe901b1fe..d16910d01 100644 --- a/src/misc/gen-id.ts +++ b/src/misc/gen-id.ts @@ -1,7 +1,6 @@ import { ulid } from 'ulid'; -import { genAid } from './aid'; -import { genAidc } from './aidc'; -import { genObjectId } from './object-id'; +import { genAid } from './id/aid'; +import { genObjectId } from './id/object-id'; import config from '../config'; const metohd = config.id.toLowerCase(); @@ -10,11 +9,7 @@ export function genId(date?: Date): string { if (!date || (date > new Date())) date = new Date(); switch (metohd) { - case 'aidc': return genAidc(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 'aid': return genAid(date); case 'ulid': return ulid(date.getTime()); case 'objectid': return genObjectId(date); default: throw 'unknown id generation method'; diff --git a/src/misc/id/aid.ts b/src/misc/id/aid.ts new file mode 100644 index 000000000..7ab0a148e --- /dev/null +++ b/src/misc/id/aid.ts @@ -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(); +} diff --git a/src/misc/object-id.ts b/src/misc/id/object-id.ts similarity index 89% rename from src/misc/object-id.ts rename to src/misc/id/object-id.ts index 6f6422e5e..392ea4330 100644 --- a/src/misc/object-id.ts +++ b/src/misc/id/object-id.ts @@ -8,7 +8,7 @@ function getTime(time: number) { time = Math.floor(time / 1000); - return time.toString(16); + return time.toString(16).padStart(8, CHARS[0]); } function getRandom() {