package nsqd

// the core algorithm here was borrowed from:
// Blake Mizerany's `noeqd` https://github.com/bmizerany/noeqd
// and indirectly:
// Twitter's `snowflake` https://github.com/twitter/snowflake

// only minor cleanup and changes to introduce a type, combine the concept
// of workerID + datacenterId into a single identifier, and modify the
// behavior when sequences rollover for our specific implementation needs

import (
    "encoding/hex"
    "errors"
    "time"
)

const (
    workerIDBits   = uint64(10)
    sequenceBits   = uint64(12)
    workerIDShift  = sequenceBits
    timestampShift = sequenceBits + workerIDBits
    sequenceMask   = int64(-1) ^ (int64(-1) << sequenceBits)

    // ( 2012-10-28 16:23:42 UTC ).UnixNano() >> 20
    twepoch = int64(1288834974288)
)

var ErrTimeBackwards = errors.New("time has gone backwards")
var ErrSequenceExpired = errors.New("sequence expired")
var ErrIDBackwards = errors.New("ID went backward")

type guid int64

type guidFactory struct {
    sequence      int64
    lastTimestamp int64
    lastID        guid
}

func (f *guidFactory) NewGUID(workerID int64) (guid, error) {
    // divide by 1048576, giving pseudo-milliseconds
    ts := time.Now().UnixNano() >> 20

    if ts < f.lastTimestamp {
        return 0, ErrTimeBackwards
    }

    if f.lastTimestamp == ts {
        f.sequence = (f.sequence + 1) & sequenceMask
        if f.sequence == 0 {
            return 0, ErrSequenceExpired
        }
    } else {
        f.sequence = 0
    }

    f.lastTimestamp = ts

    id := guid(((ts - twepoch) << timestampShift) |
        (workerID << workerIDShift) |
        f.sequence)

    if id <= f.lastID {
        return 0, ErrIDBackwards
    }

    f.lastID = id

    return id, nil
}

func (g guid) Hex() MessageID {
    var h MessageID
    var b [8]byte

    b[0] = byte(g >> 56)
    b[1] = byte(g >> 48)
    b[2] = byte(g >> 40)
    b[3] = byte(g >> 32)
    b[4] = byte(g >> 24)
    b[5] = byte(g >> 16)
    b[6] = byte(g >> 8)
    b[7] = byte(g)

    hex.Encode(h[:], b[:])
    return h
}

guid.go的更多相关文章

  1. UniqueIdentifier 数据类型 和 GUID 生成函数

    UniqueIdentifier 数据类型用于存储GUID的值,占用16Byte. SQL Server将UniqueIdentifier存储为16字节的二进制数值,Binary(16),按照特定的格 ...

  2. System.Guid ToString五中格式

    参考:https://msdn.microsoft.com/en-us/library/97af8hh4.aspx 测试代码: using System; using System.Collectio ...

  3. EF里Guid类型数据的自增长、时间戳和复杂类型的用法

    通过前两章Lodging和Destination类的演示,大家肯定基本了解Code First是怎么玩的了,本章继续演示一些很实用的东西.文章的开头提示下:提供的demo为了后面演示效果,前面代码有些 ...

  4. 为Guid数据类型的属性(property)赋值

    先来看看数据库表中的字段设计: 在数据库的数据类型为uniqueidentifier. 而在程序中对应的数据类型为GUID. property有get和set,也就是说能获取值也可以赋值.

  5. Guid的使用

    今天在公司做修改功能时,老大让使用部分更新,但是表中的主键是UNIQUEIDENTIFIER类型,它会在我们添加纪录时,默认生成一个unicode码, 但是我现在必须要将获取到的已经是string类型 ...

  6. GUID生成器

    这个GUID生成器是一个小巧的软件,属于笔者在空余时间开发的. http://pan.baidu.com/s/1sk6VGpn

  7. 使用Guid做主键和int做主键性能比较

    使用Guid做主键和int做主键性能比较 在数据库的设计中我们常常用Guid或int来做主键,根据所学的知识一直感觉int做主键效率要高,但没有做仔细的测试无法 说明道理.碰巧今天在数据库的优化过程中 ...

  8. 生成GUID唯一值的方法汇总(dotnet/javascript/sqlserver)

    一.在 .NET 中生成1.直接用.NET Framework 提供的 Guid() 函数,此种方法使用非常广泛.GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空中的任何两台计 ...

  9. SQLSERVER如何使用递增排序的GUID做主键

    场景: 产品表数据量较大想用Guid做表的主键,并在此字段上建立聚簇索引. 因为Guid是随机生成的,生成的值大小是不确定的,每次生成的数可能很大,也可能很小.这样会影响插入的效率 1.NEWSEQU ...

  10. COM 组件基础——GUID 和 接口

    一.前言 书接上回,话说在 doc(Word) 复合文件中,已经解决了保存 xls(Excel) 数据的问题了.那么,接下来又要解决另一个问题:当 WORD 程序读取复合文件,遇到了 xls 数据的时 ...

随机推荐

  1. The 12th tip of DB Query Analyzer, powerful in text file process

    MA Gen feng ( Guangdong Unitoll Services incorporated, Guangzhou 510300) Abstract   It's very powerf ...

  2. jquery 滚动事件

    $(window).scroll(function () { if ($(window).scrollTop() >50) {  alert('show!!'); }});

  3. add two numbers(将两个链表相加)

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  4. volatile的适用场景

    volatile保证线程间的数据是可见的(共享的),但不保证数据同步 volatile相当于synchronized的弱实现,也就是说volatile实现了类似synchronized的语义,却又没有 ...

  5. 浅析跨域的方法之一 JSONP

    概念: 什么叫跨域? 同源策略:它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略. 所谓同源是指,域名,协议,端口相同. 同源的脚本才会被执行 ...

  6. mysql统计类似SQL语句查询次数

    mysql统计类似SQL语句查询次数 vc-mysql-sniffer 工具抓取的sql分析. 1.先用shell脚本把所有enter符号替换为null,再根据语句前后的字符分隔语句 grep -Ev ...

  7. python常见模块之time,datetime模块

    一.time模块 time模块提供了一些用于管理时间和日期. time模块中时间的表现形式有三种: format_string  格式化的字符串 struct_time     结构化时间 times ...

  8. Nginx SSL功能支持的一些资料。

    http://wiki.nginx.org/HttpSslModulehttp://zou.lu/nginx-https-ssl-module/http://www.21andy.com/blog/2 ...

  9. Android开发之深入理解Android 7.0系统权限更改相关文档

    http://www.cnblogs.com/dazhao/p/6547811.html 摘要: Android 6.0之后的版本增加了运行时权限,应用程序在执行每个需要系统权限的功能时,需要添加权限 ...

  10. 一个resin启动bug的解决

    这个bug的问题后来被确认为Resin所在目录层有中文目录名.--------------------------------------------------------------------- ...