中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

外包做網(wǎng)站賺錢(qián)么app注冊(cè)推廣拉人

外包做網(wǎng)站賺錢(qián)么,app注冊(cè)推廣拉人,哪家微網(wǎng)站建設(shè),什么是網(wǎng)絡(luò)搭建ObjectID介紹 MongoDB中的ObjectId是一種特殊的12字節(jié) BSON 類(lèi)型數(shù)據(jù),用于為主文檔提供唯一的標(biāo)識(shí)符,默認(rèn)情況下作為 _id 字段的默認(rèn)值出現(xiàn)在每一個(gè)MongoDB集合中的文檔中。以下是ObjectId的具體組成: 1. 時(shí)間戳(Timestamp&…

ObjectID介紹

MongoDB中的ObjectId是一種特殊的12字節(jié) BSON 類(lèi)型數(shù)據(jù),用于為主文檔提供唯一的標(biāo)識(shí)符,默認(rèn)情況下作為 _id 字段的默認(rèn)值出現(xiàn)在每一個(gè)MongoDB集合中的文檔中。以下是ObjectId的具體組成:

1. 時(shí)間戳(Timestamp):

  • 前4個(gè)字節(jié)(32位)表示創(chuàng)建該ObjectId時(shí)的Unix時(shí)間戳,精確到秒,從1970年1月1日UTC時(shí)間零點(diǎn)開(kāi)始計(jì)算,這使得ObjectId具有一定程度的時(shí)間有序性。

2. 機(jī)器標(biāo)識(shí)符(Machine ID):

  • 接下來(lái)的3個(gè)字節(jié)(24位)代表了生成此ObjectId的機(jī)器主機(jī)的唯一標(biāo)識(shí)符。這個(gè)標(biāo)識(shí)符通常是基于主機(jī)的網(wǎng)絡(luò)接口地址哈希得到的,目的是確保不同主機(jī)生成的ObjectId是不同的。

3. 進(jìn)程標(biāo)識(shí)符(PID):

  • (舊版描述中提到的是進(jìn)程ID,但在MongoDB較新版本中已不再使用)在某些早期的描述中提及2個(gè)字節(jié)代表進(jìn)程ID,不過(guò)實(shí)際上MongoDB并不使用進(jìn)程ID來(lái)生成ObjectId,以避免因?yàn)镻ID重用導(dǎo)致的沖突?,F(xiàn)在這部分?jǐn)?shù)據(jù)通常用于其他目的以保證全局唯一性。

4. 計(jì)數(shù)器(Counter):

  • 最后的3個(gè)字節(jié)(24位)是一個(gè)自增計(jì)數(shù)器,在同一臺(tái)機(jī)器同一秒內(nèi)生成的ObjectId會(huì)通過(guò)這個(gè)計(jì)數(shù)器遞增來(lái)確保唯一性。計(jì)數(shù)器在一個(gè)秒內(nèi)是從一個(gè)隨機(jī)數(shù)開(kāi)始遞增的,這樣即使在同一秒內(nèi)創(chuàng)建多個(gè)ObjectId也能保證在單機(jī)上的唯一性。

因此,ObjectId的設(shè)計(jì)可以確保在分布式的環(huán)境下,每個(gè)文檔都能擁有一個(gè)全局唯一的標(biāo)識(shí)符,同時(shí)也包含了時(shí)間信息,這對(duì)于很多應(yīng)用場(chǎng)景來(lái)說(shuō)非常有用,比如排序、索引和邏輯處理。

ObjectID使用

分布式系統(tǒng)需要全局唯一ID且有序的,可以考慮ObjectID。

UUID太長(zhǎng)了,且是無(wú)序的。感覺(jué)不太好,ObjectID算是個(gè)還可以的選擇。當(dāng)然還有很多其它方案。

Go項(xiàng)目,在Mongodb的驅(qū)動(dòng)包里,有一個(gè)文件是objectid.go,有寫(xiě)好ObjectID生成算法。如果項(xiàng)目只要一個(gè)算法沒(méi)必要引入完整的包,可以直接把這個(gè)文件拷貝出來(lái)。

內(nèi)容如下:

