From bf4ad0972b9d1c1e9e212c5464106aac5e927987 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Sun, 14 Apr 2024 12:34:06 +0800 Subject: [PATCH] :art: Improve database sort/calc for relation and rollup --- kernel/av/sort.go | 61 ++++++++++++++++++---------------- kernel/av/table.go | 16 ++++----- kernel/av/value.go | 23 +++++++------ kernel/model/attribute_view.go | 6 ++-- kernel/model/export.go | 4 +-- kernel/treenode/node.go | 4 +-- 6 files changed, 60 insertions(+), 54 deletions(-) diff --git a/kernel/av/sort.go b/kernel/av/sort.go index 37c36aea8..bef537865 100644 --- a/kernel/av/sort.go +++ b/kernel/av/sort.go @@ -220,59 +220,62 @@ func (value *Value) Compare(other *Value, attrView *AttributeView) int { } case KeyTypeRelation: if nil != value.Relation && nil != other.Relation { + if 1 < len(value.Relation.Contents) && 1 < len(other.Relation.Contents) && KeyTypeNumber == value.Relation.Contents[0].Type && KeyTypeNumber == other.Relation.Contents[0].Type { + v1, ok1 := util.Convert2Float(value.Relation.Contents[0].String(false)) + v2, ok2 := util.Convert2Float(other.Relation.Contents[0].String(false)) + if ok1 && ok2 { + if v1 > v2 { + return 1 + } + if v1 < v2 { + return -1 + } + return 0 + } + } + vContentBuf := bytes.Buffer{} for _, c := range value.Relation.Contents { - vContentBuf.WriteString(c.String()) + vContentBuf.WriteString(c.String(true)) vContentBuf.WriteByte(' ') } vContent := strings.TrimSpace(vContentBuf.String()) oContentBuf := bytes.Buffer{} for _, c := range other.Relation.Contents { - oContentBuf.WriteString(c.String()) + oContentBuf.WriteString(c.String(true)) oContentBuf.WriteByte(' ') } oContent := strings.TrimSpace(oContentBuf.String()) - - v1, ok1 := util.Convert2Float(vContent) - v2, ok2 := util.Convert2Float(oContent) - if ok1 && ok2 { - if v1 > v2 { - return 1 - } - - if v1 < v2 { - return -1 - } - return 0 - } return strings.Compare(vContent, oContent) } case KeyTypeRollup: if nil != value.Rollup && nil != other.Rollup { + if 1 < len(value.Rollup.Contents) && 1 < len(other.Rollup.Contents) && KeyTypeNumber == value.Rollup.Contents[0].Type && KeyTypeNumber == other.Rollup.Contents[0].Type { + v1, ok1 := util.Convert2Float(value.Rollup.Contents[0].String(false)) + v2, ok2 := util.Convert2Float(other.Rollup.Contents[0].String(false)) + if ok1 && ok2 { + if v1 > v2 { + return 1 + } + if v1 < v2 { + return -1 + } + return 0 + } + } + vContentBuf := bytes.Buffer{} for _, c := range value.Rollup.Contents { - vContentBuf.WriteString(c.String()) + vContentBuf.WriteString(c.String(true)) vContentBuf.WriteByte(' ') } vContent := strings.TrimSpace(vContentBuf.String()) oContentBuf := bytes.Buffer{} for _, c := range other.Rollup.Contents { - oContentBuf.WriteString(c.String()) + oContentBuf.WriteString(c.String(true)) oContentBuf.WriteByte(' ') } oContent := strings.TrimSpace(oContentBuf.String()) - - v1, ok1 := util.Convert2Float(vContent) - v2, ok2 := util.Convert2Float(oContent) - if ok1 && ok2 { - if v1 > v2 { - return 1 - } - if v1 < v2 { - return -1 - } - return 0 - } return strings.Compare(vContent, oContent) } } diff --git a/kernel/av/table.go b/kernel/av/table.go index ec7264fcb..fb7afc849 100644 --- a/kernel/av/table.go +++ b/kernel/av/table.go @@ -1586,8 +1586,8 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - if !uniqueValues[content.String()] { - uniqueValues[content.String()] = true + if !uniqueValues[content.String(true)] { + uniqueValues[content.String(true)] = true countUniqueValues++ } } @@ -1635,7 +1635,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := util.Convert2Float(content.String()) + val, _ := util.Convert2Float(content.String(false)) sum += val } } @@ -1647,7 +1647,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := util.Convert2Float(content.String()) + val, _ := util.Convert2Float(content.String(false)) sum += val count++ } @@ -1661,7 +1661,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := util.Convert2Float(content.String()) + val, _ := util.Convert2Float(content.String(false)) values = append(values, val) } } @@ -1679,7 +1679,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := util.Convert2Float(content.String()) + val, _ := util.Convert2Float(content.String(false)) if val < minVal { minVal = val } @@ -1694,7 +1694,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := util.Convert2Float(content.String()) + val, _ := util.Convert2Float(content.String(false)) if val > maxVal { maxVal = val } @@ -1710,7 +1710,7 @@ func (table *Table) calcColRollup(col *TableColumn, colIndex int) { for _, row := range table.Rows { if nil != row.Cells[colIndex] && nil != row.Cells[colIndex].Value && nil != row.Cells[colIndex].Value.Rollup && 0 < len(row.Cells[colIndex].Value.Rollup.Contents) { for _, content := range row.Cells[colIndex].Value.Rollup.Contents { - val, _ := util.Convert2Float(content.String()) + val, _ := util.Convert2Float(content.String(false)) if val < minVal { minVal = val } diff --git a/kernel/av/value.go b/kernel/av/value.go index 0a2728d5c..5b188eedd 100644 --- a/kernel/av/value.go +++ b/kernel/av/value.go @@ -64,7 +64,7 @@ func (value *Value) SetUpdatedAt(mills int64) { } } -func (value *Value) String() string { +func (value *Value) String(format bool) string { if nil == value { return "" } @@ -84,7 +84,10 @@ func (value *Value) String() string { if nil == value.Number { return "" } - return value.Number.FormattedContent + if format { + return value.Number.FormattedContent + } + return fmt.Sprintf("%f", value.Number.Content) case KeyTypeDate: if nil == value.Date { return "" @@ -158,7 +161,7 @@ func (value *Value) String() string { } var ret []string for _, v := range value.Relation.Contents { - ret = append(ret, v.String()) + ret = append(ret, v.String(format)) } return strings.TrimSpace(strings.Join(ret, ", ")) case KeyTypeRollup: @@ -167,7 +170,7 @@ func (value *Value) String() string { } var ret []string for _, v := range value.Rollup.Contents { - ret = append(ret, v.String()) + ret = append(ret, v.String(format)) } return strings.TrimSpace(strings.Join(ret, ", ")) default: @@ -679,8 +682,8 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) { countUniqueValues := 0 uniqueValues := map[string]bool{} for _, v := range r.Contents { - if _, ok := uniqueValues[v.String()]; !ok { - uniqueValues[v.String()] = true + if _, ok := uniqueValues[v.String(true)]; !ok { + uniqueValues[v.String(true)] = true countUniqueValues++ } } @@ -688,7 +691,7 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) { case CalcOperatorCountEmpty: countEmpty := 0 for _, v := range r.Contents { - if "" == v.String() { + if "" == v.String(true) { countEmpty++ } } @@ -696,7 +699,7 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) { case CalcOperatorCountNotEmpty: countNonEmpty := 0 for _, v := range r.Contents { - if "" != v.String() { + if "" != v.String(true) { countNonEmpty++ } } @@ -704,7 +707,7 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) { case CalcOperatorPercentEmpty: countEmpty := 0 for _, v := range r.Contents { - if "" == v.String() { + if "" == v.String(true) { countEmpty++ } } @@ -714,7 +717,7 @@ func (r *ValueRollup) RenderContents(calc *RollupCalc, destKey *Key) { case CalcOperatorPercentNotEmpty: countNonEmpty := 0 for _, v := range r.Contents { - if "" != v.String() { + if "" != v.String(true) { countNonEmpty++ } } diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go index d7bcd6918..d0731f417 100644 --- a/kernel/model/attribute_view.go +++ b/kernel/model/attribute_view.go @@ -89,7 +89,7 @@ func GetAttributeViewPrimaryKeyValues(avID, keyword string, page, pageSize int) } keyValues.Values = []*av.Value{} for _, v := range tmp { - if strings.Contains(strings.ToLower(v.String()), strings.ToLower(keyword)) { + if strings.Contains(strings.ToLower(v.String(true)), strings.ToLower(keyword)) { keyValues.Values = append(keyValues.Values, v) } } @@ -912,7 +912,7 @@ func renderTemplateCol(ial map[string]string, flashcard *Flashcard, rowValues [] dataModel[rowValue.Key.Name] = v.Rollup.Contents[0].Number.Content } } else { - dataModel[rowValue.Key.Name] = v.String() + dataModel[rowValue.Key.Name] = v.String(true) } } } @@ -1218,7 +1218,7 @@ func renderAttributeViewTable(attrView *av.AttributeView, view *av.View, query s hit := false for _, cell := range row.Cells { for _, keyword := range keywords { - if strings.Contains(strings.ToLower(cell.Value.String()), strings.ToLower(keyword)) { + if strings.Contains(strings.ToLower(cell.Value.String(true)), strings.ToLower(keyword)) { hit = true break } diff --git a/kernel/model/export.go b/kernel/model/export.go index f3012adfe..c86f9165b 100644 --- a/kernel/model/export.go +++ b/kernel/model/export.go @@ -148,7 +148,7 @@ func ExportAv2CSV(avID, blockID string) (zipPath string, err error) { } } - val = cell.Value.String() + val = cell.Value.String(true) } rowVal = append(rowVal, val) @@ -2335,7 +2335,7 @@ func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros, keepFold bool, } } - val = cell.Value.String() + val = cell.Value.String(true) } mdTableCell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(val)}) } diff --git a/kernel/treenode/node.go b/kernel/treenode/node.go index 91b31af85..810b978c9 100644 --- a/kernel/treenode/node.go +++ b/kernel/treenode/node.go @@ -598,7 +598,7 @@ func getAttributeViewContent(avID string) (content string) { if nil == cell.Value { continue } - buf.WriteString(cell.Value.String()) + buf.WriteString(cell.Value.String(true)) buf.WriteByte(' ') } } @@ -1055,7 +1055,7 @@ func renderTemplateCol(ial map[string]string, rowValues []*av.KeyValues, tplCont dataModel[rowValue.Key.Name] = v.Rollup.Contents[0].Number.Content } } else { - dataModel[rowValue.Key.Name] = v.String() + dataModel[rowValue.Key.Name] = v.String(true) } } }