mirror of
https://github.com/taigrr/bitcask
synced 2025-01-18 04:03:17 -08:00
* Add Unit Test for testing a corrupted config * Add Unit Test for testing errors from .Stats() * Refactor Datafile into an interface and add Unit Tests for testing Merge() errors * Refactor indexer into an interface and add Unit Tests for .Close() errors * Add Unit Tests for .Delete() errors * Add Unit Tests for testing Put/Get errors * Add Unit Test for testing Open errors (bad path for example) * Refactor out bitcask.writeConfig * Add more tests for config errors * Add unit test for options that might error * Add more test cases for close errors * Add test case for rotating datafiles * Fix a possible data race in .Stats() * Add test case for checksum errors * Add test case for Sync errors with Put and WithSync enabled * Refactor and use testify.mock for mocks and generate mocks for all interfaces * Refactor TestCloseErrors * Refactored TestDeleteErrors * Refactored TestGetErrors * Refactored TestPutErrors * Refactored TestMergeErrors and fixed a bug with .Fold() * Add test case for Scan() errors * Apparently only Scan() can return nil Node()s?
55 lines
991 B
Go
55 lines
991 B
Go
package config
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"os"
|
|
)
|
|
|
|
// Config contains the bitcask configuration parameters
|
|
type Config struct {
|
|
MaxDatafileSize int `json:"max_datafile_size"`
|
|
MaxKeySize int `json:"max_key_size"`
|
|
MaxValueSize int `json:"max_value_size"`
|
|
Sync bool `json:"sync"`
|
|
}
|
|
|
|
// Load loads a configuration from the given path
|
|
func Load(path string) (*Config, error) {
|
|
var cfg Config
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &cfg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &cfg, nil
|
|
}
|
|
|
|
// Save saves the configuration to the provided path
|
|
func (c *Config) Save(path string) error {
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
data, err := json.Marshal(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if _, err = f.Write(data); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err = f.Sync(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return f.Close()
|
|
}
|