Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
78 changed files
with
26,057 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ go 1.12 | |
|
||
require ( | ||
github.com/gogf/gf v1.9.10 | ||
github.com/xujiajun/nutsdb v0.4.0 | ||
github.com/zserge/lorca v0.1.8 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package nustdb | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func TestDBClient(t *testing.T) { | ||
|
||
//最后关闭连接 | ||
defer DBs.db.Close() | ||
|
||
//写入数据 | ||
keys:="kkkk" | ||
value:="vvvv" | ||
if err := DBs.SetData(keys, value);err!=nil{ | ||
log.Fatal(err) | ||
}else{ | ||
fmt.Println(keys) | ||
} | ||
|
||
//查询数据 | ||
if data,err:=DBs.GetData(keys);err!=nil{ | ||
log.Fatal(err) | ||
}else{ | ||
fmt.Println(data) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package nustdb | ||
|
||
import ( | ||
"github.com/xujiajun/nutsdb" | ||
"log" | ||
) | ||
|
||
var DBs *DBClient | ||
|
||
// 创建并打开数据库 | ||
func init(){ | ||
DBs=&DBClient{ | ||
bucket:"db0", | ||
dbdir:"/tmp/nutsdb", | ||
} | ||
DBs.OpenDB() | ||
} | ||
|
||
// IDBClient interface | ||
type IDBClient interface { | ||
OpenDB() | ||
GetData(string) | ||
SetData(string,string) | ||
} | ||
|
||
// DBClient struct | ||
type DBClient struct { | ||
db *nutsdb.DB | ||
dbdir string | ||
bucket string | ||
} | ||
|
||
// OpenDB() 打开数据库 | ||
func (d *DBClient) OpenDB(){ | ||
opt := nutsdb.DefaultOptions | ||
opt.Dir = d.dbdir | ||
db, err := nutsdb.Open(opt) | ||
if err != nil {log.Fatal(err)} | ||
d.db=db | ||
} | ||
|
||
// GetData() 读取数据 | ||
func (d *DBClient) GetData(keys string)(string,error){ | ||
key := []byte(keys) | ||
data:="" | ||
if err := d.db.View( | ||
func(tx *nutsdb.Tx) error { | ||
if e, err := tx.Get(d.bucket, key);err!=nil{ | ||
log.Fatal(err) | ||
}else{ | ||
data=string(e.Value) | ||
} | ||
return nil | ||
}); err != nil { | ||
return "",err | ||
} | ||
return data, nil | ||
} | ||
|
||
// SetData(keys,value) 写入数据 | ||
func (d *DBClient) SetData(keys,value string) error{ | ||
key := []byte(keys) | ||
val := []byte(value) | ||
if err := d.db.Update( | ||
func(tx *nutsdb.Tx) error { | ||
if err := tx.Put(d.bucket, key, val, 0); err != nil { | ||
return err | ||
} | ||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.