Compare commits

...

8 commits

Author SHA1 Message Date
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
Arnas Udovic
c03c377165 abcex v3
All checks were successful
continuous-integration/drone/tag Build is passing
continuous-integration/drone/push Build is passing
2024-09-20 11:25:02 +03:00
Arnas Udovic
2234d83153 support GetNodes interface
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-29 17:42:30 +03:00
Arnas Udovic
cb02d4a1c5 changelog reverse order
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-08 18:27:05 +03:00
f76492a71e Update CHANGELOG
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-08 15:07:41 +00:00
Arnas Udovic
28848e639c added node commands
All checks were successful
continuous-integration/drone/push Build is passing
2024-08-08 15:23:03 +03:00
Arnas Udovic
3885d53a40 update test
All checks were successful
continuous-integration/drone/push Build is passing
2024-07-18 22:53:43 +03:00
Arnas Udovic
853b8d8929 drone
Some checks failed
continuous-integration/drone Build is failing
2024-07-18 22:44:12 +03:00
7 changed files with 161 additions and 23 deletions

10
.drone.yml Normal file
View file

@ -0,0 +1,10 @@
---
kind: pipeline
type: docker
name: default
steps:
- name: backend
image: golang
commands:
- go build
- go test

View file

@ -1,3 +1,19 @@
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
@ -7,5 +23,3 @@ v1.0.0, released 2024-07-18
- Inc - increase by abcex value
- Dec - decrease by abcex value
- Now - insert datetime NOW

View file

@ -1,24 +1,42 @@
# zordfsdb
Simple filesystem based key/value db. Provided as golang lib.
[![Build Status](https://drone.arns.lt/api/badges/zordsdavini/zordfsdb/status.svg)](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).
## Configuration
Init root directory
Init root directory.
## Supported command
* 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
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
### helper commands
* Del - delete value, object or list by given path
* Keys - return possible keys for object or ids for list
## dictionary
* object - directory of key/value
* list - directory of objects named by abcex as key
* path - path to key or object or list
## DEV
Run tests: `$ go test`

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 v1.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 v1.0.0 h1:qQqlZ4DMfethCGK4I6yGaLqMrTzKNIshqpINd1l3t0E=
g.arns.lt/zordsdavini/abcex v1.0.0/go.mod h1:YRcJgts3XZwI+LEkngpfUab3DkUAW387Irpr43hIym8=
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

@ -1,14 +1,19 @@
package zordfsdb
import (
"fmt"
"os"
"path"
"strings"
"time"
"g.arns.lt/zordsdavini/abcex"
abcex "g.arns.lt/zordsdavini/abcex/v4"
)
type GetNodes interface {
GetNodes() map[string]Node
}
type DB struct {
Root string
Nodes map[string]Node
@ -22,6 +27,14 @@ type Node struct {
List bool
}
func (db *DB) GetNodes() map[string]Node {
return db.Nodes
}
func (node *Node) GetNodes() map[string]Node {
return node.Nodes
}
func InitDB(root string) (DB, error) {
db := DB{Root: root}
err := db.Refresh()
@ -159,12 +172,12 @@ func (db *DB) Inc(vpath string) bool {
val, found := db.Get(vpath)
if !found {
return false
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()
@ -181,12 +194,12 @@ func (db *DB) Dec(vpath string) bool {
val, found := db.Get(vpath)
if !found {
return false
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()
@ -244,3 +257,59 @@ func (db *DB) Del(vpath string) bool {
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
}

View file

@ -119,7 +119,7 @@ func TestNow(t *testing.T) {
db.Now("today")
val, found := db.Get("today")
fmt.Println(val)
if !found || len(val) != 25 {
if !found || len(val) < 10 {
t.Fatal("today value length wrong:", len(val))
}
}
@ -172,3 +172,30 @@ 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")
}
}