Skip to content

therastal/uid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

UID

I wanted to use ULIDs as SQLite rowid aliases, but the maximum size SQLite allows is 64 bits. This implementation allows me to benefit from ULID semantics while retaining the performance of SQLite rowid-based operations.

This is mostly a Python and Go port of Mediagone's Small UID library for PHP. The main functional difference is allowing a set of UIDs to be passed into the constructor in order to ensure true uniqueness across a given set of generated IDs, though that only becomes an issue when thousands of UIDs are being generated per millisecond.

Usage

Python

from uid import UID

COUNT = 100000

with_duplicates = set()
unique_only = set()

for _ in range(COUNT):
    with_duplicates.add(UID())
    unique_only.add(UID(existing=unique_only))

assert len(with_duplicates) != COUNT
assert len(unique_only) == COUNT

Go

package main

import "github.com/therastal/uid"

func main() {
    count := 100000

    withDuplicates := map[int64]bool{}
    uniqueOnly := map[int64]bool{}

    for i := 0; i < count; i++ {
        withDuplicates[uid.New().uid] = true
        uniqueOnly[uid.NewWithExisting(uniqueOnly).uid] = true
    }

    fmt.Println("withDuplicates:", len(withDuplicates)) // len(withDuplicates) != count
    fmt.Println("uniqueOnly:", len(uniqueOnly))         // len(uniqueOnly) == count
}