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      assert tags != null;
45      this.cell = cell;
46      this.tags = tags;
47      // tag offset will be treated as 0 and length this.tags.length
48      if (this.cell instanceof TagRewriteCell) {
49        // Cleaning the ref so that the byte[] can be GCed
50        ((TagRewriteCell) this.cell).tags = null;
51      }
52    }
53  
54    @Override
55    public byte[] getRowArray() {
56      return cell.getRowArray();
57    }
58  
59    @Override
60    public int getRowOffset() {
61      return cell.getRowOffset();
62    }
63  
64    @Override
65    public short getRowLength() {
66      return cell.getRowLength();
67    }
68  
69    @Override
70    public byte[] getFamilyArray() {
71      return cell.getFamilyArray();
72    }
73  
74    @Override
75    public int getFamilyOffset() {
76      return cell.getFamilyOffset();
77    }
78  
79    @Override
80    public byte getFamilyLength() {
81      return cell.getFamilyLength();
82    }
83  
84    @Override
85    public byte[] getQualifierArray() {
86      return cell.getQualifierArray();
87    }
88  
89    @Override
90    public int getQualifierOffset() {
91      return cell.getQualifierOffset();
92    }
93  
94    @Override
95    public int getQualifierLength() {
96      return cell.getQualifierLength();
97    }
98  
99    @Override
100   public long getTimestamp() {
101     return cell.getTimestamp();
102   }
103 
104   @Override
105   public byte getTypeByte() {
106     return cell.getTypeByte();
107   }
108 
109   @Override
110   public long getSequenceId() {
111     return cell.getSequenceId();
112   }
113 
114   @Override
115   public byte[] getValueArray() {
116     return cell.getValueArray();
117   }
118 
119   @Override
120   public int getValueOffset() {
121     return cell.getValueOffset();
122   }
123 
124   @Override
125   public int getValueLength() {
126     return cell.getValueLength();
127   }
128 
129   @Override
130   public byte[] getTagsArray() {
131     return this.tags;
132   }
133 
134   @Override
135   public int getTagsOffset() {
136     return 0;
137   }
138 
139   @Override
140   public int getTagsLength() {
141     if (null == this.tags) {
142       // Nulled out tags array optimization in constructor
143       return 0;
144     }
145     return this.tags.length;
146   }
147 
148   @Override
149   public long heapSize() {
150     long sum = CellUtil.estimatedHeapSizeOf(cell) - cell.getTagsLength();
151     sum += ClassSize.OBJECT;// this object itself
152     sum += (2 * ClassSize.REFERENCE);// pointers to cell and tags array
153     if (this.tags != null) {
154       sum += ClassSize.align(ClassSize.ARRAY);// "tags"
155       sum += this.tags.length;
156     }
157     return sum;
158   }
159 
160   @Override
161   public void setTimestamp(long ts) throws IOException {
162     // The incoming cell is supposed to be SettableTimestamp type.
163     CellUtil.setTimestamp(cell, ts);
164   }
165 
166   @Override
167   public void setTimestamp(byte[] ts, int tsOffset) throws IOException {
168     // The incoming cell is supposed to be SettableTimestamp type.
169     CellUtil.setTimestamp(cell, ts, tsOffset);
170   }
171 
172   @Override
173   public void setSequenceId(long seqId) throws IOException {
174     // The incoming cell is supposed to be SettableSequenceId type.
175     CellUtil.setSequenceId(cell, seqId);
176   }
177 }