mirror of
https://github.com/nxshock/zkv.git
synced 2024-11-27 11:21:02 +05:00
Remove duplicated code
This commit is contained in:
parent
0458ac5152
commit
412ddb11a8
49
zkv.go
49
zkv.go
@ -329,57 +329,12 @@ func (s *Store) get(key, value interface{}) error {
|
||||
return err
|
||||
}
|
||||
|
||||
offset, exists := s.bufferDataOffset[string(hashToFind[:])]
|
||||
if exists {
|
||||
reader := bytes.NewReader(s.buffer.Bytes())
|
||||
|
||||
err = skip(reader, offset)
|
||||
b, err := s.getGobBytes(hashToFind)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, record, err := readRecord(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return decode(record.ValueBytes, value)
|
||||
}
|
||||
|
||||
offset, exists = s.dataOffset[string(hashToFind[:])]
|
||||
if !exists {
|
||||
return ErrNotExists
|
||||
}
|
||||
|
||||
readF, err := os.Open(s.filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer readF.Close()
|
||||
|
||||
decompressor, err := zstd.NewReader(readF)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer decompressor.Close()
|
||||
|
||||
err = skip(decompressor, offset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, record, err := readRecord(decompressor)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !bytes.Equal(record.KeyHash[:], hashToFind[:]) {
|
||||
expectedHashStr := base64.StdEncoding.EncodeToString(hashToFind[:])
|
||||
gotHashStr := base64.StdEncoding.EncodeToString(record.KeyHash[:])
|
||||
return fmt.Errorf("wrong hash of offset %d: expected %s, got %s", offset, expectedHashStr, gotHashStr)
|
||||
}
|
||||
|
||||
return decode(record.ValueBytes, value)
|
||||
return decode(b, value)
|
||||
}
|
||||
|
||||
func (s *Store) flush() error {
|
||||
|
Loading…
Reference in New Issue
Block a user