This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DEFAULT_ID_EPOCH = 1704067200000; // 2024-01-01T00:00:00Z | |
const TIMESTAMP_BITS = 41; // dedicated bits for timestamp | |
const MAX_PAYLOAD_BITS = 64 - 1 - TIMESTAMP_BITS; // 22 bits left for clusterId, workerId, sequence | |
/** | |
* A Snowflake ID generator that produces unique 64-bit identifiers. | |
* The ID is composed of: | |
* - 1 bit: unused (sign bit) | |
* - 41 bits: timestamp in milliseconds since a custom epoch | |
* - N bits: cluster ID (configurable, default 5 bits) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useRef, useState, useEffect } from "react"; | |
export type SubProtocol = "protobuf" | "msgpack" | "json"; | |
export function useSocketClient<P extends SubProtocol, B extends BinaryType>( | |
url: string | URL, | |
protocol: P = "protobuf" as P, | |
binaryType: B = "arraybuffer" as B, | |
) { | |
type M = P extends "json" ? string : B extends "arraybuffer" ? ArrayBufferLike : B extends "blob" ? Blob : unknown; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.tiscs.sbp.snowflake; | |
public class IdWorker { | |
// 2020-01-01T00:00:00.000Z | |
public static final long DEFAULT_ID_EPOCH = 1577836800000L; | |
public static final String LOWER_HEX_FORMAT = "%016x"; | |
public static final String UPPER_HEX_FORMAT = "%016X"; | |
public IdWorker(long clusterId, long workerId) throws IllegalAccessException { | |
this(clusterId, workerId, DEFAULT_ID_EPOCH, 5L, 5L, 12L); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package snowflake | |
import ( | |
"errors" | |
"fmt" | |
"sync" | |
"time" | |
) | |
var ( |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.tiscs.sbp.snowflake | |
// 2020-01-01T00:00:00.000Z | |
const val DEFAULT_ID_EPOCH = 1577836800000L | |
const val LOWER_HEX_FORMAT = "%016x" | |
const val UPPER_HEX_FORMAT = "%016X" | |
class IdWorker( | |
private val clusterId: Long, | |
private val workerId: Long, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.jetbrains.exposed.sql.* | |
import org.jetbrains.exposed.sql.statements.InsertStatement | |
import org.jetbrains.exposed.sql.statements.UpdateStatement | |
import org.springframework.transaction.annotation.Transactional | |
@Transactional | |
class CurdRepository<M : Model<K>, K>( | |
private val primary: Column<K>, | |
private val mapper: (ResultRow) -> M | |
) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace Snowflake | |
{ | |
public class IdWorker | |
{ | |
// 2020-01-01T00:00:00.000Z | |
public const long DEFAULT_ID_EPOCH = 1577836800000L; |