Skip to content

Commit

Permalink
build index on start
Browse files Browse the repository at this point in the history
  • Loading branch information
abdulsattar committed Jan 25, 2024
1 parent 63250cf commit 7651cba
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,44 @@ func NewKV() (*KV, error) {
}
index := make(map[string]KeyDir)
kv := KV{file: file, index: index}
buildIndex(&kv)
err = buildIndex(&kv)
if err != nil {
return nil, err
}
return &kv, nil
}

func buildIndex(kv *KV) {

func buildIndex(kv *KV) error {
_, err := kv.file.Seek(0, io.SeekStart)
if err != nil {
return err
}
offset := int64(0)
for {
keyValueSizeBytes := make([]byte, 8+8)
_, err = kv.file.Read(keyValueSizeBytes)
if err != nil {
if err.Error() == "EOF" {
return nil
}
return err
}
keySize := int64(binary.BigEndian.Uint64(keyValueSizeBytes[0:8]))
valueSize := int64(binary.BigEndian.Uint64(keyValueSizeBytes[0:8]))

offset += 8 + 8

keyValueBytes := make([]byte, keySize+valueSize)
_, err = kv.file.Read(keyValueBytes)
if err != nil {
return err
}
key := string(keyValueBytes[:keySize])

kv.index[key] = KeyDir{valueSize: valueSize, valueOffset: offset + keySize}

offset += keySize + valueSize
}
}

func (kv *KV) Close() error {
Expand All @@ -38,15 +70,18 @@ func (kv *KV) Close() error {
func (kv *KV) Get(key string) (string, error) {
keyDir, exists := kv.index[key]
if !exists {
buildIndex(kv)
err := buildIndex(kv)
if err != nil {
return "", err
}
}
return readAt(kv.file, keyDir.valueOffset, keyDir.valueSize)
}

func readAt(file *os.File, offset int64, size int64) (string, error) {
_, err := file.Seek(offset, io.SeekStart)
if err != nil {
return "", nil
return "", err
}

contentBytes := make([]byte, size)
Expand Down

0 comments on commit 7651cba

Please sign in to comment.