Skip to content

Commit

Permalink
Add the ability to disable persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
TwiN committed May 17, 2020
1 parent 0e0284f commit f6a66ce
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
28 changes: 25 additions & 3 deletions gdstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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)
}
Expand Down
15 changes: 11 additions & 4 deletions persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gdstore

import (
"bufio"
"errors"
"fmt"
"os"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand All @@ -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() {
Expand All @@ -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 {
Expand Down

0 comments on commit f6a66ce

Please sign in to comment.