librime-lua icon indicating copy to clipboard operation
librime-lua copied to clipboard

LevelDb 失效(在最新weasel 0.16.3.0中).求助!

Open ruhchjdbdbhccjjc opened this issue 1 year ago • 16 comments

Windows11最近从小狼毫 0.15.0升级到0.16.3.0,升级后,lua脚本中LevelDb (https://github.com/hchunhui/librime-lua/wiki/Scripting#leveldb--%E4%B8%8D%E5%8F%AF%E7%94%A8%E6%96%BC%E5%B7%B2%E9%96%8B%E5%95%93%E7%9A%84userdb-%E5%B0%88%E7%94%A8%E6%96%BC-librime-lua-key-value-db) 失效,无法查询到值。输出值只有: XX types.cc:1459] /db_name XX XX types.cc:1459] /db_typeuserdb XX types.cc:1459] /rime_version1.9.0 XX types.cc:1459] /user_id115dfca7-c533-4b8c-b36b-18ee98f98ecd 有任何建议吗?

ruhchjdbdbhccjjc avatar Nov 07 '24 09:11 ruhchjdbdbhccjjc

看起來開檔很正常,只是沒有字典資料 ,會不會是開錯leveldb 了

shewer avatar Nov 08 '24 03:11 shewer

是查询空值字符串返回的所有值。遍历值得到的日志。

local dict = XX:query('') -- return obj of DbAccessor

for k,v in dict:iter() do 
       print(k,v) 
end

ruhchjdbdbhccjjc avatar Nov 08 '24 08:11 ruhchjdbdbhccjjc

LevelDb 要開啟 user_dict db 只能在 offline 操作 translator 使用中的 db , 無法再開啓 LevelDb 會出現錯誤 ,反之也是一樣 當初加入目的是提供librime-lua 可以存取大資料庫

建議 操作 user_dict 用 Memory , ScriptTranslator , TableTranslator 中的 user_dict

shewer avatar Nov 08 '24 23:11 shewer

谢谢。可能是误解。

--dict is not dict  db data ,is additional db data (especial made db data for english query ).
local dict = XX:query('') -- return obj of DbAccessor

for k,v in dict:iter() do 
       print(k,v) 
end

不是很明白。又确认了。weasel 0.15.0以后的版本,都不行,都会出错。似乎是版本问题。

ruhchjdbdbhccjjc avatar Nov 09 '24 08:11 ruhchjdbdbhccjjc

[0][./lua/selector.lua:33] → --在 luna_pinyin 沒打開前 測試 luna_pinyin.db

[0][./lua/selector.lua:33] → db = LevelDb('luna_pinyin') 7LuaTypeISt10shared_ptrIN4rime2DbEEE: 0x582586df3ec8 [0][./lua/selector.lua:33] → db:open() true [0][./lua/selector.lua:33] → for k,v in db:query(''):iter() do print(k,v) end /db_name luna_pinyin /db_type userdb /rime_version 1.10.0 /tick 20 /user_id 4e3e709f-d1d0-4987-bb7b-405be45217a6 abzz c=1 d=1 t=1 a te te 啊特特 c=1 d=1 t=20 ab 鐱 c=2 d=2 t=10 ab 鐱 c=2 d=2 t=18 abz abz c=1 d=1 t=2 abz abz c=1 d=0.960789 t=18 ai ta 愛他 c=1 d=1 t=19 e 額 c=1 d=0.965605 t=18 mei 沒 c=0 d=0.0970446 t=18 mei guo 美國 c=0 d=0.0980199 t=18 ta 他 c=0 d=0.0965605 t=18 ta xi tong xi wang 他系統希望 c=1 d=0.970446 t=18 tian xia 天下 c=2 d=1.98506 t=18 tian xia wei gong 天下爲公 c=1 d=1 t=18 tong xing ai 同性愛 c=0 d=0.194575 t=18 tong xing ai mei 同性愛沒 c=1 d=0.97531 t=18 tong xing ai wa gong 同性愛瓦工 c=1 d=0.980199 t=18 tong xue 同學 c=0 d=0.0980199 t=18 tong xue mei guo 同學美國 c=1 d=0.985112 t=18 wa gong 瓦工 c=0 d=0.097531 t=18 xi tong 系統 c=0 d=0.0965605 t=18 xi wang 希望 c=0 d=0.0965605 t=18


如果沒有關閉 db 啓動 luna_pinyin`` translator 時,就會發生錯誤 E20241109 16:42:45.237229 138530841136704 level_db.cc:259] Error opening db 'luna_pinyin': IO error: lock /home/shewer/proj/librime-12/debug/bin/./luna_pinyin.userdb/LOCK: already held by process

shewer avatar Nov 09 '24 08:11 shewer

又试了,0.15.0的确可像上面一样输出db值(使用复制用户词典db),0.15.0以后的版本无法输出值。 日志中有这样一条异常: error [nil:load_dict:14] : not found file from user_data or sharded_data

ruhchjdbdbhccjjc avatar Nov 09 '24 13:11 ruhchjdbdbhccjjc

可能找到問題了, librime 1.10 有一次異動 增加 class path 替代 string 最近才修正 lua 的function get_user_data_path() get_shared_data_path() 可能要暫時避開這兩個function https://github.com/hchunhui/librime-lua/pull/365

或是更新 librime-lua 版本

shewer avatar Nov 09 '24 14:11 shewer

#! /usr/bin/env lua
--
-- leveldb.lua
-- Copyright (C) 2022 Shewer Lu <[email protected]>
--
-- Distributed under terms of the MIT license.
--
-- 
--[[
leveldb 可以使用KEY 查詢 value 或是 query 查詢 frefix key (字元排序 而不是用trie)

ex ab< abc < ac < ad

-- rime.lua

user_tran=require 'leveldb'
-- schema.yaml
# append to engine/translations
lua_translator@user_tran@ecdict

ecdict:
  dictionary: ecdictdb
  initial_quality: 1.5

--]]

db_pool_={}
local function opendb(fn,dbname) 
  dbname= dbname or ""
  local db = db_pool_[fn]
  if not db then 
    log.info("open db")
    db = LevelDb(fn,dbname) 
    if not db then 
      --do not see failed log in log 
      log.info("open db fail")
      return nil 
    end
    db_pool_[fn] = db
  end
  if not db:loaded() then 
    --can see sucess log info in log
    log.info("db:loaded")
    db:open() 
  end
    
  return db
end
local function init_data(db)
  local tab = {
    ab = "于",
    cd = "金",
    ac = "金金",
  }
  for k,v in next,tab do
    db:update(k,v)
  end
end

local M={}
function M.init(env)
  local config = env.engine.schema.config

  -- system version : windows 11 ,weasel version : 0.16.3
  -- db file path : "C:\Users\Administrator\AppData\Roaming\Rime\ecdictdb"
  local dbname = config:get_string(env.name_space.."/dictionary")
  

  env.quality= tonumber( config:get_string(env.name_space .. "/initial_quality") ) or 1
  env.db = assert(opendb(dbname,'english_dict'), "leveldb cand not init")
  init_data(env.db)
end

function M.fini(env)
  env.db:close()
end

function M.func(inp,seg,env)

for k,v in env.db:query(""):iter() do 
    log.info("from leveldb"..k.."="..v)
    local type_ = k == inp and "udata" or "comp_udada"
    local cand = Candidate(type_,seg.start,seg._end,k,v)
    cand.quality= env.quality
    yield(cand)
end



嘗試后,仍然失敗。 上面的是代碼,日志中可以看到加載成功了。但仍然無法查詢到值。

ruhchjdbdbhccjjc avatar Nov 09 '24 16:11 ruhchjdbdbhccjjc

這 code 是 使用 luarocks leveldb 的庫,在librime-lua 沒有加入 LevelDb api 前的版本 require 'leveldb' -- <<-----

我不知你的版本可能要查 https://github.com/hchunhui/librime-lua/pulls?q=is%3Apr+is%3Aclosed++LevelDb 你可以用 lua script 檢查

   if UserDb then
        最新 
   elseif LevelDb then
        初版
   else
        沒有支援 LlevelDb
   end

UserDb(db_name[, "tabledb" |"stabledb" |"userdb" |"plain_userdb"]) LevelDb(db_name) -- == UserDb(db_name, "userdb") TableDb(db_name) -- == UserDb(db_name, "plain_userdb")

shewer avatar Nov 09 '24 18:11 shewer

