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.io.hfile;
019
020import static javax.swing.Spring.UNSET;
021import static org.apache.hadoop.hbase.io.ByteBuffAllocator.HEAP;
022
023import org.apache.hadoop.hbase.io.ByteBuffAllocator;
024import org.apache.hadoop.hbase.nio.ByteBuff;
025import org.apache.yetus.audience.InterfaceAudience;
026
027@InterfaceAudience.Private
028public class HFileBlockBuilder {
029
030  private BlockType blockType;
031  private int onDiskSizeWithoutHeader;
032  private int onDiskDataSizeWithHeader;
033  private int uncompressedSizeWithoutHeader;
034  private long prevBlockOffset;
035  private ByteBuff buf;
036  private boolean fillHeader = false;
037  private long offset = UNSET;
038  private int nextBlockOnDiskSize = UNSET;
039  private HFileContext fileContext;
040  private ByteBuffAllocator allocator = HEAP;
041  private boolean isShared;
042
043  public HFileBlockBuilder withBlockType(BlockType blockType) {
044    this.blockType = blockType;
045    return this;
046  }
047
048  public HFileBlockBuilder withOnDiskSizeWithoutHeader(int onDiskSizeWithoutHeader) {
049    this.onDiskSizeWithoutHeader = onDiskSizeWithoutHeader;
050    return this;
051  }
052
053  public HFileBlockBuilder withOnDiskDataSizeWithHeader(int onDiskDataSizeWithHeader) {
054    this.onDiskDataSizeWithHeader = onDiskDataSizeWithHeader;
055    return this;
056  }
057
058  public HFileBlockBuilder withUncompressedSizeWithoutHeader(int uncompressedSizeWithoutHeader) {
059    this.uncompressedSizeWithoutHeader = uncompressedSizeWithoutHeader;
060    return this;
061  }
062
063  public HFileBlockBuilder withPrevBlockOffset(long prevBlockOffset) {
064    this.prevBlockOffset = prevBlockOffset;
065    return this;
066  }
067
068  public HFileBlockBuilder withByteBuff(ByteBuff buf) {
069    this.buf = buf;
070    return this;
071  }
072
073  public HFileBlockBuilder withFillHeader(boolean fillHeader) {
074    this.fillHeader = fillHeader;
075    return this;
076  }
077
078  public HFileBlockBuilder withOffset(long offset) {
079    this.offset = offset;
080    return this;
081  }
082
083  public HFileBlockBuilder withNextBlockOnDiskSize(int nextBlockOnDiskSize) {
084    this.nextBlockOnDiskSize = nextBlockOnDiskSize;
085    return this;
086  }
087
088  public HFileBlockBuilder withHFileContext(HFileContext fileContext) {
089    this.fileContext = fileContext;
090    return this;
091  }
092
093  public HFileBlockBuilder withByteBuffAllocator(ByteBuffAllocator allocator) {
094    this.allocator = allocator;
095    return this;
096  }
097
098  public HFileBlockBuilder withShared(boolean isShared) {
099    this.isShared = isShared;
100    return this;
101  }
102
103  public HFileBlock build() {
104    if (isShared) {
105      return new SharedMemHFileBlock(blockType, onDiskSizeWithoutHeader,
106        uncompressedSizeWithoutHeader, prevBlockOffset, buf, fillHeader, offset,
107        nextBlockOnDiskSize, onDiskDataSizeWithHeader, fileContext, allocator);
108    } else {
109      return new ExclusiveMemHFileBlock(blockType, onDiskSizeWithoutHeader,
110        uncompressedSizeWithoutHeader, prevBlockOffset, buf, fillHeader, offset,
111        nextBlockOnDiskSize, onDiskDataSizeWithHeader, fileContext, allocator);
112    }
113  }
114}