flydb

package module
v0.0.0-...-45a1217 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2015 License: MIT Imports: 10 Imported by: 0

README

flydb

Travis Go doc GitHub license

In progress...

Pure Go database with simple format.

Features

  • Support different data file format: JSON, YAML, XML
  • Powerful access API

Limits

  • Performance: flydb is not built for performance, so you may not want to use it with huge data

Usage

Embed in Golang
package main

import (
    "github.com/flydb/flydb"
    "log"
)

func main() {
    db, err := flydb.Open("/path/to/db.json")
    if err != nil {
        fmt.Errorf("cannot open database")
    }
    email := db.Root().MustGet("users.3.email").MustVallue().MustString()
    log.Println(email)
}
HTTP Server

See examples/server/main.go.

HTTP API
Get
curl http://127.0.0.1:8080/users/1/email
Set
curl -x PUT -D '{"key": "value"}' http://127.0.0.1:8080/users/1/metadata
Delete
curl -x DELETE http://127.0.0.1:8080/users/1/metadata

Internal

When you are using flydb as a Golang library, it's very helpful to understand it's internal.

Node

Data is saved as a tree like structure in memory, you can find and modify every node in the tree.

We only care about three types of data:

  • map: map[string]interface{} in Golang
  • array: []interface{} in Golang
  • value: string, int, float32, bool, nil in Golang
Working with Node

Create a node from raw data:

rootNode, err := flydb.CreateNode(rawdata)

To get the value of specific node, you can:

  1. Find the node

    node, err := rootNode.Get("books.3.title")
    
  2. Assert node type

    valueNode, err := node.Value()
    // arrayNode, err := node.Array()
    // mapNode, err := node.Map()
    
  3. Assert exact value type

    title, err := typedNode.String()
    

If you know exact structure of your data, you can simply write in one line:

title := rootNode.MustGet("books.3.title").MustValue().MustString()

To update value of specific node, you can use the Set method:

rootNode.Set("books.3.date", "2015-01-01")
Format

The in memory node data can be transfered between different formats: JSON, YAML, XML and many other custom formats.

Load from formated data:

jsonData := `
{
    "key": "value",
    "key1": "value1"
}
`

format := flydb.GetFormat("json")

rawData, err := format.Unmarshal(jsonData)
node, err := flydb.CreateNode(rawData)

Export node with format:

rawData := node.GetRaw()
format := flydb.GetFormat("yaml")
yamlData, err := format.Marshal(rawData)
println(string(yamlData))

TODO

  • XML format support
  • Query API

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterFormat

func RegisterFormat(name string, format Format)

func Serve

func Serve(path, listen string) error

Types

type ArrayNode

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

Array node contains list of nodes

func NewArrayNode

func NewArrayNode(v []interface{}) (*ArrayNode, error)

func (*ArrayNode) Append

func (this *ArrayNode) Append(data interface{}) error

func (*ArrayNode) Delete

func (this *ArrayNode) Delete(key int)

func (*ArrayNode) Get

func (this *ArrayNode) Get(i int) (*Node, error)

func (*ArrayNode) GetRaw

func (this *ArrayNode) GetRaw() interface{}

func (*ArrayNode) Length

func (this *ArrayNode) Length() int

func (*ArrayNode) Set

func (this *ArrayNode) Set(i int, data interface{}) error

func (*ArrayNode) SetRaw

func (this *ArrayNode) SetRaw(raw interface{}) error

type Config

type Config struct {
	Path       string
	FormatName string
	Format     Format
	Save       bool
	// save database every `SaveInterval` milliseconds
	SaveInterval int
}

type Database

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

func Memory

func Memory() *Database

Create an in memory database

func New

func New(config Config) *Database

func Open

func Open(path string) (*Database, error)

func (*Database) Close

func (this *Database) Close() error

Close database

func (*Database) Flush

func (this *Database) Flush()

Flush changes to disk

func (*Database) Open

func (this *Database) Open() error

func (*Database) Root

func (this *Database) Root() *Node

func (*Database) Save

func (this *Database) Save() error

func (*Database) SaveAs

func (this *Database) SaveAs(path string) error

Save database as another file

func (*Database) SaveAsFormat

func (this *Database) SaveAsFormat(path string, format interface{}) error

type Format

type Format interface {
	Extensions() []string
	Marshal(v interface{}) ([]byte, error)
	Unmarshal(b []byte) (interface{}, error)
}

func CheckFormatByExtension

func CheckFormatByExtension(ext string) Format

func GetFormat

func GetFormat(name string) Format

func GuessFormat

func GuessFormat(format interface{}) Format

type JsonFormat

type JsonFormat struct {
}

func (*JsonFormat) Extensions

func (this *JsonFormat) Extensions() []string

func (*JsonFormat) Marshal

func (this *JsonFormat) Marshal(v interface{}) ([]byte, error)

func (*JsonFormat) Unmarshal

func (this *JsonFormat) Unmarshal(b []byte) (interface{}, error)

type MapNode

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

func NewMapNode

func NewMapNode(v map[string]interface{}) (*MapNode, error)

func (*MapNode) Delete