你使用的script ,移除 require 'leveldb' 可能就可以用了 function opendb() -- 是爲了解決重覆開啓 leveldb 的問題 如果沒有這問題,可以簡單一點 , 直接使用 LevelDb(...)

 local M = {}
function M.init(env)
    env.db = LevelDb(..)
end
function M.fini(env)
    env.db:close() 
end
.....
return M

shewer avatar Nov 09 '24 18:11 shewer

#! /usr/bin/env lua
--
-- leveldb.lua
-- Copyright (C) 2022 Shewer Lu <[email protected]>
--
-- Distributed under terms of the MIT license.
--
-- 
--[[
leveldb 可以使用KEY 查詢 value 或是 query 查詢 frefix key (字元排序 而不是用trie)

ex ab< abc < ac < ad

-- rime.lua

--change file name frome LevelDb to leveldb_rime


user_tran=require 'leveldb_rime'
-- schema.yaml
# append to engine/translations
lua_translator@user_tran@ecdict

ecdict:
  dictionary: ecdictdb
  initial_quality: 1.5

--]]

db_pool_={}
local function opendb(fn,dbname) 
  
  if UserDb then
     -- in log file ,is UserDb and version is Ver: librime 1.11.2 librime-lua 177 lua Lua 5.4
      log.info("最新"..Rime_Ver_info()) 
   elseif LevelDb then
      log.info("初版")
   else
       log.info("沒有支援 LlevelDb")
   end

  dbname= dbname or ""
  local db = db_pool_[fn]
  if not db then 
    log.info("open db")
   -- db = LevelDb(fn,dbname) 

   --change leveldb to UserDb ,but is the same query result
    db = UserDb(fn, "userdb")
    if not db then 
      --do not see failed log in log 
      log.info("open db fail")
      return nil 
    end
    db_pool_[fn] = db
  end
  if not db:loaded() then 
    --can see sucess log info in log
    log.info("db:loaded")
    db:open() 
  end
    
  return db
end



local function Rime_Version()
  local ver
  if LevelDb then
    ver = 177
  elseif Opencc then
    ver = 147
  elseif KeySequence and KeySequence().repr then
    ver= 139
  elseif  ConfigMap and ConfigMap().keys then
    ver= 127
  elseif Projection then
    ver= 102
  elseif KeyEvent then
    ver = 100
  elseif Memory then
    ver = 80
  else
    ver= 79
  end
  return ver
end


function Rime_Ver_info()
  return string.format("Ver: librime %s librime-lua %s lua %s",
  rime_api.get_rime_version() , Rime_Version() ,_VERSION )
end


local function init_data(db)
  local tab = {
    ab = "于",
    cd = "金",
    ac = "金金",
  }
  for k,v in next,tab do
    db:update(k,v)
  end
end

local M={}
function M.init(env)
  local config = env.engine.schema.config

  -- system version : windows 11 ,weasel version : 0.16.3
  -- db file path : "C:\Users\Administrator\AppData\Roaming\Rime\ecdictdb"
  local dbname = config:get_string(env.name_space.."/dictionary")
  env.quality= tonumber( config:get_string(env.name_space .. "/initial_quality") ) or 1
env.db = assert(opendb(dbname,' dict'), "leveldb cand not init")



  init_data(env.db)
end

function M.fini(env)
  env.db:close()
end

function M.func(inp,seg,env)

for k,v in env.db:query(""):iter() do 
    log.info("from leveldb"..k.."="..v)
    local type_ = k == inp and "udata" or "comp_udada"
    local cand = Candidate(type_,seg.start,seg._end,k,v)
    cand.quality= env.quality
    yield(cand)

    -- the result is 
    --[[
    /db_name
    /db_type
    /rime_version
    /user_id
    ab
    ac
    cd
    --]]
end



end

return M

这边更改后,结果是一样。

ruhchjdbdbhccjjc avatar Nov 10 '24 05:11 ruhchjdbdbhccjjc

#! /usr/bin/env lua
--
-- leveldb.lua
-- Copyright (C) 2022 Shewer Lu <[email protected]>
--
-- Distributed under terms of the MIT license.
--
-- 
--[[
leveldb 可以使用KEY 查詢 value 或是 query 查詢 frefix key (字元排序 而不是用trie)

ex ab< abc < ac < ad

-- rime.lua

--change file name frome LevelDb to leveldb_rime


user_tran=require 'leveldb_rime'
-- schema.yaml
# append to engine/translations
lua_translator@user_tran@ecdict

ecdict:
  dictionary: ecdictdb
  initial_quality: 1.5

--]]

db_pool_={}
local function opendb(fn,dbname) 
  
  if UserDb then
     -- in log file ,is UserDb and version is Ver: librime 1.11.2 librime-lua 177 lua Lua 5.4
      log.info("最新"..Rime_Ver_info()) 
   elseif LevelDb then
      log.info("初版")
   else
       log.info("沒有支援 LlevelDb")
   end

  dbname= dbname or ""
  local db = db_pool_[fn]
  if not db then 
    log.info("open db")
   -- db = LevelDb(fn,dbname) 

   --change leveldb to UserDb ,but is the same query result
    db = UserDb(fn, "userdb")
    if not db then 
      --do not see failed log in log 
      log.info("open db fail")
      return nil 
    end
    db_pool_[fn] = db
  end
  if not db:loaded() then 
    --can see sucess log info in log
    log.info("db:loaded")
    db:open() 
  end
    
  return db
end



local function Rime_Version()
  local ver
  if LevelDb then
    ver = 177
  elseif Opencc then
    ver = 147
  elseif KeySequence and KeySequence().repr then
    ver= 139
  elseif  ConfigMap and ConfigMap().keys then
    ver= 127
  elseif Projection then
    ver= 102
  elseif KeyEvent then
    ver = 100
  elseif Memory then
    ver = 80
  else
    ver= 79
  end
  return ver
end


function Rime_Ver_info()
  return string.format("Ver: librime %s librime-lua %s lua %s",
  rime_api.get_rime_version() , Rime_Version() ,_VERSION )
end

這是一個 example 測試寫入資料到db中

local function init_data(db) local tab = { ab = "于", cd = "金", ac = "金金", } for k,v in next,tab do db:update(k,v) end end

local M={} function M.init(env) local config = env.engine.schema.config

-- system version : windows 11 ,weasel version : 0.16.3 -- db file path : "C:\Users\Administrator\AppData\Roaming\Rime\ecdictdb" local dbname = config:get_string(env.name_space.."/dictionary") env.quality= tonumber( config:get_string(env.name_space .. "/initial_quality") ) or 1 env.db = assert(opendb(dbname,' dict'), "leveldb cand not init")

init_data(env.db) end

function M.fini(env) env.db:close() end

function M.func(inp,seg,env)

for k,v in env.db:query(""):iter() do log.info("from leveldb"..k.."="..v) local type_ = k == inp and "udata" or "comp_udada" local cand = Candidate(type_,seg.start,seg._end,k,v) cand.quality= env.quality yield(cand)

-- the result is 
    這個result 是上面測試leveldb 的資料吧  ,是不是表示開啓一個新的db  ,
    下面的亂碼應是 utf-8 ,
    是不是也表示  LevelDb 開啓一個沒有你預期已存在 db,所以新建一個
--[[     
�/db_name
�/db_type
�/rime_version
�/user_id
ab
ac
cd
--]]

end

end

return M


这边更改后,结果是一样。

shewer avatar Nov 10 '24 05:11 shewer

我描述有问题。

-- the result is this env.db:query("") key
for k,v in env.db:query(""):iter() do
end

ruhchjdbdbhccjjc avatar Nov 10 '24 06:11 ruhchjdbdbhccjjc

local function init_data(db)
local tab = {
ab = "于",
cd = "金",
ac = "金金",
}
for k,v in next,tab do
db:update(k,v)
end
end

你說明 result 也有上面的資料 表示 LevelDb 開檔 寫入 查詢 都沒有問題

shewer avatar Nov 10 '24 06:11 shewer

好吧! 那就只能退回0.15.0版本了,找不出原因。 非常感谢。

ruhchjdbdbhccjjc avatar Nov 10 '24 08:11 ruhchjdbdbhccjjc

你的問題是 librime-lua LevelDb 失效 ,可是從測試看來 沒問題哇 要不你回方案作者問問

shewer avatar Nov 10 '24 09:11 shewer