Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:recycle: Refactor av data structure

Daniel3481e93463a1
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index c31114270f..44c3148853 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -381,6 +381,92 @@ func setAttributeViewColHidden(operation *Operation) (err error) {
return
}
+func (tx *Transaction) doSortAttrViewRow(operation *Operation) (ret *TxErr) {
+ err := sortAttributeViewRow(operation)
+ if nil != err {
+ return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()}
+ }
+ return
+}
+
+func sortAttributeViewRow(operation *Operation) (err error) {
+ attrView, err := av.ParseAttributeView(operation.AvID)
+ if nil != err {
+ return
+ }
+
+ var row *av.Row
+ var index, previousIndex int
+ for i, r := range attrView.Rows {
+ if r.ID == operation.ID {
+ row = r
+ index = i
+ break
+ }
+ }
+ if nil == row {
+ return
+ }
+
+ attrView.Rows = append(attrView.Rows[:index], attrView.Rows[index+1:]...)
+ for i, r := range attrView.Rows {
+ if r.ID == operation.PreviousID {
+ previousIndex = i + 1
+ break
+ }
+ }
+ attrView.Rows = util.InsertElem(attrView.Rows, previousIndex, row)
+
+ err = av.SaveAttributeView(attrView)
+ return
+}
+
+func (tx *Transaction) doSortAttrViewColumn(operation *Operation) (ret *TxErr) {
+ err := sortAttributeViewColumn(operation)
+ if nil != err {
+ return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()}
+ }
+ return
+}
+
+func sortAttributeViewColumn(operation *Operation) (err error) {
+ attrView, err := av.ParseAttributeView(operation.AvID)
+ if nil != err {
+ return
+ }
+
+ var col *av.Column
+ var index, previousIndex int
+ for i, column := range attrView.Columns {
+ if column.ID == operation.ID {
+ col = column
+ index = i
+ break
+ }
+ }
+ if nil == col {
+ return
+ }
+
+ attrView.Columns = append(attrView.Columns[:index], attrView.Columns[index+1:]...)
+ for i, column := range attrView.Columns {
+ if column.ID == operation.PreviousID {
+ previousIndex = i + 1
+ break
+ }
+ }
+ attrView.Columns = util.InsertElem(attrView.Columns, previousIndex, col)
+
+ for _, row := range attrView.Rows {
+ cel := row.Cells[index]
+ row.Cells = append(row.Cells[:index], row.Cells[index+1:]...)
+ row.Cells = util.InsertElem(row.Cells, previousIndex, cel)
+ }
+
+ err = av.SaveAttributeView(attrView)
+ return
+}
+
// TODO 下面的方法要重写
func (tx *Transaction) doUpdateAttrViewCell(operation *Operation) (ret *TxErr) {
@@ -496,22 +582,6 @@ func (tx *Transaction) doRemoveAttrViewColumn(operation *Operation) (ret *TxErr)
return
}
-func (tx *Transaction) doSortAttrViewColumn(operation *Operation) (ret *TxErr) {
- err := sortAttributeViewColumn(operation.ID, operation.PreviousID, operation.ParentID)
- if nil != err {
- return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()}
- }
- return
-}
-
-func (tx *Transaction) doSortAttrViewRow(operation *Operation) (ret *TxErr) {
- err := sortAttributeViewRow(operation.ID, operation.PreviousID, operation.ParentID)
- if nil != err {
- return &TxErr{code: TxErrWriteAttributeView, id: operation.ParentID, msg: err.Error()}
- }
- return
-}
-
func (tx *Transaction) doSetAttrView(operation *Operation) (ret *TxErr) {
err := setAttributeView(operation)
if nil != err {
@@ -762,76 +832,6 @@ func removeAttributeViewColumn(columnID string, avID string) (err error) {
return
}
-func sortAttributeViewColumn(columnID, previousColumnID, avID string) (err error) {
- attrView, err := av.ParseAttributeView(avID)
- if nil != err {
- return
- }
-
- var col *av.Column
- var index, previousIndex int
- for i, column := range attrView.Columns {
- if column.ID == columnID {
- col = column
- index = i
- break
- }
- }
- if nil == col {
- return
- }
-
- attrView.Columns = append(attrView.Columns[:index], attrView.Columns[index+1:]...)
- for i, column := range attrView.Columns {
- if column.ID == previousColumnID {
- previousIndex = i + 1
- break
- }
- }
- attrView.Columns = util.InsertElem(attrView.Columns, previousIndex, col)
-
- for _, row := range attrView.Rows {
- cel := row.Cells[index]
- row.Cells = append(row.Cells[:index], row.Cells[index+1:]...)
- row.Cells = util.InsertElem(row.Cells, previousIndex, cel)
- }
-
- err = av.SaveAttributeView(attrView)
- return
-}
-
-func sortAttributeViewRow(rowID, previousRowID, avID string) (err error) {
- attrView, err := av.ParseAttributeView(avID)
- if nil != err {
- return
- }
-
- var row *av.Row
- var index, previousIndex int
- for i, r := range attrView.Rows {
- if r.ID == rowID {
- row = r
- index = i
- break
- }
- }
- if nil == row {
- return
- }
-
- attrView.Rows = append(attrView.Rows[:index], attrView.Rows[index+1:]...)
- for i, r := range attrView.Rows {
- if r.ID == previousRowID {
- previousIndex = i + 1
- break
- }
- }
- attrView.Rows = util.InsertElem(attrView.Rows, previousIndex, row)
-
- err = av.SaveAttributeView(attrView)
- return
-}
-
func setAttributeView(operation *Operation) (err error) {
avID := operation.ID
attrViewMap, err := av.ParseAttributeViewMap(avID)
diff --git a/kernel/model/transaction.go b/kernel/model/transaction.go
index 9800db0a66..25da3aade6 100644
--- a/kernel/model/transaction.go
+++ b/kernel/model/transaction.go
@@ -229,23 +229,24 @@ func performTx(tx *Transaction) (ret *TxErr) {
ret = tx.doSetAttrViewColumnWrap(op)
case "setAttrViewColHidden":
ret = tx.doSetAttrViewColumnHidden(op)
- // TODO 下面的方法要重写
+ case "sortAttrViewRow":
+ ret = tx.doSortAttrViewRow(op)
+ case "sortAttrViewCol":
+ ret = tx.doSortAttrViewColumn(op)
case "insertAttrViewBlock":
ret = tx.doInsertAttrViewBlock(op)
case "removeAttrViewBlock":
ret = tx.doRemoveAttrViewBlock(op)
+ // TODO 下面的方法要重写
+
case "addAttrViewCol":
ret = tx.doAddAttrViewColumn(op)
case "updateAttrViewCol":
ret = tx.doUpdateAttrViewColumn(op)
case "removeAttrViewCol":
ret = tx.doRemoveAttrViewColumn(op)
- case "sortAttrViewCol":
- ret = tx.doSortAttrViewColumn(op)
case "updateAttrViewCell":
ret = tx.doUpdateAttrViewCell(op)
- case "sortAttrViewRow":
- ret = tx.doSortAttrViewRow(op)
case "setAttrView":
ret = tx.doSetAttrView(op)
case "updateAttrViewColOptions":