db

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 13 Imported by: 0

README

DataBase

donation link

A simple database that anyone can build off of.

Notice: This Module Is Currently In Beta!

Notice: For Any Version 0.0 And Below, DataBase Files May Not Be Compatible With Newer Versions!

Note: If you have any name suggestions, please share.

Installation

go get https://github.com/AspieSoft/db

Usage


import "github.com/AspieSoft/db"

func main(){
  myDB, err := db.Open("path/to/file.db")
  defer myDB.Close()

  myDB.AddTable("MyTable")
  myTable, err := myDB.GetTable("MyTable")
  myTable.AddRow("Row1", "val1")
  myTable.AddRow("Row2", "val2")
  row1, err := myTable.GetRow("Row1")

  fmt.Println("key": row1.Key)
  fmt.Println("value": row1.Value)

  myDB.Optimize()
}

Custom Database


import db "github.com/AspieSoft/db/custom"

func main(){
  myDB, err := db.Open("path/to/file.db", 1024, []byte{'$', ':'})
  defer myDB.Close()

  // a custom database is similar, but it does not come with any data structure, such as tables and rows
  // instead, you can build these yourself using some of the core methods for reading and writing to the database

  db.AddDataObj(myDB, '$', []byte("MyTable"), []byte{})

  db.File.Seek(0, io.SeekStart)
  myTable, err := db.GetDataObj(myDB, '$', []byte("MyTable"), []byte{0}) // a prefix of 0 allows a regex match, or blank to match all

  db.File.Seek(myTable.Line * int64(myDB.BitSize), io.SeekStart)
  db.SetDataObj(myDB, '$', []byte("MyTable"), []byte("MyRowList"))

  db.File.Seek(myTable.Line * int64(myDB.BitSize), io.SeekStart)
  db.DelDataObj(myDB, '$')

  //note: to have tabes map to rows, you will need to handle this manually
  //recommended: create your own module as a wrapper for your custom database using this module
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DebugMode = false

Functions

This section is empty.

Types

type Data

type Data struct {
	Key   string
	Value string
	// contains filtered or unexported fields
}

func (*Data) Del

func (data *Data) Del(noLock ...bool) error

Del removes the key value pair from the database

func (*Data) SetValue

func (data *Data) SetValue(value string, noLock ...bool) error

SetValue changes the value of the row

type Database

type Database struct {
	// contains filtered or unexported fields
}

func Open

func Open(path string, encKey []byte, bitSize ...uint16) (*Database, error)

Open opens an existing database or creates a new one

@bitSize tells the database what bit size to use (this value must always be consistent)

  • (default: 128)
  • (0 = default 128)
  • (min = 64)
  • (max = 64000)

note: in debug mode, (min = 16)

func (*Database) AddData

func (db *Database) AddData(key string, value string, noLock ...bool) (*Data, error)

AddData adds a new key value pair to the database

this method returns the new data

func (*Database) AddTable

func (db *Database) AddTable(name string, noLock ...bool) (*Table, error)

AddTable adds a new table to the database

this method returns the new table

func (*Database) Close

func (db *Database) Close() error

Close closes the database file

func (*Database) FindData

func (db *Database) FindData(key []byte, value []byte, noLock ...bool) ([]*Data, error)

FindData allows you to do a more complex search for a list of key value pairs

for security, you can authorize a regex search by setting the first byte of the name to 0

if you include nothing else, or a '*' as the second byte, the name will match all tables

anything else with a 0 as first byte will run an RE2 regex match

if you are dealing with user input, it is recommended to sanitize it and remove the first byte of 0, to ensure the input cannot run regex, and will be treated as a literal string

func (*Database) FindTables

func (db *Database) FindTables(name []byte, noLock ...bool) ([]*Table, error)

FindTables allows you to do a more complex search for a list of tables

for security, you can authorize a regex search by setting the first byte of the name to 0

if you include nothing else, or a '*' as the second byte, the name will match all tables

anything else with a 0 as first byte will run an RE2 regex match

if you are dealing with user input, it is recommended to sanitize it and remove the first byte of 0, to ensure the input cannot run regex, and will be treated as a literal string

func (*Database) GetData

func (db *Database) GetData(key string, noLock ...bool) (*Data, error)

GetData retrieves an existing key value pair from the database

func (*Database) GetTable

func (db *Database) GetTable(name string, noLock ...bool) (*Table, error)

GetTable retrieves an existing table from the database

func (*Database) Optimize

func (db *Database) Optimize() error

Optimize will optimize a database file by cloning the tables and their rows to a new file

this method will remove any orphaned data (rows without a table, etc), and will move existing tables to the top of the database file for quicker access

row indexes are referenced from the tables, so having tables at the top is best for performance

type Row

type Row struct {
	Key   string
	Value string
	// contains filtered or unexported fields
}

func (*Row) Del

func (row *Row) Del(noLock ...bool) error

DelRow removes the key value pair from the table

func (*Row) Rename

func (row *Row) Rename(key string, noLock ...bool) error

Rename changes the key of the row

func (*Row) SetValue

func (row *Row) SetValue(value string, noLock ...bool) error

SetValue changes the value of the row

type Table

type Table struct {
	Name string
	// contains filtered or unexported fields
}

func (*Table) AddRow

func (table *Table) AddRow(key string, value string, noLock ...bool) (*Row, error)

AddRow adds a new key value pair to the table

this method returns the new row

func (*Table) Del

func (table *Table) Del(noLock ...bool) error

Del removes the table from the database

func (*Table) FindRows

func (table *Table) FindRows(key []byte, value []byte, noLock ...bool) ([]*Row, error)

FindRows allows you to do a more complex search for a list of rows

for security, you can authorize a regex search by setting the first byte to 0 (for both the key and the value)

if you include nothing else, or a '*' as the second byte, the key/value will match all rows

anything else with a 0 as first byte will run an RE2 regex match

if you are dealing with user input, it is recommended to sanitize it and remove the first byte of 0, to ensure the input cannot run regex, and will be treated as a literal string

func (*Table) GetRow

func (table *Table) GetRow(key string, noLock ...bool) (*Row, error)

GetRow retrieves an existing row from the table

func (*Table) Rename

func (table *Table) Rename(name string, noLock ...bool) error

Rename changes the name of the table

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL