Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

7 changed files with 23 additions and 161 deletions

View file

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

View file

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

View file

@ -1,42 +1,24 @@
# zordfsdb
[![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).
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
### helper commands
* Del - delete value, object or list by given path
* Keys - return possible keys for object or ids for list
* 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
## 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/v4 v4.0.4
g.arns.lt/zordsdavini/abcex v1.0.0
github.com/otiai10/copy v1.14.0
)

4
go.sum
View file

@ -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=

View file

@ -1,19 +1,14 @@
package zordfsdb
import (
"fmt"
"os"
"path"
"strings"
"time"
abcex "g.arns.lt/zordsdavini/abcex/v4"
"g.arns.lt/zordsdavini/abcex"
)
type GetNodes interface {
GetNodes() map[string]Node
}
type DB struct {
Root string
Nodes map[string]Node
@ -27,14 +22,6 @@ 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()
@ -172,12 +159,12 @@ func (db *DB) Inc(vpath string) bool {
val, found := db.Get(vpath)
if !found {
val = "0"
return false
}
valAbc := abcex.Decode(val, abcex.BASE62)
valAbc := abcex.Decode(val)
valAbc++
val = abcex.Encode(valAbc, abcex.BASE62)
val = abcex.Encode(valAbc)
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
err := db.Refresh()
@ -194,12 +181,12 @@ func (db *DB) Dec(vpath string) bool {
val, found := db.Get(vpath)
if !found {
val = "0"
return false
}
valAbc := abcex.Decode(val, abcex.BASE62)
valAbc := abcex.Decode(val)
valAbc--
val = abcex.Encode(valAbc, abcex.BASE62)
val = abcex.Encode(valAbc)
os.WriteFile(path.Join(fullPath...), []byte(string(val)), 0644)
err := db.Refresh()
@ -257,59 +244,3 @@ 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) < 10 {
if !found || len(val) != 25 {
t.Fatal("today value length wrong:", len(val))
}
}
@ -172,30 +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")
}
}