1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase;
20
21 import org.apache.hadoop.hbase.classification.InterfaceAudience;
22 import org.apache.hadoop.hbase.exceptions.DeserializationException;
23 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
24 import org.apache.hadoop.hbase.protobuf.generated.ClusterIdProtos;
25 import org.apache.hadoop.hbase.util.Bytes;
26
27 import java.io.IOException;
28 import java.util.UUID;
29
30
31
32
33
34
35 @InterfaceAudience.Private
36 public class ClusterId {
37 private final String id;
38
39
40
41
42 public ClusterId() {
43 this(UUID.randomUUID().toString());
44 }
45
46 public ClusterId(final String uuid) {
47 this.id = uuid;
48 }
49
50
51
52
53 public byte [] toByteArray() {
54 return ProtobufUtil.prependPBMagic(convert().toByteArray());
55 }
56
57
58
59
60
61
62
63 public static ClusterId parseFrom(final byte [] bytes) throws DeserializationException {
64 if (ProtobufUtil.isPBMagicPrefix(bytes)) {
65 int pblen = ProtobufUtil.lengthOfPBMagic();
66 ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
67 ClusterIdProtos.ClusterId cid = null;
68 try {
69 ProtobufUtil.mergeFrom(builder, bytes, pblen, bytes.length - pblen);
70 cid = builder.build();
71 } catch (IOException e) {
72 throw new DeserializationException(e);
73 }
74 return convert(cid);
75 } else {
76
77 return new ClusterId(Bytes.toString(bytes));
78 }
79 }
80
81
82
83
84 ClusterIdProtos.ClusterId convert() {
85 ClusterIdProtos.ClusterId.Builder builder = ClusterIdProtos.ClusterId.newBuilder();
86 return builder.setClusterId(this.id).build();
87 }
88
89
90
91
92
93 static ClusterId convert(final ClusterIdProtos.ClusterId cid) {
94 return new ClusterId(cid.getClusterId());
95 }
96
97
98
99
100 @Override
101 public String toString() {
102 return this.id;
103 }
104 }