Compare commits

..

4 commits

Author SHA1 Message Date
Arnas Udovic
ecfd279d7e fix GetRandomNode
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-03-16 21:22:31 +02:00
Arnas Udovic
c96da91077 Node.Get
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-03-16 21:09:36 +02:00
Arnas Udovic
ddc31e4ca5 add Length, GetRandomNode; fix FixType
All checks were successful
continuous-integration/drone Build is passing
2025-03-16 02:18:12 +02:00
Arnas Udovic
d14b45abdf abcex up
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
2025-01-07 08:41:12 +02:00
6 changed files with 161 additions and 12 deletions

View file

@ -1,3 +1,27 @@
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

View file

@ -25,6 +25,14 @@ Commands can be split into value layer when you know the structure and node laye
* 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
### 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

2
go.mod
View file

@ -3,7 +3,7 @@ module g.arns.lt/zordsdavini/zordfsdb
go 1.22.5
require (
g.arns.lt/zordsdavini/abcex/v3 v3.0.0
g.arns.lt/zordsdavini/abcex/v4 v4.0.4
github.com/otiai10/copy v1.14.0
)

4
go.sum
View file

@ -1,5 +1,5 @@
g.arns.lt/zordsdavini/abcex/v3 v3.0.0 h1:LnlDtaBBOpgicMI/dcBUxlu4r4+IFt/MD19JemphgFc=
g.arns.lt/zordsdavini/abcex/v3 v3.0.0/go.mod h1:z2xqDlRFVnLMnCGpqRjbs9T9EY6lJKJnQmU3zJLSNq8=
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=
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=

View file

@ -2,12 +2,13 @@ package zordfsdb
import (
"fmt"
"math/rand"
"os"
"path"
"strings"
"time"
abcex "g.arns.lt/zordsdavini/abcex/v3"
abcex "g.arns.lt/zordsdavini/abcex/v4"
)
type GetNodes interface {
@ -35,6 +36,14 @@ 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()
@ -111,6 +120,8 @@ func (node *Node) FixType() {
node.List = false
node.Object = false
for _, child := range node.Nodes {
child.FixType()
if child.Object {
node.List = true
break
@ -175,9 +186,9 @@ func (db *DB) Inc(vpath string) bool {
val = "0"
}
valAbc := abcex.Decode(val)
valAbc := abcex.Decode(val, abcex.BASE62)
valAbc++
val = abcex.Encode(valAbc)
val = abcex.Encode(valAbc, abcex.BASE62)
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
err := db.Refresh()
@ -197,9 +208,9 @@ func (db *DB) Dec(vpath string) bool {
val = "0"
}
valAbc := abcex.Decode(val)
valAbc := abcex.Decode(val, abcex.BASE62)
valAbc--
val = abcex.Encode(valAbc)
val = abcex.Encode(valAbc, abcex.BASE62)
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
err := db.Refresh()
@ -287,7 +298,7 @@ func (db *DB) AddObject(vpath string) (string, error) {
maxId := int64(0)
notEmpty := false
for _, id := range ids {
idInt := abcex.Decode(id.Name())
idInt := abcex.Decode(id.Name(), abcex.BASE62)
if maxId <= idInt {
maxId = idInt
notEmpty = true
@ -298,8 +309,8 @@ func (db *DB) AddObject(vpath string) (string, error) {
maxId++
}
fullPath = append(fullPath, abcex.Encode(maxId))
fmt.Println(fullPath, maxId, abcex.Encode(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 {
@ -311,5 +322,38 @@ func (db *DB) AddObject(vpath string) (string, error) {
return "", err
}
return abcex.Encode(maxId), nil
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
}

View file

@ -199,3 +199,76 @@ func TestCreateList(t *testing.T) {
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")
}
}