001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import java.util.Collections; 021import java.util.List; 022import org.apache.hadoop.hbase.CellComparator; 023import org.apache.hadoop.hbase.util.ClassSize; 024import org.apache.yetus.audience.InterfaceAudience; 025 026/** 027 * ImmutableSegment is an abstract class that extends the API supported by a {@link Segment}, and is 028 * not needed for a {@link MutableSegment}. 029 */ 030@InterfaceAudience.Private 031public abstract class ImmutableSegment extends Segment { 032 033 public static final long DEEP_OVERHEAD = 034 Segment.DEEP_OVERHEAD + ClassSize.NON_SYNC_TIMERANGE_TRACKER; 035 036 // each sub-type of immutable segment knows whether it is flat or not 037 protected abstract boolean canBeFlattened(); 038 039 public int getNumUniqueKeys() { 040 return getCellSet().getNumUniqueKeys(); 041 } 042 043 ///////////////////// CONSTRUCTORS ///////////////////// 044 /** 045 * ------------------------------------------------------------------------ Empty C-tor to be used 046 * only for CompositeImmutableSegment 047 */ 048 protected ImmutableSegment(CellComparator comparator) { 049 super(comparator, TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC)); 050 } 051 052 protected ImmutableSegment(CellComparator comparator, List<ImmutableSegment> segments) { 053 super(comparator, segments, TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC)); 054 } 055 056 /** 057 * ------------------------------------------------------------------------ C-tor to be used to 058 * build the derived classes 059 */ 060 protected ImmutableSegment(CellSet cs, CellComparator comparator, MemStoreLAB memStoreLAB) { 061 super(cs, comparator, memStoreLAB, TimeRangeTracker.create(TimeRangeTracker.Type.NON_SYNC)); 062 } 063 064 /** 065 * ------------------------------------------------------------------------ Copy C-tor to be used 066 * when new CSLMImmutableSegment (derived) is being built from a Mutable one. This C-tor should be 067 * used when active MutableSegment is pushed into the compaction pipeline and becomes an 068 * ImmutableSegment. 069 */ 070 protected ImmutableSegment(Segment segment) { 071 super(segment); 072 } 073 074 ///////////////////// PUBLIC METHODS ///////////////////// 075 076 public int getNumOfSegments() { 077 return 1; 078 } 079 080 public List<Segment> getAllSegments() { 081 return Collections.singletonList(this); 082 } 083 084 @Override 085 public String toString() { 086 String res = super.toString(); 087 res += "Num uniques " + getNumUniqueKeys() + "; "; 088 return res; 089 } 090 091 /** 092 * We create a new {@link SnapshotSegmentScanner} to increase the reference count of 093 * {@link MemStoreLABImpl} used by this segment. 094 */ 095 List<KeyValueScanner> getSnapshotScanners() { 096 return Collections.singletonList(new SnapshotSegmentScanner(this)); 097 } 098}