1
0
mirror of https://github.com/taigrr/bitcask synced 2025-01-18 04:03:17 -08:00

Unexport some internal implemtnation details

This commit is contained in:
James Mills 2019-03-18 17:22:55 +10:00
parent 2a35976cdd
commit 1298240f53
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
14 changed files with 46 additions and 39 deletions

View File

@ -13,11 +13,11 @@ dev: build
build: clean generate build: clean generate
@go build \ @go build \
-tags "netgo static_build" -installsuffix netgo \ -tags "netgo static_build" -installsuffix netgo \
-ldflags "-w -X $(shell go list).Version=$(VERSION) -X $(shell go list).Commit=$(COMMIT)" \ -ldflags "-w -X $(shell go list)/internal.Version=$(VERSION) -X $(shell go list)/internal.Commit=$(COMMIT)" \
./cmd/bitcask/... ./cmd/bitcask/...
@go build \ @go build \
-tags "netgo static_build" -installsuffix netgo \ -tags "netgo static_build" -installsuffix netgo \
-ldflags "-w -X $(shell go list).Version=$(VERSION) -X $(shell go list).Commit=$(COMMIT)" \ -ldflags "-w -X $(shell go list)/internal.Version=$(VERSION) -X $(shell go list)/internal.Commit=$(COMMIT)" \
./cmd/bitcaskd/... ./cmd/bitcaskd/...
generate: generate:

View File

@ -11,6 +11,8 @@ import (
"github.com/gofrs/flock" "github.com/gofrs/flock"
"github.com/prologic/trie" "github.com/prologic/trie"
"github.com/prologic/bitcask/internal"
) )
type Bitcask struct { type Bitcask struct {
@ -18,9 +20,9 @@ type Bitcask struct {
config *config config *config
path string path string
curr *Datafile curr *internal.Datafile
keydir *Keydir keydir *internal.Keydir
datafiles []*Datafile datafiles []*internal.Datafile
trie *trie.Trie trie *trie.Trie
maxDatafileSize int64 maxDatafileSize int64
@ -43,14 +45,14 @@ func (b *Bitcask) Sync() error {
} }
func (b *Bitcask) Get(key string) ([]byte, error) { func (b *Bitcask) Get(key string) ([]byte, error) {
var df *Datafile var df *internal.Datafile
item, ok := b.keydir.Get(key) item, ok := b.keydir.Get(key)
if !ok { if !ok {
return nil, fmt.Errorf("error: key not found %s", key) return nil, fmt.Errorf("error: key not found %s", key)
} }
if item.FileID == b.curr.id { if item.FileID == b.curr.FileID() {
df = b.curr df = b.curr
} else { } else {
df = b.datafiles[item.FileID] df = b.datafiles[item.FileID]
@ -82,7 +84,7 @@ func (b *Bitcask) Put(key string, value []byte) error {
return err return err
} }
item := b.keydir.Add(key, b.curr.id, offset) item := b.keydir.Add(key, b.curr.FileID(), offset)
b.trie.Add(key, item) b.trie.Add(key, item)
return nil return nil
@ -131,22 +133,22 @@ func (b *Bitcask) put(key string, value []byte) (int64, error) {
return -1, err return -1, err
} }
df, err := NewDatafile(b.path, b.curr.id, true) df, err := internal.NewDatafile(b.path, b.curr.FileID(), true)
if err != nil { if err != nil {
return -1, err return -1, err
} }
b.datafiles = append(b.datafiles, df) b.datafiles = append(b.datafiles, df)
id := b.curr.id + 1 id := b.curr.FileID() + 1
curr, err := NewDatafile(b.path, id, false) curr, err := internal.NewDatafile(b.path, id, false)
if err != nil { if err != nil {
return -1, err return -1, err
} }
b.curr = curr b.curr = curr
} }
e := NewEntry(key, value) e := internal.NewEntry(key, value)
return b.curr.Write(e) return b.curr.Write(e)
} }
@ -156,12 +158,12 @@ func (b *Bitcask) setMaxDatafileSize(size int64) error {
} }
func Merge(path string, force bool) error { func Merge(path string, force bool) error {
fns, err := getDatafiles(path) fns, err := internal.GetDatafiles(path)
if err != nil { if err != nil {
return err return err
} }
ids, err := parseIds(fns) ids, err := internal.ParseIds(fns)
if err != nil { if err != nil {
return err return err
} }
@ -191,9 +193,9 @@ func Merge(path string, force bool) error {
id := ids[i] id := ids[i]
keydir := NewKeydir() keydir := internal.NewKeydir()
df, err := NewDatafile(path, id, true) df, err := internal.NewDatafile(path, id, true)
if err != nil { if err != nil {
return err return err
} }
@ -217,7 +219,7 @@ func Merge(path string, force bool) error {
keydir.Add(e.Key, ids[i], e.Offset) keydir.Add(e.Key, ids[i], e.Offset)
} }
tempdf, err := NewDatafile(temp, id, false) tempdf, err := internal.NewDatafile(temp, id, false)
if err != nil { if err != nil {
return err return err
} }
@ -271,23 +273,23 @@ func Open(path string, options ...option) (*Bitcask, error) {
return nil, err return nil, err
} }
fns, err := getDatafiles(path) fns, err := internal.GetDatafiles(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
ids, err := parseIds(fns) ids, err := internal.ParseIds(fns)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var datafiles []*Datafile var datafiles []*internal.Datafile
keydir := NewKeydir() keydir := internal.NewKeydir()
trie := trie.New() trie := trie.New()
for i, fn := range fns { for i, fn := range fns {
df, err := NewDatafile(path, ids[i], true) df, err := internal.NewDatafile(path, ids[i], true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -300,7 +302,7 @@ func Open(path string, options ...option) (*Bitcask, error) {
} }
defer f.Close() defer f.Close()
hint, err := NewKeydirFromBytes(f) hint, err := internal.NewKeydirFromBytes(f)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -337,7 +339,7 @@ func Open(path string, options ...option) (*Bitcask, error) {
id = ids[(len(ids) - 1)] id = ids[(len(ids) - 1)]
} }
curr, err := NewDatafile(path, id, false) curr, err := internal.NewDatafile(path, id, false)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -8,13 +8,13 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"github.com/prologic/bitcask" "github.com/prologic/bitcask/internal"
) )
// RootCmd represents the base command when called without any subcommands // RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{ var RootCmd = &cobra.Command{
Use: "bitcask", Use: "bitcask",
Version: bitcask.FullVersion(), Version: internal.FullVersion(),
Short: "Command-line tools for bitcask", Short: "Command-line tools for bitcask",
Long: `This is the command-line tool to interact with a bitcask database. Long: `This is the command-line tool to interact with a bitcask database.

View File

@ -10,6 +10,7 @@ import (
"github.com/tidwall/redcon" "github.com/tidwall/redcon"
"github.com/prologic/bitcask" "github.com/prologic/bitcask"
"github.com/prologic/bitcask/internal"
) )
var ( var (
@ -43,7 +44,7 @@ func main() {
} }
if version { if version {
fmt.Printf("bitcaskd version %s", bitcask.FullVersion()) fmt.Printf("bitcaskd version %s", internal.FullVersion())
os.Exit(0) os.Exit(0)
} }
@ -60,7 +61,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
log.WithField("bind", bind).WithField("path", path).Infof("starting bitcaskd v%s", bitcask.FullVersion()) log.WithField("bind", bind).WithField("path", path).Infof("starting bitcaskd v%s", internal.FullVersion())
err = redcon.ListenAndServe(bind, err = redcon.ListenAndServe(bind,
func(conn redcon.Conn, cmd redcon.Command) { func(conn redcon.Conn, cmd redcon.Command) {

View File

@ -1,4 +1,4 @@
package bitcask package internal
import ( import (
"fmt" "fmt"
@ -8,8 +8,8 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
pb "github.com/prologic/bitcask/proto" pb "github.com/prologic/bitcask/internal/proto"
"github.com/prologic/bitcask/streampb" "github.com/prologic/bitcask/internal/streampb"
) )
const ( const (
@ -71,6 +71,10 @@ func NewDatafile(path string, id int, readonly bool) (*Datafile, error) {
}, nil }, nil
} }
func (df *Datafile) FileID() int {
return df.id
}
func (df *Datafile) Name() string { func (df *Datafile) Name() string {
return df.r.Name() return df.r.Name()
} }

View File

@ -1,9 +1,9 @@
package bitcask package internal
import ( import (
"hash/crc32" "hash/crc32"
pb "github.com/prologic/bitcask/proto" pb "github.com/prologic/bitcask/internal/proto"
) )
func NewEntry(key string, value []byte) pb.Entry { func NewEntry(key string, value []byte) pb.Entry {

View File

@ -1,4 +1,4 @@
package bitcask package internal
import ( import (
"bytes" "bytes"

View File

@ -1,4 +1,4 @@
package bitcask package internal
import ( import (
"fmt" "fmt"
@ -8,7 +8,7 @@ import (
"strings" "strings"
) )
func getDatafiles(path string) ([]string, error) { func GetDatafiles(path string) ([]string, error) {
fns, err := filepath.Glob(fmt.Sprintf("%s/*.data", path)) fns, err := filepath.Glob(fmt.Sprintf("%s/*.data", path))
if err != nil { if err != nil {
return nil, err return nil, err
@ -17,7 +17,7 @@ func getDatafiles(path string) ([]string, error) {
return fns, nil return fns, nil
} }
func parseIds(fns []string) ([]int, error) { func ParseIds(fns []string) ([]int, error) {
var ids []int var ids []int
for _, fn := range fns { for _, fn := range fns {
fn = filepath.Base(fn) fn = filepath.Base(fn)

View File

@ -1,4 +1,4 @@
package bitcask package internal
import ( import (
"fmt" "fmt"

View File

@ -1,4 +1,4 @@
package bitcask package internal
import ( import (
"fmt" "fmt"