diff --git a/gdstore.go b/gdstore.go index 676afb5..f20a567 100644 --- a/gdstore.go +++ b/gdstore.go @@ -19,8 +19,19 @@ type GDStore struct { // // In contrast, writing to the file without a buffer is slower, but more reliable if your // application is prone to suddenly crashing + // + // Defaults to false useBuffer bool + // persistence lets the user set whether to persist data to the file or not. Meant to be used for testing without + // having to clean up the file. + // + // (default) If set to true, data is available both in-memory and persisted to the file found at FilePath. + // If set to false, data is available only in-memory, and upon destruction, all data will be lost. + // + // Defaults to true + persistence bool + file *os.File writer *bufio.Writer data map[string][]byte @@ -30,8 +41,9 @@ type GDStore struct { // New creates a new GDStore func New(filePath string) *GDStore { store := &GDStore{ - FilePath: filePath, - data: make(map[string][]byte), + FilePath: filePath, + data: make(map[string][]byte), + persistence: true, } err := store.loadFromDisk() if err != nil { @@ -48,6 +60,16 @@ func (store *GDStore) WithBuffer(useBuffer bool) *GDStore { return store } +// WithPersistence sets GDStore's persistence parameter to the value passed as parameter +// +// The ability to set persistence to false is there mainly for testing purposes +// +// The default value for persistence is true +func (store *GDStore) WithPersistence(persistence bool) *GDStore { + store.persistence = persistence + return store +} + // Get returns the value of a key as well as a bool that indicates whether an entry exists for that key. // // The bool is particularly useful if you want to differentiate between a key that has a nil value, and a @@ -60,7 +82,7 @@ func (store *GDStore) Get(key string) (value []byte, ok bool) { // GetString does the same thing as Get, but converts the value to a string func (store *GDStore) GetString(key string) (valueAsString string, ok bool) { var value []byte - value, ok = store.data[key] + value, ok = store.Get(key) if ok { valueAsString = string(value) } diff --git a/persistence.go b/persistence.go index 8bc24a9..0d7532f 100644 --- a/persistence.go +++ b/persistence.go @@ -2,7 +2,6 @@ package gdstore import ( "bufio" - "errors" "fmt" "os" ) @@ -41,17 +40,20 @@ func (store *GDStore) Flush() error { func (store *GDStore) Consolidate() error { store.mux.Lock() defer store.mux.Unlock() + if !store.persistence { + return nil + } // Close the file because we need to rename it store.Close() // Back up the old file before doing the consolidation err := os.Rename(store.FilePath, fmt.Sprintf("%s.bak", store.FilePath)) if err != nil { - return errors.New(fmt.Sprintf("unable to rename %s to %s.bak during consolidation: %s", store.FilePath, store.FilePath, err.Error())) + return fmt.Errorf("unable to rename %s to %s.bak during consolidation: %s", store.FilePath, store.FilePath, err.Error()) } // Create a new empty file file, err := os.Create(store.FilePath) if err != nil { - return errors.New(fmt.Sprintf("unable to create new empty file at %s during consolidation: %s", store.FilePath, err.Error())) + return fmt.Errorf("unable to create new empty file at %s during consolidation: %s", store.FilePath, err.Error()) } err = file.Close() if err != nil { @@ -66,6 +68,9 @@ func (store *GDStore) Consolidate() error { // loadFromDisk loads the store from the disk and consolidates the entries, or creates an empty file if there is no file func (store *GDStore) loadFromDisk() error { store.data = make(map[string][]byte) + if !store.persistence { + return nil + } file, err := os.Open(store.FilePath) if err != nil { // Check if the file exists, if it doesn't, then create it and return. @@ -79,7 +84,6 @@ func (store *GDStore) loadFromDisk() error { return err } } - // File doesn't exist, so we need to read it. scanner := bufio.NewScanner(file) for scanner.Scan() { @@ -104,6 +108,9 @@ func (store *GDStore) appendEntryToFile(entry *Entry) error { // appendEntriesToFile appends a list of entries to the store's file func (store *GDStore) appendEntriesToFile(entries []*Entry) (err error) { + if !store.persistence { + return + } if store.file == nil { store.file, err = os.OpenFile(store.FilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) if err != nil {