MongoDb--ObjectId

MongoDb的collection中,每个文档都有一个唯一标识:_id

ObjectId存储结构:

ObjectId是_id的默认类型,它占用12个字节的存储空间,以24位16进制数形式存储(一个字节用2个16进制数代表)

1-4字节:从标准纪元开始的时间,单位是秒

5-7字节:所在主机的唯一标识符,通常是机器主机名的hash值,可以确保在分布式环境中,不同主机生成不同的hash值,不产生冲突;

8-9字节:产生ObjectId的进程的标识符,可以确保一个机器上并发的多个线程产生的ObjectId的唯一性

有了前9个字节,1秒内不同机器上不同进程产生的ObjectId可以保证唯一性,一个机器一个进程,最后3个字节是自增计数器。一秒钟最多允许每个进程拥有256^3(16 777 216)个不同的ObjectId。

ObjectId可以在服务器端生成,也可以有驱动生成,我们看一下mongo-java-driver中是怎么生成的……

我们在程序里往数据库里增加数据时,会调驱动提供的insertOne方法,我们看一下insertOne的实现


@Override
public void insertOne(final TDocument document, final InsertOneOptions options) {
    notNull("document", document);
    TDocument insertDocument = document;
    if (getCodec() instanceof CollectibleCodec) {
        insertDocument = ((CollectibleCodec) getCodec()).generateIdIfAbsentFromDocument(document);
    }
    executeSingleWriteRequest(new InsertRequest(documentToBsonDocument(insertDocument)), options.getBypassDocumentValidation());
}

这个方法里,调用generateIdIfAbsentFromDocument生成文档的唯一标识_id,

private static final String ID_FIELD_NAME = "_id";

@Override
public BsonDocument generateIdIfAbsentFromDocument(final BsonDocument document) {
    if (!documentHasId(document)) {
        document.put(ID_FIELD_NAME, new BsonObjectId(new ObjectId()));
    }
    return document;
}

很传统,如果文档中没有_id字段,调用new ObjectId()生成一个,放入文档,接下来看看ObjectId的构造方法,

**
 * Create a new object id.
 */
public ObjectId() {
    this(new Date());//获取当前时间 (time.getTime() / 1000)
}

/**
 * Constructs a new instance using the given date.
 *
 * @param date the date
 */
public ObjectId(final Date date) {
    this(dateToTimestampSeconds(date), MACHINE_IDENTIFIER, PROCESS_IDENTIFIER, NEXT_COUNTER.getAndIncrement(), false);
}

构造方法里,我们可以看到ObjectId的四个基本要素,时间&机器标识&进程号&计数器,时间的生成方式比较简单,从注释里可以看出来。接下来,我们看看剩下三个要素在代码里是怎么生成的。
MACHINE_IDENTIFIER是int类型,PROCESS_IDENTIFIER是short类型,在static块中初始化:

static {
    try {
        MACHINE_IDENTIFIER = createMachineIdentifier();
        PROCESS_IDENTIFIER = createProcessIdentifier();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

1)MACHINE_IDENTIFIER初始化

private static int createMachineIdentifier() {
    // build a 2-byte machine piece based on NICs info
    int machinePiece;
    try {
        StringBuilder sb = new StringBuilder();
        Enumeration e = NetworkInterface.getNetworkInterfaces();
        while (e.hasMoreElements()) {
            NetworkInterface ni = e.nextElement();
            sb.append(ni.toString());
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                ByteBuffer bb = ByteBuffer.wrap(mac);
                try {
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                    sb.append(bb.getChar());
                } catch (BufferUnderflowException shortHardwareAddressException) { //NOPMD
                    // mac with less than 6 bytes. continue
                }
            }
        }
        machinePiece = sb.toString().hashCode();
    } catch (Throwable t) {
        // exception sometimes happens with IBM JVM, use random
        machinePiece = (new SecureRandom().nextInt());
        LOGGER.warn("Failed to get machine identifier from network interface, using random number instead", t);
    }
    machinePiece = machinePiece & LOW_ORDER_THREE_BYTES;
    return machinePiece;
}

2)PROCESS_IDENTIFIER

// Creates the process identifier.  This does not have to be unique per class loader because
// NEXT_COUNTER will provide the uniqueness.
private static short createProcessIdentifier() {
    short processId;
    try {
        String processName = java.lang.management.ManagementFactory.getRuntimeMXBean().getName();
        if (processName.contains("@")) {
            processId = (short) Integer.parseInt(processName.substring(0, processName.indexOf('@')));
        } else {
            processId = (short) java.lang.management.ManagementFactory.getRuntimeMXBean().getName().hashCode();
        }

    } catch (Throwable t) {
        processId = (short) new SecureRandom().nextInt();
        LOGGER.warn("Failed to get process identifier from JMX, using random number instead", t);
    }

    return processId;
}​

3)NEXT_COUNTER

private static final AtomicInteger NEXT_COUNTER = new AtomicInteger(new SecureRandom().nextInt());

最后将四个要素组合,转换成24位的16进制数

private ObjectId(final int timestamp, final int machineIdentifier, final short processIdentifier, final int counter,
                 final boolean checkCounter) {
    if ((machineIdentifier & 0xff000000) != 0) {
        throw new IllegalArgumentException("The machine identifier must be between 0 and 16777215 (it must fit in three bytes).");
    }
    if (checkCounter && ((counter & 0xff000000) != 0)) {
        throw new IllegalArgumentException("The counter must be between 0 and 16777215 (it must fit in three bytes).");
    }
    this.timestamp = timestamp;
    this.machineIdentifier = machineIdentifier;
    this.processIdentifier = processIdentifier;
    this.counter = counter & LOW_ORDER_THREE_BYTES;
}​

参考:
1.MongoDB权威指南(第2版)
2.mongo-java-driver 3.5.0

你可能感兴趣的:(MongoDb--ObjectId)