<commit_before>package snowflake

import "time"

const (
	/* 2015-01-01 */
	twepoch     int64 = 1420041600000
	wkShift     uint  = 12
	tsShift     uint  = 12 + 10
	workerIDMax       = 0x3ff
	sequenceMax       = 0xfff
)

func init() {
	go func() {
		var sq, ms int64
		var ts = time.Now().UnixNano() / 1000 / 1000
		for true {
			wid := <-worker
			ms = time.Now().UnixNano() / 1000 / 1000
			if ms > ts {
				ts = ms
				sq = 0
			}
			sq++
			sequence <- &snowflakeID{ms, wid, sq}
		}
	}()
}

var worker = make(chan int64)
var sequence = make(chan *snowflakeID)

type snowflakeID struct {
	timestamp int64
	workerID  int64
	sequence  int64
}

// NextID used to gets an unique id with worker id : 0
func NextID() int64 {
	return NextIDWorker(0)
}

// NextIDWorker used to gets an unique id with explicit worker id
func NextIDWorker(workerID int64) int64 {
	if workerID >= 0 && workerID <= workerIDMax {
		worker <- workerID
		return (<-sequence).ToInt64()
	}
	return -1
}

func (id *snowflakeID) ToInt64() int64 {
	return (id.timestamp-twepoch)<<tsShift | id.workerID<<wkShift | id.sequence%sequenceMax
}<commit_msg>worker -> workers<commit_after>package snowflake

import "time"

const (
	/* 2015-01-01 */
	twepoch     int64 = 1420041600000
	wkShift     uint  = 12
	tsShift     uint  = 12 + 10
	workerIDMax       = 0x3ff
	sequenceMax       = 0xfff
)

func init() {
	go func() {
		var workers [workerIDMax]chan chan *snowflakeID
		for true {
			ctx := <-worker
			wc := workers[ctx.wid]
			if wc == nil {
				wc = make(chan chan *snowflakeID)
				workers[ctx.wid] = wc
				go func(n int64, c chan chan *snowflakeID) {
					var ms, ts, sq int64
					for true {
						sfc := <-c
						ms = time.Now().UnixNano()
						if ms > ts {
							ts = ms
							sq = 0
						}
						sq++
						sfc <- &snowflakeID{ms / 1000 / 1000, n, sq - 1}
					}
				}(ctx.wid, wc)
			}
			go func() {
				wc <- ctx.sfc
			}()
		}
	}()
}

var worker = make(chan *context)

type context struct {
	wid int64
	sfc chan *snowflakeID
}

type snowflakeID struct {
	timestamp int64
	workerID  int64
	sequence  int64
}

// NextID used to gets an unique id with worker id : 0
func NextID() int64 {
	return NextIDWorker(0)
}

// NextIDWorker used to gets an unique id with explicit worker id
func NextIDWorker(workerID int64) int64 {
	if workerID >= 0 && workerID <= workerIDMax {
		var ctx = &context{workerID, make(chan *snowflakeID)}
		worker <- ctx
		return (<-ctx.sfc).ToInt64()
	}
	return -1
}

func (id *snowflakeID) ToInt64() int64 {
	return (id.timestamp-twepoch)<<tsShift | id.workerID<<wkShift | id.sequence%sequenceMax
}
{'fraction_non_alphanumeric': 0.11893764434180139, 'fraction_numerical': 0.05581216320246343, 'mean_word_length': 2.6502808988764044, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3042488', 'n_tokens_mistral': 1208, 'n_tokens_neox': 976, 'n_words': 282}