feat(cmd): added listcmd and refactored transactions to use TransactionArgs
This commit is contained in:
parent
cc206a7c8a
commit
2da42de7ab
5 changed files with 146 additions and 23 deletions
12
cmd/del.go
12
cmd/del.go
|
|
@ -36,9 +36,17 @@ var delCmd = &cobra.Command{
|
|||
|
||||
func del(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error {
|
||||
|
||||
trans := TransactionArgs{
|
||||
key: args[0],
|
||||
readonly: false,
|
||||
sync: false,
|
||||
transact: func(tx *badger.Txn, k []byte) error {
|
||||
return tx.Delete(k)
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
return store.Transaction(trans)
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
|||
13
cmd/get.go
13
cmd/get.go
|
|
@ -36,17 +36,26 @@ var getCmd = &cobra.Command{
|
|||
|
||||
func get(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
|
||||
var v []byte
|
||||
if err := store.Transaction(args[0], true, func(tx *badger.Txn, k []byte) error {
|
||||
trans := TransactionArgs{
|
||||
key: args[0],
|
||||
readonly: true,
|
||||
sync: false,
|
||||
transact: func(tx *badger.Txn, k []byte) error {
|
||||
item, err := tx.Get(k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
v, err = item.ValueCopy(nil)
|
||||
return err
|
||||
}); err != nil {
|
||||
},
|
||||
}
|
||||
|
||||
if err := store.Transaction(trans); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
store.Print("%s", v)
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
81
cmd/list.go
Normal file
81
cmd/list.go
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
/*
|
||||
Copyright © 2025 Lewis Wynne <lew@ily.rs>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// listCmd represents the set command
|
||||
var listCmd = &cobra.Command{
|
||||
Use: "list [@DB]",
|
||||
Short: "List the contents of a db.",
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: list,
|
||||
}
|
||||
|
||||
func list(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
var pf string
|
||||
var err error
|
||||
pf, err = strconv.Unquote(fmt.Sprintf(`"%%s%s%%s\n"`, "\t\t"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(args) == 0 {
|
||||
args = append(args, "@default")
|
||||
}
|
||||
|
||||
trans := TransactionArgs{
|
||||
key: args[0],
|
||||
readonly: true,
|
||||
sync: true,
|
||||
transact: func(tx *badger.Txn, k []byte) error {
|
||||
opts := badger.DefaultIteratorOptions
|
||||
opts.PrefetchSize = 10
|
||||
it := tx.NewIterator(opts)
|
||||
defer it.Close()
|
||||
for it.Rewind(); it.Valid(); it.Next() {
|
||||
item := it.Item()
|
||||
k := item.Key()
|
||||
if err := item.Value(func(v []byte) error {
|
||||
store.Print(pf, k, v)
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
return store.Transaction(trans)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
}
|
||||
27
cmd/set.go
27
cmd/set.go
|
|
@ -38,22 +38,31 @@ var setCmd = &cobra.Command{
|
|||
|
||||
func set(cmd *cobra.Command, args []string) error {
|
||||
store := &Store{}
|
||||
if len(args) == 2 {
|
||||
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error {
|
||||
return tx.Set(k, []byte(args[1]))
|
||||
})
|
||||
}
|
||||
|
||||
bts, err := io.ReadAll(cmd.InOrStdin())
|
||||
var value []byte
|
||||
if len(args) == 2 {
|
||||
value = []byte(args[1])
|
||||
} else {
|
||||
bytes, err := io.ReadAll(cmd.InOrStdin())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
value = bytes
|
||||
}
|
||||
|
||||
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error {
|
||||
return tx.Set(k, bts)
|
||||
})
|
||||
trans := TransactionArgs{
|
||||
key: args[0],
|
||||
readonly: false,
|
||||
sync: false,
|
||||
transact: func(tx *badger.Txn, k []byte) error {
|
||||
return tx.Set(k, value)
|
||||
},
|
||||
}
|
||||
|
||||
return store.Transaction(trans)
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(setCmd)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -90,18 +90,34 @@ func (s *Store) Print(pf string, vs ...[]byte) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Store) Transaction(key string, readonly bool, fn func(tx *badger.Txn, key []byte) error) error {
|
||||
k, dbName, err := s.parse(key)
|
||||
type TransactionArgs struct {
|
||||
key string
|
||||
readonly bool
|
||||
sync bool
|
||||
transact func(tx *badger.Txn, key []byte) error
|
||||
}
|
||||
|
||||
func (s *Store) Transaction(args TransactionArgs) error {
|
||||
k, dbName, err := s.parse(args.key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
db, err := s.open(dbName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
tx := db.NewTransaction(!readonly)
|
||||
if err := fn(tx, k); err != nil {
|
||||
|
||||
if args.sync {
|
||||
err = db.Sync()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
tx := db.NewTransaction(!args.readonly)
|
||||
if err := args.transact(tx, k); err != nil {
|
||||
tx.Discard()
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue