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 { func NewBoltPair(parent *BoltBucket, key, val []byte) *BoltPair {
tp := BoltPair{ tp := BoltPair{
parent: parent,
key: key, key: key,
val: val, 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) { func (bd *BoltDB) getGenericFromStringPath(path []PathNode) (*BoltBucket, *BoltPair, error) {
return bd.getGenericFromPath(path) return bd.getGenericFromPath(path)
} }
@ -311,7 +337,7 @@ func (bd *BoltDB) refreshDatabase() *BoltDB {
memBolt = new(BoltDB) memBolt = new(BoltDB)
db.View(func(tx *bolt.Tx) error { db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(nm []byte, b *bolt.Bucket) error { return tx.ForEach(func(nm []byte, b *bolt.Bucket) error {
bb, err := readBucket(b) bb, err := readBucket(nil, b, nm)
if err == nil { if err == nil {
bb.SetName(nm) bb.SetName(nm)
bb.expanded = false bb.expanded = false
@ -331,7 +357,7 @@ Datatype ToString function
func (b *BoltBucket) GetStringName() string { func (b *BoltBucket) GetStringName() string {
ret, err := b.nameDatatype.ToString(b.name) ret, err := b.nameDatatype.ToString(b.name)
if err != nil { if err != nil {
return stringify(b.name) return "ERROR"
} }
return ret return ret
} }
@ -410,34 +436,13 @@ func (b *BoltBucket) getPair(k []byte) (*BoltPair, error) {
return nil, errors.New("Pair Not Found") 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 GetPath Returns the path of the BoltPair
*/ */
func (p *BoltPair) GetPath() []PathNode { func (p *BoltPair) GetPath() []PathNode {
if p.parent == nil {
return []PathNode{p.GetPathNode()}
}
return append( return append(
p.parent.GetPath(), p.parent.GetPath(),
p.GetPathNode(), p.GetPathNode(),
@ -505,15 +510,12 @@ func deleteKey(path []PathNode) error {
} }
// Recursively read a bucket and pairs from the DB // Recursively read a bucket and pairs from the DB
// We assume all datatypes are string func readBucket(parent *BoltBucket, b *bolt.Bucket, nm []byte) (*BoltBucket, error) {
func readBucket(b *bolt.Bucket) (*BoltBucket, error) { bb := NewBoltBucket(parent, nm)
bb := new(BoltBucket)
b.ForEach(func(k, v []byte) error { b.ForEach(func(k, v []byte) error {
if v == nil { if v == nil {
tb, err := readBucket(b.Bucket(k)) tb, err := readBucket(bb, b.Bucket(k), k)
if err == nil { if err == nil {
tb.parent = bb
tb.SetName(k)
bb.buckets = append(bb.buckets, *tb) bb.buckets = append(bb.buckets, *tb)
} }
} else { } else {
@ -530,45 +532,18 @@ func renameBucket(path []PathNode, name []byte) error {
return nil return nil
} }
var bb *BoltBucket // For caching the current bucket var bb *BoltBucket // For caching the current bucket
err := db.View(func(tx *bolt.Tx) error { bb, err := memBolt.getBucketFromPath(path)
// len(b.path)-1 is the key we need to delete, if err != nil {
// the rest are buckets leading to that key return err
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)
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 { if err = deleteKey(path); err != nil {
return err return err
} }
// Rechristen our cached bucket // Rechristen our cached bucket
bb.SetName(name) bb.SetName(name)
// And re-add it // And re-add it
parentPath := path[:len(path)-1] parentPath := path[:len(path)-1]
if err = addBucketFromBoltBucket(parentPath, bb); err != nil { if err = addBucketFromBoltBucket(parentPath, bb); err != nil {
return err return err

View File

@ -41,7 +41,6 @@ type BrowserScreen struct {
leftPaneBuffer []Line leftPaneBuffer []Line
rightPaneBuffer []Line rightPaneBuffer []Line
niceLog []string
} }
/* /*
@ -243,34 +242,49 @@ func (screen *BrowserScreen) handleInputKeyEvent(event termbox.Event) int {
if b != nil { if b != nil {
if screen.mode == modeChangeKey { if screen.mode == modeChangeKey {
newName := screen.inputModal.GetValue() newName := screen.inputModal.GetValue()
if renameBucket(screen.currentPath, []byte(newName)) != nil { btName, err := b.nameDatatype.FromString(newName)
screen.setMessage("Error renaming bucket.") if err != nil {
} else { screen.setMessage("Error renaming bucket: " + err.Error())
b.name = []byte(newName) } else {
screen.currentPath[len(screen.currentPath)-1].name = b.name if renameBucket(screen.currentPath, btName) != nil {
screen.setMessage("Bucket Renamed!") screen.setMessage("Error renaming bucket.")
screen.refreshDatabase() } else {
b.name = btName
screen.currentPath[len(screen.currentPath)-1].name = b.name
screen.setMessage("Bucket Renamed!")
screen.refreshDatabase()
}
} }
} }
} else if p != nil { } else if p != nil {
if screen.mode == modeChangeKey { if screen.mode == modeChangeKey {
newKey := screen.inputModal.GetValue() newKey := screen.inputModal.GetValue()
if updatePairKey(screen.currentPath, []byte(newKey)) != nil { btKey, err := p.keyDatatype.FromString(newKey)
screen.setMessage("Error occurred updating Pair.") if err != nil {
} else { screen.setMessage("Error updating pair: " + err.Error())
p.key = []byte(newKey) } else {
screen.currentPath[len(screen.currentPath)-1].name = p.key if updatePairKey(screen.currentPath, btKey) != nil {
screen.setMessage("Pair updated!") screen.setMessage("Error occurred updating Pair.")
screen.refreshDatabase() } else {
p.key = btKey
screen.currentPath[len(screen.currentPath)-1].name = p.key
screen.setMessage("Pair updated!")
screen.refreshDatabase()
}
} }
} else if screen.mode == modeChangeVal { } else if screen.mode == modeChangeVal {
newVal := screen.inputModal.GetValue() newVal := screen.inputModal.GetValue()
if updatePairValue(screen.currentPath, []byte(newVal)) != nil { btVal, err := p.valDatatype.FromString(newVal)
screen.setMessage("Error occurred updating Pair.") if err != nil {
} else { screen.setMessage("Error updating pair: " + err.Error())
p.val = []byte(newVal) } else {
screen.setMessage("Pair updated!") if updatePairValue(screen.currentPath, btVal) != nil {
screen.refreshDatabase() screen.setMessage("Error occurred updating Pair.")
} else {
p.val = btVal
screen.setMessage("Pair updated!")
screen.refreshDatabase()
}
} }
} }
} }
@ -612,17 +626,6 @@ func (screen *BrowserScreen) drawLeftPane(style Style) {
func (screen *BrowserScreen) buildRightPane(style Style) { func (screen *BrowserScreen) buildRightPane(style Style) {
screen.rightPaneBuffer = nil 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) b, p, err := screen.db.getGenericFromStringPath(screen.currentPath)
if err == nil { 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}) Line{fmt.Sprintf("Buckets: %d", len(b.buckets)), style.defaultFg, style.defaultBg})
screen.rightPaneBuffer = append(screen.rightPaneBuffer, screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{fmt.Sprintf("Pairs: %d", len(b.pairs)), style.defaultFg, style.defaultBg}) 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 { } else if p != nil {
screen.rightPaneBuffer = append(screen.rightPaneBuffer, screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{fmt.Sprintf("Path: %s", strings.Join(nodePathToStringPath(p.GetPath()), " → ")), style.defaultFg, style.defaultBg}) Line{fmt.Sprintf("Path: %s", strings.Join(nodePathToStringPath(p.GetPath()), " → ")), style.defaultFg, style.defaultBg})
screen.rightPaneBuffer = append(screen.rightPaneBuffer, 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") value := strings.Split(string(formatValue([]byte(p.val))), "\n")
if len(value) == 1 { if len(value) == 1 {
@ -660,12 +666,6 @@ func (screen *BrowserScreen) buildRightPane(style Style) {
} }
screen.rightPaneBuffer = append(screen.rightPaneBuffer, screen.rightPaneBuffer = append(screen.rightPaneBuffer,
Line{"", style.defaultFg, style.defaultBg}) 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) { func (screen *BrowserScreen) drawRightPane(style Style) {