2023-06-15 09:41:30 +08:00
|
|
|
package store
|
2023-03-27 18:27:33 +08:00
|
|
|
|
|
|
|
import (
|
2023-06-15 09:41:30 +08:00
|
|
|
"chatplus/store/vo"
|
2023-03-27 18:27:33 +08:00
|
|
|
"encoding/json"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LevelDB struct {
|
|
|
|
driver *leveldb.DB
|
|
|
|
}
|
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
func NewLevelDB() (*LevelDB, error) {
|
|
|
|
db, err := leveldb.OpenFile("data/leveldb", nil)
|
2023-03-27 18:27:33 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &LevelDB{
|
|
|
|
driver: db,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *LevelDB) Put(key string, value interface{}) error {
|
|
|
|
bytes, err := json.Marshal(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return db.driver.Put([]byte(key), bytes, nil)
|
|
|
|
}
|
|
|
|
|
2023-07-04 17:15:02 +08:00
|
|
|
func (db *LevelDB) Get(key string, value interface{}) error {
|
2023-03-27 18:27:33 +08:00
|
|
|
bytes, err := db.driver.Get([]byte(key), nil)
|
|
|
|
if err != nil {
|
2023-07-04 17:15:02 +08:00
|
|
|
return err
|
2023-07-02 20:51:13 +08:00
|
|
|
}
|
|
|
|
|
2023-07-04 17:15:02 +08:00
|
|
|
return json.Unmarshal(bytes, &value)
|
2023-03-27 18:27:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *LevelDB) Search(prefix string) []string {
|
|
|
|
var items = make([]string, 0)
|
|
|
|
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
|
2023-05-03 21:59:19 +08:00
|
|
|
defer iter.Release()
|
|
|
|
|
2023-03-27 18:27:33 +08:00
|
|
|
for iter.Next() {
|
|
|
|
items = append(items, string(iter.Value()))
|
|
|
|
}
|
|
|
|
return items
|
|
|
|
}
|
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
func (db *LevelDB) SearchPage(prefix string, page int, pageSize int) *vo.Page {
|
2023-05-03 21:59:19 +08:00
|
|
|
var items = make([]string, 0)
|
|
|
|
iter := db.driver.NewIterator(util.BytesPrefix([]byte(prefix)), nil)
|
|
|
|
defer iter.Release()
|
|
|
|
|
2023-06-15 09:41:30 +08:00
|
|
|
res := &vo.Page{Page: page, PageSize: pageSize}
|
2023-05-03 21:59:19 +08:00
|
|
|
// 计算数据总数和总页数
|
|
|
|
total := 0
|
|
|
|
for iter.Next() {
|
|
|
|
total++
|
|
|
|
}
|
|
|
|
res.TotalPage = (total + pageSize - 1) / pageSize
|
2023-06-15 09:41:30 +08:00
|
|
|
res.Total = int64(total)
|
2023-05-03 21:59:19 +08:00
|
|
|
|
|
|
|
// 计算目标页码的起始和结束位置
|
|
|
|
start := (page - 1) * pageSize
|
|
|
|
if start > total {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
end := start + pageSize
|
|
|
|
if end > total {
|
|
|
|
end = total
|
|
|
|
}
|
|
|
|
|
|
|
|
// 跳转到目标页码的起始位置
|
|
|
|
count := 0
|
|
|
|
for iter.Next() {
|
|
|
|
if count >= start {
|
|
|
|
items = append(items, string(iter.Value()))
|
|
|
|
}
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
iter.Release()
|
|
|
|
res.Items = items
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-03-27 18:27:33 +08:00
|
|
|
func (db *LevelDB) Delete(key string) error {
|
|
|
|
return db.driver.Delete([]byte(key), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close release resources
|
|
|
|
func (db *LevelDB) Close() error {
|
|
|
|
return db.driver.Close()
|
|
|
|
}
|