Type switching / saving is mostly working

This commit is contained in:
Brian Buller 2019-10-25 17:37:50 -05:00
parent 930d064962
commit 3c25a93d4d
2 changed files with 78 additions and 103 deletions

View File

@ -72,6 +72,7 @@ type BoltPair struct {
func NewBoltPair(parent *BoltBucket, key, val []byte) *BoltPair {
tp := BoltPair{
parent: parent,
key: key,
val: val,
}
@ -97,6 +98,31 @@ func (p *BoltPair) GetPathNode() PathNode {
}
}
/*
GetStringKey returns the key of the pair after passing it through it's
Datatype ToString function
*/
func (p *BoltPair) GetStringKey() string {
ret, err := p.keyDatatype.ToString(p.key)
if err != nil {
return "ERROR"
}
return ret
}
/*
GetStringVal returns the val of the pair after passing it through it's
Datatype ToString function
*/
func (p *BoltPair) GetStringVal() string {
ret, err := p.valDatatype.ToString(p.val)
if err != nil {
return "ERROR"
}
return ret
}
func (bd *BoltDB) getGenericFromStringPath(path []PathNode) (*BoltBucket, *BoltPair, error) {
return bd.getGenericFromPath(path)
}
@ -311,7 +337,7 @@ func (bd *BoltDB) refreshDatabase() *BoltDB {
memBolt = new(BoltDB)
db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
bb, err := readBucket(b)
bb, err := readBucket(nil, b, nm)
if err == nil {
bb.SetName(nm)
bb.expanded = false
@ -331,7 +357,7 @@ Datatype ToString function
func (b *BoltBucket) GetStringName() string {
ret, err := b.nameDatatype.ToString(b.name)
if err != nil {
return stringify(b.name)
return "ERROR"
}
return ret
}
@ -410,34 +436,13 @@ func (b *BoltBucket) getPair(k []byte) (*BoltPair, error) {
return nil, errors.New("Pair Not Found")
}
/*
GetStringKey returns the key of the pair after passing it through it's
Datatype ToString function
*/
func (p *BoltPair) GetStringKey() string {
ret, err := p.keyDatatype.ToString(p.key)
if err != nil {
return stringify(p.key)
}
return ret
}
/*
GetStringVal returns the val of the pair after passing it through it's
Datatype ToString function
*/
func (p *BoltPair) GetStringVal() string {
ret, err := p.valDatatype.ToString(p.val)
if err != nil {
return stringify(p.val)
}
return ret
}
/*
GetPath Returns the path of the BoltPair
*/
func (p *BoltPair) GetPath() []PathNode {
if p.parent == nil {
return []PathNode{p.GetPathNode()}
}
return append(
p.parent.GetPath(),
p.GetPathNode(),
@ -505,15 +510,12 @@ func deleteKey(path []PathNode) error {
}
// Recursively read a bucket and pairs from the DB
// We assume all datatypes are string
func readBucket(b *bolt.Bucket) (*BoltBucket, error) {
bb := new(BoltBucket)
func readBucket(parent *BoltBucket, b *bolt.Bucket, nm []byte) (*BoltBucket, error) {
bb := NewBoltBucket(parent, nm)
b.ForEach(func(k, v []byte) error {
if v == nil {
tb, err := readBucket(b.Bucket(k))
tb, err := readBucket(bb, b.Bucket(k), k)
if err == nil {
tb.parent = bb
tb.SetName(k)
bb.buckets = append(bb.buckets, *tb)
}
} else {
@ -530,45 +532,18 @@ func renameBucket(path []PathNode, name []byte) error {
return nil
}
var bb *BoltBucket // For caching the current bucket
err := db.View(func(tx *bolt.Tx) error {
// len(b.path)-1 is the key we need to delete,
// the rest are buckets leading to that key
b := tx.Bucket(path[0].name)
if b != nil {
if len(path) > 1 {
for i := range path[1:len(path)] {
b = b.Bucket(path[i+1].name)
if b == nil {
return errors.New("renameBucket: Invalid Path")
}
}
}
var err error
// Ok, cache b
bb, err = readBucket(b)
bb, err := memBolt.getBucketFromPath(path)
if err != nil {
return err
}
} else {
return errors.New("renameBucket: Invalid Bucket")
}
return nil
})
if err != nil {
return err
}
if bb == nil {
return errors.New("renameBucket: Couldn't find Bucket")
}
// Ok, we have the bucket cached, now delete the current instance
// Ok, we have the bucket, now delete the current instance
if err = deleteKey(path); err != nil {
return err
}
// Rechristen our cached bucket
bb.SetName(name)
// And re-add it
parentPath := path[:len(path)-1]
if err = addBucketFromBoltBucket(parentPath, bb); err != nil {
return err

View File

@ -41,7 +41,6 @@ type BrowserScreen struct {
leftPaneBuffer []Line
rightPaneBuffer []Line
niceLog []string
}
/*
@ -243,37 +242,52 @@ func (screen *BrowserScreen) handleInputKeyEvent(event termbox.Event) int {
if b != nil {
if screen.mode == modeChangeKey {
newName := screen.inputModal.GetValue()
if renameBucket(screen.currentPath, []byte(newName)) != nil {
btName, err := b.nameDatatype.FromString(newName)
if err != nil {
screen.setMessage("Error renaming bucket: " + err.Error())
} else {
if renameBucket(screen.currentPath, btName) != nil {
screen.setMessage("Error renaming bucket.")
} else {
b.name = []byte(newName)
b.name = btName
screen.currentPath[len(screen.currentPath)-1].name = b.name
screen.setMessage("Bucket Renamed!")
screen.refreshDatabase()
}
}
}
} else if p != nil {
if screen.mode == modeChangeKey {
newKey := screen.inputModal.GetValue()
if updatePairKey(screen.currentPath, []byte(newKey)) != nil {
btKey, err := p.keyDatatype.FromString(newKey)
if err != nil {
screen.setMessage("Error updating pair: " + err.Error())
} else {
if updatePairKey(screen.currentPath, btKey) != nil {
screen.setMessage("Error occurred updating Pair.")
} else {
p.key = []byte(newKey)
p.key = btKey
screen.currentPath[len(screen.currentPath)-1].name = p.key
screen.setMessage("Pair updated!")
screen.refreshDatabase()
}
}
} else if screen.mode == modeChangeVal {
newVal := screen.inputModal.GetValue()
if updatePairValue(screen.currentPath, []byte(newVal)) != nil {
btVal, err := p.valDatatype.FromString(newVal)
if err != nil {
screen.setMessage("Error updating pair: " + err.Error())
} else {
if updatePairValue(screen.currentPath, btVal) != nil {
screen.setMessage("Error occurred updating Pair.")
} else {
p.val = []byte(newVal)
p.val = btVal
screen.setMessage("Pair updated!")
screen.refreshDatabase()
}
}
}
}
screen.mode = modeBrowse
screen.inputModal.Clear()
}
@ -612,17 +626,6 @@ func (screen *BrowserScreen) drawLeftPane(style Style) {
func (screen *BrowserScreen) buildRightPane(style Style) {
screen.rightPaneBuffer = nil
screen.niceLog = []string{}
var logstr string
for _, v := range screen.currentPath {
vStr, err := v.dataType.ToString(v.name)
if err != nil {
vStr = "ERROR"
}
logstr = fmt.Sprintf("%s / %s", logstr, vStr)
}
screen.niceLog = append(screen.niceLog, logstr)
b, p, err := screen.db.getGenericFromStringPath(screen.currentPath)
if err == nil {
@ -633,11 +636,14 @@ func (screen *BrowserScreen) buildRightPane(style Style) {
Line{fmt.Sprintf("Buckets: %d", len(b.buckets)), style.defaultFg, style.defaultBg})
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{fmt.Sprintf("Pairs: %d", len(b.pairs)), style.defaultFg, style.defaultBg})
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{fmt.Sprintf("Name Datatype: %s", b.nameDatatype.Name()), style.defaultFg, style.defaultBg})
} else if p != nil {
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{fmt.Sprintf("Path: %s", strings.Join(nodePathToStringPath(p.GetPath()), " → ")), style.defaultFg, style.defaultBg})
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{fmt.Sprintf("Key (%s): %s", p.keyDatatype.Name(), stringify([]byte(p.key))), style.defaultFg, style.defaultBg})
Line{fmt.Sprintf("Key (%s): %s", p.keyDatatype.Name(), p.GetStringKey()), style.defaultFg, style.defaultBg})
value := strings.Split(string(formatValue([]byte(p.val))), "\n")
if len(value) == 1 {
@ -660,12 +666,6 @@ func (screen *BrowserScreen) buildRightPane(style Style) {
}
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{"", style.defaultFg, style.defaultBg})
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{"== Log ==", style.defaultBg, style.defaultFg})
for _, v := range screen.niceLog {
screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{v, style.defaultFg, style.defaultBg})
}
}
func (screen *BrowserScreen) drawRightPane(style Style) {