objectid.go源码阅读
生活随笔
收集整理的這篇文章主要介紹了
objectid.go源码阅读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*http://docs.mongodb.org/manual/reference/object-id/ObjectId 按照字節順序,一次代表:ObjectId is a 12-byte BSON type, constructed using:4個字節代表1970年元月一日到現在毫秒數 UNIX時間戳a 4-byte value representing the seconds since the Unix epoch,3個字節代表機器的唯一標識符 表示運行的機器a 3-byte machine identifier,2個字節代表進程的id 表示生成此_id的進程a 2-byte process id, and3個字節代表計數器,開始帶著一個隨機數 由一個隨機數開始的計數器生成的值a 3-byte counter, starting with a random value.
*/
package objectid
import (????"crypto/md5"????"encoding/hex"????"fmt"????"math/rand"????"os"????"sync/atomic"????"time")
var staticMachine = getMachineHash() //獲取機器的idvar staticIncrement = getRandomNumber()//獲取隨機數var staticPid = int32(os.Getpid())//獲取進程id//type ObjectId struct {????timestamp int64????machine int32????pid int32????increment int32}//func New() *ObjectId {????timestamp := time.Now().Unix()????return &ObjectId{timestamp, staticMachine, staticPid, atomic.AddInt32(&staticIncrement, 1) & 0xffffff}}//func Parse(input string) *ObjectId {????if len(input) == 0 {????????panic("The input is empty.")????}????if value, ok := tryParse(input); ok {????????return value????}????panic(fmt.Sprintf("%s is not a valid 24 digit hex string.", input))}//func (this *ObjectId) Timestamp() int64 {????return this.timestamp}//func (this *ObjectId) Machine() int32 {????return this.machine}//func (this *ObjectId) Pid() int32 {????return this.pid}//func (this *ObjectId) Increment() int32 {????return this.increment & 0xffffff}//func (this *ObjectId) CreationTime() time.Time {????return time.Unix(this.timestamp, 0)}//func (this *ObjectId) Equal(other *ObjectId) bool {????return this.timestamp == other.timestamp &&????????this.machine == other.machine &&????????this.pid == other.pid &&????????this.increment == other.increment}//func (this *ObjectId) String() string {????array := []byte{????????byte(this.timestamp >> 0x18),????????byte(this.timestamp >> 0x10),????????byte(this.timestamp >> 8),????????byte(this.timestamp),????????byte(this.machine >> 0x10),????????byte(this.machine >> 8),????????byte(this.machine),????????byte(this.pid >> 8),????????byte(this.pid),????????byte(this.increment >> 0x10),????????byte(this.increment >> 8),????????byte(this.increment),????}????return hex.EncodeToString(array)}//獲取機器唯一標識符func getMachineHash() int32 {????machineName, err := os.Hostname()????if err != nil {????????panic(err)????}????buf := md5.Sum([]byte(machineName))????return (int32(buf[0])<<0x10 + int32(buf[1])<<8) + int32(buf[2])}//獲取隨機數開始的計數器生成的值func getRandomNumber() int32 {????rand.Seed(time.Now().UnixNano())????return rand.Int31()}//從字符串objectid 解析成為ObjectIdfunc tryParse(input string) (*ObjectId, bool) {????if len(input) != 0x18 {????????return nil, false????}????array, err := hex.DecodeString(input) //十六進制的字符串 轉化為字節切片 ????if err != nil {????????return nil, false????}????return &ObjectId{????????timestamp: int64(array[0])<<0x18 + int64(array[1])<<0x10 + int64(array[2])<<8 + int64(array[3]), //轉化為十進制的int64 新紀元時間 毫秒????????machine: int32(array[4])<<0x10 + int32(array[5])<<8 + int32(array[6]), //轉化為十進制的int32數據 機器唯一標識符????????pid: int32(array[7])<<8 + int32(array[8]), // 當前進程id????????increment: int32(array[9])<<0x10 + (int32(array[10]) << 8) + int32(array[11]), // 隨機數開始的計數器生成的值????}, true}
*/
package objectid
import (????"crypto/md5"????"encoding/hex"????"fmt"????"math/rand"????"os"????"sync/atomic"????"time")
var staticMachine = getMachineHash() //獲取機器的idvar staticIncrement = getRandomNumber()//獲取隨機數var staticPid = int32(os.Getpid())//獲取進程id//type ObjectId struct {????timestamp int64????machine int32????pid int32????increment int32}//func New() *ObjectId {????timestamp := time.Now().Unix()????return &ObjectId{timestamp, staticMachine, staticPid, atomic.AddInt32(&staticIncrement, 1) & 0xffffff}}//func Parse(input string) *ObjectId {????if len(input) == 0 {????????panic("The input is empty.")????}????if value, ok := tryParse(input); ok {????????return value????}????panic(fmt.Sprintf("%s is not a valid 24 digit hex string.", input))}//func (this *ObjectId) Timestamp() int64 {????return this.timestamp}//func (this *ObjectId) Machine() int32 {????return this.machine}//func (this *ObjectId) Pid() int32 {????return this.pid}//func (this *ObjectId) Increment() int32 {????return this.increment & 0xffffff}//func (this *ObjectId) CreationTime() time.Time {????return time.Unix(this.timestamp, 0)}//func (this *ObjectId) Equal(other *ObjectId) bool {????return this.timestamp == other.timestamp &&????????this.machine == other.machine &&????????this.pid == other.pid &&????????this.increment == other.increment}//func (this *ObjectId) String() string {????array := []byte{????????byte(this.timestamp >> 0x18),????????byte(this.timestamp >> 0x10),????????byte(this.timestamp >> 8),????????byte(this.timestamp),????????byte(this.machine >> 0x10),????????byte(this.machine >> 8),????????byte(this.machine),????????byte(this.pid >> 8),????????byte(this.pid),????????byte(this.increment >> 0x10),????????byte(this.increment >> 8),????????byte(this.increment),????}????return hex.EncodeToString(array)}//獲取機器唯一標識符func getMachineHash() int32 {????machineName, err := os.Hostname()????if err != nil {????????panic(err)????}????buf := md5.Sum([]byte(machineName))????return (int32(buf[0])<<0x10 + int32(buf[1])<<8) + int32(buf[2])}//獲取隨機數開始的計數器生成的值func getRandomNumber() int32 {????rand.Seed(time.Now().UnixNano())????return rand.Int31()}//從字符串objectid 解析成為ObjectIdfunc tryParse(input string) (*ObjectId, bool) {????if len(input) != 0x18 {????????return nil, false????}????array, err := hex.DecodeString(input) //十六進制的字符串 轉化為字節切片 ????if err != nil {????????return nil, false????}????return &ObjectId{????????timestamp: int64(array[0])<<0x18 + int64(array[1])<<0x10 + int64(array[2])<<8 + int64(array[3]), //轉化為十進制的int64 新紀元時間 毫秒????????machine: int32(array[4])<<0x10 + int32(array[5])<<8 + int32(array[6]), //轉化為十進制的int32數據 機器唯一標識符????????pid: int32(array[7])<<8 + int32(array[8]), // 當前進程id????????increment: int32(array[9])<<0x10 + (int32(array[10]) << 8) + int32(array[11]), // 隨機數開始的計數器生成的值????}, true}
轉載于:https://www.cnblogs.com/zhangboyu/p/7461907.html
總結
以上是生活随笔為你收集整理的objectid.go源码阅读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搭建excel在线编辑服务器,网站如何实
- 下一篇: 微信小程序-利用wxParse将html