Skip to content

Commit

Permalink
Rename IndexedDBComparer to Comparer
Browse files Browse the repository at this point in the history
  • Loading branch information
cions committed Dec 3, 2021
1 parent 0418a2f commit 614dce6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 64 deletions.
95 changes: 42 additions & 53 deletions commands.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package leveldbcli

import (
"bytes"
"fmt"
"io"
"os"
Expand All @@ -24,13 +23,18 @@ type entry struct {

var leveldbFilenamePattern = regexp.MustCompile(`^(?:LOCK|LOG(?:\.old)?|CURRENT(?:\.bak|\.\d+)?|MANIFEST-\d+|\d+\.(?:ldb|log|sst|tmp))$`)

func initCmd(c *cli.Context) error {
var cmp comparer.Comparer = comparer.DefaultComparer
func getComparer(c *cli.Context) comparer.Comparer {
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
return indexeddb.Comparer
}
return comparer.DefaultComparer
}

opts := &opt.Options{Comparer: cmp, ErrorIfExist: true}
func initCmd(c *cli.Context) error {
opts := &opt.Options{
Comparer: getComparer(c),
ErrorIfExist: true,
}
db, err := leveldb.OpenFile(c.String("dbpath"), opts)
if err != nil {
return err
Expand All @@ -57,12 +61,11 @@ func getCmd(c *cli.Context) (err error) {
return err
}

var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
opts := &opt.Options{
Comparer: getComparer(c),
ErrorIfMissing: true,
ReadOnly: true,
}

opts := &opt.Options{Comparer: cmp, ErrorIfMissing: true, ReadOnly: true}
db, err := leveldb.OpenFile(c.String("dbpath"), opts)
if err != nil {
return err
Expand Down Expand Up @@ -114,12 +117,10 @@ func putCmd(c *cli.Context) (err error) {
return err
}

var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
opts := &opt.Options{
Comparer: getComparer(c),
ErrorIfMissing: true,
}

opts := &opt.Options{Comparer: cmp, ErrorIfMissing: true}
db, err := leveldb.OpenFile(c.String("dbpath"), opts)
if err != nil {
return err
Expand Down Expand Up @@ -152,12 +153,10 @@ func deleteCmd(c *cli.Context) (err error) {
return err
}

var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
opts := &opt.Options{
Comparer: getComparer(c),
ErrorIfMissing: true,
}

opts := &opt.Options{Comparer: cmp, ErrorIfMissing: true}
db, err := leveldb.OpenFile(c.String("dbpath"), opts)
if err != nil {
return err
Expand Down Expand Up @@ -239,11 +238,8 @@ func getKeyRange(c *cli.Context) (*util.Range, error) {
}

if slice.Start != nil && slice.Limit != nil {
var cmp func([]byte, []byte) int = bytes.Compare
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer.Compare
}
if cmp(slice.Start, slice.Limit) > 0 {
cmp := getComparer(c)
if cmp.Compare(slice.Start, slice.Limit) > 0 {
slice.Limit = slice.Start
}
}
Expand All @@ -262,17 +258,16 @@ func keysCmd(c *cli.Context) error {
w = newPrettyPrinter(color.Output)
}

var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
}

slice, err := getKeyRange(c)
if err != nil {
return err
}

opts := &opt.Options{Comparer: cmp, ErrorIfMissing: true, ReadOnly: true}
opts := &opt.Options{
Comparer: getComparer(c),
ErrorIfMissing: true,
ReadOnly: true,
}
db, err := leveldb.OpenFile(c.String("dbpath"), opts)
if err != nil {
return err
Expand Down Expand Up @@ -321,17 +316,16 @@ func showCmd(c *cli.Context) error {
SetParseJSON(!c.Bool("no-json"))
}

var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
}

slice, err := getKeyRange(c)
if err != nil {
return err
}

opts := &opt.Options{Comparer: cmp, ErrorIfMissing: true, ReadOnly: true}
opts := &opt.Options{
Comparer: getComparer(c),
ErrorIfMissing: true,
ReadOnly: true,
}
db, err := leveldb.OpenFile(c.String("dbpath"), opts)
if err != nil {
return err
Expand Down Expand Up @@ -374,7 +368,11 @@ func showCmd(c *cli.Context) error {
}

func dumpDB(dbpath string, cmp comparer.Comparer, w io.Writer) error {
opts := &opt.Options{Comparer: cmp, ErrorIfMissing: true, ReadOnly: true}
opts := &opt.Options{
Comparer: cmp,
ErrorIfMissing: true,
ReadOnly: true,
}
db, err := leveldb.OpenFile(dbpath, opts)
if err != nil {
return err
Expand Down Expand Up @@ -446,7 +444,9 @@ func loadDB(dbpath string, cmp comparer.Comparer, r io.Reader) error {
entries[i].Value = value
}

opts := &opt.Options{Comparer: cmp}
opts := &opt.Options{
Comparer: cmp,
}
db, err := leveldb.OpenFile(dbpath, opts)
if err != nil {
return err
Expand Down Expand Up @@ -502,28 +502,17 @@ func destroyDB(dbpath string, dryRun bool) error {
}

func dumpCmd(c *cli.Context) error {
var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
}
return dumpDB(c.String("dbpath"), cmp, os.Stdout)
return dumpDB(c.String("dbpath"), getComparer(c), os.Stdout)
}

func loadCmd(c *cli.Context) error {
var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
}
return loadDB(c.String("dbpath"), cmp, os.Stdin)
return loadDB(c.String("dbpath"), getComparer(c), os.Stdin)
}

func compactCmd(c *cli.Context) error {
dbpath := c.String("dbpath")
cmp := getComparer(c)
bakfile := path.Join(dbpath, "leveldb.bak")
var cmp comparer.Comparer = comparer.DefaultComparer
if c.Bool("indexeddb") {
cmp = indexeddb.IndexedDBComparer
}

bak, err := os.OpenFile(bakfile, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions indexeddb/comparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,9 @@ func compareKeyPrefix(a, b *keyPrefix) int {
return 0
}

type indexedDBComparer struct{}
type idbCmp1 struct{}

func (indexedDBComparer) Compare(a, b []byte) int {
func (idbCmp1) Compare(a, b []byte) int {
defer func(a, b []byte) {
if err := recover(); err != nil {
fmt.Fprintln(os.Stderr, "leveldb: warning: idb_cmp1: invalid IndexedDB key found")
Expand Down Expand Up @@ -545,17 +545,17 @@ func (indexedDBComparer) Compare(a, b []byte) int {
}
}

func (indexedDBComparer) Name() string {
func (idbCmp1) Name() string {
return "idb_cmp1"
}

func (indexedDBComparer) Separator(dst, a, b []byte) []byte {
func (idbCmp1) Separator(dst, a, b []byte) []byte {
return nil
}

func (indexedDBComparer) Successor(dst, b []byte) []byte {
func (idbCmp1) Successor(dst, b []byte) []byte {
return nil
}

// IndexedDBComparer implements the idb_cmp1 comparer used in Chromium IndexedDB implementation.
var IndexedDBComparer comparer.Comparer = indexedDBComparer{}
// Comparer implements the idb_cmp1 comparer used in Chromium's IndexedDB implementation.
var Comparer comparer.Comparer = idbCmp1{}
8 changes: 4 additions & 4 deletions indexeddb/prefix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestPrefix(t *testing.T) {
if !bytes.Equal(slice.Limit, limit) {
t.Errorf(`Limit("%s") expects "%s" but got "%x"`, tc.Prefix, tc.Limit, slice.Limit)
}
if slice.Limit != nil && IndexedDBComparer.Compare(slice.Start, slice.Limit) > 0 {
if slice.Limit != nil && Comparer.Compare(slice.Start, slice.Limit) > 0 {
t.Errorf(`Start("%s") is greater than Limit("%s")`, tc.Prefix, tc.Prefix)
t.Logf(`Start("%s") == "%x"`, tc.Prefix, slice.Start)
t.Logf(`Limit("%s") == "%x"`, tc.Prefix, slice.Limit)
Expand Down Expand Up @@ -182,15 +182,15 @@ func TestPrefix(t *testing.T) {
prefix := key[:i]
slice := Prefix(prefix)

if slice.Start != nil && IndexedDBComparer.Compare(slice.Start, key) > 0 {
if slice.Start != nil && Comparer.Compare(slice.Start, key) > 0 {
t.Errorf(`Start("%x") is greater than "%s"`, prefix, keyString)
t.Logf(`Start("%x") == "%x"`, prefix, slice.Start)
}
if slice.Limit != nil && IndexedDBComparer.Compare(slice.Limit, key) <= 0 {
if slice.Limit != nil && Comparer.Compare(slice.Limit, key) <= 0 {
t.Errorf(`Limit("%x") is less than or equals to "%s"`, prefix, keyString)
t.Logf(`Limit("%x") == "%x"`, prefix, slice.Limit)
}
if slice.Limit != nil && IndexedDBComparer.Compare(slice.Start, slice.Limit) > 0 {
if slice.Limit != nil && Comparer.Compare(slice.Start, slice.Limit) > 0 {
t.Errorf(`Start("%x") is greater than Limit("%x")`, prefix, prefix)
t.Logf(`Start("%x") == "%x"`, prefix, slice.Start)
t.Logf(`Limit("%x") == "%x"`, prefix, slice.Limit)
Expand Down

0 comments on commit 614dce6

Please sign in to comment.