jsondb/jsondb.go

460 lines
9.8 KiB
Go
Raw Permalink Normal View History

2023-10-18 23:25:04 +02:00
package jsondb
import (
"encoding/json"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
2023-10-24 21:10:49 +02:00
"time"
2023-10-18 23:25:04 +02:00
"git.0x0001f346.de/andreas/utils"
)
type Cluster map[string]Database
2023-10-23 23:36:22 +02:00
type Database map[string]interface{}
2023-10-18 23:25:04 +02:00
var cluster Cluster = Cluster{}
var locationOfCluster string = utils.GetPathToParrentFolderOfThisExecutable()
var nameOfCluster string = "jsondb"
2023-10-24 21:10:49 +02:00
var databaseIsLocked map[string]bool = map[string]bool{}
2023-10-18 23:25:04 +02:00
const databaseExtension string = ".json"
// CreateDatabase creates a database or returns nil if it already exists
2023-10-24 21:10:49 +02:00
func CreateDatabase(nameOfDatabase string) error {
_, databaseAlreadyExists := cluster[nameOfDatabase]
2023-10-18 23:25:04 +02:00
if databaseAlreadyExists {
return nil
}
2023-10-24 21:10:49 +02:00
err := safeDatabase(nameOfDatabase, Database{})
2023-10-18 23:25:04 +02:00
if err != nil {
return err
}
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return nil
}
// DeleteDatabase deletes a database
func DeleteDatabase(nameOfDatabase string) error {
2023-10-24 21:10:49 +02:00
_, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return nil
}
lockDatabase(nameOfDatabase)
err := deleteDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
if err != nil {
return err
}
return nil
}
// DeleteKeyFromDatabase deletes a key in a database and write it to file
func DeleteKeyFromDatabase(nameOfDatabase string, key string) error {
2023-10-24 21:10:49 +02:00
_, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return errors.New("database does not exist")
}
lockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
err := ReloadDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
database, err := getDeepCopyOfDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
delete(database, key)
err = safeDatabase(nameOfDatabase, database)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
cluster[nameOfDatabase] = database
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return nil
}
// GetDatabase returns a database if it exists
2023-10-24 21:10:49 +02:00
func GetDatabase(nameOfDatabase string) (Database, error) {
database, err := getDeepCopyOfDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
if err != nil {
return nil, err
}
return database, nil
}
// GetKeyFromDatabase returns a key from a database if both exist
2023-10-23 23:36:22 +02:00
func GetKeyFromDatabase(nameOfDatabase string, key string) (interface{}, error) {
2023-10-18 23:25:04 +02:00
database, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return nil, errors.New("database does not exist")
}
value, keyExists := database[key]
if !keyExists {
return nil, errors.New("key does not exist")
}
return value, nil
}
// GetLocationOfCluster returns locationOfCluster
func GetLocationOfCluster() string {
return locationOfCluster
}
// GetNameOfCluster returns nameOfCluster
func GetNameOfCluster() string {
return nameOfCluster
}
// LoadCluster loads all databases from the current cluster
func LoadCluster() error {
if !utils.DoesFolderExist(getFullPathToCluster()) {
err := createCluster()
if err != nil {
return err
}
}
for _, p := range utils.GetAllFilesInFolderByExtension(getFullPathToCluster(), databaseExtension) {
databaseName := strings.TrimSuffix(path.Base(p), filepath.Ext(path.Base(p)))
if !isValidName(databaseName) {
continue
}
database, err := loadDatabase(databaseName)
if err != nil {
return err
}
cluster[databaseName] = database
2023-10-24 21:10:49 +02:00
databaseIsLocked[databaseName] = false
2023-10-18 23:25:04 +02:00
}
return nil
}
// ReloadDatabase reloads a database from file
2023-10-24 21:10:49 +02:00
func ReloadDatabase(nameOfDatabase string) error {
_, databaseExists := cluster[nameOfDatabase]
2023-10-18 23:25:04 +02:00
if !databaseExists {
return errors.New("database does not exist")
}
2023-10-24 21:10:49 +02:00
database, err := loadDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
if err != nil {
return err
}
2023-10-24 21:10:49 +02:00
cluster[nameOfDatabase] = database
2023-10-18 23:25:04 +02:00
return nil
}
// RenameDatabase renames a database
func RenameDatabase(nameOfDatabase string, newNameOfDatabase string) error {
2023-10-24 21:10:49 +02:00
_, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return errors.New("database does not exist")
}
lockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
err := ReloadDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
database, err := getDeepCopyOfDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
err = safeDatabase(newNameOfDatabase, database)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
cluster[newNameOfDatabase] = database
2023-10-24 21:10:49 +02:00
lockDatabase(newNameOfDatabase)
2023-10-18 23:25:04 +02:00
2023-10-24 21:10:49 +02:00
err = deleteDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
unlockDatabase(newNameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
2023-10-24 21:10:49 +02:00
unlockDatabase(newNameOfDatabase)
2023-10-18 23:25:04 +02:00
return nil
}
// RenameKeyInDatabase renames a key in a database and write it to file
func RenameKeyInDatabase(nameOfDatabase string, keyOld string, keyNew string) error {
2023-10-24 21:10:49 +02:00
_, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return errors.New("database does not exist")
}
lockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
err := ReloadDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
database, err := getDeepCopyOfDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
database[keyNew] = database[keyOld]
delete(database, keyOld)
err = safeDatabase(nameOfDatabase, database)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
cluster[nameOfDatabase] = database
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return nil
}
// SetDatabase sets a database and writes it to file
2023-10-24 21:10:49 +02:00
func SetDatabase(nameOfDatabase string, database Database) error {
_, databaseExists := cluster[nameOfDatabase]
2023-10-18 23:25:04 +02:00
if !databaseExists {
return errors.New("database does not exist")
}
2023-10-24 21:10:49 +02:00
lockDatabase(nameOfDatabase)
err := safeDatabase(nameOfDatabase, database)
2023-10-18 23:25:04 +02:00
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
2023-10-24 21:10:49 +02:00
cluster[nameOfDatabase] = database
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return nil
}
// SetKeyInDatabase sets a key from in a database and write it to file
2023-10-23 23:36:22 +02:00
func SetKeyInDatabase(nameOfDatabase string, key string, value interface{}) error {
2023-10-24 21:10:49 +02:00
_, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return errors.New("database does not exist")
}
lockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
err := ReloadDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
database, err := getDeepCopyOfDatabase(nameOfDatabase)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
database[key] = value
err = safeDatabase(nameOfDatabase, database)
if err != nil {
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return err
}
cluster[nameOfDatabase] = database
2023-10-24 21:10:49 +02:00
unlockDatabase(nameOfDatabase)
2023-10-18 23:25:04 +02:00
return nil
}
// SetLocationOfCluster sets locationOfCluster if p is a valid folder
func SetLocationOfCluster(location string) error {
if !utils.DoesFolderExist(location) {
return errors.New("not a valid cluster location")
}
locationOfCluster = location
return nil
}
// SetNameOfCluster sets nameOfCluster if s is a valid cluster name
func SetNameOfCluster(name string) error {
if !isValidName(name) {
return errors.New("not a valid cluster name")
}
nameOfCluster = name
return nil
}
2023-10-24 21:10:49 +02:00
func createCluster() error {
if utils.DoesFolderExist(getFullPathToCluster()) {
return nil
}
err := utils.CreateFolder(getFullPathToCluster())
if err != nil {
return err
}
return nil
}
func deleteDatabase(nameOfDatabase string) error {
_, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return nil
}
err := os.Remove(getFullPathToDatabase(nameOfDatabase))
if err != nil {
return err
}
delete(cluster, nameOfDatabase)
delete(databaseIsLocked, nameOfDatabase)
return nil
}
func getDeepCopyOfDatabase(nameOfDatabase string) (Database, error) {
originalDatabase, databaseExists := cluster[nameOfDatabase]
if !databaseExists {
return nil, errors.New("database does not exist")
}
copyOfDatabase := make(Database)
for key, value := range originalDatabase {
copyOfDatabase[key] = value
}
return copyOfDatabase, nil
}
2023-10-18 23:25:04 +02:00
func getFullPathToCluster() string {
return filepath.Join(
locationOfCluster,
nameOfCluster,
)
}
2023-10-24 21:10:49 +02:00
func getFullPathToDatabase(nameOfDatabase string) string {
2023-10-18 23:25:04 +02:00
return filepath.Join(
getFullPathToCluster(),
2023-10-24 21:10:49 +02:00
fmt.Sprintf("%s%s", nameOfDatabase, databaseExtension),
2023-10-18 23:25:04 +02:00
)
}
func isValidName(s string) bool {
if len(s) == 0 {
return false
}
whitelist := utils.BuildWhitelistForStringSanitisation(
[]string{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
},
)
return !utils.DoesStringContainsNonWhitelistedSubstrings(s, whitelist)
}
2023-10-24 21:10:49 +02:00
func loadDatabase(nameOfDatabase string) (Database, error) {
2023-10-18 23:25:04 +02:00
dbContentAsString, err := utils.LoadStringFromFile(
2023-10-24 21:10:49 +02:00
getFullPathToDatabase(nameOfDatabase),
2023-10-18 23:25:04 +02:00
)
if err != nil {
return Database{}, err
}
var database Database
err = json.Unmarshal([]byte(dbContentAsString), &database)
if err != nil {
return Database{}, err
}
return database, nil
}
2023-10-24 21:10:49 +02:00
func lockDatabase(nameOfDatabase string) {
waitForDatabaseToUnlock(nameOfDatabase)
databaseIsLocked[nameOfDatabase] = true
}
func safeDatabase(nameOfDatabase string, database Database) error {
2023-10-18 23:25:04 +02:00
databaseAsJSONBytes, err := json.MarshalIndent(database, "", "\t")
if err != nil {
2023-10-24 21:10:49 +02:00
return errors.New("could not marshal database: " + nameOfDatabase)
2023-10-18 23:25:04 +02:00
}
2023-10-24 21:10:49 +02:00
err = utils.WriteStringToFile(getFullPathToDatabase(nameOfDatabase), string(databaseAsJSONBytes))
2023-10-18 23:25:04 +02:00
if err != nil {
2023-10-24 21:10:49 +02:00
return errors.New("could not write to file: " + getFullPathToDatabase(nameOfDatabase))
2023-10-18 23:25:04 +02:00
}
return nil
}
2023-10-24 21:10:49 +02:00
func unlockDatabase(nameOfDatabase string) {
databaseIsLocked[nameOfDatabase] = false
}
func waitForDatabaseToUnlock(nameOfDatabase string) {
isLocked, databaseExists := databaseIsLocked[nameOfDatabase]
2023-10-18 23:25:04 +02:00
if !databaseExists {
2023-10-24 21:10:49 +02:00
return
2023-10-18 23:25:04 +02:00
}
2023-10-24 21:10:49 +02:00
for {
if !isLocked {
break
}
2023-10-18 23:25:04 +02:00
2023-10-24 21:10:49 +02:00
time.Sleep(5 * time.Millisecond)
isLocked = databaseIsLocked[nameOfDatabase]
}
2023-10-18 23:25:04 +02:00
}