为Couchbase生成唯一的UInt32 ID [英] Generate Unique UInt32 ID for Couchbase

查看:146
本文介绍了为Couchbase生成唯一的UInt32 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种为nosql数据库生成唯一ID的方法。与关系数据库不同,没有行的概念,这意味着没有最后一行要递增。

I am looking for a way to generate a unique ID for nosql database. Unlike relational database there is no idea of rows which means there is no last row to increment from.

最常见的处理方法是使用 UUID的。但是我的问题是我需要添加另一个ID(除了UUID ),该ID应该是:

The most common way to handle this is to use UUID's. But my problem is I need to add another ID (other than the UUID) which needs to be:


  • 唯一

  • 未签名的Int32

总数据可能达到 50,000,000 。那么,如何生成一些 唯一 uint32 ID?

Total data could reach around 50,000,000. So how would you generate somewhat unique uint32 ID's?


UInt32值类型表示值为0到4,294,967,295之间的无符号整数。

The UInt32 value type represents unsigned integers with values ranging from 0 to 4,294,967,295.




  • 仅在新用户注册时生成。 / li>
  • 为每个新用户提供3个ID。

  • 当前使用 Couchbase Server

    • Only generated when a new user registers.
    • 3 Id's are given to each new user.
    • Currently using Couchbase Server.
    • 推荐答案

      此问题已解决-我建议使用原子增量(或减量)函数-这些是常见的生成模式唯一的ID。

      This problem has already been solved - I would suggest using the atomic Increment (or Decrement) functions in Couchbase - these are a common pattern to generate unique IDs.

      每当调用 incr()方法时,它原子地递增计数器返回指定值,并返回旧值,因此,如果两个客户端尝试同时进行递增操作是安全的。

      Whenever the incr() method is called, it atomically increments the counter by the specified value, and returns the old value, therefore it's safe if two clients try to increment at the same time.

      伪代码示例(我不是Node) .JS专家!):

      Pseudocode example (I'm no Node.JS expert!):

      // Once, at the beginning of time we init the counter:
      client.set("user::count", 0);
      ...
      // Then, whenever a new user is needed: 
      nextID = client.incr("user::count", 1); // increments counter and returns 'old' value.
      newKey = "user_" + nextID;
      client.add(newKey, value);
      

      请参见 Node.JS SDK 供参考,请参阅使用Couchbase开发人员指南中的查阅参考文档部分中的完整用法示例。

      See the Node.JS SDK for reference, and see Using Reference Doucments for Lookups section in the Couchbase Developer Guide for a complete usage example.

      这篇关于为Couchbase生成唯一的UInt32 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