objectid.go源码阅读
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                objectid.go源码阅读
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                
                            
                            
                            /*http://docs.mongodb.org/manual/reference/object-id/ObjectId 按照字節(jié)順序,一次代表:ObjectId is a 12-byte BSON type, constructed using:4個(gè)字節(jié)代表1970年元月一日到現(xiàn)在毫秒數(shù)  UNIX時(shí)間戳a 4-byte value representing the seconds since the Unix epoch,3個(gè)字節(jié)代表機(jī)器的唯一標(biāo)識(shí)符  表示運(yùn)行的機(jī)器a 3-byte machine identifier,2個(gè)字節(jié)代表進(jìn)程的id 表示生成此_id的進(jìn)程a 2-byte process id, and3個(gè)字節(jié)代表計(jì)數(shù)器,開始帶著一個(gè)隨機(jī)數(shù)   由一個(gè)隨機(jī)數(shù)開始的計(jì)數(shù)器生成的值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() //獲取機(jī)器的idvar staticIncrement = getRandomNumber()//獲取隨機(jī)數(shù)var staticPid = int32(os.Getpid())//獲取進(jìn)程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)}//獲取機(jī)器唯一標(biāo)識(shí)符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])}//獲取隨機(jī)數(shù)開始的計(jì)數(shù)器生成的值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) //十六進(jìn)制的字符串 轉(zhuǎn)化為字節(jié)切片 ????if err != nil {????????return nil, false????}????return &ObjectId{????????timestamp: int64(array[0])<<0x18 + int64(array[1])<<0x10 + int64(array[2])<<8 + int64(array[3]), //轉(zhuǎn)化為十進(jìn)制的int64 新紀(jì)元時(shí)間 毫秒????????machine: int32(array[4])<<0x10 + int32(array[5])<<8 + int32(array[6]), //轉(zhuǎn)化為十進(jìn)制的int32數(shù)據(jù) 機(jī)器唯一標(biāo)識(shí)符????????pid: int32(array[7])<<8 + int32(array[8]), // 當(dāng)前進(jìn)程id????????increment: int32(array[9])<<0x10 + (int32(array[10]) << 8) + int32(array[11]), // 隨機(jī)數(shù)開始的計(jì)數(shù)器生成的值????}, true}
                        
                        
                        */
package objectid
import (????"crypto/md5"????"encoding/hex"????"fmt"????"math/rand"????"os"????"sync/atomic"????"time")
var staticMachine = getMachineHash() //獲取機(jī)器的idvar staticIncrement = getRandomNumber()//獲取隨機(jī)數(shù)var staticPid = int32(os.Getpid())//獲取進(jìn)程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)}//獲取機(jī)器唯一標(biāo)識(shí)符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])}//獲取隨機(jī)數(shù)開始的計(jì)數(shù)器生成的值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) //十六進(jìn)制的字符串 轉(zhuǎn)化為字節(jié)切片 ????if err != nil {????????return nil, false????}????return &ObjectId{????????timestamp: int64(array[0])<<0x18 + int64(array[1])<<0x10 + int64(array[2])<<8 + int64(array[3]), //轉(zhuǎn)化為十進(jìn)制的int64 新紀(jì)元時(shí)間 毫秒????????machine: int32(array[4])<<0x10 + int32(array[5])<<8 + int32(array[6]), //轉(zhuǎn)化為十進(jìn)制的int32數(shù)據(jù) 機(jī)器唯一標(biāo)識(shí)符????????pid: int32(array[7])<<8 + int32(array[8]), // 當(dāng)前進(jìn)程id????????increment: int32(array[9])<<0x10 + (int32(array[10]) << 8) + int32(array[11]), // 隨機(jī)數(shù)開始的計(jì)數(shù)器生成的值????}, true}
轉(zhuǎn)載于:https://www.cnblogs.com/zhangboyu/p/7461907.html
總結(jié)
以上是生活随笔為你收集整理的objectid.go源码阅读的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 搭建excel在线编辑服务器,网站如何实
 - 下一篇: 微信小程序-利用wxParse将html