3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 package org
.apache
.hadoop
.hbase
.regionserver
;
21 import org
.apache
.hadoop
.hbase
.Cell
;
22 import org
.apache
.hadoop
.hbase
.CellComparator
;
23 import org
.apache
.hadoop
.hbase
.CellUtil
;
24 import org
.apache
.yetus
.audience
.InterfaceAudience
;
25 import org
.apache
.hadoop
.hbase
.util
.ClassSize
;
27 import java
.io
.IOException
;
30 * CellArrayImmutableSegment extends the API supported by a {@link Segment},
31 * and {@link ImmutableSegment}. This immutable segment is working with CellSet with
32 * CellArrayMap delegatee.
34 @InterfaceAudience.Private
35 public class CellArrayImmutableSegment
extends ImmutableSegment
{
37 public static final long DEEP_OVERHEAD_CAM
= DEEP_OVERHEAD
+ ClassSize
.CELL_ARRAY_MAP
;
39 ///////////////////// CONSTRUCTORS /////////////////////
40 /**------------------------------------------------------------------------
41 * C-tor to be used when new CellArrayImmutableSegment is a result of compaction of a
42 * list of older ImmutableSegments.
43 * The given iterator returns the Cells that "survived" the compaction.
45 protected CellArrayImmutableSegment(CellComparator comparator
, MemStoreSegmentsIterator iterator
,
46 MemStoreLAB memStoreLAB
, int numOfCells
, MemStoreCompactionStrategy
.Action action
) {
47 super(null, comparator
, memStoreLAB
); // initiailize the CellSet with NULL
48 incMemStoreSize(0, DEEP_OVERHEAD_CAM
, 0, 0); // CAM is always on-heap
49 // build the new CellSet based on CellArrayMap and update the CellSet of the new Segment
50 initializeCellSet(numOfCells
, iterator
, action
);
53 /**------------------------------------------------------------------------
54 * C-tor to be used when new CellChunkImmutableSegment is built as a result of flattening
55 * of CSLMImmutableSegment
56 * The given iterator returns the Cells that "survived" the compaction.
58 protected CellArrayImmutableSegment(CSLMImmutableSegment segment
, MemStoreSizing mss
,
59 MemStoreCompactionStrategy
.Action action
) {
60 super(segment
); // initiailize the upper class
61 long indexOverhead
= DEEP_OVERHEAD_CAM
- CSLMImmutableSegment
.DEEP_OVERHEAD_CSLM
;
62 incMemStoreSize(0, indexOverhead
, 0, 0); // CAM is always on-heap
63 mss
.incMemStoreSize(0, indexOverhead
, 0, 0);
64 int numOfCells
= segment
.getCellsCount();
65 // build the new CellSet based on CellChunkMap and update the CellSet of this Segment
66 reinitializeCellSet(numOfCells
, segment
.getScanner(Long
.MAX_VALUE
), segment
.getCellSet(),
68 // arrange the meta-data size, decrease all meta-data sizes related to SkipList;
69 // add sizes of CellArrayMap entry (reinitializeCellSet doesn't take the care for the sizes)
70 long newSegmentSizeDelta
=
71 numOfCells
* (indexEntrySize() - ClassSize
.CONCURRENT_SKIPLISTMAP_ENTRY
);
72 incMemStoreSize(0, newSegmentSizeDelta
, 0, 0);
73 mss
.incMemStoreSize(0, newSegmentSizeDelta
, 0, 0);
77 protected long indexEntrySize() {
78 return ClassSize
.CELL_ARRAY_MAP_ENTRY
;
82 protected boolean canBeFlattened() {
86 ///////////////////// PRIVATE METHODS /////////////////////
87 /*------------------------------------------------------------------------*/
88 // Create CellSet based on CellArrayMap from compacting iterator
89 private void initializeCellSet(int numOfCells
, MemStoreSegmentsIterator iterator
,
90 MemStoreCompactionStrategy
.Action action
) {
92 boolean merge
= (action
== MemStoreCompactionStrategy
.Action
.MERGE
||
93 action
== MemStoreCompactionStrategy
.Action
.MERGE_COUNT_UNIQUE_KEYS
);
94 Cell
[] cells
= new Cell
[numOfCells
]; // build the Cell Array
98 while (iterator
.hasNext()) {
99 Cell c
= iterator
.next();
100 // The scanner behind the iterator is doing all the elimination logic
102 // if this is merge we just move the Cell object without copying MSLAB
103 // the sizes still need to be updated in the new segment
106 // now we just copy it to the new segment (also MSLAB copy)
107 cells
[i
] = maybeCloneWithAllocator(c
, false);
109 // second parameter true, because in compaction/merge the addition of the cell to new segment
110 // is always successful
111 updateMetaInfo(cells
[i
], true, null); // updates the size per cell
112 if(action
== MemStoreCompactionStrategy
.Action
.MERGE_COUNT_UNIQUE_KEYS
) {
113 //counting number of unique keys
115 if (!CellUtil
.matchingRowColumnBytes(prev
, c
)) {
125 if(action
== MemStoreCompactionStrategy
.Action
.COMPACT
) {
126 numUniqueKeys
= numOfCells
;
127 } else if(action
!= MemStoreCompactionStrategy
.Action
.MERGE_COUNT_UNIQUE_KEYS
) {
128 numUniqueKeys
= CellSet
.UNKNOWN_NUM_UNIQUES
;
130 // build the immutable CellSet
131 CellArrayMap cam
= new CellArrayMap(getComparator(), cells
, 0, i
, false);
132 this.setCellSet(null, new CellSet(cam
, numUniqueKeys
)); // update the CellSet of this Segment
135 /*------------------------------------------------------------------------*/
136 // Create CellSet based on CellChunkMap from current ConcurrentSkipListMap based CellSet
137 // (without compacting iterator)
138 // We do not consider cells bigger than chunks!
139 private void reinitializeCellSet(
140 int numOfCells
, KeyValueScanner segmentScanner
, CellSet oldCellSet
,
141 MemStoreCompactionStrategy
.Action action
) {
142 Cell
[] cells
= new Cell
[numOfCells
]; // build the Cell Array
148 while ((curCell
= segmentScanner
.next()) != null) {
149 cells
[idx
++] = curCell
;
150 if(action
== MemStoreCompactionStrategy
.Action
.FLATTEN_COUNT_UNIQUE_KEYS
) {
151 //counting number of unique keys
153 if (!CellUtil
.matchingRowColumn(prev
, curCell
)) {
162 } catch (IOException ie
) {
163 throw new IllegalStateException(ie
);
165 segmentScanner
.close();
167 if(action
!= MemStoreCompactionStrategy
.Action
.FLATTEN_COUNT_UNIQUE_KEYS
) {
168 numUniqueKeys
= CellSet
.UNKNOWN_NUM_UNIQUES
;
170 // build the immutable CellSet
171 CellArrayMap cam
= new CellArrayMap(getComparator(), cells
, 0, idx
, false);
172 // update the CellSet of this Segment
173 this.setCellSet(oldCellSet
, new CellSet(cam
, numUniqueKeys
));