package hobjectidimport ("crypto/rand""encoding""encoding/binary""encoding/hex""encoding/json""errors""fmt""io""sync/atomic""time"
)// 代碼來(lái)自  https://github.com/mongodb/mongo-go-driver/blob/v1/bson/primitive/objectid.go// ErrInvalidHex indicates that a hex string cannot be converted to an ObjectID.
var ErrInvalidHex = errors.New("the provided hex string is not a valid ObjectID")// ObjectID is the BSON ObjectID type.
type ObjectID [12]byte// NilObjectID is the zero value for ObjectID.
var NilObjectID ObjectIDvar objectIDCounter = readRandomUint32()
var processUnique = processUniqueBytes()var _ encoding.TextMarshaler = ObjectID{}
var _ encoding.TextUnmarshaler = &ObjectID{}// NewObjectID generates a new ObjectID.
func NewObjectID() ObjectID {return NewObjectIDFromTimestamp(time.Now())
}// NewObjectIDFromTimestamp generates a new ObjectID based on the given time.
func NewObjectIDFromTimestamp(timestamp time.Time) ObjectID {var b [12]bytebinary.BigEndian.PutUint32(b[0:4], uint32(timestamp.Unix()))copy(b[4:9], processUnique[:])putUint24(b[9:12], atomic.AddUint32(&objectIDCounter, 1))return b
}// Timestamp extracts the time part of the ObjectId.
func (id ObjectID) Timestamp() time.Time {unixSecs := binary.BigEndian.Uint32(id[0:4])return time.Unix(int64(unixSecs), 0).UTC()
}// Hex returns the hex encoding of the ObjectID as a string.
func (id ObjectID) Hex() string {var buf [24]bytehex.Encode(buf[:], id[:])return string(buf[:])
}func (id ObjectID) String() string {return fmt.Sprintf("ObjectID(%q)", id.Hex())
}// IsZero returns true if id is the empty ObjectID.
func (id ObjectID) IsZero() bool {return id == NilObjectID
}// ObjectIDFromHex creates a new ObjectID from a hex string. It returns an error if the hex string is not a
// valid ObjectID.
func ObjectIDFromHex(s string) (ObjectID, error) {if len(s) != 24 {return NilObjectID, ErrInvalidHex}var oid [12]byte_, err := hex.Decode(oid[:], []byte(s))if err != nil {return NilObjectID, err}return oid, nil
}// IsValidObjectID returns true if the provided hex string represents a valid ObjectID and false if not.
//
// Deprecated: Use ObjectIDFromHex and check the error instead.
func IsValidObjectID(s string) bool {_, err := ObjectIDFromHex(s)return err == nil
}// MarshalText returns the ObjectID as UTF-8-encoded text. Implementing this allows us to use ObjectID
// as a map key when marshalling JSON. See https://pkg.go.dev/encoding#TextMarshaler
func (id ObjectID) MarshalText() ([]byte, error) {return []byte(id.Hex()), nil
}// UnmarshalText populates the byte slice with the ObjectID. Implementing this allows us to use ObjectID
// as a map key when unmarshalling JSON. See https://pkg.go.dev/encoding#TextUnmarshaler
func (id *ObjectID) UnmarshalText(b []byte) error {oid, err := ObjectIDFromHex(string(b))if err != nil {return err}*id = oidreturn nil
}// MarshalJSON returns the ObjectID as a string
func (id ObjectID) MarshalJSON() ([]byte, error) {return json.Marshal(id.Hex())
}// UnmarshalJSON populates the byte slice with the ObjectID. If the byte slice is 24 bytes long, it
// will be populated with the hex representation of the ObjectID. If the byte slice is twelve bytes
// long, it will be populated with the BSON representation of the ObjectID. This method also accepts empty strings and
// decodes them as NilObjectID. For any other inputs, an error will be returned.
func (id *ObjectID) UnmarshalJSON(b []byte) error {// Ignore "null" to keep parity with the standard library. Decoding a JSON null into a non-pointer ObjectID field// will leave the field unchanged. For pointer values, encoding/json will set the pointer to nil and will not// enter the UnmarshalJSON hook.if string(b) == "null" {return nil}var err errorswitch len(b) {case 12:copy(id[:], b)default:// Extended JSONvar res interface{}err := json.Unmarshal(b, &res)if err != nil {return err}str, ok := res.(string)if !ok {m, ok := res.(map[string]interface{})if !ok {return errors.New("not an extended JSON ObjectID")}oid, ok := m["$oid"]if !ok {return errors.New("not an extended JSON ObjectID")}str, ok = oid.(string)if !ok {return errors.New("not an extended JSON ObjectID")}}// An empty string is not a valid ObjectID, but we treat it as a special value that decodes as NilObjectID.if len(str) == 0 {copy(id[:], NilObjectID[:])return nil}if len(str) != 24 {return fmt.Errorf("cannot unmarshal into an ObjectID, the length must be 24 but it is %d", len(str))}_, err = hex.Decode(id[:], []byte(str))if err != nil {return err}}return err
}func processUniqueBytes() [5]byte {var b [5]byte_, err := io.ReadFull(rand.Reader, b[:])if err != nil {panic(fmt.Errorf("cannot initialize objectid package with crypto.rand.Reader: %w", err))}return b
}func readRandomUint32() uint32 {var b [4]byte_, err := io.ReadFull(rand.Reader, b[:])if err != nil {panic(fmt.Errorf("cannot initialize objectid package with crypto.rand.Reader: %w", err))}return (uint32(b[0]) << 0) | (uint32(b[1]) << 8) | (uint32(b[2]) << 16) | (uint32(b[3]) << 24)
}func putUint24(b []byte, v uint32) {b[0] = byte(v >> 16)b[1] = byte(v >> 8)b[2] = byte(v)
}

