Hack ObjectId to indicate the object type

筅森魡賤 提交于 2019-12-23 19:58:07

问题


I want to hack ObjectId by manipulating the machineId like this:

         <timestamp> <machineId> <processId> <inc>
UserId   XXXXXXXX    XXXX01      XXXX        XXXXXX
OrderId  XXXXXXXX    XXXX02      XXXX        XXXXXX
CardId   XXXXXXXX    XXXX03      XXXX        XXXXXX
...

the basic idea is to use 1 byte of machineId to distinguish the object type, my question is: is there any problem when doing so (considering uniqueness and sharding)?

--- update on Dec 9 ---

Due to the difference between the spec and implementation Why the bson java implementation uses 4 bytes inc field?, I'm going to change my solution a bit to following style:

         <timestamp> <machineId> <processId> <inc>
UserId   XXXXXXXX    XXXX        XXXX        01XXXXXX
OrderId  XXXXXXXX    XXXX        XXXX        02XXXXXX
CardId   XXXXXXXX    XXXX        XXXX        03XXXXXX
...

回答1:


Assuming two bytes of machine id is sufficiently unique for your deployment, that should be fine. Neat idea!




回答2:


As long as you don't violate the spec, and the ObjectID is still the correct form, there is no reason you can't do this, and it will save you some space of course.

A couple of notes though:

First, if you are planning to do this by, say, overloading the code in a driver and altering how the ObjectID is generated, then you will be inserting the modified ObjectID as-is and the old "normal" one would never be inserted (most drivers are the ones actually generating the ObjectID, not the server, though it can be the server too).

That scenario, where it is inserted already altered, would be preferred. The reason?

If you update and change the generated ObjectID instead, or if you change the type later on and have to alter the ObjectID, you may hit issues if you try to use that field as a shard key (or part thereof) further down the road (shard key is immutable). The way around this in a sharded environment when you have to update a shard key, is to remove the document in question and re-add it rather than update.

Additionally, the update method is a needless operation you can avoid (may or may not be a concern for you).

Finally, there is uniqueness - since the machine ID is not going to change a lot anyway, I don't think you are going to hit any issues here, the inc field should take care of collisions within a single second, but just be careful in case the method you use to alter the ObjectID causes something odd to occur.



来源:https://stackoverflow.com/questions/13769013/hack-objectid-to-indicate-the-object-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!