Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

完成日志恢复模块 #18

Open
wants to merge 24 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions src/main/java/net/kaaass/rumbase/dataitem/IItemStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@

import net.kaaass.rumbase.dataitem.exception.PageCorruptedException;
import net.kaaass.rumbase.dataitem.exception.UUIDException;
import net.kaaass.rumbase.page.exception.FileException;
import net.kaaass.rumbase.page.exception.PageException;
import net.kaaass.rumbase.recovery.IRecoveryStorage;
import net.kaaass.rumbase.recovery.exception.LogException;
import net.kaaass.rumbase.transaction.TransactionContext;

import java.io.IOException;
import java.util.List;

/**
Expand All @@ -12,13 +17,44 @@
* @author kaito
*/
public interface IItemStorage {

void setMetaUuid(long uuid) throws PageException, IOException;

/**
* 获得日志管理器
*
* @return
*/
IRecoveryStorage getRecoveryStorage();

/**
* 获取表的tempFreePage
*
* @return
*/
public int getMaxPageId();

/**
* 将uuid对应的页强制写回
*/
public void flush(long uuid) throws FileException;

/**
* 插入数据项
*
* @param item 数据项
* @param txContext 事务上下文
* @param item 数据项
* @return 返回数据项的UUID
*/
long insertItem(TransactionContext txContext, byte[] item);
long insertItem(TransactionContext txContext, byte[] item) throws PageCorruptedException;

/**
* 不用日志进行插入,用于日志的管理
*
* @param item 数据项
* @return uuid
*/
long insertItemWithoutLog(byte[] item);

/**
* 插入一个有UUID的数据项,唯一使用的地方是日志恢复时使用
Expand All @@ -30,7 +66,7 @@ public interface IItemStorage {
* @param item 数据项
* @param uuid 编号
*/
void insertItemWithUuid(TransactionContext txContext, byte[] item, long uuid);
void insertItemWithUuid(byte[] item, long uuid);

/**
* 通过UUID查询数据项
Expand Down Expand Up @@ -59,6 +95,8 @@ public interface IItemStorage {
*/
void updateItemByUuid(TransactionContext txContext, long uuid, byte[] item) throws UUIDException, PageCorruptedException;

byte[] updateItemWithoutLog(long uuid, byte[] item) throws UUIDException;

/**
* 获得数据项存储的元数据(可以用于头)
*
Expand All @@ -71,14 +109,33 @@ public interface IItemStorage {
*
* @param metadata 头信息
*/
void setMetadata(TransactionContext txContext, byte[] metadata) throws PageCorruptedException;
long setMetadata(TransactionContext txContext, byte[] metadata);

/**
* 不使用日志设置元数据
*
* @param metadata 头信息
*/
long setMetadataWithoutLog(byte[] metadata);

/**
* 清理多余的数据项,空间清理时使用。
*
* @param uuids 数据项UUID的编号列表
*/
void removeItems(List<Long> uuids);

/**
* 日志恢复时用,回退对应的insert操作,做法是删除对应的uuid
*
* @param uuid
*/
void deleteUuid(long uuid) throws PageException, IOException;

/**
* 建议存储进行一次回写
*/
void flush();
}


29 changes: 26 additions & 3 deletions src/main/java/net/kaaass/rumbase/dataitem/ItemManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package net.kaaass.rumbase.dataitem;


import net.kaaass.rumbase.dataitem.exception.PageCorruptedException;
import net.kaaass.rumbase.page.exception.FileException;
import net.kaaass.rumbase.page.exception.PageException;
import net.kaaass.rumbase.recovery.exception.LogException;
import net.kaaass.rumbase.transaction.TransactionContext;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -25,7 +26,7 @@ public class ItemManager {
* @param fileName 文件名
* @return 数据项管理器,用于管理数据项
*/
public static IItemStorage fromFile(String fileName) throws FileException, IOException, PageException {
public static IItemStorage fromFile(String fileName) throws FileException, PageException, LogException {
if (maps.containsKey(fileName)) {
return maps.get(fileName);
} else {
Expand All @@ -35,6 +36,17 @@ public static IItemStorage fromFile(String fileName) throws FileException, IOExc
}
}

public static IItemStorage fromFileWithoutLog(String fileName) throws FileException, PageException, PageCorruptedException {
if (maps.containsKey(fileName)) {
return maps.get(fileName);
} else {
IItemStorage iItemStorage = ItemStorage.ofFileWithoutLog(fileName);
maps.put(fileName, iItemStorage);
return iItemStorage;
}
}


/**
* 新建一个数据库,并且将上层提供的头信息写入。
*
Expand All @@ -44,7 +56,7 @@ public static IItemStorage fromFile(String fileName) throws FileException, IOExc
* @return 数据项管理器
* @throws FileException 想新建的文件已经存在的异常
*/
public static IItemStorage createFile(TransactionContext txContext, String fileName, byte[] metadata) throws FileException, IOException, PageException {
public static IItemStorage createFile(TransactionContext txContext, String fileName, byte[] metadata) throws FileException, PageException, LogException {
// 如果文件已经存在,那么就抛出文件已存在异常
if (maps.containsKey(fileName)) {
throw new FileException(1);
Expand All @@ -54,6 +66,17 @@ public static IItemStorage createFile(TransactionContext txContext, String fileN
maps.put(fileName, iItemStorage);
return iItemStorage;
}
}

public static IItemStorage createFileWithoutLog(String fileName, byte[] metadata) throws FileException, PageException {
// 如果文件已经存在,那就返回
if (maps.containsKey(fileName)) {
return maps.get(fileName);
} else {
// 若文件不存在,则创建文件。
IItemStorage iItemStorage = ItemStorage.ofNewFileWithoutLog(fileName, metadata);
maps.put(fileName, iItemStorage);
return iItemStorage;
}
}
}
Loading