View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.io.HeapSize;
24  import org.apache.hadoop.hbase.util.ClassSize;
25  
26  /**
27   * This can be used when a Cell has to change with addition/removal of one or more tags. This is an
28   * efficient way to do so in which only the tags bytes part need to recreated and copied. All other
29   * parts, refer to the original Cell.
30   */
31  @InterfaceAudience.Private
32  public class TagRewriteCell implements Cell, SettableSequenceId, SettableTimestamp, HeapSize {
33  
34    private Cell cell;
35    private byte[] tags;
36  
37    /**
38     * @param cell The original Cell which it rewrites
39     * @param tags the tags bytes. The array suppose to contain the tags bytes alone.
40     */
41    public TagRewriteCell(Cell cell, byte[] tags) {
42      assert cell instanceof SettableSequenceId;
43      assert cell instanceof SettableTimestamp;
44      this.cell = cell;
45      this.tags = tags;
46      // tag offset will be treated as 0 and length this.tags.length
47      if (this.cell instanceof TagRewriteCell) {
48        // Cleaning the ref so that the byte[] can be GCed
49        ((TagRewriteCell) this.cell).tags = null;
50      }
51    }
52  
53    @Override
54    public byte[] getRowArray() {
55      return cell.getRowArray();
56    }
57  
58    @Override
59    public int getRowOffset() {
60      return cell.getRowOffset();
61    }
62  
63    @Override
64    public short getRowLength() {
65      return cell.getRowLength();
66    }
67  
68    @Override
69    public byte[] getFamilyArray() {
70      return cell.getFamilyArray();
71    }
72  
73    @Override
74    public int getFamilyOffset() {
75      return cell.getFamilyOffset();
76    }
77  
78    @Override
79    public byte getFamilyLength() {
80      return cell.getFamilyLength();
81    }
82  
83    @Override
84    public byte[] getQualifierArray() {
85      return cell.getQualifierArray();
86    }
87  
88    @Override
89    public int getQualifierOffset() {
90      return cell.getQualifierOffset();
91    }
92  
93    @Override
94    public int getQualifierLength() {
95      return cell.getQualifierLength();
96    }
97  
98    @Override
99    public long getTimestamp() {
100     return cell.getTimestamp();
101   }
102 
103   @Override
104   public byte getTypeByte() {
105     return cell.getTypeByte();
106   }
107 
108   @Override
109   @Deprecated
110   public long getMvccVersion() {
111     return getSequenceId();
112   }
113 
114   @Override
115   public long getSequenceId() {
116     return cell.getSequenceId();
117   }
118 
119   @Override
120   public byte[] getValueArray() {
121     return cell.getValueArray();
122   }
123 
124   @Override
125   public int getValueOffset() {
126     return cell.getValueOffset();
127   }
128 
129   @Override
130   public int getValueLength() {
131     return cell.getValueLength();
132   }
133 
134   @Override
135   public byte[] getTagsArray() {
136     return this.tags;
137   }
138 
139   @Override
140   public int getTagsOffset() {
141     return 0;
142   }
143 
144   @Override
145   public int getTagsLength() {
146     return this.tags.length;
147   }
148 
149   @Override
150   @Deprecated
151   public byte[] getValue() {
152     return cell.getValue();
153   }
154 
155   @Override
156   @Deprecated
157   public byte[] getFamily() {
158     return cell.getFamily();
159   }
160 
161   @Override
162   @Deprecated
163   public byte[] getQualifier() {
164     return cell.getQualifier();
165   }
166 
167   @Override
168   @Deprecated
169   public byte[] getRow() {
170     return cell.getRow();
171   }
172 
173   @Override
174   public long heapSize() {
175     long sum = CellUtil.estimatedHeapSizeOf(cell) - cell.getTagsLength();
176     sum += ClassSize.OBJECT;// this object itself
177     sum += (2 * ClassSize.REFERENCE);// pointers to cell and tags array
178     if (this.tags != null) {
179       sum += ClassSize.align(ClassSize.ARRAY);// "tags"
180       sum += this.tags.length;
181     }
182     return sum;
183   }
184 
185   @Override
186   public void setTimestamp(long ts) throws IOException {
187     // The incoming cell is supposed to be SettableTimestamp type.
188     CellUtil.setTimestamp(cell, ts);
189   }
190 
191   @Override
192   public void setTimestamp(byte[] ts, int tsOffset) throws IOException {
193     // The incoming cell is supposed to be SettableTimestamp type.
194     CellUtil.setTimestamp(cell, ts, tsOffset);
195   }
196 
197   @Override
198   public void setSequenceId(long seqId) throws IOException {
199     // The incoming cell is supposed to be SettableSequenceId type.
200     CellUtil.setSequenceId(cell, seqId);
201   }
202 }