Skip to content

Commit

Permalink
Merge pull request #7 from waldoweng/r0.0.1
Browse files Browse the repository at this point in the history
R0.0.1 -> master
  • Loading branch information
waldoweng authored Oct 12, 2019
2 parents 7faccda + 7cd5d5c commit 2851b84
Show file tree
Hide file tree
Showing 13 changed files with 1,488 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# exclude data file
*.dat
.DS_Store
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
branches:
only:
- r0.0.1

language: go

go:
- 1.9

sudo: required

install:
- go get -d github.com/emicklei/go-restful
- go get -d github.com/emicklei/go-restful-openapi
- go get -d github.com/common-nighthawk/go-figure
- go get -d github.com/spf13/cobra
- go get -d github.com/spf13/viper
- go build github.com/waldoweng/beancask

script:
- go test -timeout=30m github.com/waldoweng/beancask/storage -v -coverprofile=coverage.txt -covermode=atomic

after_success:
- bash <(curl -s https://codecov.io/bash)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 weng zhao

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 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# beancask

[![Build Status](https://travis-ci.org/waldoweng/beancask.svg?branch=r0.0.1)](https://travis-ci.org/waldoweng/beancask)
[![codecov](https://codecov.io/gh/waldoweng/beancask/branch/r0.0.1/graph/badge.svg)](https://codecov.io/gh/waldoweng/beancask)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

bitcask storage engine for fun.
116 changes: 116 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package cmd

import (
"fmt"
"log"
"net/http"
"os"

"github.com/common-nighthawk/go-figure"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/waldoweng/beancask/storage"
"github.com/waldoweng/beancask/wsgi"
)

type configS struct {
Host string
Port int
MaxKeySize int
MaxValueSize int
CompactSize int
DataDir string
ActiveDataDir string
LogDir string
LogLevel string
}

var configFile string
var config configS

func printBanner() {
fmt.Println("")
figure.NewFigure("Beancask", "smslant", false).Print()
fmt.Println("")
}

func initConfig() {
log.SetFlags(log.LstdFlags)

viper.SetConfigName("beancask")
viper.AddConfigPath("$HOME/.beancask")

err := viper.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Panicf("Fatal error reading config file: %s", err.Error())
}
}

RootCmd.PersistentFlags().StringVar(&configFile, "config", "$HOME/.beancask/beancask.yaml", "config file")
RootCmd.PersistentFlags().StringVarP(&config.Host, "host", "H", "0.0.0.0", "listening host")
RootCmd.PersistentFlags().IntVarP(&config.Port, "port", "P", 3680, "listening port")

viper.BindPFlag("Host", RootCmd.PersistentFlags().Lookup("host"))
viper.BindPFlag("Port", RootCmd.PersistentFlags().Lookup("port"))

viper.SetDefault("MaxKeySize", 1024)
viper.SetDefault("MaxValueSize", 1024*1024)
viper.SetDefault("CompactSize", 512*1024*1024)
viper.SetDefault("DataDir", "/usr/local/var/beancask")
viper.SetDefault("ActiveDataDir", "/usr/local/var/beancask")
viper.SetDefault("LogDir", "/var/log/beancask")
viper.SetDefault("LogLevel", "debug")
}

func init() {
cobra.OnInitialize(printBanner)
}

// RootCmd the root command of cobra-based command line tool.
var RootCmd = &cobra.Command{
Use: "beancask-server",
Short: "beancask is a bitcask-like key-value store",
Long: "a simple and powerful key-value store in bitcask model",
Run: func(cmd *cobra.Command, args []string) {
logFile, err := os.OpenFile(
fmt.Sprintf("%s/beancask.log", viper.Get("LogDir").(string)),
os.O_WRONLY|os.O_APPEND|os.O_SYNC|os.O_CREATE,
0666)
if err != nil {
panic(err)
}

option := storage.CreateOption{
MaxKeySize: viper.Get("MaxKeySize").(int),
MaxValueSize: viper.Get("MaxValueSize").(int),
CompactSize: viper.Get("CompactSize").(int),
DataDir: viper.Get("DataDir").(string),
ActiveDataDir: viper.Get("ActiveDataDir").(string),
LogLevel: viper.Get("LogLevel").(string),
InfoLog: log.New(logFile, "|info|", log.LstdFlags),
DebugLog: log.New(logFile, "|debug|", log.LstdFlags),
ErrorLog: log.New(logFile, "|error|", log.LstdFlags),
}

u := wsgi.Wsgi{
Bitcask: storage.NewBitcask(option),
}
defer func() { u.Bitcask.Close() }()

log.Printf("beancask server start. listening http://%s:%d\n", config.Host, config.Port)
wsgi.SetUpRestfulService(u)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", config.Host, config.Port), nil))
},
}

// Execute execute the command
func Execute() {

initConfig()

if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
20 changes: 20 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cmd

import (
"fmt"

"github.com/spf13/cobra"
)

func init() {
RootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "Print the version number of beancask-server",
Long: `all software has versions. this is beancask-server's`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("beancask key value store v0.0.1 -- HEAD")
},
}
18 changes: 18 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package errors

import "errors"

var (
// ErrorSystemInternal system internal error, something should not happend happens
ErrorSystemInternal = errors.New("system internal error")
// ErrorDataNotFound data not found
ErrorDataNotFound = errors.New("data not found")
// ErrorReadFileData read wal file for record error
ErrorReadFileData = errors.New("read file data error")
// ErrorParseFileData parse wal file to record error
ErrorParseFileData = errors.New("parse file data error")
// ErrorIterateWalFile iterate throught wal file error
ErrorIterateWalFile = errors.New("iterate over wal file error")
// ErrorSystemShuttingDown system is shutting down, not write allow now
ErrorSystemShuttingDown = errors.New("system is shutting down")
)
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/waldoweng/beancask/cmd"
)

func main() {
cmd.Execute()
}
Loading

0 comments on commit 2851b84

Please sign in to comment.