This commit is contained in:
parent
3885d53a40
commit
28848e639c
5 changed files with 113 additions and 11 deletions
36
README.md
36
README.md
|
@ -2,25 +2,41 @@
|
|||
|
||||
[![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.
|
||||
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
2
go.mod
|
@ -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 v1.0.1
|
||||
github.com/otiai10/copy v1.14.0
|
||||
)
|
||||
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,5 +1,7 @@
|
|||
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 v1.0.1 h1:ZN/xSDQb8CiR9+jXHYp+tUtGILMeRu9PvvqhypZDkyI=
|
||||
g.arns.lt/zordsdavini/abcex v1.0.1/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=
|
||||
|
|
57
zordfsdb.go
57
zordfsdb.go
|
@ -1,6 +1,7 @@
|
|||
package zordfsdb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
@ -244,3 +245,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())
|
||||
if maxId <= idInt {
|
||||
maxId = idInt
|
||||
notEmpty = true
|
||||
}
|
||||
}
|
||||
|
||||
if notEmpty {
|
||||
maxId++
|
||||
}
|
||||
|
||||
fullPath = append(fullPath, abcex.Encode(maxId))
|
||||
fmt.Println(fullPath, maxId, abcex.Encode(maxId))
|
||||
|
||||
err = os.Mkdir(path.Join(fullPath...), 0750)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
err = db.Refresh()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return abcex.Encode(maxId), nil
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue