设置最小的ID值, 使下次nextId()的结果不小于此值
(long minId)
| 122 | * 设置最小的ID值, 使下次nextId()的结果不小于此值 |
| 123 | */ |
| 124 | public boolean setMinId(long minId) { |
| 125 | int serverId = module.zeze.getConfig().getServerId(); |
| 126 | if (serverId < 0) // serverId不应该<0,因为会导致nextId返回负值 |
| 127 | throw new IllegalStateException("AutoKey.setMinId: serverId(" + serverId + ") < 0"); |
| 128 | if (serverId == 0) |
| 129 | return setSeed(minId); // WriteULong(minId)再ToLongBE得到的值一定不小于minId,其实还能再选出符合条件的更小seed值,但serverId极少=0,所以不考虑那么多了 |
| 130 | var bb = ByteBuffer.Allocate(8); |
| 131 | bb.WriteUInt(serverId); |
| 132 | bb.WriteULong(0); |
| 133 | long id = ByteBuffer.ToLongBE(bb.Bytes, 0, bb.WriteIndex); |
| 134 | long seed = 0; |
| 135 | while (id < minId) { |
| 136 | if ((id & 0xff80_0000_0000_0000L) != 0) { |
| 137 | throw new IllegalStateException("AutoKey.setMinId: minId(" + minId |
| 138 | + ") is too large for serverId(" + serverId + ')'); |
| 139 | } |
| 140 | id <<= 8; |
| 141 | seed = seed == 0 ? 0x80 : seed << 7; // 每多7位,WriteULong序列化就要多1字节 |
| 142 | } |
| 143 | return setSeed(seed); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * 设置当前serverId的种子,新种子必须比当前值大。 |
nothing calls this directly
no test coverage detected