56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
//go:build !windows
|
||
|
||
// 织忆 MemoryWeave — 存储+图谱初始化(非 Windows,SQLite CGO 可用)
|
||
package api
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
|
||
"github.com/xiaoxue/memoryweave/internal/governance"
|
||
"github.com/xiaoxue/memoryweave/internal/storage"
|
||
)
|
||
|
||
// initStorageBackend 初始化存储后端(非 Windows:LanceDB + SQLite + 内存)
|
||
func initStorageBackend(backend string, emb *storage.Embedder) storage.LanceDB {
|
||
switch backend {
|
||
case "lancedb":
|
||
sockPath := os.Getenv("LANCEDB_SOCKET")
|
||
if sockPath == "" {
|
||
sockPath = "/tmp/zhiyi-ipc.sock"
|
||
}
|
||
ldb := storage.NewRustLanceDBClient(sockPath, emb)
|
||
log.Printf("[zhiyid] 存储后端: LanceDB (Rust IPC) — %s", sockPath)
|
||
return ldb
|
||
case "sqlite":
|
||
dbPath := os.Getenv("SQLITE_PATH")
|
||
if dbPath == "" {
|
||
dbPath = "/var/lib/memoryweave/zhiyi.db"
|
||
}
|
||
sqliteDB, err := storage.NewSQLiteClient(dbPath)
|
||
if err != nil {
|
||
log.Printf("[zhiyid] SQLite 初始化失败 (%v),降级为内存存储", err)
|
||
return storage.NewMemLanceClient(emb)
|
||
}
|
||
log.Printf("[zhiyid] 存储后端: SQLite (CGO) — %s", dbPath)
|
||
return sqliteDB
|
||
default:
|
||
log.Printf("[zhiyid] 存储后端: 内存(零依赖)")
|
||
return storage.NewMemLanceClient(emb)
|
||
}
|
||
}
|
||
|
||
// initGraphStore 初始化图谱(非 Windows:SQLite 图谱 + 内存降级)
|
||
func initGraphStore() governance.GraphStore {
|
||
graphPath := os.Getenv("GRAPH_PATH")
|
||
if graphPath == "" {
|
||
graphPath = "/var/lib/memoryweave/graph.db"
|
||
}
|
||
gs, err := governance.NewSQLiteGraphStore(graphPath)
|
||
if err != nil {
|
||
log.Printf("[zhiyid] WARN: SQLite 图谱初始化失败 (%v),降级为 InMemoryGraph", err)
|
||
return governance.NewInMemoryGraph()
|
||
}
|
||
log.Printf("[zhiyid] 图谱后端: SQLiteGraphStore — %s", graphPath)
|
||
return gs
|
||
} |