Style fixes

This commit is contained in:
Přemysl Eric Janouch 2019-04-19 12:29:44 +02:00
parent 47de2cebaa
commit c7597b73c2
Signed by: p
GPG Key ID: A0420B94F92B9493
2 changed files with 14 additions and 14 deletions

24
db.go
View File

@ -12,20 +12,20 @@ import (
var ErrClosed = errors.New("database has been closed") var ErrClosed = errors.New("database has been closed")
type DB struct { type DB struct {
sync.RWMutex // Locking sync.RWMutex // locking
data map[string]string // Current state of the database data map[string]string // current state of the database
file *os.File // Data storage file *os.File // data storage
} }
// Open an existing database file, loading the contents to memory // OpenDB opens an existing database file, loading the contents to memory.
func OpenDB(path string) (*DB, error) { func OpenDB(path string) (*DB, error) {
file, err := os.OpenFile(path, os.O_RDWR, 0 /* not used */) file, err := os.OpenFile(path, os.O_RDWR, 0 /* not used */)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// TODO we might want a recover flag that just reads as much as it can // TODO: We might want a recover flag that just reads as much as it can
// instead of returning io.ErrUnexpectedEOF // instead of returning io.ErrUnexpectedEOF.
db := &DB{data: make(map[string]string), file: file} db := &DB{data: make(map[string]string), file: file}
for { for {
var header struct{ KeyLen, ValueLen int32 } var header struct{ KeyLen, ValueLen int32 }
@ -64,7 +64,7 @@ func OpenDB(path string) (*DB, error) {
return db, nil return db, nil
} }
// Create a new database, overwriting any previous contents of the file // CreateDB creates a new database, overwriting any previous file contents.
func CreateDB(path string) (*DB, error) { func CreateDB(path string) (*DB, error) {
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil { if err != nil {
@ -73,7 +73,7 @@ func CreateDB(path string) (*DB, error) {
return &DB{data: make(map[string]string), file: file}, nil return &DB{data: make(map[string]string), file: file}, nil
} }
// Retrieve the value corresponding to the given key // Get retrieves the value corresponding to the given key.
func (db *DB) Get(key string) (string, bool, error) { func (db *DB) Get(key string) (string, bool, error) {
db.RLock() db.RLock()
defer db.RUnlock() defer db.RUnlock()
@ -106,7 +106,7 @@ func put(file *os.File, key, value string) error {
return nil return nil
} }
// Save a key-value pair in the database storage // Put saves a key-value pair in the database storage.
func (db *DB) Put(key, value string) error { func (db *DB) Put(key, value string) error {
db.Lock() db.Lock()
defer db.Unlock() defer db.Unlock()
@ -127,7 +127,7 @@ func (db *DB) Put(key, value string) error {
return nil return nil
} }
// Delete a key from the database storage // Delete deletes a key from the database storage.
func (db *DB) Delete(key string) error { func (db *DB) Delete(key string) error {
db.Lock() db.Lock()
defer db.Unlock() defer db.Unlock()
@ -157,7 +157,7 @@ func (db *DB) Delete(key string) error {
return nil return nil
} }
// Get rid of historical data in the database file // Checkpoint gets rid of historical data in the database file.
func (db *DB) Checkpoint() error { func (db *DB) Checkpoint() error {
db.Lock() db.Lock()
defer db.Unlock() defer db.Unlock()
@ -194,7 +194,7 @@ func (db *DB) Checkpoint() error {
return nil return nil
} }
// Close the database file, rendering the object unusable // Close closes the database file, rendering the object unusable.
func (db *DB) Close() error { func (db *DB) Close() error {
db.Lock() db.Lock()
defer db.Unlock() defer db.Unlock()

View File

@ -1,4 +1,4 @@
// Demos a trivial key-value database backed by a file // Demos a trivial key-value database backed by a file.
package main package main
import ( import (
@ -77,7 +77,7 @@ func main() {
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig <-sig
// For simplicity, we'll wait for everything to finish, including snapshots // For simplicity, we'll wait for everything to finish, including snapshots.
if err := server.Shutdown(context.Background()); err != nil { if err := server.Shutdown(context.Background()); err != nil {
log.Fatalln(err) log.Fatalln(err)
} }