jx

package module
v0.0.0-...-1a7279d Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2014 License: MIT Imports: 9 Imported by: 0

README

#jx

jx - a simple json storage engine by Golang.

jx is a storage engine use simple json by kv map encoded as documentation. It provides simple api methods to operate value in storage. It can be used as embedded storage.

Getting Started

jx saves real data in files. so create a *jx.Storage in directory.

import "github.com/fuxiaohei/jx"

s,e := jx.NewStorage("data")
if e != nil{
    panic(e) // remember error
}

It creates storage directory for saving data. But it doesn't load existing data now.

1. Sync

Then sync a struct to storage, so storage can save the struct value.

type User struct {
    Id       int64    `jx:"pk-auto"`
    UserName string 
    Password string
    Email    string 
}

type School struct {
    Id      int64 `jx:"pk"`
    Address string
    Rank    int 
}

e := s.Sync(new(User),new(School))

s.Sync(...) only support struct pointer type.

jx:"pk" means primary key for this field, only support int64, string, float64 type.

jx:"pk-auto" means auto-increment primary key for this field, only support int64 type.Insert

The pk field is unique, so if two "pk" tag in struct, the last one is assigned.

Notice:

When sync a struct, it creates default data files if not exist yet.

Or it reads old data for memory as preparation.

2. Insert

Insert a new struct value into storage:

u := new(User)
u.UserName = "abcdef"
u.Password = "12345678"
u.Email = "abcdef@xyz.com"

e := s.Put(u) // u.Id is auto increasing.

Insert only support struct pointer.

The pk field u.Id is assigned as max id in storage and increasing one by one.

If pk-auto tag, no matter the pk you set in struct, use storage max pk int.

s := new(School)
s.Id = 123
s.Address = "address"
S.Rank = 4

e := s.Put(s) // pk field 
if e == jx.Conflict{
    // means the pk is used in storage.
}
if e == jx.Wrong{
    // means the pk field is wrong value, type error and empty
}

When s.Insert(u), it checks pk field value unique.

Then write data to chunk files.

3. Get

Now only support get value by pk field

u := &User{Id:100}
e := s.Get(u)
if e == jx.Nil{
    println("get no data")
}else{
    println(u.UserName) // if found, field is filled.
}

Get only support struct pointer and by pk field.

If value is not synced, return error.

If value is found, u is filled by value.

4. Update

Only support update value by pk field:

u := new(User)
u.Id = 1
u.UserName = "mnopq"
u.Password = "9876543"
u.Email = "xyz@abc.com"

e := s.Update(u)
5. Delete

Delete value by pk:

u := new(User)
u.Id = 1

e := s.Delete(u)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Nil      = errors.New("nil")
	Conflict = errors.New("conflict")
	Wrong    = errors.New("wrong")
)

Functions

This section is empty.

Types

type Object

type Object struct {
	DataType reflect.Type

	Pk     string
	PkType reflect.Type
	PkAuto bool

	Index map[string]reflect.Type
}

func NewObject

func NewObject(v interface{}) (obj *Object, e error)

create new object from value. pk field need int64,float64 or string. auto pk field need int64. pk field must be set.

type Storage

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

func NewStorage

func NewStorage(directory string) (s *Storage, e error)

create storage in directory. it doesn't load data, util call Sync(...) to load data.

func (*Storage) Delete

func (s *Storage) Delete(v interface{}) (e error)

delete struct value by its pk field.

func (*Storage) Get

func (s *Storage) Get(v interface{}) (e error)

get struct value by its pk field value.

func (*Storage) Insert

func (s *Storage) Insert(v interface{}) (e error)

insert new struct value. it must be synced struct.

func (*Storage) Optimize

func (s *Storage) Optimize() (e error)

optimize storage data. clean deleted data and pk.

func (*Storage) Sync

func (s *Storage) Sync(value ...interface{}) (e error)

sync struct pointer to create table. it parses struct field to create or read table data.

func (*Storage) Table

func (s *Storage) Table(v interface{}) *Table

get struct table. the struct must be synced.

func (*Storage) Tables

func (s *Storage) Tables() map[string]*Table

get all tables in storage. the tables are followed by synced struct objects.

func (*Storage) Update

func (s *Storage) Update(v interface{}) (e error)

update struct value by its pk value

type Table

type Table struct {
	Object *Object

	Chunk *col.Chunk
	Pk    *col.PK
	// contains filtered or unexported fields
}

func NewTable

func NewTable(directory string, obj *Object) (t *Table, e error)

create new table in directory with object definition.

func (*Table) Delete

func (t *Table) Delete(v interface{}) (e error)

delete value in table. delete pk and data in chunk together.

func (*Table) Get

func (t *Table) Get(v interface{}) (e error)

get value by value pk field. it not found, return error Nil.

func (*Table) Insert

func (t *Table) Insert(v interface{}) (e error)

insert value to table. save value to chunk and pk.

func (*Table) Optimize

func (t *Table) Optimize() (e error)

optimize table data. chunk and pk are all optimized.

func (*Table) Update

func (t *Table) Update(v interface{}) (e error)

update value in table. update data and pk together.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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