added list templates
This commit is contained in:
parent
785c07dcf5
commit
067bf95201
8 changed files with 487 additions and 315 deletions
12
CHANGELOG
12
CHANGELOG
|
@ -1,3 +1,15 @@
|
|||
v1.0.10, released 2025-07-11
|
||||
* features
|
||||
- AddTemplate - add template to list to be used as commands during adding object
|
||||
|
||||
v1.0.9, released 2025-07-11
|
||||
* bugfixes
|
||||
- fix tests
|
||||
|
||||
v1.0.8, released 2025-07-10
|
||||
* features
|
||||
- ExecuteCommands - execute commands (script). Moved from zordfsdb-client
|
||||
|
||||
v1.0.7, released 2025-03-16
|
||||
* bugfixes
|
||||
- GetRandomNode public command
|
||||
|
|
|
@ -27,6 +27,7 @@ Commands can be split into value layer when you know the structure and node laye
|
|||
* AddObject - add object to given list. Should assign abcex id
|
||||
* Length - get length of list
|
||||
* GetRandomNode - get random object from list
|
||||
* AddTemplate - add template to list to execute commands on AddObject
|
||||
|
||||
### Node as Object commands
|
||||
|
||||
|
@ -60,6 +61,7 @@ DB can run one or many commands separated by semicolon. Same commands are used i
|
|||
* SAVE [path] [value] - set value to property
|
||||
* CREATE [path] - create list/object in path (parent path should exist)
|
||||
* ADD [path] - add abcex indexed object to list. Will return index
|
||||
* TEMPLATE [path] [template] - set template to list for object creation
|
||||
* INFO [path] - get all info about property/object/list
|
||||
* TREE [path] - draw db part in path with values
|
||||
* HELP - will print this help
|
||||
|
@ -73,6 +75,13 @@ Short commands can be as:
|
|||
|
||||
KEYS(\K), GET(\G), INC(\I), DEC(\D), NOW(\N), DEL(\R), SAVE(\S), CREATE(\C), ADD(\A), HELP(\H), QUIT(\Q)
|
||||
|
||||
### Template
|
||||
|
||||
If template is set it is used on AddObject method to execute commands on newly added object. Sintax is same as commands. Example:
|
||||
|
||||
```
|
||||
NOW _.$.created; SAVE _.$.auto 1;
|
||||
```
|
||||
|
||||
## DEV
|
||||
|
||||
|
|
57
command.go
57
command.go
|
@ -13,7 +13,7 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func ExecuteCommands(db DB, commands string, lastPathPtr *string, lastKeyPtr *string) error {
|
||||
func ExecuteCommands(db *DB, commands string, lastPathPtr *string, lastKeyPtr *string) error {
|
||||
for _, command := range strings.Split(commands, ";") {
|
||||
lastPath := *lastPathPtr
|
||||
lastKey := *lastKeyPtr
|
||||
|
@ -47,10 +47,36 @@ func ExecuteCommands(db DB, commands string, lastPathPtr *string, lastKeyPtr *st
|
|||
return nil
|
||||
}
|
||||
|
||||
func ExecuteCommand(db DB, parts []string) (error, string) {
|
||||
func ExecuteCommand(db *DB, parts []string) (error, string) {
|
||||
var newId string
|
||||
|
||||
switch strings.ToUpper(parts[0]) {
|
||||
case "HELP", "\\H":
|
||||
fmt.Println(`
|
||||
ZordFsDB shell supported commands:
|
||||
|
||||
KEYS [path] - get list of existing properties/objects/lists
|
||||
GET [path] - get value of property
|
||||
INC [path] - increase abcex value in property
|
||||
DEC [path] - decrease abcex value in property
|
||||
NOW [path] - set value to datetime string in property
|
||||
DEL [path] - remove property/object/list (no confirmation)
|
||||
SAVE [path] [value] - set value to property
|
||||
CREATE [path] - create list/object in path (parent path should exist)
|
||||
ADD [path] - add abcex indexed object to list. Will return index
|
||||
TEMPLATE [path] [template] - set template to list for object creation
|
||||
INFO [path] - get all info about property/object/list
|
||||
TREE [path] - draw db part in path with values
|
||||
HELP - will print this help
|
||||
QUIT - exit the shell
|
||||
|
||||
Last used path can be accessed by _
|
||||
Last returned index (after ADD command) can be accessed by $
|
||||
|
||||
Short commands can be as:
|
||||
KEYS(\K), GET(\G), INC(\I), DEC(\D), NOW(\N), DEL(\R), SAVE(\S), CREATE(\C), ADD(\A), INFO(\?), TREE(\T), HELP(\H), QUIT(\Q)
|
||||
`)
|
||||
|
||||
case "KEYS", "\\K":
|
||||
kpath := "."
|
||||
if !(len(parts) == 1 || len(parts[1]) == 0) {
|
||||
|
@ -91,7 +117,7 @@ func ExecuteCommand(db DB, parts []string) (error, string) {
|
|||
case "TREE", "\\T":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 || parts[1] == "." {
|
||||
fmt.Printf("TREE\n-----------------\n\\\n")
|
||||
printNodeTree(&db, 1)
|
||||
printNodeTree(db, 1)
|
||||
fmt.Println("")
|
||||
break
|
||||
}
|
||||
|
@ -229,6 +255,31 @@ func ExecuteCommand(db DB, parts []string) (error, string) {
|
|||
newId = id
|
||||
}
|
||||
|
||||
case "TEMPLATE":
|
||||
if len(parts) < 3 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: TEMPLATE [path] [value|long value]\n"), newId
|
||||
}
|
||||
|
||||
template := strings.Trim(strings.Join(parts[2:], " "), "\"")
|
||||
|
||||
node, ok := db.GetNode(parts[1])
|
||||
if !ok {
|
||||
return fmt.Errorf("No path found\n"), newId
|
||||
}
|
||||
|
||||
if !node.List {
|
||||
return fmt.Errorf("Path is not a list\n"), newId
|
||||
}
|
||||
|
||||
ok = db.AddTemplate(parts[1], template)
|
||||
if !ok {
|
||||
fmt.Println("Template not added.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} TEMPLATE ADDED\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("err[00] unknown command, try HELP"), newId
|
||||
}
|
||||
|
|
335
db.go
Normal file
335
db.go
Normal file
|
@ -0,0 +1,335 @@
|
|||
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
|
||||
//
|
||||
// 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.
|
||||
|
||||
package zordfsdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
abcex "g.arns.lt/zordsdavini/abcex/v4"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
Root string
|
||||
Nodes map[string]Node
|
||||
}
|
||||
|
||||
func (db *DB) GetNodes() map[string]Node {
|
||||
return db.Nodes
|
||||
}
|
||||
|
||||
func (db *DB) Refresh() error {
|
||||
db.Nodes = make(map[string]Node)
|
||||
|
||||
files, err := os.ReadDir(db.Root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
node := Node{Key: file.Name()}
|
||||
node.Nodes = make(map[string]Node)
|
||||
|
||||
if file.IsDir() {
|
||||
node, err = readDir(db.Root, node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
b, err := os.ReadFile(path.Join(db.Root, file.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if file.Name() == ".template" {
|
||||
node.Template = strings.Trim(string(b), "\n")
|
||||
continue
|
||||
}
|
||||
|
||||
node.Value = strings.Trim(string(b), "\n")
|
||||
}
|
||||
|
||||
db.Nodes[file.Name()] = node
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) Keys(vpath string) []string {
|
||||
keys := []string{}
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
root := path.Join(fullPath...)
|
||||
files, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
return keys
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.Name() == ".template" {
|
||||
continue
|
||||
}
|
||||
|
||||
keys = append(keys, file.Name())
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (db *DB) GetNode(vpath string) (Node, bool) {
|
||||
nodes := db.Nodes
|
||||
current := Node{}
|
||||
STEP_LOOP:
|
||||
for _, step := range strings.Split(vpath, ".") {
|
||||
for _, node := range nodes {
|
||||
if step == node.Key {
|
||||
nodes = node.Nodes
|
||||
current = node
|
||||
continue STEP_LOOP
|
||||
}
|
||||
}
|
||||
|
||||
return current, false
|
||||
}
|
||||
|
||||
return current, true
|
||||
}
|
||||
|
||||
func (db *DB) Get(vpath string) (string, bool) {
|
||||
node, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return node.Value, true
|
||||
}
|
||||
|
||||
func (db *DB) Inc(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
val, found := db.Get(vpath)
|
||||
if !found {
|
||||
val = "0"
|
||||
}
|
||||
|
||||
valAbc := abcex.Decode(val, abcex.BASE62)
|
||||
valAbc++
|
||||
val = abcex.Encode(valAbc, abcex.BASE62)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Dec(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
val, found := db.Get(vpath)
|
||||
if !found {
|
||||
val = "0"
|
||||
}
|
||||
|
||||
valAbc := abcex.Decode(val, abcex.BASE62)
|
||||
valAbc--
|
||||
val = abcex.Encode(valAbc, abcex.BASE62)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Now(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
now := time.Now()
|
||||
formatted := now.Format(time.RFC3339)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(formatted), 0644)
|
||||
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Save(vpath string, value string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
err := os.WriteFile(path.Join(fullPath...), []byte(value), 0644)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Del(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
os.RemoveAll(path.Join(fullPath...))
|
||||
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) CreateNode(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
err := os.Mkdir(path.Join(fullPath...), 0750)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) AddObject(vpath string) (string, error) {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
root := path.Join(fullPath...)
|
||||
ids, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
maxId := int64(0)
|
||||
notEmpty := false
|
||||
for _, id := range ids {
|
||||
if id.Name() == ".template" {
|
||||
continue
|
||||
}
|
||||
|
||||
idInt := abcex.Decode(id.Name(), abcex.BASE62)
|
||||
if maxId <= idInt {
|
||||
maxId = idInt
|
||||
notEmpty = true
|
||||
}
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
maxId++
|
||||
}
|
||||
fmt.Println(fullPath, maxId)
|
||||
|
||||
maxIdAbc := abcex.Encode(maxId, abcex.BASE62)
|
||||
fullPath = append(fullPath, maxIdAbc)
|
||||
fmt.Println(fullPath, maxId, maxIdAbc)
|
||||
|
||||
err = os.Mkdir(path.Join(fullPath...), 0750)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
list, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return "", fmt.Errorf("Node not found")
|
||||
}
|
||||
|
||||
if list.Template != "" {
|
||||
fmt.Println("Template", list.Template, vpath, maxIdAbc)
|
||||
err = db.ExecuteCommands(list.Template, &vpath, &maxIdAbc)
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return abcex.Encode(maxId, abcex.BASE62), nil
|
||||
}
|
||||
|
||||
func (db *DB) Length(vpath string) (int64, error) {
|
||||
node, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return 0, fmt.Errorf("Node not found")
|
||||
}
|
||||
|
||||
if !node.List {
|
||||
return 0, fmt.Errorf("Node is not a list")
|
||||
}
|
||||
|
||||
return int64(len(node.Nodes)), nil
|
||||
}
|
||||
|
||||
func (db *DB) GetRandomNode(vpath string) (Node, error) {
|
||||
node, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return Node{}, fmt.Errorf("Node not found")
|
||||
}
|
||||
|
||||
if !node.List {
|
||||
return Node{}, fmt.Errorf("Not a list")
|
||||
}
|
||||
|
||||
if len(node.Nodes) == 0 {
|
||||
return Node{}, fmt.Errorf("List is empty")
|
||||
}
|
||||
|
||||
keys := db.Keys(vpath)
|
||||
randomKey := keys[rand.Intn(len(keys))]
|
||||
|
||||
return node.Nodes[randomKey], nil
|
||||
}
|
||||
|
||||
func (db *DB) ExecuteCommands(commands string, lastPath *string, lastKey *string) error {
|
||||
return ExecuteCommands(db, commands, lastPath, lastKey)
|
||||
}
|
||||
|
||||
func (db *DB) AddTemplate(vpath string, template string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
fullPath = append(fullPath, ".template")
|
||||
|
||||
err := os.WriteFile(path.Join(fullPath...), []byte(template), 0644)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
50
node.go
Normal file
50
node.go
Normal file
|
@ -0,0 +1,50 @@
|
|||
// Copyright (C) 2025 Arns Udovič <zordsdavini@arns.lt>
|
||||
//
|
||||
// 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.
|
||||
|
||||
package zordfsdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Node struct {
|
||||
Key string
|
||||
Value string
|
||||
Nodes map[string]Node
|
||||
Object bool
|
||||
List bool
|
||||
Template string
|
||||
}
|
||||
|
||||
func (node *Node) GetNodes() map[string]Node {
|
||||
return node.Nodes
|
||||
}
|
||||
|
||||
func (node *Node) FixType() {
|
||||
node.List = false
|
||||
node.Object = false
|
||||
for _, child := range node.Nodes {
|
||||
child.FixType()
|
||||
|
||||
if child.Object {
|
||||
node.List = true
|
||||
break
|
||||
}
|
||||
|
||||
node.Object = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func (node *Node) Get(key string) (string, error) {
|
||||
if _, ok := node.Nodes[key]; ok {
|
||||
return node.Nodes[key].Value, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Key not found")
|
||||
}
|
1
testdata/list/.template
vendored
Normal file
1
testdata/list/.template
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
NOW _.$.created; SAVE _.$.auto 1;
|
317
zordfsdb.go
317
zordfsdb.go
|
@ -9,49 +9,15 @@
|
|||
package zordfsdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
abcex "g.arns.lt/zordsdavini/abcex/v4"
|
||||
)
|
||||
|
||||
type GetNodes interface {
|
||||
GetNodes() map[string]Node
|
||||
}
|
||||
|
||||
type DB struct {
|
||||
Root string
|
||||
Nodes map[string]Node
|
||||
}
|
||||
|
||||
type Node struct {
|
||||
Key string
|
||||
Value string
|
||||
Nodes map[string]Node
|
||||
Object bool
|
||||
List bool
|
||||
}
|
||||
|
||||
func (db *DB) GetNodes() map[string]Node {
|
||||
return db.Nodes
|
||||
}
|
||||
|
||||
func (node *Node) GetNodes() map[string]Node {
|
||||
return node.Nodes
|
||||
}
|
||||
|
||||
func (node *Node) Get(key string) (string, error) {
|
||||
if _, ok := node.Nodes[key]; ok {
|
||||
return node.Nodes[key].Value, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("Key not found")
|
||||
}
|
||||
|
||||
func InitDB(root string) (DB, error) {
|
||||
db := DB{Root: root}
|
||||
err := db.Refresh()
|
||||
|
@ -59,38 +25,6 @@ func InitDB(root string) (DB, error) {
|
|||
return db, err
|
||||
}
|
||||
|
||||
func (db *DB) Refresh() error {
|
||||
db.Nodes = make(map[string]Node)
|
||||
|
||||
files, err := os.ReadDir(db.Root)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
node := Node{Key: file.Name()}
|
||||
node.Nodes = make(map[string]Node)
|
||||
|
||||
if file.IsDir() {
|
||||
node, err = readDir(db.Root, node)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
b, err := os.ReadFile(path.Join(db.Root, file.Name()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
node.Value = strings.Trim(string(b), "\n")
|
||||
}
|
||||
|
||||
db.Nodes[file.Name()] = node
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readDir(root string, node Node) (Node, error) {
|
||||
newRoot := path.Join(root, node.Key)
|
||||
files, err := os.ReadDir(newRoot)
|
||||
|
@ -113,6 +47,11 @@ func readDir(root string, node Node) (Node, error) {
|
|||
return node, err
|
||||
}
|
||||
|
||||
if file.Name() == ".template" {
|
||||
node.Template = strings.Trim(string(b), "\n")
|
||||
continue
|
||||
}
|
||||
|
||||
child.Value = strings.Trim(string(b), "\n")
|
||||
}
|
||||
|
||||
|
@ -124,249 +63,3 @@ func readDir(root string, node Node) (Node, error) {
|
|||
return node, nil
|
||||
}
|
||||
|
||||
func (node *Node) FixType() {
|
||||
node.List = false
|
||||
node.Object = false
|
||||
for _, child := range node.Nodes {
|
||||
child.FixType()
|
||||
|
||||
if child.Object {
|
||||
node.List = true
|
||||
break
|
||||
}
|
||||
|
||||
node.Object = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func (db *DB) Keys(vpath string) []string {
|
||||
keys := []string{}
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
root := path.Join(fullPath...)
|
||||
files, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
return keys
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
keys = append(keys, file.Name())
|
||||
}
|
||||
|
||||
return keys
|
||||
}
|
||||
|
||||
func (db *DB) GetNode(vpath string) (Node, bool) {
|
||||
nodes := db.Nodes
|
||||
current := Node{}
|
||||
STEP_LOOP:
|
||||
for _, step := range strings.Split(vpath, ".") {
|
||||
for _, node := range nodes {
|
||||
if step == node.Key {
|
||||
nodes = node.Nodes
|
||||
current = node
|
||||
continue STEP_LOOP
|
||||
}
|
||||
}
|
||||
|
||||
return current, false
|
||||
}
|
||||
|
||||
return current, true
|
||||
}
|
||||
|
||||
func (db *DB) Get(vpath string) (string, bool) {
|
||||
node, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return node.Value, true
|
||||
}
|
||||
|
||||
func (db *DB) Inc(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
val, found := db.Get(vpath)
|
||||
if !found {
|
||||
val = "0"
|
||||
}
|
||||
|
||||
valAbc := abcex.Decode(val, abcex.BASE62)
|
||||
valAbc++
|
||||
val = abcex.Encode(valAbc, abcex.BASE62)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Dec(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
val, found := db.Get(vpath)
|
||||
if !found {
|
||||
val = "0"
|
||||
}
|
||||
|
||||
valAbc := abcex.Decode(val, abcex.BASE62)
|
||||
valAbc--
|
||||
val = abcex.Encode(valAbc, abcex.BASE62)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Now(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
now := time.Now()
|
||||
formatted := now.Format(time.RFC3339)
|
||||
|
||||
os.WriteFile(path.Join(fullPath...), []byte(formatted), 0644)
|
||||
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Save(vpath string, value string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
err := os.WriteFile(path.Join(fullPath...), []byte(value), 0644)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) Del(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
os.RemoveAll(path.Join(fullPath...))
|
||||
|
||||
err := db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) CreateNode(vpath string) bool {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
|
||||
err := os.Mkdir(path.Join(fullPath...), 0750)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *DB) AddObject(vpath string) (string, error) {
|
||||
fullPath := []string{db.Root}
|
||||
fullPath = append(fullPath, strings.Split(vpath, ".")...)
|
||||
root := path.Join(fullPath...)
|
||||
ids, err := os.ReadDir(root)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
maxId := int64(0)
|
||||
notEmpty := false
|
||||
for _, id := range ids {
|
||||
idInt := abcex.Decode(id.Name(), abcex.BASE62)
|
||||
if maxId <= idInt {
|
||||
maxId = idInt
|
||||
notEmpty = true
|
||||
}
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
maxId++
|
||||
}
|
||||
|
||||
fullPath = append(fullPath, abcex.Encode(maxId, abcex.BASE62))
|
||||
fmt.Println(fullPath, maxId, abcex.Encode(maxId, abcex.BASE62))
|
||||
|
||||
err = os.Mkdir(path.Join(fullPath...), 0750)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return abcex.Encode(maxId, abcex.BASE62), nil
|
||||
}
|
||||
|
||||
func (db *DB) Length(vpath string) (int64, error) {
|
||||
node, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return 0, fmt.Errorf("Node not found")
|
||||
}
|
||||
|
||||
if !node.List {
|
||||
return 0, fmt.Errorf("Node is not a list")
|
||||
}
|
||||
|
||||
return int64(len(node.Nodes)), nil
|
||||
}
|
||||
|
||||
func (db *DB) GetRandomNode(vpath string) (Node, error) {
|
||||
node, found := db.GetNode(vpath)
|
||||
if !found {
|
||||
return Node{}, fmt.Errorf("Node not found")
|
||||
}
|
||||
|
||||
if !node.List {
|
||||
return Node{}, fmt.Errorf("Not a list")
|
||||
}
|
||||
|
||||
if len(node.Nodes) == 0 {
|
||||
return Node{}, fmt.Errorf("List is empty")
|
||||
}
|
||||
|
||||
keys := db.Keys(vpath)
|
||||
randomKey := keys[rand.Intn(len(keys))]
|
||||
|
||||
return node.Nodes[randomKey], nil
|
||||
}
|
||||
|
||||
func (db *DB) ExecuteCommands(commands string, lastPath *string, lastKey *string) error {
|
||||
return ExecuteCommands(*db, commands, lastPath, lastKey)
|
||||
}
|
||||
|
||||
|
|
|
@ -302,3 +302,24 @@ func TestExecuteCommands(t *testing.T) {
|
|||
t.Fatal("newKey2 value get fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestTemplate(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.AddObject("list")
|
||||
|
||||
val, found := db.Get("list.3.created")
|
||||
if !found {
|
||||
t.Fatal("No list.3.created added by template")
|
||||
}
|
||||
|
||||
val, found = db.Get("list.3.auto")
|
||||
if !found || val != "1" {
|
||||
t.Fatal("No list.3.auto added by template")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue