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

com.github.ltsopensource.kv.txlog.StoreTxLogFileHeader Maven / Gradle / Ivy

package com.github.ltsopensource.kv.txlog;

import com.github.ltsopensource.kv.AbstractFileHeader;

import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * 头部 格式
 * 1. magic      2 byte
 * 3. firstRecordId 8 byte
 * 

* * @author Robert HG ([email protected]) on 12/17/15. */ public class StoreTxLogFileHeader extends AbstractFileHeader { // 2 byte private final static short magic = (short) 0xBE02; // 8 byte private long firstRecordId; public long getFirstRecordId() { return firstRecordId; } public void setFirstRecordId(long firstRecordId) { this.firstRecordId = firstRecordId; } @Override public int getLength() { return 2 + 8; } @Override public void read(FileChannel fileChannel) throws IOException { if (fileChannel.size() == 0) { return; } fileChannel.position(0); fileChannel.read(byteBuffer()); byteBuffer().position(0); short readMagic = byteBuffer().getShort(); if (readMagic != magic) { throw new IOException("Invalid file type magic number 0x" + Integer.toHexString(readMagic & 0xFFFF)); } this.firstRecordId = byteBuffer().getLong(); } @Override public void write(FileChannel fileChannel) throws IOException { byteBuffer().position(0); byteBuffer().putShort(magic); byteBuffer().putLong(this.firstRecordId); byteBuffer().flip(); fileChannel.position(0); fileChannel.write(byteBuffer()); fileChannel.force(true); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy