Skip to content

Commit

Permalink
fix vet and lint (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
siddontang committed Mar 29, 2018
1 parent 36de957 commit 0c604eb
Show file tree
Hide file tree
Showing 24 changed files with 586 additions and 415 deletions.
2 changes: 1 addition & 1 deletion cmd/ledis-cli/main.go
Expand Up @@ -199,7 +199,7 @@ func loadHisotry() {

func saveHisotry() {
if f, err := os.Create(historyPath); err != nil {
fmt.Printf("Error writing history file: ", err)
fmt.Printf("Error writing history file, err: %v", err)
} else {
line.WriteHistory(f)
f.Close()
Expand Down
52 changes: 29 additions & 23 deletions ledis/const.go
Expand Up @@ -4,8 +4,10 @@ import (
"errors"
)

// Version is for version
const Version = "0.5"

// DataType is defined for the different types
type DataType byte

// for out use
Expand Down Expand Up @@ -34,6 +36,7 @@ func (d DataType) String() string {
}
}

// For different type name
const (
KVName = "KV"
ListName = "LIST"
Expand Down Expand Up @@ -71,24 +74,23 @@ const (
MetaType byte = 201
)

var (
TypeName = map[byte]string{
KVType: "kv",
HashType: "hash",
HSizeType: "hsize",
ListType: "list",
LMetaType: "lmeta",
ZSetType: "zset",
ZSizeType: "zsize",
ZScoreType: "zscore",
// BitType: "bit",
// BitMetaType: "bitmeta",
SetType: "set",
SSizeType: "ssize",
ExpTimeType: "exptime",
ExpMetaType: "expmeta",
}
)
// TypeName is the map of type -> name
var TypeName = map[byte]string{
KVType: "kv",
HashType: "hash",
HSizeType: "hsize",
ListType: "list",
LMetaType: "lmeta",
ZSetType: "zset",
ZSizeType: "zsize",
ZScoreType: "zscore",
// BitType: "bit",
// BitMetaType: "bitmeta",
SetType: "set",
SSizeType: "ssize",
ExpTimeType: "exptime",
ExpMetaType: "expmeta",
}

const (
defaultScanCount int = 10
Expand All @@ -104,25 +106,28 @@ var (
errListIndex = errors.New("invalid list index")
)

// For different const size configuration
const (
// max allowed databases
MaxDatabases int = 10240

//max key size
// max key size
MaxKeySize int = 1024

//max hash field size
// max hash field size
MaxHashFieldSize int = 1024

//max zset member size
// max zset member size
MaxZSetMemberSize int = 1024

//max set member size
// max set member size
MaxSetMemberSize int = 1024

//max value size
// max value size
MaxValueSize int = 1024 * 1024 * 1024
)

// For different common errors
var (
ErrScoreMiss = errors.New("zset score miss")
ErrWriteInROnly = errors.New("write not support in readonly mode")
Expand All @@ -136,6 +141,7 @@ var (
// DBInMulti uint8 = 0x2
// )

// For bit operation
const (
BitAND = "and"
BitOR = "or"
Expand Down
21 changes: 9 additions & 12 deletions ledis/dump.go
Expand Up @@ -11,26 +11,22 @@ import (
"github.com/siddontang/ledisdb/store"
)

// DumpHead is the head of a dump.
type DumpHead struct {
CommitID uint64
}

// Read reads meta from the Reader.
func (h *DumpHead) Read(r io.Reader) error {
if err := binary.Read(r, binary.BigEndian, &h.CommitID); err != nil {
return err
}

return nil
return binary.Read(r, binary.BigEndian, &h.CommitID)
}

// Write writes meta to the Writer
func (h *DumpHead) Write(w io.Writer) error {
if err := binary.Write(w, binary.BigEndian, h.CommitID); err != nil {
return err
}

return nil
return binary.Write(w, binary.BigEndian, h.CommitID)
}

// DumpFile dumps data to the file
func (l *Ledis) DumpFile(path string) error {
f, err := os.Create(path)
if err != nil {
Expand All @@ -41,6 +37,7 @@ func (l *Ledis) DumpFile(path string) error {
return l.Dump(f)
}

// Dump dumps data to the Writer.
func (l *Ledis) Dump(w io.Writer) error {
var err error

Expand Down Expand Up @@ -118,7 +115,7 @@ func (l *Ledis) Dump(w io.Writer) error {
return nil
}

// clear all data and load dump file to db
// LoadDumpFile clears all data and loads dump file to db
func (l *Ledis) LoadDumpFile(path string) (*DumpHead, error) {
f, err := os.Open(path)
if err != nil {
Expand All @@ -129,7 +126,7 @@ func (l *Ledis) LoadDumpFile(path string) (*DumpHead, error) {
return l.LoadDump(f)
}

// clear all data and load dump file to db
// LoadDump clears all data and loads dump file to db
func (l *Ledis) LoadDump(r io.Reader) (*DumpHead, error) {
l.wLock.Lock()
defer l.wLock.Unlock()
Expand Down
118 changes: 64 additions & 54 deletions ledis/event.go
Expand Up @@ -29,95 +29,105 @@ func formatEventKey(buf []byte, k []byte) ([]byte, error) {

switch k[1] {
case KVType:
if key, err := db.decodeKVKey(k); err != nil {
key, err := db.decodeKVKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
}
buf = strconv.AppendQuote(buf, hack.String(key))
case HashType:
if key, field, err := db.hDecodeHashKey(k); err != nil {
key, field, err := db.hDecodeHashKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(field))
}

buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(field))
case HSizeType:
if key, err := db.hDecodeSizeKey(k); err != nil {
key, err := db.hDecodeSizeKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
}

buf = strconv.AppendQuote(buf, hack.String(key))
case ListType:
if key, seq, err := db.lDecodeListKey(k); err != nil {
key, seq, err := db.lDecodeListKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, int64(seq), 10)
}

buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, int64(seq), 10)
case LMetaType:
if key, err := db.lDecodeMetaKey(k); err != nil {
key, err := db.lDecodeMetaKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
}

buf = strconv.AppendQuote(buf, hack.String(key))
case ZSetType:
if key, m, err := db.zDecodeSetKey(k); err != nil {
key, m, err := db.zDecodeSetKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(m))
}

buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(m))
case ZSizeType:
if key, err := db.zDecodeSizeKey(k); err != nil {
key, err := db.zDecodeSizeKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
}

buf = strconv.AppendQuote(buf, hack.String(key))
case ZScoreType:
if key, m, score, err := db.zDecodeScoreKey(k); err != nil {
key, m, score, err := db.zDecodeScoreKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(m))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, score, 10)
}
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(m))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, score, 10)
case SetType:
if key, member, err := db.sDecodeSetKey(k); err != nil {
key, member, err := db.sDecodeSetKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(member))
}

buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(member))
case SSizeType:
if key, err := db.sDecodeSizeKey(k); err != nil {
key, err := db.sDecodeSizeKey(k)
if err != nil {
return nil, err
} else {
buf = strconv.AppendQuote(buf, hack.String(key))
}

buf = strconv.AppendQuote(buf, hack.String(key))
case ExpTimeType:
if tp, key, t, err := db.expDecodeTimeKey(k); err != nil {
tp, key, t, err := db.expDecodeTimeKey(k)
if err != nil {
return nil, err
} else {
buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, t, 10)
}

buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(key))
buf = append(buf, ' ')
buf = strconv.AppendInt(buf, t, 10)
case ExpMetaType:
if tp, key, err := db.expDecodeMetaKey(k); err != nil {
tp, key, err := db.expDecodeMetaKey(k)
if err != nil {
return nil, err
} else {
buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(key))
}

buf = append(buf, TypeName[tp]...)
buf = append(buf, ' ')
buf = strconv.AppendQuote(buf, hack.String(key))
default:
return nil, errInvalidEvent
}
Expand Down
9 changes: 8 additions & 1 deletion ledis/ledis.go
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/siddontang/ledisdb/store"
)

// Ledis is the core structure to handle the database.
type Ledis struct {
cfg *config.Config

Expand Down Expand Up @@ -42,6 +43,7 @@ type Ledis struct {
ttlCheckerCh chan *ttlChecker
}

// Open opens the Ledis with a config.
func Open(cfg *config.Config) (*Ledis, error) {
if len(cfg.DataDir) == 0 {
cfg.DataDir = config.DefaultDataDir
Expand Down Expand Up @@ -96,6 +98,7 @@ func Open(cfg *config.Config) (*Ledis, error) {
return l, nil
}

// Close closes the Ledis.
func (l *Ledis) Close() {
close(l.quit)
l.wg.Wait()
Expand All @@ -113,6 +116,7 @@ func (l *Ledis) Close() {
}
}

// Select chooses a database.
func (l *Ledis) Select(index int) (*DB, error) {
if index < 0 || index >= l.cfg.Databases {
return nil, fmt.Errorf("invalid db index %d, must in [0, %d]", index, l.cfg.Databases-1)
Expand All @@ -136,7 +140,7 @@ func (l *Ledis) Select(index int) (*DB, error) {
return db, nil
}

// Flush All will clear all data and replication logs
// FlushAll will clear all data and replication logs
func (l *Ledis) FlushAll() error {
l.wLock.Lock()
defer l.wLock.Unlock()
Expand Down Expand Up @@ -181,6 +185,7 @@ func (l *Ledis) flushAll() error {
return nil
}

// IsReadOnly returns whether Ledis is read only or not.
func (l *Ledis) IsReadOnly() bool {
if l.cfg.GetReadonly() {
return true
Expand Down Expand Up @@ -229,10 +234,12 @@ func (l *Ledis) checkTTL() {

}

// StoreStat returns the statistics.
func (l *Ledis) StoreStat() *store.Stat {
return l.ldb.Stat()
}

// CompactStore compacts the backend storage.
func (l *Ledis) CompactStore() error {
l.wLock.Lock()
defer l.wLock.Unlock()
Expand Down

0 comments on commit 0c604eb

Please sign in to comment.