func (this *MapNode) Delete(k string)

func (*MapNode) Get

func (this *MapNode) Get(k string) (*Node, bool)

func (*MapNode) GetRaw

func (this *MapNode) GetRaw() interface{}

func (*MapNode) Has

func (this *MapNode) Has(k string) bool

func (*MapNode) Set

func (this *MapNode) Set(k string, data interface{}) error

func (*MapNode) SetRaw

func (this *MapNode) SetRaw(raw interface{}) error

type Node

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

Wrapper for all types of node

func CreateNode

func CreateNode(data interface{}) (*Node, error)

func (*Node) Array

func (this *Node) Array() (*ArrayNode, error)

Convert to array node

func (*Node) Delete

func (this *Node) Delete(path interface{}) error

Delete node by path

func (*Node) Get

func (this *Node) Get(path interface{}) (*Node, error)

Get node with path

func (*Node) GetRaw

func (this *Node) GetRaw() interface{}

func (*Node) Has

func (this *Node) Has(path interface{}) bool

Check path existance

func (*Node) IsArray

func (this *Node) IsArray() bool

Check if this is an array node

func (*Node) IsMap

func (this *Node) IsMap() bool

Check if this is a map node

func (*Node) IsValue

func (this *Node) IsValue() bool

Check if this is a value node

func (*Node) Map

func (this *Node) Map() (*MapNode, error)

Convert to map node

func (*Node) MustArray

func (this *Node) MustArray() *ArrayNode

func (*Node) MustGet

func (this *Node) MustGet(path interface{}) *Node

func (*Node) MustMap

func (this *Node) MustMap() *MapNode

func (*Node) MustValue

func (this *Node) MustValue() *ValueNode

func (*Node) Set

func (this *Node) Set(path interface{}, v interface{}) error

Set node value by path

func (*Node) Value

func (this *Node) Value() (*ValueNode, error)

Convert to value node

type Server

type Server struct {
	Database *Database
}

func (*Server) DoDelete

func (this *Server) DoDelete(w http.ResponseWriter, r *http.Request)

func (*Server) DoGet

func (this *Server) DoGet(w http.ResponseWriter, r *http.Request)

func (*Server) DoSet

func (this *Server) DoSet(w http.ResponseWriter, r *http.Request)

func (*Server) Error

func (this *Server) Error(w http.ResponseWriter, err error)

func (*Server) GetRequestPath

func (this *Server) GetRequestPath(r *http.Request) string

func (*Server) Serve

func (this *Server) Serve(listen string) error

type ValueNode

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

func NewValueNode

func NewValueNode(v interface{}) (*ValueNode, error)

func (*ValueNode) Bool

func (this *ValueNode) Bool() (bool, error)

func (*ValueNode) Float32

func (this *ValueNode) Float32() (float32, error)

func (*ValueNode) Float64

func (this *ValueNode) Float64() (float64, error)

func (*ValueNode) GetRaw

func (this *ValueNode) GetRaw() interface{}

func (*ValueNode) Int

func (this *ValueNode) Int() (int, error)

func (*ValueNode) Int32

func (this *ValueNode) Int32() (int32, error)

func (*ValueNode) Int64

func (this *ValueNode) Int64() (int64, error)

func (*ValueNode) IsNull

func (this *ValueNode) IsNull() bool

func (*ValueNode) MustBool

func (this *ValueNode) MustBool() bool

func (*ValueNode) MustFloat32

func (this *ValueNode) MustFloat32() float32

func (*ValueNode) MustFloat64

func (this *ValueNode) MustFloat64() float64

func (*ValueNode) MustInt

func (this *ValueNode) MustInt() int

func (*ValueNode) MustInt32

func (this *ValueNode) MustInt32() int32

func (*ValueNode) MustInt64

func (this *ValueNode) MustInt64() int64

func (*ValueNode) MustString

func (this *ValueNode) MustString() string

func (*ValueNode) SetRaw

func (this *ValueNode) SetRaw(v interface{}) error

func (*ValueNode) String

func (this *ValueNode) String() (string, error)

type WrapNode

type WrapNode interface {
	SetRaw(interface{}) error
	GetRaw() interface{}
}

func CreateWrapNode

func CreateWrapNode(data interface{}) (WrapNode, error)

func CreateWrapNodeFromRawData

func CreateWrapNodeFromRawData(rawData interface{}) (WrapNode, error)

type XMLFormat

type XMLFormat struct {
}

func (*XMLFormat) Extensions

func (this *XMLFormat) Extensions() []string

func (*XMLFormat) Marshal

func (this *XMLFormat) Marshal(v interface{}) ([]byte, error)

func (*XMLFormat) Unmarshal

func (this *XMLFormat) Unmarshal(b []byte) (interface{}, error)

type YamlFormat

type YamlFormat struct {
}

func (*YamlFormat) Extensions

func (this *YamlFormat) Extensions() []string

func (*YamlFormat) Marshal

func (this *YamlFormat) Marshal(v interface{}) ([]byte, error)

func (*YamlFormat) Unmarshal

func (this *YamlFormat) Unmarshal(b []byte) (interface{}, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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