ULIDのライブラリをv1.0.2にアップデートした

主な変更点はBASE32に使っている文字種変更です。

base_convert()をつかって下記で生成していたのを

0123456789ABCDEFGHIJKLMNOPQRSTUV

互換性を考慮して https://github.com/ulid/spec に記載がある

0123456789ABCDEFGHJKMNPQRSTVWXYZ

に変更しました。

またULIDのsort仕様には

Sorting
The left-most character must be sorted first, and the right-most character sorted last (lexical order). The default ASCII character set must be used. Within the same millisecond, sort order is not guaranteed

とあり同一タイムスタンプの間はソート順は保証しないとあるのですが(ほかの実装もこのあたりまちまち)

同じくULIDのMonotonicityの仕様 には

When generating a ULID within the same millisecond, we can provide some guarantees regarding sort order. Namely, if the same millisecond is detected, the random component is incremented by 1 bit in the least significant bit position (with carrying). For example:

import { monotonicFactory } from 'ulid'

const ulid = monotonicFactory()

// Assume that these calls occur within the same millisecond
ulid() // 01BX5ZZKBKACTAV9WEVGEMMVRZ
ulid() // 01BX5ZZKBKACTAV9WEVGEMMVS0

というようにミリ秒単位で同一のタイムスタンプであったら後半80bitの値は乱数で生成ではなくインクリメントするように記載されている。矛盾するような気もしますが
When generating a ULID within the same millisecond, we can provide some guarantees regarding sort order.
という箇所の解釈としては「~することもできる」というOptionalな仕様なのかなと思っています。

とはいえログなどに使った際に多少ソートしやすくなると思われるため、この仕様も今回のバージョンで盛り込みました。

カテゴリー: php

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です