Skip to content

Commit

Permalink
Merge branch 'pog'
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jun 9, 2024
2 parents e3f2117 + 2f3e234 commit 08626b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 40 deletions.
21 changes: 0 additions & 21 deletions pogreb/LICENSE

This file was deleted.

47 changes: 28 additions & 19 deletions pogreb/pogreb.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,33 +150,39 @@ func normalizeOptions(opts ...any) *WrappedOptions {
return pogrebopts
}

// Init opens a pogreb store at the given path to be referenced by storeName.
func (db *DB) Init(storeName string, opts ...any) error {
pogrebopts := defaultPogrebOptions
if len(opts) > 0 {
pogrebopts = normalizeOptions(opts...)
if pogrebopts == nil {
return ErrBadOptions
}
}
db.mu.Lock()
defer db.mu.Unlock()
func (db *DB) initStore(storeName string, pogrebOpts *WrappedOptions) error {
if _, ok := db.store[storeName]; ok {
return ErrStoreExists
}
path := db.Path()
if _, err := os.Stat(filepath.Join(path, storeName, "lock")); !os.IsNotExist(err) && !pogrebopts.AllowRecovery {
if _, err := os.Stat(filepath.Join(path, storeName, "lock")); !os.IsNotExist(err) && !pogrebOpts.AllowRecovery {
return fmt.Errorf("%w: and seems to be running... "+
"Please close it first, or use InitWithRecovery", ErrStoreExists)
}
c, e := pogreb.Open(filepath.Join(path, storeName), pogrebopts.Options)
c, e := pogreb.Open(filepath.Join(path, storeName), pogrebOpts.Options)
if e != nil {
return e
}
db.store[storeName] = &Store{DB: c}
return nil
}

// Init opens a pogreb store at the given path to be referenced by storeName.
func (db *DB) Init(storeName string, opts ...any) error {
pogrebopts := defaultPogrebOptions
if len(opts) > 0 {
pogrebopts = normalizeOptions(opts...)
if pogrebopts == nil {
return ErrBadOptions
}
}
db.mu.Lock()
err := db.initStore(storeName, pogrebopts)
db.mu.Unlock()

return err
}

// With calls the given underlying pogreb instance.
func (db *DB) With(storeName string) database.Store {
db.mu.RLock()
Expand All @@ -197,16 +203,19 @@ func (db *DB) WithNew(storeName string, opts ...any) database.Filer {
}
}

db.mu.RLock()
defer db.mu.RUnlock()
db.mu.Lock()
defer db.mu.Unlock()

d, ok := db.store[storeName]
if ok {
if ok && d == nil {
delete(db.store, storeName)
ok = false
}

if ok && d != nil {
return d
}
db.mu.RUnlock()
err := db.Init(storeName, pogrebopts)
db.mu.RLock()
err := db.initStore(storeName, pogrebopts)
if err == nil {
return db.store[storeName]
}
Expand Down

0 comments on commit 08626b0

Please sign in to comment.