这是indexloc提供的服务,不要输入任何密码
Skip to content

Add Index manager #1

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

Merged
merged 15 commits into from
Jun 16, 2021
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*.dll
*.so
*.dylib
*.index
.vscode

# Test binary, built with `go test -c`
*.test
Expand Down
17 changes: 17 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [


{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
83 changes: 43 additions & 40 deletions src/BufferManager/buffermanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,94 @@ import (
"os"
)

const BlockSize = 4096
var blockBuffer *LRUCache
var connector="*"
const BlockSize = 4096 // for debug
var blockBuffer *LRUCache
var connector = "*"

var posNum=0
var posNum = 0
var fileNamePos2Int map[nameAndPos]int

type nameAndPos struct {
fileName string
blockId uint16
}

//It used at the beginging of program!!!
func InitBuffer() {
blockBuffer=NewLRUCache()
fileNamePos2Int=make(map[nameAndPos]int,InitSize*4)
posNum=0
func InitBuffer() {
blockBuffer = NewLRUCache()
fileNamePos2Int = make(map[nameAndPos]int, InitSize*4)
posNum = 0
}

//读byte,不检查block id和filename, 同时加互斥锁
func BlockRead(filename string, block_id uint16) (*Block, error) {
t:=Query2Int(nameAndPos{fileName: filename,blockId: block_id})
ok,block:=blockBuffer.GetBlock(t)
t := Query2Int(nameAndPos{fileName: filename, blockId: block_id})
ok, block := blockBuffer.GetBlock(t)
if ok {
block.mutex.Lock()
return block,nil
return block, nil
}
newBlock:=Block{
newBlock := Block{
blockid: block_id,
filename: filename,
Data: make([]byte,BlockSize),
Data: make([]byte, BlockSize),
}
err:=newBlock.read()
if err!=nil{
return nil,err
err := newBlock.read()
if err != nil {
return nil, err
}
blockBuffer.PutBlock(&newBlock,t)
blockBuffer.PutBlock(&newBlock, t)
newBlock.mutex.Lock()
return &newBlock,nil
return &newBlock, nil
}

//返回当前总共多少个块,一定是4KB的倍数
func GetBlockNumber(fileName string) (uint16,error) {
fileInfo, err:= os.Stat(fileName)
if err!=nil {
return 0,err
func GetBlockNumber(fileName string) (uint16, error) {
fileInfo, err := os.Stat(fileName)
if err != nil {
return 0, err
}
//fmt.Println("size is ",fileInfo.Size())
tmp:=fileInfo.Size()/BlockSize
return uint16(tmp),nil
tmp := fileInfo.Size() / BlockSize
return uint16(tmp), nil
}

// 返回的 block id 是指新的块在文件中的 block id
func NewBlock(filename string) (uint16, error) {
block_id,err:=GetBlockNumber(filename)
if err!=nil {
return 0,err
block_id, err := GetBlockNumber(filename)
if err != nil {
return 0, err
}
newBlock:=Block{
newBlock := Block{
blockid: block_id,
filename: filename,
Data: make([]byte,BlockSize),
Data: make([]byte, BlockSize),
dirty: true,
}
newBlock.flush()
t:=Query2Int(nameAndPos{fileName: filename,blockId: block_id})
blockBuffer.PutBlock(&newBlock,t)
return block_id,nil
t := Query2Int(nameAndPos{fileName: filename, blockId: block_id})
blockBuffer.PutBlock(&newBlock, t)
return block_id, nil
}


func BlockFlushAll() (bool, error) {
for _,item:=range blockBuffer.blockMap {
for _, item := range blockBuffer.blockMap {
if item.dirty {
err:=item.flush()
if err!=nil{}
return false,err
err := item.flush()
if err != nil {
}
return false, err
}
item.reset()
}
return true,nil
return true, nil
}

func Query2Int(pos nameAndPos) int {
if index,ok:=fileNamePos2Int[pos];ok{
if index, ok := fileNamePos2Int[pos]; ok {
return index
}
posNum++
fileNamePos2Int[pos]=posNum
fileNamePos2Int[pos] = posNum
return posNum
}
Loading