feat(cmd): added listcmd and refactored transactions to use TransactionArgs

This commit is contained in:
Lewis Wynne 2025-11-06 17:48:38 +00:00
parent cc206a7c8a
commit 2da42de7ab
5 changed files with 146 additions and 23 deletions

View file

@ -36,9 +36,17 @@ var delCmd = &cobra.Command{
func del(cmd *cobra.Command, args []string) error { func del(cmd *cobra.Command, args []string) error {
store := &Store{} store := &Store{}
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error {
return tx.Delete(k) 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() { func init() {

View file

@ -36,17 +36,26 @@ var getCmd = &cobra.Command{
func get(cmd *cobra.Command, args []string) error { func get(cmd *cobra.Command, args []string) error {
store := &Store{} store := &Store{}
var v []byte var v []byte
if err := store.Transaction(args[0], true, func(tx *badger.Txn, k []byte) error { trans := TransactionArgs{
item, err := tx.Get(k) key: args[0],
if err != nil { 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 return err
} },
v, err = item.ValueCopy(nil) }
return err
}); err != nil { if err := store.Transaction(trans); err != nil {
return err return err
} }
store.Print("%s", v) store.Print("%s", v)
return nil return nil
} }

81
cmd/list.go Normal file
View 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)
}

View file

@ -38,22 +38,31 @@ var setCmd = &cobra.Command{
func set(cmd *cobra.Command, args []string) error { func set(cmd *cobra.Command, args []string) error {
store := &Store{} store := &Store{}
var value []byte
if len(args) == 2 { if len(args) == 2 {
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error { value = []byte(args[1])
return tx.Set(k, []byte(args[1])) } else {
}) bytes, err := io.ReadAll(cmd.InOrStdin())
if err != nil {
return err
}
value = bytes
} }
bts, err := io.ReadAll(cmd.InOrStdin()) trans := TransactionArgs{
if err != nil { key: args[0],
return err readonly: false,
sync: false,
transact: func(tx *badger.Txn, k []byte) error {
return tx.Set(k, value)
},
} }
return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error { return store.Transaction(trans)
return tx.Set(k, bts)
})
} }
func init() { func init() {
rootCmd.AddCommand(setCmd) rootCmd.AddCommand(setCmd)
} }

View file

@ -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 { type TransactionArgs struct {
k, dbName, err := s.parse(key) 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 { if err != nil {
return err return err
} }
db, err := s.open(dbName) db, err := s.open(dbName)
if err != nil { if err != nil {
return err return err
} }
defer db.Close() 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() tx.Discard()
return err return err
} }