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.io.IOException;
021import java.util.List;
022import org.apache.hadoop.hbase.Cell;
023import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
024import org.apache.yetus.audience.InterfaceAudience;
025
026/**
027 * The MemStore holds in-memory modifications to the Store. Modifications are {@link Cell}s.
028 * <p>
029 * The MemStore functions should not be called in parallel. Callers should hold write and read
030 * locks. This is done in {@link HStore}.
031 * </p>
032 */
033@InterfaceAudience.Private
034public interface MemStore {
035
036  /**
037   * Creates a snapshot of the current memstore. Snapshot must be cleared by call to
038   * {@link #clearSnapshot(long)}.
039   * @return {@link MemStoreSnapshot}
040   */
041  MemStoreSnapshot snapshot();
042
043  /**
044   * Clears the current snapshot of the Memstore. nn * @see #snapshot()
045   */
046  void clearSnapshot(long id) throws UnexpectedStateException;
047
048  /**
049   * Flush will first clear out the data in snapshot if any (It will take a second flush invocation
050   * to clear the current Cell set). If snapshot is empty, current Cell set will be flushed.
051   * @return On flush, how much memory we will clear.
052   */
053  MemStoreSize getFlushableSize();
054
055  /**
056   * Return the size of the snapshot(s) if any
057   * @return size of the memstore snapshot
058   */
059  MemStoreSize getSnapshotSize();
060
061  /**
062   * Write an update n * @param memstoreSizing The delta in memstore size will be passed back via
063   * this. This will include both data size and heap overhead delta.
064   */
065  void add(final Cell cell, MemStoreSizing memstoreSizing);
066
067  /**
068   * Write the updates n * @param memstoreSizing The delta in memstore size will be passed back via
069   * this. This will include both data size and heap overhead delta.
070   */
071  void add(Iterable<Cell> cells, MemStoreSizing memstoreSizing);
072
073  /**
074   * @return Oldest timestamp of all the Cells in the MemStore
075   */
076  long timeOfOldestEdit();
077
078  /**
079   * Update or insert the specified cells.
080   * <p>
081   * For each Cell, insert into MemStore. This will atomically upsert the value for that
082   * row/family/qualifier. If a Cell did already exist, it will then be removed.
083   * <p>
084   * Currently the memstoreTS is kept at 0 so as each insert happens, it will be immediately
085   * visible. May want to change this so it is atomic across all KeyValues.
086   * <p>
087   * This is called under row lock, so Get operations will still see updates atomically. Scans will
088   * only see each KeyValue update as atomic. n * @param readpoint readpoint below which we can
089   * safely remove duplicate Cells.
090   * @param memstoreSizing The delta in memstore size will be passed back via this. This will
091   *                       include both data size and heap overhead delta.
092   */
093  void upsert(Iterable<Cell> cells, long readpoint, MemStoreSizing memstoreSizing);
094
095  /**
096   * @return scanner over the memstore. This might include scanner over the snapshot when one is
097   *         present.
098   */
099  List<KeyValueScanner> getScanners(long readPt) throws IOException;
100
101  /**
102   * @return Total memory occupied by this MemStore. This won't include any size occupied by the
103   *         snapshot. We assume the snapshot will get cleared soon. This is not thread safe and the
104   *         memstore may be changed while computing its size. It is the responsibility of the
105   *         caller to make sure this doesn't happen.
106   */
107  MemStoreSize size();
108
109  /**
110   * This method is called before the flush is executed.
111   * @return an estimation (lower bound) of the unflushed sequence id in memstore after the flush is
112   *         executed. if memstore will be cleared returns {@code HConstants.NO_SEQNUM}.
113   */
114  long preFlushSeqIDEstimation();
115
116  /* Return true if the memstore may use some extra memory space */
117  boolean isSloppy();
118
119  /**
120   * This message intends to inform the MemStore that next coming updates are going to be part of
121   * the replaying edits from WAL
122   */
123  default void startReplayingFromWAL() {
124    return;
125  }
126
127  /**
128   * This message intends to inform the MemStore that the replaying edits from WAL are done
129   */
130  default void stopReplayingFromWAL() {
131    return;
132  }
133}