Merge branch 'develop'

This commit is contained in:
siddontang 2014-07-02 10:16:40 +08:00
commit 4a63a1e658
8 changed files with 2230 additions and 216 deletions

1090
cmd/ledis-cli/linenoise.c Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,43 @@
package main
//#include <stdlib.h>
//#include "linenoise.h"
import "C"
import (
"errors"
"unsafe"
)
func line(prompt string) (string, error) {
promptCString := C.CString(prompt)
resultCString := C.linenoise(promptCString)
C.free(unsafe.Pointer(promptCString))
defer C.free(unsafe.Pointer(resultCString))
if resultCString == nil {
return "", errors.New("quited by a signal")
}
result := C.GoString(resultCString)
return result, nil
}
func addHistory(line string) error {
lineCString := C.CString(line)
res := C.linenoiseHistoryAdd(lineCString)
C.free(unsafe.Pointer(lineCString))
if res != 1 {
return errors.New("Could not add line to history.")
}
return nil
}
func setHistoryCapacity(capacity int) error {
res := C.linenoiseHistorySetMaxLen(C.int(capacity))
if res != 1 {
return errors.New("Could not set history max len.")
}
return nil
}

66
cmd/ledis-cli/linenoise.h Normal file
View File

@ -0,0 +1,66 @@
/* linenoise.h -- guerrilla line editing library against the idea that a
* line editing lib needs to be 20,000 lines of C code.
*
* See linenoise.c for more information.
*
* ------------------------------------------------------------------------
*
* Copyright (c) 2010, Salvatore Sanfilippo <antirez at gmail dot com>
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __LINENOISE_H
#define __LINENOISE_H
#ifdef __cplusplus
extern "C" {
#endif
typedef struct linenoiseCompletions {
size_t len;
char **cvec;
} linenoiseCompletions;
typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *);
void linenoiseSetCompletionCallback(linenoiseCompletionCallback *);
void linenoiseAddCompletion(linenoiseCompletions *, const char *);
char *linenoise(const char *prompt);
int linenoiseHistoryAdd(const char *line);
int linenoiseHistorySetMaxLen(int len);
int linenoiseHistorySave(const char *filename);
int linenoiseHistoryLoad(const char *filename);
void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
#ifdef __cplusplus
}
#endif
#endif /* __LINENOISE_H */

View File

@ -1,11 +1,10 @@
package main
import (
"bufio"
"flag"
"fmt"
"github.com/siddontang/ledisdb/client/go/ledis"
"os"
"regexp"
"strings"
)
@ -27,20 +26,26 @@ func main() {
c := ledis.NewClient(cfg)
reader := bufio.NewReader(os.Stdin)
setHistoryCapacity(100)
reg, _ := regexp.Compile(`'.*?'|".*?"|\S+`)
for {
fmt.Printf("ledis %s > ", cfg.Addr)
cmd, err := line(fmt.Sprintf("%s> ", cfg.Addr))
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
cmd, _ := reader.ReadString('\n')
cmds := strings.Fields(cmd)
cmds := reg.FindAllString(cmd, -1)
if len(cmds) == 0 {
continue
} else {
addHistory(cmd)
args := make([]interface{}, len(cmds[1:]))
for i := range args {
args[i] = strings.Trim(string(cmds[1+i]), "\"")
args[i] = strings.Trim(string(cmds[1+i]), "\"'")
}
r, err := c.Do(cmds[0], args...)
if err != nil {

View File

@ -9,7 +9,7 @@ import (
"path"
)
var fileName = flag.String("config", "/etc/ledis.config", "ledisdb config file")
var fileName = flag.String("config", "/etc/ledis.json", "ledisdb config file")
func main() {
flag.Parse()

View File

@ -17,18 +17,21 @@ func main() {
flag.Parse()
if len(*configFile) == 0 {
panic("must use a config file")
println("must use a config file")
return
}
cfg, err := server.NewConfigWithFile(*configFile)
if err != nil {
panic(err)
println(err.Error())
return
}
var app *server.App
app, err = server.NewApp(cfg)
if err != nil {
panic(err)
println(err.Error())
return
}
sc := make(chan os.Signal, 1)

File diff suppressed because it is too large Load Diff

View File

@ -88,8 +88,7 @@ func Repair(cfg *Config) error {
db.cfg = cfg
err := db.open()
db.Close()
defer db.Close()
//open ok, do not need repair
if err == nil {