mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
refact: migrated aliases to islazy/data.UnsortedKV
This commit is contained in:
parent
2e5b2df7de
commit
7f808ac059
9 changed files with 171 additions and 182 deletions
3
vendor/github.com/evilsocket/islazy/data/doc.go
generated
vendored
Normal file
3
vendor/github.com/evilsocket/islazy/data/doc.go
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Package data contains basic threadsafe data structures with
|
||||
// filesystem persistance and configurable flushing policies.
|
||||
package data
|
14
vendor/github.com/evilsocket/islazy/data/flush.go
generated
vendored
Normal file
14
vendor/github.com/evilsocket/islazy/data/flush.go
generated
vendored
Normal file
|
@ -0,0 +1,14 @@
|
|||
package data
|
||||
|
||||
// FlushPolicy is the type of flush policy to use.
|
||||
type FlushPolicy int
|
||||
|
||||
const (
|
||||
// FlushOnEdit saves the object to disk after every modification.
|
||||
FlushOnEdit FlushPolicy = iota
|
||||
// FlushExplicit saves the object to disk only if the Flush method of
|
||||
// the object is explicitly called.
|
||||
FlushExplicit
|
||||
// FlushNone never saves the object to disk.
|
||||
FlushNone
|
||||
)
|
134
vendor/github.com/evilsocket/islazy/data/unsortedkv.go
generated
vendored
Normal file
134
vendor/github.com/evilsocket/islazy/data/unsortedkv.go
generated
vendored
Normal file
|
@ -0,0 +1,134 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/gob"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/evilsocket/islazy/fs"
|
||||
)
|
||||
|
||||
// UnsortedKV is a thread safe and unsorted key-value
|
||||
// storage with optional persistency on disk.
|
||||
type UnsortedKV struct {
|
||||
sync.Mutex
|
||||
fileName string
|
||||
m map[string]string
|
||||
policy FlushPolicy
|
||||
}
|
||||
|
||||
// NewUnsortedKV creates a new UnsortedKV with the given flush policy.
|
||||
// If fileName already exists, it will be deserialized and loaded.
|
||||
func NewUnsortedKV(fileName string, flushPolicy FlushPolicy) (*UnsortedKV, error) {
|
||||
ukv := &UnsortedKV{
|
||||
fileName: fileName,
|
||||
m: make(map[string]string),
|
||||
policy: flushPolicy,
|
||||
}
|
||||
|
||||
if fileName != "" && fs.Exists(fileName) {
|
||||
raw, err := ioutil.ReadFile(fileName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
decoder := gob.NewDecoder(bytes.NewReader(raw))
|
||||
if err = decoder.Decode(&ukv.m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ukv, nil
|
||||
}
|
||||
|
||||
// Has return true if name exists in the store.
|
||||
func (u *UnsortedKV) Has(name string) bool {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
_, found := u.m[name]
|
||||
return found
|
||||
}
|
||||
|
||||
// Get return the value of the named object if present, or returns
|
||||
// found as false otherwise.
|
||||
func (u *UnsortedKV) Get(name string) (v string, found bool) {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
v, found = u.m[name]
|
||||
return
|
||||
}
|
||||
|
||||
// GetOr will return the value of the named object if present,
|
||||
// or a default value.
|
||||
func (u *UnsortedKV) GetOr(name, or string) string {
|
||||
if v, found := u.Get(name); found {
|
||||
return v
|
||||
}
|
||||
return or
|
||||
}
|
||||
|
||||
func (u *UnsortedKV) flushUnlocked() error {
|
||||
buf := new(bytes.Buffer)
|
||||
encoder := gob.NewEncoder(buf)
|
||||
if err := encoder.Encode(u.m); err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(u.fileName, buf.Bytes(), os.ModePerm)
|
||||
}
|
||||
|
||||
// Flush flushes the store to disk if the flush policy
|
||||
// is different than FlushNone
|
||||
func (u *UnsortedKV) Flush() error {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
if u.policy != FlushNone {
|
||||
return u.flushUnlocked()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UnsortedKV) onEdit() error {
|
||||
if u.policy == FlushOnEdit {
|
||||
return u.flushUnlocked()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set sets a value for a named object.
|
||||
func (u *UnsortedKV) Set(name, value string) error {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
u.m[name] = value
|
||||
return u.onEdit()
|
||||
}
|
||||
|
||||
// Del deletes a named object from the store.
|
||||
func (u *UnsortedKV) Del(name string) error {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
delete(u.m, name)
|
||||
return u.onEdit()
|
||||
}
|
||||
|
||||
// Clear deletes every named object from the store.
|
||||
func (u *UnsortedKV) Clear() error {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
u.m = make(map[string]string)
|
||||
return u.onEdit()
|
||||
}
|
||||
|
||||
// Each iterates each named object in the store by
|
||||
// executing the callback cb on them, if the callback
|
||||
// returns true the iteration is interrupted.
|
||||
func (u *UnsortedKV) Each(cb func(k, v string) bool) {
|
||||
u.Lock()
|
||||
defer u.Unlock()
|
||||
for k, v := range u.m {
|
||||
if stop := cb(k, v); stop {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue