mirror of
https://github.com/asdine/storm.git
synced 2025-10-06 15:26:49 +08:00
60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
package storm
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"github.com/asdine/storm/index"
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
// Init creates the indexes and buckets for a given structure
|
|
func (n *node) Init(data interface{}) error {
|
|
v := reflect.ValueOf(data)
|
|
cfg, err := extract(&v)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return n.readWriteTx(func(tx *bolt.Tx) error {
|
|
return n.init(tx, cfg)
|
|
})
|
|
}
|
|
|
|
func (n *node) init(tx *bolt.Tx, cfg *structConfig) error {
|
|
bucket, err := n.CreateBucketIfNotExists(tx, cfg.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// save node configuration in the bucket
|
|
err = n.saveMetadata(bucket)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for fieldName, fieldCfg := range cfg.Fields {
|
|
if fieldCfg.Index == "" {
|
|
continue
|
|
}
|
|
switch fieldCfg.Index {
|
|
case tagUniqueIdx:
|
|
_, err = index.NewUniqueIndex(bucket, []byte(indexPrefix+fieldName))
|
|
case tagIdx:
|
|
_, err = index.NewListIndex(bucket, []byte(indexPrefix+fieldName))
|
|
default:
|
|
err = ErrIdxNotFound
|
|
}
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Init creates the indexes and buckets for a given structure
|
|
func (s *DB) Init(data interface{}) error {
|
|
return s.root.Init(data)
|
|
}
|