diff --git a/cmd/del.go b/cmd/del.go index 3a79320..6d2ce08 100644 --- a/cmd/del.go +++ b/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 { - 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() { diff --git a/cmd/get.go b/cmd/get.go index 515bd12..64234c9 100644 --- a/cmd/get.go +++ b/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 { - item, err := tx.Get(k) - if err != nil { + 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 - } - v, err = item.ValueCopy(nil) - return err - }); err != nil { + }, + } + + if err := store.Transaction(trans); err != nil { return err } + store.Print("%s", v) return nil } diff --git a/cmd/list.go b/cmd/list.go new file mode 100644 index 0000000..5bd01c6 --- /dev/null +++ b/cmd/list.go @@ -0,0 +1,81 @@ +/* +Copyright © 2025 Lewis Wynne + +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) +} diff --git a/cmd/set.go b/cmd/set.go index 3e099b8..5db254d 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -38,22 +38,31 @@ var setCmd = &cobra.Command{ func set(cmd *cobra.Command, args []string) error { store := &Store{} + + var value []byte if len(args) == 2 { - return store.Transaction(args[0], false, func(tx *badger.Txn, k []byte) error { - return tx.Set(k, []byte(args[1])) - }) + value = []byte(args[1]) + } else { + bytes, err := io.ReadAll(cmd.InOrStdin()) + if err != nil { + return err + } + value = bytes } - bts, err := io.ReadAll(cmd.InOrStdin()) - if err != nil { - return err + 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(args[0], false, func(tx *badger.Txn, k []byte) error { - return tx.Set(k, bts) - }) + return store.Transaction(trans) } func init() { rootCmd.AddCommand(setCmd) } + diff --git a/cmd/shared.go b/cmd/shared.go index 2dd1eba..5796db6 100644 --- a/cmd/shared.go +++ b/cmd/shared.go @@ -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 }