mirror of
https://github.com/gogrlx/bitcask.git
synced 2026-04-04 03:52:45 -07:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
238ff6ab59 | ||
|
|
6a39d742b7 | ||
|
|
f4b7918e93 | ||
|
|
f88919ecd0 | ||
|
|
108cb54cb2 | ||
|
|
904f6b19a0 | ||
|
|
4b52dea172 | ||
|
|
e9997642fe |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,5 +3,6 @@
|
||||
|
||||
/coverage.txt
|
||||
/bitcask
|
||||
/bitcaskd
|
||||
/tmp
|
||||
/dist
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
builds:
|
||||
-
|
||||
binary: bitcask
|
||||
main: ./cmd/bitcask
|
||||
flags: -tags "static_build"
|
||||
ldflags: -w -X .Version={{.Version}} -X .Commit={{.Commit}}
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
-
|
||||
binary: bitcaskd
|
||||
main: ./cmd/bitcaskd
|
||||
flags: -tags "static_build"
|
||||
ldflags: -w -X .Version={{.Version}} -X .Commit={{.Commit}}
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
sign:
|
||||
artifacts: checksum
|
||||
archive:
|
||||
|
||||
76
CODE_OF_CONDUCT.md
Normal file
76
CODE_OF_CONDUCT.md
Normal file
@@ -0,0 +1,76 @@
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, sex characteristics, gender identity and expression,
|
||||
level of experience, education, socio-economic status, nationality, personal
|
||||
appearance, race, religion, or sexual identity and orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at . All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
|
||||
|
||||
[homepage]: https://www.contributor-covenant.org
|
||||
|
||||
For answers to common questions about this code of conduct, see
|
||||
https://www.contributor-covenant.org/faq
|
||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2019 James Mills
|
||||
|
||||
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.
|
||||
5
Makefile
5
Makefile
@@ -7,12 +7,17 @@ all: dev
|
||||
|
||||
dev: build
|
||||
@./bitcask --version
|
||||
@./bitcaskd --version
|
||||
|
||||
build: clean generate
|
||||
@go build \
|
||||
-tags "netgo static_build" -installsuffix netgo \
|
||||
-ldflags "-w -X $(shell go list)/.Commit=$(COMMIT)" \
|
||||
./cmd/bitcask/...
|
||||
@go build \
|
||||
-tags "netgo static_build" -installsuffix netgo \
|
||||
-ldflags "-w -X $(shell go list)/.Commit=$(COMMIT)" \
|
||||
./cmd/bitcaskd/...
|
||||
|
||||
generate:
|
||||
@go generate $(shell go list)/...
|
||||
|
||||
@@ -12,6 +12,9 @@ A Bitcask (LSM+WAL) Key/Value Store written in Go.
|
||||
|
||||
* Embeddable
|
||||
* Builtin CLI
|
||||
* Predictable read/write performance
|
||||
* Low latecny
|
||||
* High throughput (See: [Performance](README.md#Performance)
|
||||
|
||||
## Install
|
||||
|
||||
@@ -61,11 +64,11 @@ Benchmarks run on a 11" Macbook with a 1.4Ghz Intel Core i7:
|
||||
```
|
||||
$ make bench
|
||||
...
|
||||
BenchmarkGet-4 50000 33185 ns/op 600 B/op 14 allocs/op
|
||||
BenchmarkPut-4 100000 16757 ns/op 699 B/op 7 allocs/op
|
||||
BenchmarkGet-4 300000 5065 ns/op 144 B/op 4 allocs/op
|
||||
BenchmarkPut-4 100000 14640 ns/op 699 B/op 7 allocs/op
|
||||
```
|
||||
|
||||
* ~30,000 reads/sec
|
||||
* ~180,000 reads/sec
|
||||
* ~60,000 writes/sec
|
||||
|
||||
## License
|
||||
|
||||
70
bitcask.go
70
bitcask.go
@@ -11,6 +11,8 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gofrs/flock"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -18,18 +20,29 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrKeyNotFound = errors.New("error: key not found")
|
||||
ErrKeyNotFound = errors.New("error: key not found")
|
||||
ErrCannotAcquireLock = errors.New("error: cannot acquire lock")
|
||||
)
|
||||
|
||||
type Bitcask struct {
|
||||
path string
|
||||
curr *Datafile
|
||||
keydir *Keydir
|
||||
*flock.Flock
|
||||
|
||||
path string
|
||||
curr *Datafile
|
||||
keydir *Keydir
|
||||
datafiles []*Datafile
|
||||
|
||||
maxDatafileSize int64
|
||||
}
|
||||
|
||||
func (b *Bitcask) Close() error {
|
||||
defer func() {
|
||||
b.Flock.Unlock()
|
||||
}()
|
||||
|
||||
for _, df := range b.datafiles {
|
||||
df.Close()
|
||||
}
|
||||
return b.curr.Close()
|
||||
}
|
||||
|
||||
@@ -38,16 +51,18 @@ func (b *Bitcask) Sync() error {
|
||||
}
|
||||
|
||||
func (b *Bitcask) Get(key string) ([]byte, error) {
|
||||
var df *Datafile
|
||||
|
||||
item, ok := b.keydir.Get(key)
|
||||
if !ok {
|
||||
return nil, ErrKeyNotFound
|
||||
}
|
||||
|
||||
df, err := NewDatafile(b.path, item.FileID, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if item.FileID == b.curr.id {
|
||||
df = b.curr
|
||||
} else {
|
||||
df = b.datafiles[item.FileID]
|
||||
}
|
||||
defer df.Close()
|
||||
|
||||
e, err := df.ReadAt(item.Index)
|
||||
if err != nil {
|
||||
@@ -100,6 +115,9 @@ func (b *Bitcask) put(key string, value []byte) (int64, error) {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
df, err := NewDatafile(b.path, b.curr.id, true)
|
||||
b.datafiles = append(b.datafiles, df)
|
||||
|
||||
id := b.curr.id + 1
|
||||
curr, err := NewDatafile(b.path, id, false)
|
||||
if err != nil {
|
||||
@@ -117,7 +135,7 @@ func (b *Bitcask) setMaxDatafileSize(size int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func MaxDatafileSize(size int64) func(*Bitcask) error {
|
||||
func WithMaxDatafileSize(size int64) func(*Bitcask) error {
|
||||
return func(b *Bitcask) error {
|
||||
return b.setMaxDatafileSize(size)
|
||||
}
|
||||
@@ -266,8 +284,6 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keydir := NewKeydir()
|
||||
|
||||
fns, err := getDatafiles(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -278,7 +294,16 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
keydir := NewKeydir()
|
||||
var datafiles []*Datafile
|
||||
|
||||
for i, fn := range fns {
|
||||
df, err := NewDatafile(path, ids[i], true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
datafiles = append(datafiles, df)
|
||||
|
||||
if filepath.Ext(fn) == ".hint" {
|
||||
f, err := os.Open(filepath.Join(path, fn))
|
||||
if err != nil {
|
||||
@@ -296,11 +321,6 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
|
||||
keydir.Add(key, item.FileID, item.Index, item.Timestamp)
|
||||
}
|
||||
} else {
|
||||
df, err := NewDatafile(path, ids[i], true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for {
|
||||
e, err := df.Read()
|
||||
if err != nil {
|
||||
@@ -325,15 +345,18 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
|
||||
if len(ids) > 0 {
|
||||
id = ids[(len(ids) - 1)]
|
||||
}
|
||||
|
||||
curr, err := NewDatafile(path, id, false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bitcask := &Bitcask{
|
||||
path: path,
|
||||
curr: curr,
|
||||
keydir: keydir,
|
||||
Flock: flock.New(filepath.Join(path, "lock")),
|
||||
path: path,
|
||||
curr: curr,
|
||||
keydir: keydir,
|
||||
datafiles: datafiles,
|
||||
|
||||
maxDatafileSize: DefaultMaxDatafileSize,
|
||||
}
|
||||
@@ -345,5 +368,14 @@ func Open(path string, options ...func(*Bitcask) error) (*Bitcask, error) {
|
||||
}
|
||||
}
|
||||
|
||||
locked, err := bitcask.Flock.TryLock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !locked {
|
||||
return nil, ErrCannotAcquireLock
|
||||
}
|
||||
|
||||
return bitcask, nil
|
||||
}
|
||||
|
||||
@@ -198,6 +198,21 @@ func TestMerge(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestLocking(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
testdir, err := ioutil.TempDir("", "bitcask")
|
||||
assert.NoError(err)
|
||||
|
||||
db, err := Open(testdir)
|
||||
assert.NoError(err)
|
||||
defer db.Close()
|
||||
|
||||
_, err = Open(testdir)
|
||||
assert.Error(err)
|
||||
assert.Equal("error: cannot acquire lock", err.Error())
|
||||
}
|
||||
|
||||
func BenchmarkGet(b *testing.B) {
|
||||
testdir, err := ioutil.TempDir("", "bitcask")
|
||||
if err != nil {
|
||||
|
||||
123
cmd/bitcaskd/main.go
Normal file
123
cmd/bitcaskd/main.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
flag "github.com/spf13/pflag"
|
||||
"github.com/tidwall/redcon"
|
||||
|
||||
"github.com/prologic/bitcask"
|
||||
)
|
||||
|
||||
var (
|
||||
bind string
|
||||
debug bool
|
||||
version bool
|
||||
maxDatafileSize int64
|
||||
)
|
||||
|
||||
func init() {
|
||||
flag.Usage = func() {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s [options] <path>\n", os.Args[0])
|
||||
flag.PrintDefaults()
|
||||
}
|
||||
|
||||
flag.BoolVarP(&version, "version", "v", false, "display version information")
|
||||
flag.BoolVarP(&debug, "debug", "d", false, "enable debug logging")
|
||||
|
||||
flag.StringVarP(&bind, "bind", "b", ":6379", "interface and port to bind to")
|
||||
|
||||
flag.Int64Var(&maxDatafileSize, "max-datafile-size", 1<<20, "maximum datafile size in bytes")
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
if debug {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
} else {
|
||||
log.SetLevel(log.InfoLevel)
|
||||
}
|
||||
|
||||
if version {
|
||||
fmt.Printf("bitcaskd version %s", bitcask.FullVersion())
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
if len(flag.Args()) < 1 {
|
||||
flag.PrintDefaults()
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
path := flag.Arg(0)
|
||||
|
||||
db, err := bitcask.Open(path, bitcask.WithMaxDatafileSize(maxDatafileSize))
|
||||
if err != nil {
|
||||
log.WithError(err).WithField("path", path).Error("error opening database")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
log.WithField("bind", bind).WithField("path", path).Infof("starting bitcaskd v%s", bitcask.FullVersion())
|
||||
|
||||
err = redcon.ListenAndServe(bind,
|
||||
func(conn redcon.Conn, cmd redcon.Command) {
|
||||
switch strings.ToLower(string(cmd.Args[0])) {
|
||||
case "ping":
|
||||
conn.WriteString("PONG")
|
||||
case "quit":
|
||||
conn.WriteString("OK")
|
||||
conn.Close()
|
||||
case "set":
|
||||
if len(cmd.Args) != 3 {
|
||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||
return
|
||||
}
|
||||
key := string(cmd.Args[0])
|
||||
value := cmd.Args[1]
|
||||
err = db.Put(key, value)
|
||||
if err != nil {
|
||||
conn.WriteString(fmt.Sprintf("ERR: %s", err))
|
||||
} else {
|
||||
conn.WriteString("OK")
|
||||
}
|
||||
case "get":
|
||||
if len(cmd.Args) != 2 {
|
||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||
return
|
||||
}
|
||||
key := string(cmd.Args[0])
|
||||
value, err := db.Get(key)
|
||||
if err != nil {
|
||||
conn.WriteNull()
|
||||
} else {
|
||||
conn.WriteBulk(value)
|
||||
}
|
||||
case "del":
|
||||
if len(cmd.Args) != 2 {
|
||||
conn.WriteError("ERR wrong number of arguments for '" + string(cmd.Args[0]) + "' command")
|
||||
return
|
||||
}
|
||||
key := string(cmd.Args[0])
|
||||
err := db.Delete(key)
|
||||
if err != nil {
|
||||
conn.WriteInt(0)
|
||||
} else {
|
||||
conn.WriteInt(1)
|
||||
}
|
||||
default:
|
||||
conn.WriteError("ERR unknown command '" + string(cmd.Args[0]) + "'")
|
||||
}
|
||||
},
|
||||
func(conn redcon.Conn) bool {
|
||||
return true
|
||||
},
|
||||
func(conn redcon.Conn, err error) {
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.WithError(err).Fatal("oops")
|
||||
}
|
||||
}
|
||||
3
go.mod
3
go.mod
@@ -1,6 +1,7 @@
|
||||
module github.com/prologic/bitcask
|
||||
|
||||
require (
|
||||
github.com/gofrs/flock v0.7.1
|
||||
github.com/gogo/protobuf v1.2.1
|
||||
github.com/golang/protobuf v1.2.0
|
||||
github.com/gorilla/websocket v1.4.0 // indirect
|
||||
@@ -11,7 +12,9 @@ require (
|
||||
github.com/prometheus/client_golang v0.9.2 // indirect
|
||||
github.com/sirupsen/logrus v1.3.0
|
||||
github.com/spf13/cobra v0.0.3
|
||||
github.com/spf13/pflag v1.0.3
|
||||
github.com/spf13/viper v1.3.1
|
||||
github.com/stretchr/testify v1.3.0
|
||||
github.com/tidwall/redcon v0.9.0
|
||||
gopkg.in/vmihailenco/msgpack.v2 v2.9.1
|
||||
)
|
||||
|
||||
4
go.sum
4
go.sum
@@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
|
||||
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
|
||||
github.com/gofrs/flock v0.7.1 h1:DP+LD/t0njgoPBvT5MJLeliUIVQR03hiKR6vezdwHlc=
|
||||
github.com/gofrs/flock v0.7.1/go.mod h1:F1TvTiK9OcQqauNUHlbJvyl9Qa1QvF/gOUDKA14jxHU=
|
||||
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
|
||||
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
|
||||
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
|
||||
@@ -65,6 +67,8 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/tidwall/redcon v0.9.0 h1:tiT9DLAoohsdNaFg9Si5dRsv9+FjvZYnhMOEtSFwBqA=
|
||||
github.com/tidwall/redcon v0.9.0/go.mod h1:bdYBm4rlcWpst2XMwKVzWDF9CoUxEbUmM7CQrKeOZas=
|
||||
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
|
||||
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
|
||||
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
|
||||
Reference in New Issue
Block a user