1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.accumulo.core.data;
18
19 import static org.apache.accumulo.core.util.ByteBufferUtil.toBytes;
20
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25
26 import org.apache.accumulo.core.data.thrift.TColumn;
27 import org.apache.hadoop.io.WritableComparable;
28 import org.apache.hadoop.io.WritableComparator;
29
30 public class Column implements WritableComparable<Column> {
31
32 static private int compareBytes(byte[] a, byte[] b) {
33 if (a == null && b == null)
34 return 0;
35 if (a == null)
36 return -1;
37 if (b == null)
38 return 1;
39 return WritableComparator.compareBytes(a, 0, a.length, b, 0, b.length);
40 }
41
42 public int compareTo(Column that) {
43 int result;
44 result = compareBytes(this.columnFamily, that.columnFamily);
45 if (result != 0)
46 return result;
47 result = compareBytes(this.columnQualifier, that.columnQualifier);
48 if (result != 0)
49 return result;
50 return compareBytes(this.columnVisibility, that.columnVisibility);
51 }
52
53 public void readFields(DataInput in) throws IOException {
54 if (in.readBoolean()) {
55 int len = in.readInt();
56 columnFamily = new byte[len];
57 in.readFully(columnFamily);
58 } else {
59 columnFamily = null;
60 }
61
62 if (in.readBoolean()) {
63 int len = in.readInt();
64 columnQualifier = new byte[len];
65 in.readFully(columnQualifier);
66 } else {
67 columnQualifier = null;
68 }
69
70 if (in.readBoolean()) {
71 int len = in.readInt();
72 columnVisibility = new byte[len];
73 in.readFully(columnVisibility);
74 } else {
75 columnVisibility = null;
76 }
77 }
78
79 @Override
80 public void write(DataOutput out) throws IOException {
81 if (columnFamily == null) {
82 out.writeBoolean(false);
83 } else {
84 out.writeBoolean(true);
85 out.writeInt(columnFamily.length);
86 out.write(columnFamily);
87 }
88
89 if (columnQualifier == null) {
90 out.writeBoolean(false);
91 } else {
92 out.writeBoolean(true);
93 out.writeInt(columnQualifier.length);
94 out.write(columnQualifier);
95 }
96
97 if (columnVisibility == null) {
98 out.writeBoolean(false);
99 } else {
100 out.writeBoolean(true);
101 out.writeInt(columnVisibility.length);
102 out.write(columnVisibility);
103 }
104 }
105
106 public byte[] columnFamily;
107 public byte[] columnQualifier;
108 public byte[] columnVisibility;
109
110 public Column() {}
111
112 public Column(byte[] columnFamily, byte[] columnQualifier, byte[] columnVisibility) {
113 this();
114 this.columnFamily = columnFamily;
115 this.columnQualifier = columnQualifier;
116 this.columnVisibility = columnVisibility;
117 }
118
119 public Column(TColumn tcol) {
120 this(toBytes(tcol.columnFamily), toBytes(tcol.columnQualifier), toBytes(tcol.columnVisibility));
121 }
122
123 @Override
124 public boolean equals(Object that) {
125 if (that == null)
126 return false;
127 if (that instanceof Column)
128 return this.equals((Column) that);
129 return false;
130 }
131
132 public boolean equals(Column that) {
133 return this.compareTo(that) == 0;
134 }
135
136 private static int hash(byte[] b) {
137 if (b == null)
138 return 0;
139
140 return WritableComparator.hashBytes(b, b.length);
141 }
142
143 @Override
144 public int hashCode() {
145 return hash(columnFamily) + hash(columnQualifier) + hash(columnVisibility);
146 }
147
148 public byte[] getColumnFamily() {
149 return columnFamily;
150 }
151
152 public byte[] getColumnQualifier() {
153 return columnQualifier;
154 }
155
156 public byte[] getColumnVisibility() {
157 return columnVisibility;
158 }
159
160 public String toString() {
161 return new String(columnFamily == null ? new byte[0] : columnFamily) + ":" + new String(columnQualifier == null ? new byte[0] : columnQualifier) + ":"
162 + new String(columnVisibility == null ? new byte[0] : columnVisibility);
163 }
164
165 public TColumn toThrift() {
166 return new TColumn(columnFamily == null ? null : ByteBuffer.wrap(columnFamily), columnQualifier == null ? null : ByteBuffer.wrap(columnQualifier),
167 columnVisibility == null ? null : ByteBuffer.wrap(columnVisibility));
168 }
169
170 }