001/**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019package org.apache.hadoop.hbase.regionserver;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.CellComparator;
023import org.apache.yetus.audience.InterfaceAudience;
024
025import java.io.IOException;
026import java.util.ArrayList;
027import java.util.List;
028
029/**
030 * A singleton store segment factory.
031 * Generate concrete store segments.
032 */
033@InterfaceAudience.Private
034public final class SegmentFactory {
035
036  private SegmentFactory() {}
037  private static SegmentFactory instance = new SegmentFactory();
038
039  public static SegmentFactory instance() {
040    return instance;
041  }
042
043  // create composite immutable segment from a list of segments
044  // for snapshot consisting of multiple segments
045  public CompositeImmutableSegment createCompositeImmutableSegment(
046      final CellComparator comparator, List<ImmutableSegment> segments) {
047    return new CompositeImmutableSegment(comparator, segments);
048  }
049
050  // create new flat immutable segment from compacting old immutable segments
051  // for compaction
052  public ImmutableSegment createImmutableSegmentByCompaction(final Configuration conf,
053      final CellComparator comparator, MemStoreSegmentsIterator iterator, int numOfCells,
054      CompactingMemStore.IndexType idxType, MemStoreCompactionStrategy.Action action)
055      throws IOException {
056
057    MemStoreLAB memStoreLAB = MemStoreLAB.newInstance(conf);
058    return
059        createImmutableSegment(
060            conf,comparator,iterator,memStoreLAB,numOfCells,action,idxType);
061  }
062
063  // create empty immutable segment
064  // for initializations
065  public ImmutableSegment createImmutableSegment(CellComparator comparator) {
066    MutableSegment segment = generateMutableSegment(null, comparator, null);
067    return createImmutableSegment(segment);
068  }
069
070  // create not-flat immutable segment from mutable segment
071  public ImmutableSegment createImmutableSegment(MutableSegment segment) {
072    return new CSLMImmutableSegment(segment);
073  }
074
075  // create mutable segment
076  public MutableSegment createMutableSegment(final Configuration conf, CellComparator comparator) {
077    MemStoreLAB memStoreLAB = MemStoreLAB.newInstance(conf);
078    return generateMutableSegment(conf, comparator, memStoreLAB);
079  }
080
081  // create new flat immutable segment from merging old immutable segments
082  // for merge
083  public ImmutableSegment createImmutableSegmentByMerge(final Configuration conf,
084      final CellComparator comparator, MemStoreSegmentsIterator iterator, int numOfCells,
085      List<ImmutableSegment> segments, CompactingMemStore.IndexType idxType,
086      MemStoreCompactionStrategy.Action action)
087      throws IOException {
088
089    MemStoreLAB memStoreLAB = getMergedMemStoreLAB(conf, segments);
090    return
091        createImmutableSegment(
092            conf,comparator,iterator,memStoreLAB,numOfCells,action,idxType);
093
094  }
095
096  // create flat immutable segment from non-flat immutable segment
097  // for flattening
098  public ImmutableSegment createImmutableSegmentByFlattening(
099      CSLMImmutableSegment segment, CompactingMemStore.IndexType idxType,
100      MemStoreSizing memstoreSizing, MemStoreCompactionStrategy.Action action) {
101    ImmutableSegment res = null;
102    switch (idxType) {
103      case CHUNK_MAP:
104        res = new CellChunkImmutableSegment(segment, memstoreSizing, action);
105        break;
106      case CSLM_MAP:
107        assert false; // non-flat segment can not be the result of flattening
108        break;
109      case ARRAY_MAP:
110        res = new CellArrayImmutableSegment(segment, memstoreSizing, action);
111        break;
112    }
113    return res;
114  }
115
116
117  //****** private methods to instantiate concrete store segments **********//
118  private ImmutableSegment createImmutableSegment(final Configuration conf, final CellComparator comparator,
119      MemStoreSegmentsIterator iterator, MemStoreLAB memStoreLAB, int numOfCells,
120      MemStoreCompactionStrategy.Action action, CompactingMemStore.IndexType idxType) {
121
122    ImmutableSegment res = null;
123    switch (idxType) {
124    case CHUNK_MAP:
125      res = new CellChunkImmutableSegment(comparator, iterator, memStoreLAB, numOfCells, action);
126      break;
127    case CSLM_MAP:
128      assert false; // non-flat segment can not be created here
129      break;
130    case ARRAY_MAP:
131      res = new CellArrayImmutableSegment(comparator, iterator, memStoreLAB, numOfCells, action);
132      break;
133    }
134    return res;
135  }
136
137  private MutableSegment generateMutableSegment(final Configuration conf, CellComparator comparator,
138      MemStoreLAB memStoreLAB) {
139    // TBD use configuration to set type of segment
140    CellSet set = new CellSet(comparator);
141    return new MutableSegment(set, comparator, memStoreLAB);
142  }
143
144  private MemStoreLAB getMergedMemStoreLAB(Configuration conf, List<ImmutableSegment> segments) {
145    List<MemStoreLAB> mslabs = new ArrayList<>();
146    if (!conf.getBoolean(MemStoreLAB.USEMSLAB_KEY, MemStoreLAB.USEMSLAB_DEFAULT)) {
147      return null;
148    }
149    for (ImmutableSegment segment : segments) {
150      mslabs.add(segment.getMemStoreLAB());
151    }
152    return new ImmutableMemStoreLAB(mslabs);
153  }
154}