fix(be): correct sorting for boltdb

This commit is contained in:
Denis Gukov 2021-06-25 00:53:36 +05:00
parent 482d24422b
commit b60e927fc6
5 changed files with 22 additions and 4 deletions

4
.gitignore vendored
View File

@ -6,7 +6,9 @@ web/public/html/**/*.*
web/public/fonts/*.* web/public/fonts/*.*
web2/.nyc_output web2/.nyc_output
web2/dist/**/* web2/dist/**/*
config.json /config.json
/.dredd/config.json
/database.bolt
.DS_Store .DS_Store
node_modules/ node_modules/

View File

@ -33,6 +33,7 @@ type ObjectProperties struct {
ForeignColumnName string ForeignColumnName string
PrimaryColumnName string PrimaryColumnName string
SortableColumns []string SortableColumns []string
SortInverted bool
} }
var ErrNotFound = errors.New("no rows in result set") var ErrNotFound = errors.New("no rows in result set")
@ -206,6 +207,7 @@ var TaskProps = ObjectProperties{
TableName: "task", TableName: "task",
IsGlobal: true, IsGlobal: true,
PrimaryColumnName: "id", PrimaryColumnName: "id",
SortInverted: true,
} }
var TaskOutputProps = ObjectProperties{ var TaskOutputProps = ObjectProperties{

View File

@ -11,6 +11,7 @@ import (
"sort" "sort"
) )
const MaxID = 2147483647
type enumerable interface { type enumerable interface {
First() (key []byte, value []byte) First() (key []byte, value []byte)
@ -354,7 +355,7 @@ func (d *BoltDb) isObjectInUse(bucketID int, props db.ObjectProperties, objID ob
reflect.Uint16, reflect.Uint16,
reflect.Uint32, reflect.Uint32,
reflect.Uint64: reflect.Uint64:
fVal = intObjectID(f.Int()) fVal = intObjectID(2147483647 - f.Int())
case reflect.String: case reflect.String:
fVal = strObjectID(f.String()) fVal = strObjectID(f.String())
} }
@ -529,8 +530,12 @@ func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object in
if err2 != nil { if err2 != nil {
return err2 return err2
} }
if props.SortInverted {
id = MaxID - id
}
idValue.SetInt(int64(id)) idValue.SetInt(int64(id))
} }
objectID = intObjectID(idValue.Int()) objectID = intObjectID(idValue.Int())
case reflect.String: case reflect.String:
if idValue.String() == "" { if idValue.String() == "" {
@ -551,6 +556,9 @@ func (d *BoltDb) createObject(bucketID int, props db.ObjectProperties, object in
if err2 != nil { if err2 != nil {
return err2 return err2
} }
if props.SortInverted {
id = MaxID - id
}
objectID = intObjectID(id) objectID = intObjectID(id)
} }

View File

@ -101,6 +101,8 @@ func (d *BoltDb) CreateEvent(evt db.Event) (newEvent db.Event, err error) {
return err2 return err2
} }
id = MaxID - id
return b.Put(intObjectID(id).ToBytes(), str) return b.Put(intObjectID(id).ToBytes(), str)
}) })

View File

@ -9,6 +9,7 @@ import (
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"os" "os"
"path" "path"
"path/filepath"
"net/url" "net/url"
@ -326,10 +327,13 @@ func (conf *ConfigType) GenerateCookieSecrets() {
} }
func (conf *ConfigType) ScanBoltDb() { func (conf *ConfigType) ScanBoltDb() {
fmt.Print(" > DB filename (default /tmp/boltdb): ") filename, err := os.Getwd() // os.UserHomeDir()
exitOnConfigError(err)
filename = filepath.Join(filename, "database.bolt")
fmt.Print(" > DB filename (default " + filename + "): ")
ScanErrorChecker(fmt.Scanln(&conf.BoltDb.Hostname)) ScanErrorChecker(fmt.Scanln(&conf.BoltDb.Hostname))
if len(conf.BoltDb.Hostname) == 0 { if len(conf.BoltDb.Hostname) == 0 {
conf.BoltDb.Hostname = "/tmp/boltdb" conf.BoltDb.Hostname = filename
} }
} }