BSON ObjectID Specification
A BSON ObjectID is a 12-byte value consisting of a 4-byte timestamp (seconds since epoch), a 3-byte machine>
0
1
2
3
4
5
6
7
8
9
10
11
time
machine
pid
inc
TimeStamp. This is a unix style timestamp. It is a signed int representing the number of seconds before or after January 1st 1970 (UTC).
Machine. This is the first three bytes of the (md5) hash of the machine host name, or of the mac/network address, or the virtual machine>
Pid. This is 2 bytes of the process>
Increment. This is an ever incrementing value, or a random number if a counter can't be used in the language/runtime.
BSON ObjectIds can be any 12 byte binary string that is unique; however, the server itself and almost all drivers use the format above.
分段查看ObjectId的指令及结果如下:
> db.test.findOne()._id.toString()
ObjectId("50c6b336ba95d7738d1042e3")
> db.test.findOne()._id.toString().substring(10,18)
50c6b336
> db.test.findOne()._id.toString().substring(18,24)
ba95d7
> db.test.findOne()._id.toString().substring(24,28)
738d
> db.test.findOne()._id.toString().substring(28,34)
1042e3
ObjectId占用12字节的存储空间,由“时间戳” 、“机器名”、“PID号”和“计数器”组成。使用机器名的好处是在分布式环境中能够避免单点计数的性能瓶颈。使用PID号的好处是支持同一机器内运行多个mongod实例。最终采用时间戳和计数器的组合来保证唯一性。
时间戳
确保ObjectId唯一性依赖的是时间的顺序,不依赖时间的取值,因此集群节点的时间不必完全同步。既然ObjectId已经有了时间戳,那么在文档中就可以省掉一个时间戳了。在使用ObjectID提取时间时,应注意到MongoDB允许各节点时间不一致这一细节。
下面是查看时间戳的两种写法:
> db.test1.findOne()._id.getTimestamp()
ISODate("2012-12-12T03:52:45Z")
> Date(parseInt(db.test1.findOne()._id.toString().substring(10,18),16))
Wed Dec 12 2012 12:11:02 GMT+0800
机器名
机器名通过Md5加密后取前三个字节,应该还是有重复概率的,配置生产集群时检查一下总不会错。另外,我也注意到重启MongoDB后MD5加密结果会发生变化,在利用ObjectID提取机器名信息时需格外注意。
PID号
注意到每次重启mongod进程后PID号通常会发生变化就可以了。
计数器
计数器占3个字节,表示的取值范围就是256*256*256-1=16777215。不妨认为MongDB性能的极限是单台设备一秒钟插入一千万条记录。以目前的水平看,单台设备一秒钟插入一万条就很不错了,因此ObjectID计数器的设计是够用的。
循环插入了一些记录,下面的查询中b是循环计数器,可以看出我机器上的ObjectId计数器是按顺序增加的:
> parseInt(db.test.findOne({b:1000})._id.toString().substring(28,34),16)
1947382
> parseInt(db.test.findOne({b:1001})._id.toString().substring(28,34),16)
1947383
> parseInt(db.test.findOne({b:1002})._id.toString().substring(28,34),16)
1947384
> parseInt(db.test.findOne({b:1003})._id.toString().substring(28,34),16)
1947385
以下代码源自:http://www.cnblogs.com/xjk15082/archive/2011/09/18/2180792.html
构建objectId
public class ObjectId implements Comparable , java.io.Serializable {
final int _time;
final int _machine;
final int _inc;
boolean _new;
public ObjectId(){
_time = (int) (System.currentTimeMillis() / 1000);
_machine = _genmachine;
_inc = _nextInc.getAndIncrement();
_new = true;
}
……
}
机器码和进程码的生成
private static final int _genmachine;
static {
try {
final int machinePiece;
{
StringBuilder sb = new StringBuilder();
Enumeration e = NetworkInterface.getNetworkInterfaces();
while ( e.hasMoreElements() ){
NetworkInterface ni = e.nextElement();
sb.append( ni.toString() );
}
machinePiece = sb.toString().hashCode()
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com