Compare commits
No commits in common. "master" and "v1.0.0" have entirely different histories.
11 changed files with 212 additions and 989 deletions
10
.drone.yml
10
.drone.yml
|
@ -1,10 +0,0 @@
|
|||
---
|
||||
kind: pipeline
|
||||
type: docker
|
||||
name: default
|
||||
steps:
|
||||
- name: backend
|
||||
image: golang
|
||||
commands:
|
||||
- go build
|
||||
- go test
|
45
CHANGELOG
45
CHANGELOG
|
@ -1,46 +1,3 @@
|
|||
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
|
||||
|
||||
v1.0.6, released 2025-03-16
|
||||
* features
|
||||
- Node.Get - get object property value
|
||||
|
||||
v1.0.5, released 2025-03-16
|
||||
* bugfixes
|
||||
- FixType - run recursevily into child nodes to get correct value
|
||||
* features
|
||||
- Length - get length of list
|
||||
- GetRandomNode - get random node from list
|
||||
|
||||
v1.0.4, released 2025-01-07
|
||||
* abcex to v4
|
||||
|
||||
v1.0.3, released 2024-09-20
|
||||
* abcex to v3
|
||||
|
||||
v1.0.2, released 2024-08-29
|
||||
* use GetNode interface
|
||||
|
||||
v1.0.1, released 2024-08-08
|
||||
* split commands to value and object layer
|
||||
* abcex to v1.0.1
|
||||
* features
|
||||
- CreateNode - create directory to hold key/value as object or as list for objects
|
||||
- AddObject - create directory in list object by abcex id
|
||||
|
||||
v1.0.0, released 2024-07-18
|
||||
* features
|
||||
- Init
|
||||
|
@ -50,3 +7,5 @@ v1.0.0, released 2024-07-18
|
|||
- Inc - increase by abcex value
|
||||
- Dec - decrease by abcex value
|
||||
- Now - insert datetime NOW
|
||||
|
||||
|
||||
|
|
84
README.md
84
README.md
|
@ -1,88 +1,24 @@
|
|||
# zordfsdb
|
||||
|
||||
[](https://drone.arns.lt/zordsdavini/zordfsdb)
|
||||
|
||||
Simple filesystem based key/value db. Provided as golang lib. Main idea is that user knows structure and gets value from known path. For operations that should search or do more should be used Nodes (golang structure).
|
||||
Simple filesystem based key/value db. Provided as golang lib.
|
||||
|
||||
## Configuration
|
||||
|
||||
Init root directory.
|
||||
Init root directory
|
||||
|
||||
## Supported command
|
||||
|
||||
Commands can be split into value layer when you know the structure and node layer for deaper operations.
|
||||
|
||||
### value layer commands
|
||||
|
||||
* Get - to get value from path
|
||||
* Save - update or create value. Depends on path. If path directs into not existing parent object - will return false
|
||||
* Inc - increase abcex value
|
||||
* Dec - decrease abcex value
|
||||
* Now - save current datetime
|
||||
|
||||
### node layer commands
|
||||
|
||||
* GetNode - to get node. It can be object, list or value object. Returns false if not exist
|
||||
* CreatNode - create list (director) to add many same objects (structure should be controlled by user) or single object. Parent Node should exist
|
||||
* 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
|
||||
|
||||
* Node.Get - get object property value
|
||||
* Node.GetNodes - get object properties
|
||||
* Node.FixType - checks and sets object/list type
|
||||
|
||||
### helper commands
|
||||
|
||||
* Del - delete value, object or list by given path
|
||||
* Keys - return possible keys for object or ids for list
|
||||
* ExecuteCommands - execute commands separated by semicolon. See **Commands** section
|
||||
* GET - to get value. Depends on return type. Can be single value or map (to object or to list of object)
|
||||
* INS - insert object into list
|
||||
* SAVE - update value or object. Depends on path
|
||||
* INC - increase abcex value
|
||||
* DEC - decrease abcex value
|
||||
* NOW - save current datetime
|
||||
* DEL - delete
|
||||
* KEYS - return possible keys
|
||||
|
||||
## dictionary
|
||||
|
||||
* object - directory of key/value
|
||||
* list - directory of objects named by abcex as key
|
||||
* path - path to key or object or list
|
||||
|
||||
|
||||
## Commands
|
||||
|
||||
DB can run one or many commands separated by semicolon. Same commands are used in template to add new list object.
|
||||
|
||||
* 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), 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
|
||||
|
||||
Run tests: `$ go test`
|
||||
|
|
306
command.go
306
command.go
|
@ -1,306 +0,0 @@
|
|||
// 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ExecuteCommands(db *DB, commands string, lastPathPtr *string, lastKeyPtr *string) error {
|
||||
for _, command := range strings.Split(commands, ";") {
|
||||
lastPath := *lastPathPtr
|
||||
lastKey := *lastKeyPtr
|
||||
command = strings.Trim(command, " ")
|
||||
parts := strings.Split(command, " ")
|
||||
if len(parts) > 1 {
|
||||
if lastPath != "" {
|
||||
if parts[1] == "_" {
|
||||
parts[1] = lastPath
|
||||
} else {
|
||||
parts[1] = strings.Replace(parts[1], "_.", lastPath+".", 1)
|
||||
}
|
||||
}
|
||||
|
||||
if lastKey != "" {
|
||||
parts[1] = strings.Replace(parts[1], "$", lastKey, 1)
|
||||
}
|
||||
|
||||
lastPath = parts[1]
|
||||
}
|
||||
|
||||
err, gotKey := ExecuteCommand(db, parts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if gotKey != "" {
|
||||
*lastKeyPtr = gotKey
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
kpath = parts[1]
|
||||
}
|
||||
|
||||
fmt.Printf("{%s} KEYS: ", kpath)
|
||||
fmt.Println(db.Keys(kpath))
|
||||
fmt.Println("")
|
||||
|
||||
case "INFO", "\\?":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: INFO [path]\n"), newId
|
||||
}
|
||||
|
||||
node, ok := db.GetNode(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No path found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} INFO\n-----------------\n", parts[1])
|
||||
ntype := "property"
|
||||
if node.List {
|
||||
ntype = "list"
|
||||
} else if node.Object {
|
||||
ntype = "object"
|
||||
}
|
||||
fmt.Println("type:", ntype)
|
||||
if ntype == "property" {
|
||||
fmt.Println("value:", node.Value)
|
||||
} else {
|
||||
fmt.Println("keys:", db.Keys(parts[1]))
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "TREE", "\\T":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 || parts[1] == "." {
|
||||
fmt.Printf("TREE\n-----------------\n\\\n")
|
||||
printNodeTree(db, 1)
|
||||
fmt.Println("")
|
||||
break
|
||||
}
|
||||
|
||||
node, ok := db.GetNode(parts[1])
|
||||
if !ok {
|
||||
return fmt.Errorf("err format: TREE [path]\n"), newId
|
||||
} else {
|
||||
fmt.Printf("{%s} TREE\n-----------------\n", parts[1])
|
||||
if !node.List && !node.Object {
|
||||
fmt.Printf("%s: %s\n\n", node.Key, node.Value)
|
||||
break
|
||||
}
|
||||
|
||||
fmt.Println(node.Key)
|
||||
printNodeTree(&node, 1)
|
||||
}
|
||||
|
||||
fmt.Println("")
|
||||
|
||||
case "GET", "\\G":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: GET [path]\n"), newId
|
||||
}
|
||||
|
||||
value, ok := db.Get(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No value found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} VALUE: ", parts[1])
|
||||
fmt.Println(value)
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "INC", "\\I":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: INC [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Inc(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No value found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} INC OK\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "DEC", "\\D":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: DEC [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Dec(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No value found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} DEC OK\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "NOW", "\\N":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: NOW [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Now(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No value found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} NOW OK\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "DEL", "\\R":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: DEL [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Del(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No value/object found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} DELETED\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "SAVE", "\\S":
|
||||
if len(parts) < 3 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: SAVE [path] [value|long value]\n"), newId
|
||||
}
|
||||
|
||||
value := strings.Trim(strings.Join(parts[2:], " "), "\"")
|
||||
|
||||
ok := db.Save(parts[1], value)
|
||||
if !ok {
|
||||
fmt.Println("No value found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} SAVED\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
db.Refresh()
|
||||
|
||||
case "CREATE", "\\C":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: CREATE [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.CreateNode(parts[1])
|
||||
if !ok {
|
||||
fmt.Println("No path found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} CREATED\n", parts[1])
|
||||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "ADD", "\\A":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: ADD [path]\n"), newId
|
||||
}
|
||||
|
||||
id, err := db.AddObject(parts[1])
|
||||
if err != nil {
|
||||
fmt.Println("No path found.")
|
||||
fmt.Println("")
|
||||
} else {
|
||||
fmt.Printf("{%s} ADDED with ID: %s\n", parts[1], id)
|
||||
fmt.Println("")
|
||||
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
|
||||
}
|
||||
|
||||
return nil, newId
|
||||
}
|
||||
|
||||
func printNodeTree(node GetNodes, i int) {
|
||||
for key, child := range node.GetNodes() {
|
||||
j := i - 1
|
||||
for j > 0 {
|
||||
fmt.Printf("│ ")
|
||||
j--
|
||||
}
|
||||
|
||||
fmt.Printf("├── %s", key)
|
||||
if !child.List && !child.Object {
|
||||
fmt.Printf(": %s\n", child.Value)
|
||||
} else {
|
||||
fmt.Println("")
|
||||
printNodeTree(&child, i+1)
|
||||
}
|
||||
}
|
||||
}
|
335
db.go
335
db.go
|
@ -1,335 +0,0 @@
|
|||
// 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
|
||||
}
|
||||
|
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module g.arns.lt/zordsdavini/zordfsdb
|
|||
go 1.22.5
|
||||
|
||||
require (
|
||||
g.arns.lt/zordsdavini/abcex/v4 v4.0.4
|
||||
g.arns.lt/zordsdavini/abcex v1.0.0
|
||||
github.com/otiai10/copy v1.14.0
|
||||
)
|
||||
|
||||
|
|
4
go.sum
4
go.sum
|
@ -1,5 +1,5 @@
|
|||
g.arns.lt/zordsdavini/abcex/v4 v4.0.4 h1:idjvgkCjrjZfDxLyOcX7lCIdIndISDAkj77VCvhu8/c=
|
||||
g.arns.lt/zordsdavini/abcex/v4 v4.0.4/go.mod h1:/+//gYSUtJrdsmTtWNoffRO4xD1BuPRUMGW4ynet7iE=
|
||||
g.arns.lt/zordsdavini/abcex v1.0.0 h1:qQqlZ4DMfethCGK4I6yGaLqMrTzKNIshqpINd1l3t0E=
|
||||
g.arns.lt/zordsdavini/abcex v1.0.0/go.mod h1:YRcJgts3XZwI+LEkngpfUab3DkUAW387Irpr43hIym8=
|
||||
github.com/otiai10/copy v1.14.0 h1:dCI/t1iTdYGtkvCuBG2BgR6KZa83PTclw4U5n2wAllU=
|
||||
github.com/otiai10/copy v1.14.0/go.mod h1:ECfuL02W+/FkTWZWgQqXPWZgW9oeKCSQ5qVfSc4qc4w=
|
||||
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
|
||||
|
|
50
node.go
50
node.go
|
@ -1,50 +0,0 @@
|
|||
// 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
1
testdata/list/.template
vendored
|
@ -1 +0,0 @@
|
|||
NOW _.$.created; SAVE _.$.auto 1;
|
211
zordfsdb.go
211
zordfsdb.go
|
@ -1,21 +1,25 @@
|
|||
// 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 (
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"g.arns.lt/zordsdavini/abcex"
|
||||
)
|
||||
|
||||
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 InitDB(root string) (DB, error) {
|
||||
|
@ -25,6 +29,38 @@ 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)
|
||||
|
@ -47,11 +83,6 @@ 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")
|
||||
}
|
||||
|
||||
|
@ -63,3 +94,153 @@ 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 {
|
||||
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 {
|
||||
return false
|
||||
}
|
||||
|
||||
valAbc := abcex.Decode(val)
|
||||
valAbc++
|
||||
val = abcex.Encode(valAbc)
|
||||
|
||||
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 {
|
||||
return false
|
||||
}
|
||||
|
||||
valAbc := abcex.Decode(val)
|
||||
valAbc--
|
||||
val = abcex.Encode(valAbc)
|
||||
|
||||
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
|
||||
}
|
||||
|
|
153
zordfsdb_test.go
153
zordfsdb_test.go
|
@ -1,11 +1,3 @@
|
|||
// 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 (
|
||||
|
@ -127,7 +119,7 @@ func TestNow(t *testing.T) {
|
|||
db.Now("today")
|
||||
val, found := db.Get("today")
|
||||
fmt.Println(val)
|
||||
if !found || len(val) < 10 {
|
||||
if !found || len(val) != 25 {
|
||||
t.Fatal("today value length wrong:", len(val))
|
||||
}
|
||||
}
|
||||
|
@ -180,146 +172,3 @@ func TestDel(t *testing.T) {
|
|||
t.Fatal("delete failed #2")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateList(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
success := db.CreateNode("object.list2")
|
||||
if !success {
|
||||
t.Fatal("object.list2 node was not created")
|
||||
}
|
||||
|
||||
id, err := db.AddObject("object.list2")
|
||||
fmt.Println("Added object to object.list2 id:", id)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Save("object.list2."+id+".newKey4", "newValue4")
|
||||
val, found := db.Get("object.list2." + id + ".newKey4")
|
||||
fmt.Println(val)
|
||||
if !found || val != "newValue4" {
|
||||
t.Fatal("object.list2.0.newKey4 value wrong")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListLength(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
success := db.CreateNode("object.list3")
|
||||
if !success {
|
||||
t.Fatal("object.list3 node was not created")
|
||||
}
|
||||
|
||||
id, _ := db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
id, _ = db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
id, _ = db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
id, _ = db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
|
||||
node, _ := db.GetNode("object.list3")
|
||||
node.FixType()
|
||||
|
||||
fmt.Println(db.GetNode("object.list3"))
|
||||
|
||||
length, err := db.Length("object.list3")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if length != 4 {
|
||||
t.Fatal("object.list3 length wrong")
|
||||
}
|
||||
|
||||
length, err = db.Length("object")
|
||||
if err == nil {
|
||||
t.Fatal("Error expected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestListRandomNode(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
success := db.CreateNode("object.list3")
|
||||
if !success {
|
||||
t.Fatal("object.list3 node was not created")
|
||||
}
|
||||
|
||||
id, _ := db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
id, _ = db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
id, _ = db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
id, _ = db.AddObject("object.list3")
|
||||
db.Save("object.list3."+id+".key", "value")
|
||||
|
||||
node, _ := db.GetNode("object.list3")
|
||||
node.FixType()
|
||||
|
||||
node, _ = db.GetRandomNode("object.list3")
|
||||
if node.Nodes["key"].Value != "value" {
|
||||
fmt.Println(node)
|
||||
t.Fatal("Random node value wrong")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteCommands(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var lastPath string
|
||||
var lastKey string
|
||||
err = db.ExecuteCommands("SAVE newKey2 newValue", &lastPath, &lastKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Refresh()
|
||||
val, found := db.Get("newKey2")
|
||||
if !found || val != "newValue" {
|
||||
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