|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner; |
| 18 | + |
| 19 | +import java.security.SecureRandom; |
| 20 | + |
| 21 | +public class XGoogSpannerRequestId { |
| 22 | + // 1. Generate the random process Id singleton. |
| 23 | + public static String RAND_PROCESS_ID = XGoogSpannerRequestId.generateRandProcessId(); |
| 24 | + public static long VERSION = 1; // The version of the specification being implemented. |
| 25 | + private long nthClientId; |
| 26 | + private long nthChannelId; |
| 27 | + private long nthRequest; |
| 28 | + private long attempt; |
| 29 | + |
| 30 | + public XGoogSpannerRequestId(long nthClientId, long nthChannelId, long nthRequest, long attempt) { |
| 31 | + this.nthClientId = nthClientId; |
| 32 | + this.nthChannelId = nthChannelId; |
| 33 | + this.nthRequest = nthRequest; |
| 34 | + this.attempt = attempt; |
| 35 | + } |
| 36 | + |
| 37 | + public static XGoogSpannerRequestId of( |
| 38 | + long nthClientId, long nthChannelId, long nthRequest, long attempt) { |
| 39 | + return new XGoogSpannerRequestId(nthClientId, nthChannelId, nthRequest, attempt); |
| 40 | + } |
| 41 | + |
| 42 | + private static String generateRandProcessId() { |
| 43 | + byte[] rBytes = new byte[8]; |
| 44 | + SecureRandom srng = new SecureRandom(); |
| 45 | + srng.nextBytes(rBytes); |
| 46 | + int result = |
| 47 | + Byte.toUnsignedInt(rBytes[0]) |
| 48 | + | Byte.toUnsignedInt(rBytes[1]) << 8 |
| 49 | + | Byte.toUnsignedInt(rBytes[2]) << 16 |
| 50 | + | Byte.toUnsignedInt(rBytes[3]) << 24; |
| 51 | + |
| 52 | + return Integer.toHexString(result); |
| 53 | + } |
| 54 | + |
| 55 | + public String toString() { |
| 56 | + return String.format( |
| 57 | + "%d.%s.%d.%d.%d.%d", |
| 58 | + this.VERSION, |
| 59 | + this.RAND_PROCESS_ID, |
| 60 | + this.nthClientId, |
| 61 | + this.nthChannelId, |
| 62 | + this.nthRequest, |
| 63 | + this.attempt); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public boolean equals(Object other) { |
| 68 | + if (other == null) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + |
| 72 | + if (!(other instanceof XGoogSpannerRequestId)) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + XGoogSpannerRequestId otherReqId = (XGoogSpannerRequestId) (other); |
| 77 | + return otherReqId.toString().equals(this.toString()); |
| 78 | + } |
| 79 | +} |
0 commit comments