• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 79  文章 - 58  trackbacks - 0
            <2007年5月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            常用鏈接

            留言簿(9)

            隨筆分類

            隨筆檔案

            文章檔案

            相冊(cè)

            搜索

            •  

            積分與排名

            • 積分 - 296056
            • 排名 - 87

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            1、實(shí)現(xiàn)原理

            為了實(shí)現(xiàn)自增ID,,需要引入專門計(jì)算id的Collection。
            該collection中存放的記錄格式類似如下:
            {'_id': 1,  'current_id_value':1} , 
            其中current_id_value對(duì)應(yīng)著當(dāng)前最大id值+1,每次需要獲取一個(gè)ID時(shí),需要先到該collection查詢對(duì)應(yīng)的currentIdValue 值并把這個(gè)值+1
            mongodb提供了findAndModify接口,并且是線程安全的,做到查詢并加1的原子操作。

            2、代碼如下

            int64_t GetID()
            {
                int64_t ret = -1;
                
                mongoc_collection_t *pCountCollection = mongoc_client_get_collection(m_pClient, "test_db", "id_generator");
                
                bson_error_t error;
                bson_t *doc = bson_new();

                bson_t child;
                bson_append_document_begin(doc, "$inc", -1, &child);
                BSON_APPEND_INT64(&child, "current_id_value", 1);
                bson_append_document_end(doc, &child);

                bson_t query;
                bson_init(&query);
                BSON_APPEND_INT64(&query, "_id", 1);

                bson_t reply;
                bool r = mongoc_collection_find_and_modify(pCountCollection,
                    &query,
                    NULL,
                    doc,
                    NULL,
                    false,
                    true,
                    true,
                    &reply,
                    &error);
                if (!r)
                {
                    cout << "GetID Failure: " << error.message;
                }
                else
                {
                    bson_iter_t iter;
                    bson_iter_init(&iter, &reply);
                    if (bson_iter_find(&iter, "value"))
                    {
                        const uint8_t *buf;
                        uint32_t len;
                        bson_iter_document(&iter, &len, &buf);
                        bson_t rec;
                        bson_init_static(&rec, buf, len);

                        bson_iter_init(&iter, &rec);
                        if (bson_iter_find(&iter, "current_id_value"))
                        {
                            ret = bson_iter_int64(&iter);
                        }
                        bson_destroy(&rec);
                    }
                }

                bson_destroy(&query);
                bson_destroy(&reply);
                bson_destroy(doc);

                return ret;
            }
            posted on 2014-09-27 16:23 merlinfang 閱讀(3912) 評(píng)論(0)  編輯 收藏 引用 所屬分類: mongodb
            日产精品久久久久久久性色| 国产精品成人99久久久久91gav| 囯产极品美女高潮无套久久久 | 青青青青久久精品国产h| 久久国产精品久久国产精品| 欧美久久一级内射wwwwww.| 亚洲午夜久久久影院伊人| 四虎国产精品免费久久久| 久久婷婷色综合一区二区| 久久成人影院精品777| 国内精品久久久久影院老司| 97久久超碰国产精品2021| 久久久无码精品亚洲日韩蜜臀浪潮| 久久99国产精品久久久| 亚洲国产另类久久久精品黑人| 美女久久久久久| segui久久国产精品| 精品一区二区久久| 久久精品国产亚洲av水果派 | 国产午夜精品理论片久久影视| 无码人妻少妇久久中文字幕 | 国产精品午夜久久| 99久久精品国产高清一区二区| 久久久SS麻豆欧美国产日韩| 亚洲精品NV久久久久久久久久| 久久精品国产亚洲沈樵| 久久九九精品99国产精品| 久久综合精品国产二区无码| 久久天天躁狠狠躁夜夜2020一| 久久久久国产精品嫩草影院 | 一本久久久久久久| 久久免费国产精品一区二区| 国产一区二区三区久久| 国产精品久久波多野结衣| 国产精品99久久精品| 国产精品久久一区二区三区| 7777久久亚洲中文字幕| 久久美女人爽女人爽| 精品水蜜桃久久久久久久| 午夜不卡888久久| 污污内射久久一区二区欧美日韩 |