2020-12-01 20:06:49 +01:00
|
|
|
package bolt
|
|
|
|
|
2021-04-09 23:02:19 +02:00
|
|
|
import (
|
2021-05-08 19:25:00 +02:00
|
|
|
"encoding/json"
|
2021-05-07 12:08:34 +02:00
|
|
|
"fmt"
|
2021-05-08 19:25:00 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/db"
|
2021-04-09 23:02:19 +02:00
|
|
|
"github.com/ansible-semaphore/semaphore/util"
|
2021-05-07 12:08:34 +02:00
|
|
|
"go.etcd.io/bbolt"
|
2021-05-08 19:25:00 +02:00
|
|
|
"reflect"
|
|
|
|
"sort"
|
2021-04-09 23:02:19 +02:00
|
|
|
)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
|
|
|
|
type enumerable interface {
|
|
|
|
First() (key []byte, value []byte)
|
|
|
|
Next() (key []byte, value []byte)
|
|
|
|
}
|
|
|
|
|
2021-05-14 00:37:45 +02:00
|
|
|
type emptyEnumerable struct {}
|
|
|
|
|
|
|
|
func (d emptyEnumerable) First() (key []byte, value []byte) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d emptyEnumerable) Next() (key []byte, value []byte) {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2021-05-08 22:25:31 +02:00
|
|
|
|
2021-04-09 23:02:19 +02:00
|
|
|
type BoltDb struct {
|
2021-05-14 13:47:32 +02:00
|
|
|
Filename string
|
2021-05-07 12:08:34 +02:00
|
|
|
db *bbolt.DB
|
2020-12-01 20:06:49 +01:00
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
type objectID interface {
|
|
|
|
ToBytes() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type intObjectID int
|
|
|
|
type strObjectID string
|
|
|
|
|
|
|
|
func (d intObjectID) ToBytes() []byte {
|
|
|
|
return []byte(fmt.Sprintf("%010d", d))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d strObjectID) ToBytes() []byte {
|
|
|
|
return []byte(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeObjectId(ids ...int) []byte {
|
|
|
|
n := len(ids)
|
|
|
|
|
|
|
|
id := ""
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if id != "" {
|
|
|
|
id += "_"
|
|
|
|
}
|
|
|
|
id += fmt.Sprintf("%010d", ids[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(id)
|
|
|
|
}
|
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
func makeBucketId(props db.ObjectProperties, ids ...int) []byte {
|
2021-05-07 12:08:34 +02:00
|
|
|
n := len(ids)
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
id := props.TableName
|
2021-05-09 22:08:10 +02:00
|
|
|
|
|
|
|
if !props.IsGlobal {
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
id += fmt.Sprintf("_%010d", ids[i])
|
|
|
|
}
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 19:25:00 +02:00
|
|
|
return []byte(id)
|
2021-05-07 12:08:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *BoltDb) Migrate() error {
|
|
|
|
return nil
|
2020-12-01 20:06:49 +01:00
|
|
|
}
|
|
|
|
|
2021-04-09 23:02:19 +02:00
|
|
|
func (d *BoltDb) Connect() error {
|
2021-05-14 13:47:32 +02:00
|
|
|
var filename string
|
|
|
|
if d.Filename == "" {
|
|
|
|
config, err := util.Config.GetDBConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
filename = config.Hostname
|
|
|
|
} else {
|
|
|
|
filename = d.Filename
|
2021-04-09 23:02:19 +02:00
|
|
|
}
|
2021-05-14 13:47:32 +02:00
|
|
|
|
|
|
|
var err error
|
|
|
|
d.db, err = bbolt.Open(filename, 0666, nil)
|
2021-04-09 23:02:19 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-14 13:47:32 +02:00
|
|
|
|
2021-04-09 23:02:19 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-01 20:06:49 +01:00
|
|
|
|
2021-04-09 23:02:19 +02:00
|
|
|
func (d *BoltDb) Close() error {
|
|
|
|
return d.db.Close()
|
|
|
|
}
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
func (d *BoltDb) getObject(bucketID int, props db.ObjectProperties, objectID objectID, object interface{}) (err error) {
|
2021-05-08 19:25:00 +02:00
|
|
|
err = d.db.View(func(tx *bbolt.Tx) error {
|
2021-05-09 22:08:10 +02:00
|
|
|
b := tx.Bucket(makeBucketId(props, bucketID))
|
2021-05-08 19:25:00 +02:00
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
str := b.Get(objectID.ToBytes())
|
2021-05-08 19:25:00 +02:00
|
|
|
if str == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal(str, &object)
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFieldNameByTag(t reflect.Type, tag string, value string) (string, error) {
|
|
|
|
n := t.NumField()
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if t.Field(i).Tag.Get(tag) == value {
|
|
|
|
return t.Field(i).Name, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortObjects(objects interface{}, sortBy string, sortInverted bool) error {
|
|
|
|
objectsValue := reflect.ValueOf(objects).Elem()
|
|
|
|
objType := objectsValue.Type().Elem()
|
2021-05-08 22:25:31 +02:00
|
|
|
|
2021-05-08 19:25:00 +02:00
|
|
|
fieldName, err := getFieldNameByTag(objType, "db", sortBy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.SliceStable(objectsValue.Interface(), func (i, j int) bool {
|
2021-05-08 22:25:31 +02:00
|
|
|
valueI := objectsValue.Index(i).FieldByName(fieldName)
|
|
|
|
valueJ := objectsValue.Index(j).FieldByName(fieldName)
|
|
|
|
|
|
|
|
less := false
|
|
|
|
|
|
|
|
switch valueI.Kind() {
|
2021-05-08 19:25:00 +02:00
|
|
|
case reflect.Int:
|
|
|
|
case reflect.Int8:
|
|
|
|
case reflect.Int16:
|
|
|
|
case reflect.Int32:
|
|
|
|
case reflect.Int64:
|
|
|
|
case reflect.Uint:
|
|
|
|
case reflect.Uint8:
|
|
|
|
case reflect.Uint16:
|
|
|
|
case reflect.Uint32:
|
|
|
|
case reflect.Uint64:
|
2021-05-08 22:25:31 +02:00
|
|
|
less = valueI.Int() < valueJ.Int()
|
2021-05-08 19:25:00 +02:00
|
|
|
case reflect.Float32:
|
|
|
|
case reflect.Float64:
|
2021-05-08 22:25:31 +02:00
|
|
|
less = valueI.Float() < valueJ.Float()
|
2021-05-08 19:25:00 +02:00
|
|
|
case reflect.String:
|
2021-05-08 22:25:31 +02:00
|
|
|
less = valueI.String() < valueJ.String()
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
2021-05-08 22:25:31 +02:00
|
|
|
|
|
|
|
if sortInverted {
|
|
|
|
less = !less
|
|
|
|
}
|
|
|
|
|
|
|
|
return less
|
2021-05-08 19:25:00 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 00:37:45 +02:00
|
|
|
func createObjectType(t reflect.Type) reflect.Type {
|
2021-05-13 23:17:16 +02:00
|
|
|
if t.Kind() == reflect.Ptr {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
|
|
|
|
|
|
|
n := t.NumField()
|
|
|
|
|
|
|
|
fields := make([]reflect.StructField, n)
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
f := t.Field(i)
|
|
|
|
tag := f.Tag.Get("db")
|
2021-05-14 00:37:45 +02:00
|
|
|
if tag != "" {
|
|
|
|
f.Tag = reflect.StructTag(`json:"` + tag + `"`)
|
|
|
|
} else {
|
|
|
|
if f.Type.Kind() == reflect.Struct {
|
|
|
|
f.Type = createObjectType(f.Type)
|
|
|
|
}
|
2021-05-13 23:17:16 +02:00
|
|
|
}
|
|
|
|
fields[i] = f
|
|
|
|
}
|
|
|
|
|
|
|
|
return reflect.StructOf(fields)
|
|
|
|
}
|
|
|
|
|
|
|
|
func unmarshalObject(data []byte, obj interface{}) error {
|
2021-05-14 00:37:45 +02:00
|
|
|
newType := createObjectType(reflect.TypeOf(obj))
|
2021-05-13 23:17:16 +02:00
|
|
|
ptr := reflect.New(newType).Interface()
|
|
|
|
|
|
|
|
err := json.Unmarshal(data, ptr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
value := reflect.ValueOf(ptr).Elem()
|
|
|
|
|
|
|
|
objValue := reflect.ValueOf(obj).Elem()
|
|
|
|
|
|
|
|
for i := 0; i < newType.NumField(); i++ {
|
|
|
|
objValue.Field(i).Set(value.Field(i))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-14 00:37:45 +02:00
|
|
|
func copyObject(obj interface{}, newType reflect.Type) interface{} {
|
2021-05-13 23:17:16 +02:00
|
|
|
newValue := reflect.New(newType).Elem()
|
|
|
|
|
|
|
|
oldValue := reflect.ValueOf(obj)
|
|
|
|
|
|
|
|
for i := 0; i < newType.NumField(); i++ {
|
2021-05-14 00:37:45 +02:00
|
|
|
var v interface{}
|
|
|
|
if newValue.Field(i).Kind() == reflect.Struct &&
|
|
|
|
newValue.Field(i).Type().PkgPath() == "" {
|
|
|
|
v = copyObject(oldValue.Field(i).Interface(), newValue.Field(i).Type())
|
|
|
|
} else {
|
|
|
|
v = oldValue.Field(i).Interface()
|
|
|
|
}
|
|
|
|
newValue.Field(i).Set(reflect.ValueOf(v))
|
2021-05-13 23:17:16 +02:00
|
|
|
}
|
|
|
|
|
2021-05-14 00:37:45 +02:00
|
|
|
return newValue.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
func marshalObject(obj interface{}) ([]byte, error) {
|
|
|
|
newType := createObjectType(reflect.TypeOf(obj))
|
|
|
|
return json.Marshal(copyObject(obj, newType))
|
2021-05-13 23:17:16 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
func unmarshalObjects(rawData enumerable, props db.ObjectProperties, params db.RetrieveQueryParams, filter func(interface{}) bool, objects interface{}) (err error) {
|
2021-05-08 19:25:00 +02:00
|
|
|
objectsValue := reflect.ValueOf(objects).Elem()
|
|
|
|
objType := objectsValue.Type().Elem()
|
|
|
|
|
2021-05-14 01:23:23 +02:00
|
|
|
objectsValue.Set(reflect.MakeSlice(objectsValue.Type(), 0, 0))
|
|
|
|
|
2021-05-13 00:56:31 +02:00
|
|
|
i := 0 // offset counter
|
2021-05-08 22:25:31 +02:00
|
|
|
n := 0 // number of added items
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
for k, v := rawData.First(); k != nil; k, v = rawData.Next() {
|
2021-05-13 00:56:31 +02:00
|
|
|
if params.Offset > 0 && i < params.Offset {
|
|
|
|
i++
|
2021-05-08 22:25:31 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
tmp := reflect.New(objType)
|
|
|
|
ptr := tmp.Interface()
|
2021-05-13 23:17:16 +02:00
|
|
|
err = unmarshalObject(v, ptr)
|
2021-05-13 21:45:54 +02:00
|
|
|
obj := reflect.ValueOf(ptr).Elem().Interface()
|
|
|
|
|
|
|
|
if err != nil {
|
2021-05-13 23:17:16 +02:00
|
|
|
return
|
2021-05-08 22:25:31 +02:00
|
|
|
}
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
if filter != nil {
|
|
|
|
if !filter(obj) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
newObjectValues := reflect.Append(objectsValue, reflect.ValueOf(obj))
|
|
|
|
objectsValue.Set(newObjectValues)
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
n++
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-14 13:47:32 +02:00
|
|
|
if params.Count > 0 && n > params.Count {
|
2021-05-08 22:25:31 +02:00
|
|
|
break
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
2021-05-08 22:25:31 +02:00
|
|
|
}
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
sortable := false
|
|
|
|
|
2021-05-08 22:25:31 +02:00
|
|
|
if params.SortBy != "" {
|
2021-05-09 22:08:10 +02:00
|
|
|
for _, v := range props.SortableColumns {
|
|
|
|
if v == params.SortBy {
|
|
|
|
sortable = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if sortable {
|
2021-05-08 22:25:31 +02:00
|
|
|
err = sortObjects(objects, params.SortBy, params.SortInverted)
|
|
|
|
}
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-14 00:37:45 +02:00
|
|
|
|
2021-05-08 19:25:00 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-13 15:49:32 +02:00
|
|
|
func (d *BoltDb) getObjects(bucketID int, props db.ObjectProperties, params db.RetrieveQueryParams, filter func(interface{}) bool, objects interface{}) error {
|
2021-05-08 22:25:31 +02:00
|
|
|
return d.db.View(func(tx *bbolt.Tx) error {
|
2021-05-09 22:08:10 +02:00
|
|
|
b := tx.Bucket(makeBucketId(props, bucketID))
|
2021-05-14 00:37:45 +02:00
|
|
|
var c enumerable
|
2021-05-13 16:33:04 +02:00
|
|
|
if b == nil {
|
2021-05-14 00:37:45 +02:00
|
|
|
c = emptyEnumerable{}
|
|
|
|
} else {
|
|
|
|
c = b.Cursor()
|
2021-05-13 16:33:04 +02:00
|
|
|
}
|
2021-05-13 15:49:32 +02:00
|
|
|
return unmarshalObjects(c, props, params, filter, objects)
|
2021-05-08 22:25:31 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:25:00 +02:00
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
func (d *BoltDb) isObjectInUse(bucketID int, props db.ObjectProperties, objectID objectID) (inUse bool, err error) {
|
2021-05-14 14:31:24 +02:00
|
|
|
return false, nil
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
func (d *BoltDb) deleteObject(bucketID int, props db.ObjectProperties, objectID objectID) error {
|
|
|
|
inUse, err := d.isObjectInUse(bucketID, props, objectID)
|
2021-05-08 19:25:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if inUse {
|
|
|
|
return db.ErrInvalidOperation
|
|
|
|
}
|
|
|
|
|
|
|
|
return d.db.Update(func (tx *bbolt.Tx) error {
|
2021-05-14 14:31:24 +02:00
|
|
|
b := tx.Bucket(makeBucketId(props, bucketID))
|
2021-05-08 19:25:00 +02:00
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
2021-05-09 22:08:10 +02:00
|
|
|
return b.Delete(objectID.ToBytes())
|
2021-05-08 19:25:00 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
func (d *BoltDb) deleteObjectSoft(bucketID int, props db.ObjectProperties, objectID objectID) error {
|
|
|
|
return d.deleteObject(bucketID, props, objectID)
|
2021-05-08 19:25:00 +02:00
|
|
|
}
|
2021-05-08 22:25:31 +02:00
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
// updateObject updates data for object in database.
|
|
|
|
func (d *BoltDb) updateObject(bucketID int, props db.ObjectProperties, object interface{}) error {
|
2021-05-08 22:25:31 +02:00
|
|
|
return d.db.Update(func(tx *bbolt.Tx) error {
|
2021-05-09 22:08:10 +02:00
|
|
|
b := tx.Bucket(makeBucketId(props, bucketID))
|
2021-05-08 22:25:31 +02:00
|
|
|
if b == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
idValue := reflect.ValueOf(object).FieldByName("ID")
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
id := makeObjectId(int(idValue.Int()))
|
2021-05-08 22:25:31 +02:00
|
|
|
if b.Get(id) == nil {
|
|
|
|
return db.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-05-13 23:17:16 +02:00
|
|
|
str, err := marshalObject(object)
|
2021-05-08 22:25:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Put(id, str)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object interface{}) (interface{}, error) {
|
2021-05-08 22:25:31 +02:00
|
|
|
err := d.db.Update(func(tx *bbolt.Tx) error {
|
2021-05-09 22:08:10 +02:00
|
|
|
b, err2 := tx.CreateBucketIfNotExists(makeBucketId(props, bucketID))
|
2021-05-08 22:25:31 +02:00
|
|
|
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
objPtr := reflect.ValueOf(&object).Elem()
|
2021-05-09 22:08:10 +02:00
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
tmpObj := reflect.New(objPtr.Elem().Type()).Elem()
|
|
|
|
tmpObj.Set(objPtr.Elem())
|
|
|
|
|
|
|
|
idValue := tmpObj.FieldByName("ID")
|
|
|
|
var objectID objectID
|
|
|
|
idKind := idValue.Kind()
|
|
|
|
switch {
|
|
|
|
case idKind >= reflect.Int && idKind <= reflect.Uint64:
|
2021-05-09 22:08:10 +02:00
|
|
|
if idValue.Int() == 0 {
|
|
|
|
id, err2 := b.NextSequence()
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
idValue.SetInt(int64(id))
|
|
|
|
}
|
2021-05-13 21:45:54 +02:00
|
|
|
objectID = intObjectID(idValue.Int())
|
|
|
|
case idKind == reflect.String:
|
2021-05-09 22:08:10 +02:00
|
|
|
if idValue.String() == "" {
|
|
|
|
return fmt.Errorf("object ID can not be empty string")
|
|
|
|
}
|
|
|
|
objectID = strObjectID(idValue.String())
|
2021-05-14 00:37:45 +02:00
|
|
|
case idKind == reflect.Invalid:
|
|
|
|
id, err2 := b.NextSequence()
|
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
objectID = intObjectID(id)
|
2021-05-09 22:08:10 +02:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported ID type")
|
|
|
|
}
|
2021-05-08 22:25:31 +02:00
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
if objectID == nil {
|
|
|
|
return fmt.Errorf("object ID can not be nil")
|
|
|
|
}
|
2021-05-08 22:25:31 +02:00
|
|
|
|
2021-05-13 21:45:54 +02:00
|
|
|
|
|
|
|
objPtr.Set(tmpObj)
|
2021-05-14 00:37:45 +02:00
|
|
|
str, err2 := marshalObject(object)
|
2021-05-08 22:25:31 +02:00
|
|
|
if err2 != nil {
|
|
|
|
return err2
|
|
|
|
}
|
|
|
|
|
2021-05-09 22:08:10 +02:00
|
|
|
return b.Put(objectID.ToBytes(), str)
|
2021-05-08 22:25:31 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return object, err
|
|
|
|
}
|