All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ltsopensource.kv.replay.TxLogReplay Maven / Gradle / Ivy

package com.github.ltsopensource.kv.replay;

import com.github.ltsopensource.kv.*;
import com.github.ltsopensource.kv.cache.DataCache;
import com.github.ltsopensource.kv.data.DataAppendResult;
import com.github.ltsopensource.kv.data.DataBlockEngine;
import com.github.ltsopensource.kv.index.Index;
import com.github.ltsopensource.kv.index.IndexItem;
import com.github.ltsopensource.kv.txlog.StoreTxLogCursorEntry;
import com.github.ltsopensource.kv.txlog.StoreTxLogEngine;
import com.github.ltsopensource.kv.txlog.StoreTxLogEntry;
import com.github.ltsopensource.kv.txlog.StoreTxLogPosition;
import com.github.ltsopensource.core.logger.Logger;

/**
 * @author Robert HG ([email protected]) on 12/19/15.
 */
public class TxLogReplay {

    private static final Logger LOGGER = DB.LOGGER;
    private StoreTxLogEngine storeTxLogEngine;
    private DataBlockEngine dataBlockEngine;
    private Index index;
    private DataCache dataCache;

    public TxLogReplay(StoreTxLogEngine storeTxLogEngine, DataBlockEngine dataBlockEngine, Index index, DataCache dataCache) {
        this.storeTxLogEngine = storeTxLogEngine;
        this.dataBlockEngine = dataBlockEngine;
        this.index = index;
        this.dataCache = dataCache;
    }

    public void replay(StoreTxLogPosition startPosition) {

        LOGGER.info("start to replay txLog ...");

        Cursor> cursor = storeTxLogEngine.cursor(startPosition);

        int count = 0;

        while (cursor.hasNext()) {
            StoreTxLogCursorEntry storeTxLogCursorEntry = cursor.next();

            StoreTxLogEntry storeTxLogEntry = storeTxLogCursorEntry.getStoreTxLogEntry();

            Operation op = storeTxLogEntry.getOp();

            StoreTxLogPosition position = storeTxLogCursorEntry.getPosition();

            K key = storeTxLogEntry.getKey();
            V value = storeTxLogEntry.getValue();

            if (op == Operation.PUT) {
                // 1. 写Data
                DataAppendResult dataAppendResult = dataBlockEngine.append(position, key, value);
                // 2. 写Index
                index.putIndexItem(position, key, DBImpl.convertToIndex(key, dataAppendResult));
                // 3. 写缓存
                dataCache.put(key, value);

            } else if (op == Operation.REMOVE) {
                // 1. 移除Index
                IndexItem indexItem = index.removeIndexItem(position, key);
                if (indexItem != null) {
                    // 2. 移除Data
                    dataBlockEngine.remove(position, indexItem);
                }
                // 2. 移除缓存
                dataCache.remove(key);
            } else {
                throw new DBException("error op=" + op);
            }

            count++;
        }

        LOGGER.info("replay txLog complete, txLog size:" + count);

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy