Skip to content

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

:art: Improve database view name search https://github.com/siyuan-note/siyuan/issues/16406

Danielf51594c5f733
diff --git a/docs/API.ja.md b/docs/API.ja.md
index 04113c19fc..5b81f7db70 100644
--- a/docs/API.ja.md
+++ b/docs/API.ja.md
@@ -1940,12 +1940,14 @@
```json
{
"keyword": "API",
- "excludes": []
+ "excludes": [],
+ "includeViewMatches": true
}
```
* `keyword`: 検索キーワード(データベース名に一致)
* `excludes`: 任意。結果から除外するデータベース ID のリスト
+ * `includeViewMatches`: 任意。`true` の場合はビュー名も検索対象になり、一致した子ビューに `"matched": true` が含まれます
* 戻り値(実際のレスポンス):
```json
@@ -1969,6 +1971,7 @@
"viewName": "テーブル",
"viewID": "20240118120204-7rnmyc1",
"viewLayout": "table",
+ "matched": true,
"blockID": "20240118120201-kldj15t",
"hPath": "正在跟进的问题/数据库/API"
}
@@ -1979,7 +1982,7 @@
}
```
- * `data.results[]`: 各トップレベル結果は `avID` ごとにデータベースを集約します;その `children[]` が各ビュー(`viewName`/`viewID`/`viewLayout`)を列挙します
+ * `data.results[]`: 各トップレベル結果は `avID` ごとにデータベースを集約します。その `children[]` が各ビュー(`viewName`/`viewID`/`viewLayout`)を列挙し、`includeViewMatches` が有効な場合は `matched` で名前が一致したビューを示します
### セル値を設定
diff --git a/docs/API.md b/docs/API.md
index 561d7d6c5c..b69d459495 100644
--- a/docs/API.md
+++ b/docs/API.md
@@ -1951,12 +1951,14 @@ The field types (`keyType`) are:
```json
{
"keyword": "API",
- "excludes": []
+ "excludes": [],
+ "includeViewMatches": true
}
```
* `keyword`: Search keyword (matches database name)
* `excludes`: Optional list of database IDs to exclude from the results
+ * `includeViewMatches`: Optional. When `true`, view names are also searched and matching child views contain `"matched": true`
* Return value (real response):
```json
@@ -1980,6 +1982,7 @@ The field types (`keyType`) are:
"viewName": "表格",
"viewID": "20240118120204-7rnmyc1",
"viewLayout": "table",
+ "matched": true,
"blockID": "20240118120201-kldj15t",
"hPath": "正在跟进的问题/数据库/API"
}
@@ -1990,7 +1993,7 @@ The field types (`keyType`) are:
}
```
- * `data.results[]`: Each top-level result groups a database by `avID`; its `children[]` list the individual views (`viewName`/`viewID`/`viewLayout`)
+ * `data.results[]`: Each top-level result groups a database by `avID`; its `children[]` list the individual views (`viewName`/`viewID`/`viewLayout`), and `matched` identifies a view-name match when `includeViewMatches` is enabled
### Set a cell value
diff --git a/docs/API.zh-CN.md b/docs/API.zh-CN.md
index 8c15e4cf7c..ea8450cece 100644
--- a/docs/API.zh-CN.md
+++ b/docs/API.zh-CN.md
@@ -1940,12 +1940,14 @@
```json
{
"keyword": "API",
- "excludes": []
+ "excludes": [],
+ "includeViewMatches": true
}
```
* `keyword`: 搜索关键字(匹配数据库名称)
* `excludes`: 可选,需从结果中排除的数据库 ID 列表
+ * `includeViewMatches`: 可选,设为 `true` 时同时搜索视图名称,命中的子视图包含 `"matched": true`
* 返回值(真实响应):
```json
@@ -1969,6 +1971,7 @@
"viewName": "表格",
"viewID": "20240118120204-7rnmyc1",
"viewLayout": "table",
+ "matched": true,
"blockID": "20240118120201-kldj15t",
"hPath": "正在跟进的问题/数据库/API"
}
@@ -1979,7 +1982,7 @@
}
```
- * `data.results[]`: 每个顶层结果按 `avID` 聚合一个数据库;其 `children[]` 列出该数据库的各个视图(`viewName`/`viewID`/`viewLayout`)
+ * `data.results[]`: 每个顶层结果按 `avID` 聚合一个数据库;其 `children[]` 列出该数据库的各个视图(`viewName`/`viewID`/`viewLayout`),启用 `includeViewMatches` 时由 `matched` 标识名称命中的视图
### 设置单元格值
diff --git a/kernel/api/av.go b/kernel/api/av.go
index c694fbaa8d..279a6d3694 100644
--- a/kernel/api/av.go
+++ b/kernel/api/av.go
@@ -993,7 +993,7 @@ func searchAttributeView(c *gin.Context) {
}
}
includeViewMatches, _ := arg["includeViewMatches"].(bool)
- results := model.SearchAttributeView(model.SearchAttributeViewOptions{
+ results := model.SearchAttributeViewWithOptions(model.SearchAttributeViewOptions{
Keyword: keyword,
ExcludeAvIDs: excludes,
CurrentAvID: currentAvID,
diff --git a/kernel/av/av.go b/kernel/av/av.go
index e39a50c28e..fd6dd876e7 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -566,8 +566,10 @@ func GetAttributeViewSearchInfoInBox(avID, boxID string) (ret *AttributeViewSear
}
var data []byte
- if cached, ok := cache.GetAVDataInBox(avID, avBoxID); ok {
+ var dataVersion uint64
+ if cached, version, ok := cache.GetAVDataWithVersionInBox(avID, avBoxID); ok {
data = cached
+ dataVersion = version
} else {
if data, err = filelock.ReadFile(avJSONPath); err != nil {
logging.LogErrorf("read attribute view [%s] failed: %s", avJSONPath, err)
@@ -581,14 +583,14 @@ func GetAttributeViewSearchInfoInBox(avID, boxID string) (ret *AttributeViewSear
} else if util.IsCiphertext(data) {
return
}
- cache.SetAVDataInBox(avID, avBoxID, data)
+ dataVersion = cache.SetAVDataWithVersionInBox(avID, avBoxID, data)
}
if ret, err = parseAttributeViewSearchInfo(data); err != nil {
logging.LogErrorf("unmarshal attribute view search info [%s] failed: %s", avID, err)
return nil, err
}
- cache.SetAVSearchDataInBox(avID, avBoxID, ret)
+ cache.SetAVSearchDataInBox(avID, avBoxID, dataVersion, ret)
return
}
diff --git a/kernel/cache/av.go b/kernel/cache/av.go
index 0034cc5450..4fe76ba3b0 100644
--- a/kernel/cache/av.go
+++ b/kernel/cache/av.go
@@ -28,10 +28,22 @@ var avCache, _ = ristretto.NewCache(&ristretto.Config{
BufferItems: 64,
})
+type avCacheEntry struct {
+ raw []byte
+ version uint64
+}
+
+type avSearchDataEntry struct {
+ data any
+ version uint64
+}
+
var avCacheKeys = map[string]map[string]struct{}{}
var avCacheKeysLock sync.Mutex
-var avSearchDataCache = map[string]any{}
+var avDataVersion uint64
+var avDataVersions = map[string]uint64{}
+var avSearchDataCache = map[string]*avSearchDataEntry{}
var avSearchDataCacheLock sync.RWMutex
func avCacheKey(avID, boxID string) string {
@@ -43,11 +55,17 @@ func GetAVData(avID string) (raw []byte, ok bool) {
}
func GetAVDataInBox(avID, boxID string) (raw []byte, ok bool) {
+ raw, _, ok = GetAVDataWithVersionInBox(avID, boxID)
+ return
+}
+
+func GetAVDataWithVersionInBox(avID, boxID string) (raw []byte, version uint64, ok bool) {
v, _ := avCache.Get(avCacheKey(avID, boxID))
if nil == v {
- return nil, false
+ return
}
- return v.([]byte), true
+ entry := v.(*avCacheEntry)
+ return entry.raw, entry.version, true
}
func SetAVData(avID string, raw []byte) {
@@ -55,12 +73,20 @@ func SetAVData(avID string, raw []byte) {
}
func SetAVDataInBox(avID, boxID string, raw []byte) {
+ setAVDataInBox(avID, boxID, raw)
+}
+
+func SetAVDataWithVersionInBox(avID, boxID string, raw []byte) uint64 {
+ return setAVDataInBox(avID, boxID, raw)
+}
+
+func setAVDataInBox(avID, boxID string, raw []byte) (version uint64) {
if raw == nil {
return
}
key := avCacheKey(avID, boxID)
- avCache.Set(key, raw, int64(len(raw)))
- removeAVSearchDataByKey(key)
+ version = invalidateAVSearchDataByKey(key)
+ avCache.Set(key, &avCacheEntry{raw: raw, version: version}, int64(len(raw)))
avCacheKeysLock.Lock()
defer avCacheKeysLock.Unlock()
@@ -70,12 +96,13 @@ func SetAVDataInBox(avID, boxID string, raw []byte) {
avCacheKeys[avID] = keys
}
keys[key] = struct{}{}
+ return
}
func RemoveAVDataInBox(avID, boxID string) {
key := avCacheKey(avID, boxID)
avCache.Del(key)
- removeAVSearchDataByKey(key)
+ invalidateAVSearchDataByKey(key)
avCacheKeysLock.Lock()
defer avCacheKeysLock.Unlock()
@@ -97,9 +124,9 @@ func RemoveAVData(avID string) {
avCache.Del(avCacheKey(avID, ""))
for key := range keys {
avCache.Del(key)
- removeAVSearchDataByKey(key)
+ invalidateAVSearchDataByKey(key)
}
- removeAVSearchDataByKey(avCacheKey(avID, ""))
+ invalidateAVSearchDataByKey(avCacheKey(avID, ""))
}
func ClearAVCache() {
@@ -109,32 +136,44 @@ func ClearAVCache() {
avCache.Clear()
avSearchDataCacheLock.Lock()
- avSearchDataCache = map[string]any{}
+ avDataVersions = map[string]uint64{}
+ avSearchDataCache = map[string]*avSearchDataEntry{}
avSearchDataCacheLock.Unlock()
}
func GetAVSearchDataInBox[T any](avID, boxID string) (ret T, ok bool) {
avSearchDataCacheLock.RLock()
- data := avSearchDataCache[avCacheKey(avID, boxID)]
+ key := avCacheKey(avID, boxID)
+ entry := avSearchDataCache[key]
+ version := avDataVersions[key]
avSearchDataCacheLock.RUnlock()
- if data == nil {
+ if entry == nil || entry.version != version {
return
}
- ret, ok = data.(T)
+ ret, ok = entry.data.(T)
return
}
-func SetAVSearchDataInBox(avID, boxID string, data any) {
- if data == nil {
+func SetAVSearchDataInBox(avID, boxID string, version uint64, data any) (ok bool) {
+ if data == nil || version == 0 {
return
}
avSearchDataCacheLock.Lock()
- avSearchDataCache[avCacheKey(avID, boxID)] = data
- avSearchDataCacheLock.Unlock()
+ defer avSearchDataCacheLock.Unlock()
+ key := avCacheKey(avID, boxID)
+ if avDataVersions[key] != version {
+ return
+ }
+ avSearchDataCache[key] = &avSearchDataEntry{data: data, version: version}
+ return true
}
-func removeAVSearchDataByKey(key string) {
+func invalidateAVSearchDataByKey(key string) (version uint64) {
avSearchDataCacheLock.Lock()
+ avDataVersion++
+ version = avDataVersion
+ avDataVersions[key] = version
delete(avSearchDataCache, key)
avSearchDataCacheLock.Unlock()
+ return
}
diff --git a/kernel/cache/av_test.go b/kernel/cache/av_test.go
index 789549f619..0c9991e247 100644
--- a/kernel/cache/av_test.go
+++ b/kernel/cache/av_test.go
@@ -22,17 +22,28 @@ func TestAVSearchDataInvalidation(t *testing.T) {
const avID = "20260728120000-search"
const boxID = "20260728120000-box"
- SetAVSearchDataInBox(avID, boxID, "cached")
+ version := SetAVDataWithVersionInBox(avID, boxID, []byte(`{"name":"old"}`))
+ if !SetAVSearchDataInBox(avID, boxID, version, "cached") {
+ t.Fatal("failed to cache search data")
+ }
if cached, ok := GetAVSearchDataInBox[string](avID, boxID); !ok || cached != "cached" {
t.Fatalf("unexpected cached search data: %q, %v", cached, ok)
}
- SetAVDataInBox(avID, boxID, []byte("{}"))
+ newVersion := SetAVDataWithVersionInBox(avID, boxID, []byte(`{"name":"new"}`))
if _, ok := GetAVSearchDataInBox[string](avID, boxID); ok {
t.Fatal("setting AV data should invalidate search data")
}
+ if SetAVSearchDataInBox(avID, boxID, version, "stale") {
+ t.Fatal("stale search data should not be cached")
+ }
+ if _, ok := GetAVSearchDataInBox[string](avID, boxID); ok {
+ t.Fatal("stale search data should remain invalid")
+ }
- SetAVSearchDataInBox(avID, boxID, "cached")
+ if !SetAVSearchDataInBox(avID, boxID, newVersion, "cached") {
+ t.Fatal("failed to cache current search data")
+ }
RemoveAVDataInBox(avID, boxID)
if _, ok := GetAVSearchDataInBox[string](avID, boxID); ok {
t.Fatal("removing AV data should invalidate search data")
diff --git a/kernel/cli/cmd/database.go b/kernel/cli/cmd/database.go
index f14d2b5995..3db74890ee 100644
--- a/kernel/cli/cmd/database.go
+++ b/kernel/cli/cmd/database.go
@@ -40,7 +40,7 @@ var databaseSearchCmd = &cobra.Command{
Short: "Search databases by name",
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
- results := model.SearchAttributeView(model.SearchAttributeViewOptions{Keyword: args[0]})
+ results := model.SearchAttributeView(args[0], nil, "", "")
switch outputFormat {
case "json":
data, _ := json.MarshalIndent(results, "", " ")
diff --git a/kernel/mcp/tools/database.go b/kernel/mcp/tools/database.go
index c14f694dc2..5e7590841c 100644
--- a/kernel/mcp/tools/database.go
+++ b/kernel/mcp/tools/database.go
@@ -102,7 +102,7 @@ func databaseSearch(args map[string]any) (CallToolResult, error) {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "keyword is required"}}, IsError: true}, nil
}
- results := model.SearchAttributeView(model.SearchAttributeViewOptions{Keyword: keyword})
+ results := model.SearchAttributeView(keyword, nil, "", "")
if len(results) == 0 {
return CallToolResult{Content: []ContentItem{{Type: "text", Text: "no attribute views found"}}}, nil
}
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 877e752252..72f197cf1a 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -2707,7 +2707,16 @@ func matchAttributeViewSearchName(name string, keywords []string) (score float64
return
}
-func SearchAttributeView(options SearchAttributeViewOptions) (ret []*AvSearchResult) {
+func SearchAttributeView(keyword string, excludeAvIDs []string, currentAvID, currentBlockID string) []*AvSearchResult {
+ return SearchAttributeViewWithOptions(SearchAttributeViewOptions{
+ Keyword: keyword,
+ ExcludeAvIDs: excludeAvIDs,
+ CurrentAvID: currentAvID,
+ CurrentBlockID: currentBlockID,
+ })
+}
+
+func SearchAttributeViewWithOptions(options SearchAttributeViewOptions) (ret []*AvSearchResult) {
waitForSyncingStorages()
ret = []*AvSearchResult{}