使用生成算法,生成的ID 可以與環(huán)境無(wú)關(guān)、業(yè)務(wù)無(wú)關(guān)。通用性更好。

http://www.risenshineclean.com/news/59399.html

相關(guān)文章:

  • 濟(jì)南做網(wǎng)站哪家便宜seo項(xiàng)目經(jīng)理
  • 網(wǎng)站怎么添加js廣告位免費(fèi)seo搜索優(yōu)化
  • 常德論壇網(wǎng)站看廣告賺錢(qián)
  • 住房和城鄉(xiāng)建設(shè)部網(wǎng)站安廣東省淘寶指數(shù)官網(wǎng)
  • 優(yōu)化網(wǎng)站用軟件好嗎山東一級(jí)造價(jià)師
  • 建設(shè)摩托車(chē)官網(wǎng)旗艦店寶雞seo排名
  • 甘肅省蘭州市建設(shè)廳網(wǎng)站硬件優(yōu)化大師
  • 用dreamever如何建設(shè)網(wǎng)站百度指數(shù)數(shù)據(jù)官網(wǎng)
  • 什么系統(tǒng)做網(wǎng)站好2022年最新新聞播報(bào)稿件
  • 濰坊做網(wǎng)站個(gè)人工作室cms自助建站系統(tǒng)
  • 個(gè)人網(wǎng)站建設(shè)工作室深圳網(wǎng)站設(shè)計(jì)十年樂(lè)云seo
  • 怎么做賣(mài)花的網(wǎng)站9個(gè)廣州seo推廣神技
  • 星沙做淘寶店鋪網(wǎng)站seo偽原創(chuàng)工具
  • 江門(mén)做公司網(wǎng)站百度競(jìng)價(jià)排名公式
  • 網(wǎng)站后臺(tái) 更新緩存品牌推廣方式
  • 網(wǎng)站維護(hù)開(kāi)發(fā)合同seo怎么做推廣
  • 課程網(wǎng)站怎么做關(guān)鍵詞優(yōu)化app
  • 從化專(zhuān)業(yè)做網(wǎng)站培訓(xùn)網(wǎng)站模板
  • 內(nèi)蒙古網(wǎng)站建設(shè)價(jià)格qq推廣官網(wǎng)
  • 遼寧新聞網(wǎng)站的建設(shè)seo官網(wǎng)
  • 個(gè)人網(wǎng)站 cdn學(xué)生個(gè)人網(wǎng)頁(yè)設(shè)計(jì)作品
  • 網(wǎng)站主機(jī)空間價(jià)格廣州網(wǎng)站運(yùn)營(yíng)專(zhuān)注樂(lè)云seo
  • 做淘寶客服的網(wǎng)站數(shù)字營(yíng)銷(xiāo)
  • 深圳網(wǎng)站建設(shè) 設(shè)計(jì)創(chuàng)公司搜索排名廣告營(yíng)銷(xiāo)怎么做
  • 深圳做網(wǎng)站專(zhuān)業(yè)公司產(chǎn)品軟文范例100字
  • 圖展網(wǎng)站源碼云和數(shù)據(jù)培訓(xùn)機(jī)構(gòu)怎么樣
  • 個(gè)體戶可以備案網(wǎng)站嗎上海網(wǎng)站建設(shè)公司
  • 做淘寶頭像的網(wǎng)站有哪些百度競(jìng)價(jià)查詢
  • 做外貿(mào)要自己建網(wǎng)站嗎百度指數(shù)的各項(xiàng)功能
  • 電子商務(wù)網(wǎng)站建設(shè)與完整實(shí)例seo網(wǎng)站推廣是什么