dynamo-browse/internal/dynamo-browse/models/models.go

221 lines
4.7 KiB
Go
Raw Normal View History

package models
import (
"sort"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
2022-06-27 06:05:59 +00:00
type ResultSet struct {
// Query information
TableInfo *TableInfo
Query Queryable
Created time.Time
ExclusiveStartKey map[string]types.AttributeValue
// Result information
LastEvaluatedKey map[string]types.AttributeValue
items []Item
attributes []ItemAttribute
2022-06-27 06:05:59 +00:00
columns []string
sortCriteria SortCriteria
mutex sync.Mutex
cachedMarkedItems []ItemIndex
hasCachedMarkedItems bool
}
type Queryable interface {
String() string
Initial version of scripting (#40) * scripting: added service and controller for scripting * scripting: have got prompts working Scripts are now running in a separate go-routine. When a prompt is encountered, the script is paused and the user is prompted for input. This means that the script no longer needs to worry about synchronisation issues. * scripting: started working on the session methods * scripting: added methods to get items and attributes * scripting: have got loading of scripts working These act more like plugins and allow defining new commands. * scripting: have got script scheduling working Scripts are now executed on a dedicated goroutine and only one script can run at any one time. * scripting: added session.set_result_set(rs) * scripting: upgraded tamarin to 0.14 * scripting: started working on set_value * tamarin: replaced ad-hoc path with query expressions * scripting: changed value() and set_value() to attr() and set_attr() Also added 'delete_attr()' * scripting: added os.exec() This method is controlled by permissions which govern whether shellouts are allowed Also fixed a resizing bug with the status window which was not properly handling status messages with newlines * scripting: added the session.current_item() method * scripting: added placeholders to query expressions * scripting: added support for setting and deleteing items with placeholders Also refactored the dot AST type so that it support placeholders. Placeholders are not yet supported for subrefs yet, they need to be identifiers. * scripting: made setting the result-set push the current result-set to the backstack * scripting: started working on byte encoding of attribute values * scripting: finished attrcodec * scripting: integrated codec into expression * scripting: added equals and hashcode to queryexpr This finally finishes the work required to store queries in the backstack * scripting: fixed some bugs with the back-stack * scripting: upgraded Tamarin * scripting: removed some commented out code
2023-01-10 11:27:13 +00:00
SerializeToBytes() ([]byte, error)
HashCode() uint64
Plan(tableInfo *TableInfo) (*QueryExecutionPlan, error)
}
type ItemAttribute struct {
Marked bool
Hidden bool
Dirty bool
New bool
}
func (rs *ResultSet) NoResults() bool {
return len(rs.items) == 0 && rs.LastEvaluatedKey == nil
}
func (rs *ResultSet) Items() []Item {
return rs.items
}
func (rs *ResultSet) SetItems(items []Item) {
rs.items = items
rs.attributes = make([]ItemAttribute, len(items))
rs.mutex.Lock()
defer rs.mutex.Unlock()
rs.hasCachedMarkedItems = false
rs.cachedMarkedItems = nil
}
func (rs *ResultSet) SortCriteria() SortCriteria {
return rs.sortCriteria
}
func (rs *ResultSet) AddNewItem(item Item, attrs ItemAttribute) {
rs.items = append(rs.items, item)
rs.attributes = append(rs.attributes, attrs)
rs.mutex.Lock()
defer rs.mutex.Unlock()
rs.hasCachedMarkedItems = false
rs.cachedMarkedItems = nil
}
func (rs *ResultSet) SetMark(idx int, marked bool) {
rs.attributes[idx].Marked = marked
if !rs.hasCachedMarkedItems {
return
}
rs.mutex.Lock()
defer rs.mutex.Unlock()
rs.hasCachedMarkedItems = false
rs.cachedMarkedItems = nil
}
func (rs *ResultSet) SetHidden(idx int, hidden bool) {
rs.attributes[idx].Hidden = hidden
}
func (rs *ResultSet) SetDirty(idx int, dirty bool) {
rs.attributes[idx].Dirty = dirty
}
func (rs *ResultSet) SetNew(idx int, isNew bool) {
rs.attributes[idx].New = isNew
}
func (rs *ResultSet) Marked(idx int) bool {
return rs.attributes[idx].Marked
}
func (rs *ResultSet) Hidden(idx int) bool {
return rs.attributes[idx].Hidden
}
func (rs *ResultSet) IsDirty(idx int) bool {
return rs.attributes[idx].Dirty
}
func (rs *ResultSet) IsNew(idx int) bool {
return rs.attributes[idx].New
}
func (rs *ResultSet) MarkedItems() []ItemIndex {
rs.mutex.Lock()
defer rs.mutex.Unlock()
if rs.hasCachedMarkedItems {
return rs.cachedMarkedItems
}
items := make([]ItemIndex, 0)
for i, itemAttr := range rs.attributes {
if itemAttr.Marked && !itemAttr.Hidden {
items = append(items, ItemIndex{Index: i, Item: rs.items[i]})
}
}
rs.cachedMarkedItems = items
rs.hasCachedMarkedItems = true
return items
}
2022-06-27 06:05:59 +00:00
func (rs *ResultSet) Columns() []string {
if rs.columns == nil {
rs.RefreshColumns()
}
return rs.columns
}
func (rs *ResultSet) RefreshColumns() {
seenColumns := make(map[string]int)
seenColumns[rs.TableInfo.Keys.PartitionKey] = 0
if rs.TableInfo.Keys.SortKey != "" {
seenColumns[rs.TableInfo.Keys.SortKey] = 1
}
for _, definedAttribute := range rs.TableInfo.DefinedAttributes {
if _, seen := seenColumns[definedAttribute]; !seen {
seenColumns[definedAttribute] = len(seenColumns)
}
}
otherColsRank := len(seenColumns)
for _, result := range rs.items {
for k := range result {
if _, isSeen := seenColumns[k]; !isSeen {
seenColumns[k] = otherColsRank
}
}
}
columns := make([]string, 0, len(seenColumns))
for k := range seenColumns {
columns = append(columns, k)
}
sort.Slice(columns, func(i, j int) bool {
if seenColumns[columns[i]] == seenColumns[columns[j]] {
return columns[i] < columns[j]
}
return seenColumns[columns[i]] < seenColumns[columns[j]]
})
rs.columns = columns
}
func (rs *ResultSet) HasNextPage() bool {
return rs.LastEvaluatedKey != nil
}
func (rs *ResultSet) Sort(criteria SortCriteria) {
rs.sortCriteria = criteria
Sort(rs.items, criteria)
}
2025-05-19 12:14:22 +00:00
func (rs *ResultSet) MergeWith(otherRS *ResultSet) *ResultSet {
type pksk struct {
pk types.AttributeValue
sk types.AttributeValue
}
if !rs.TableInfo.Equal(otherRS.TableInfo) {
return nil
}
itemsInI := make(map[pksk]Item)
newItems := make([]Item, 0, len(rs.Items())+len(otherRS.Items()))
for _, item := range rs.Items() {
pk, sk := item.PKSK(rs.TableInfo)
itemsInI[pksk{pk, sk}] = item
newItems = append(newItems, item)
}
for _, item := range otherRS.Items() {
pk, sk := item.PKSK(rs.TableInfo)
if _, hasItem := itemsInI[pksk{pk, sk}]; !hasItem {
newItems = append(newItems, item)
}
}
newResultSet := &ResultSet{
Created: time.Now(),
TableInfo: rs.TableInfo,
}
newResultSet.SetItems(newItems)
return newResultSet
}