2025-07-10 21:53:25 +03:00
// 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.
2024-07-18 12:40:53 +03:00
package zordfsdb
import (
2024-08-08 15:23:03 +03:00
"fmt"
2025-03-16 02:18:12 +02:00
"math/rand"
2024-07-18 12:40:53 +03:00
"os"
"path"
"strings"
2024-07-18 13:53:28 +03:00
"time"
2024-07-18 12:40:53 +03:00
2025-01-07 08:41:12 +02:00
abcex "g.arns.lt/zordsdavini/abcex/v4"
2024-07-18 12:40:53 +03:00
)
2024-08-29 17:42:30 +03:00
type GetNodes interface {
GetNodes ( ) map [ string ] Node
}
2024-07-18 12:40:53 +03:00
type DB struct {
Root string
Nodes map [ string ] Node
}
type Node struct {
Key string
Value string
Nodes map [ string ] Node
Object bool
List bool
}
2024-08-29 17:42:30 +03:00
func ( db * DB ) GetNodes ( ) map [ string ] Node {
return db . Nodes
}
func ( node * Node ) GetNodes ( ) map [ string ] Node {
return node . Nodes
}
2025-03-16 21:09:36 +02:00
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" )
}
2024-07-18 12:40:53 +03:00
func InitDB ( root string ) ( DB , error ) {
db := DB { Root : root }
err := db . Refresh ( )
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 )
if err != nil {
return node , err
}
for _ , file := range files {
child := Node { Key : file . Name ( ) }
child . Nodes = make ( map [ string ] Node )
if file . IsDir ( ) {
child , err = readDir ( newRoot , child )
if err != nil {
return node , err
}
} else {
b , err := os . ReadFile ( path . Join ( newRoot , file . Name ( ) ) )
if err != nil {
return node , err
}
child . Value = strings . Trim ( string ( b ) , "\n" )
}
node . Nodes [ file . Name ( ) ] = child
}
node . FixType ( )
return node , nil
}
func ( node * Node ) FixType ( ) {
node . List = false
node . Object = false
for _ , child := range node . Nodes {
2025-03-16 02:18:12 +02:00
child . FixType ( )
2024-07-18 12:40:53 +03:00
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 {
2024-07-18 22:53:43 +03:00
val = "0"
2024-07-18 12:40:53 +03:00
}
2025-01-07 08:41:12 +02:00
valAbc := abcex . Decode ( val , abcex . BASE62 )
2024-07-18 12:40:53 +03:00
valAbc ++
2025-01-07 08:41:12 +02:00
val = abcex . Encode ( valAbc , abcex . BASE62 )
2024-07-18 12:40:53 +03:00
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 {
2024-07-18 22:53:43 +03:00
val = "0"
2024-07-18 12:40:53 +03:00
}
2025-01-07 08:41:12 +02:00
valAbc := abcex . Decode ( val , abcex . BASE62 )
2024-07-18 12:40:53 +03:00
valAbc --
2025-01-07 08:41:12 +02:00
val = abcex . Encode ( valAbc , abcex . BASE62 )
2024-07-18 12:40:53 +03:00
os . WriteFile ( path . Join ( fullPath ... ) , [ ] byte ( string ( val ) ) , 0644 )
err := db . Refresh ( )
if err != nil {
return false
}
return true
}
2024-07-18 13:53:28 +03:00
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
}
2024-08-08 15:23:03 +03:00
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 {
2025-01-07 08:41:12 +02:00
idInt := abcex . Decode ( id . Name ( ) , abcex . BASE62 )
2024-08-08 15:23:03 +03:00
if maxId <= idInt {
maxId = idInt
notEmpty = true
}
}
if notEmpty {
maxId ++
}
2025-01-07 08:41:12 +02:00
fullPath = append ( fullPath , abcex . Encode ( maxId , abcex . BASE62 ) )
fmt . Println ( fullPath , maxId , abcex . Encode ( maxId , abcex . BASE62 ) )
2024-08-08 15:23:03 +03:00
err = os . Mkdir ( path . Join ( fullPath ... ) , 0750 )
if err != nil {
return "" , err
}
err = db . Refresh ( )
if err != nil {
return "" , err
}
2025-01-07 08:41:12 +02:00
return abcex . Encode ( maxId , abcex . BASE62 ) , nil
2024-08-08 15:23:03 +03:00
}
2025-03-16 02:18:12 +02:00
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
}
2025-03-16 21:22:31 +02:00
func ( db * DB ) GetRandomNode ( vpath string ) ( Node , error ) {
2025-03-16 02:18:12 +02:00
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
}
2025-07-10 21:53:25 +03:00
2025-07-11 21:06:20 +03:00
func ( db * DB ) ExecuteCommands ( commands string , lastPath * string , lastKey * string ) error {
2025-07-10 21:53:25 +03:00
return ExecuteCommands ( * db , commands , lastPath , lastKey )
}