Skip to content

Commit

Permalink
Implement readKey and readValue utilities for efficient index managem…
Browse files Browse the repository at this point in the history
…ent and value retrieval
  • Loading branch information
rezkam committed Sep 17, 2023
1 parent f7e4198 commit 2641635
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 146 deletions.
86 changes: 45 additions & 41 deletions dataFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -115,53 +114,58 @@ func extractFileNumber(filename string) int {
return -1
}

func loadLogsFromDataFiles(filePaths []string) ([]*log, error) {
sort.Slice(filePaths, func(i, j int) bool {
return extractFileNumber(filePaths[i]) < extractFileNumber(filePaths[j])
})
logs := make([]*log, 0, len(filePaths))
func readKey(file *os.File, offset int64) (string, error) {
_, err := file.Seek(offset, io.SeekStart)
if err != nil {
return "", err
}

for _, dataFile := range filePaths {
file, err := os.OpenFile(dataFile, os.O_RDONLY, 0o644)
currentLog := &log{file: file, index: make(map[string]int64)}
logs = append(logs, currentLog)
if err != nil {
return nil, err
}
keySizeBuffer := make([]byte, 4)
_, err = file.Read(keySizeBuffer)
if err != nil {
return "", err
}

keySizeBuffer := make([]byte, 4)
if _, err := file.Read(keySizeBuffer); err != nil {
return nil, err
}
keySize := binary.LittleEndian.Uint32(keySizeBuffer)
keyBuffer := make([]byte, keySize)
_, err = file.Read(keyBuffer)
if err != nil {
return "", err
}

keySize := binary.LittleEndian.Uint32(keySizeBuffer)
keyBuffer := make([]byte, keySize)
if _, err := file.Read(keyBuffer); err != nil {
return nil, err
}
return string(keyBuffer), nil
}

currentPosition, err := file.Seek(0, io.SeekCurrent)
if err != nil {
return nil, err
}
func readValue(filePath string, offset int64, tombStone string) (string, error) {
file, err := os.OpenFile(filePath, os.O_RDONLY, 0644)
if err != nil {
return "", err
}
defer file.Close()

currentLog.index[string(keyBuffer)] = currentPosition
_, err = file.Seek(offset, io.SeekStart)
if err != nil {
return "", err
}

valueSizeBuffer := make([]byte, 4)
if _, err := file.Read(valueSizeBuffer); err != nil {
return nil, err
}
valueSize := binary.LittleEndian.Uint32(valueSizeBuffer)
_, err = file.Seek(int64(valueSize), io.SeekCurrent)
if err != nil {
return nil, err
}
valueSizeBuffer := make([]byte, 4)
_, err = file.Read(valueSizeBuffer)
if err != nil {
return "", err
}

err = file.Close()
if err != nil {
return nil, err
}
keySize := binary.LittleEndian.Uint32(valueSizeBuffer)
valueBuffer := make([]byte, keySize)
_, err = file.Read(valueBuffer)
if err != nil {
return "", err
}

value := string(valueBuffer)

if value == tombStone {
return "", fmt.Errorf("key not found")
}

return logs, nil
return value, nil
}
Loading

0 comments on commit 2641635

Please sign in to comment.