OSDN Git Service

compress .lua cache by gzip
[luatex-ja/luatexja.git] / src / ltj-base.lua
index 1ed8ab8..d8a758e 100644 (file)
@@ -1,5 +1,5 @@
 --
--- luatexja/ltj-base.lua
+-- ltj-base.lua
 --
 local ltb = luatexbase
 local tostring = tostring
@@ -185,7 +185,7 @@ end
 -- not used in current LuaTeX-ja
 do
 --! ixbase.counter と同じ
-  counter = {}
+  local counter = {}
   local mt_counter = {}
   setmetatable(counter, mt_counter)
 
@@ -231,6 +231,7 @@ end
 
 require('lualibs-lpeg') -- string.split
 require('lualibs-os')   -- os.type
+require('lualibs-gzip') -- gzip.*
 
 do
    local kpse_var_value = kpse.var_value
@@ -248,6 +249,7 @@ do
    local join, isreadable = file.join, file.isreadable
    local tofile, serialize = table.tofile, table.serialize
    local luc_suffix = jit and '.lub' or '.luc'
+   local dump = string.dump
 
    -- determine save path
    local savepath = ''
@@ -256,43 +258,44 @@ do
       if not lfs.isdir(testpath) then dir.mkdirs(testpath) end
       if lfs.isdir(testpath) then savepath = testpath; break end
    end
-
-   save_cache_luc = function (filename, t, serialized)
+   local serial_spec = {functions=false, noquotes=true}
+   local function save_cache_luc(filename, t, serialized)
       local fullpath = savepath .. '/' ..  filename .. luc_suffix
-      local s = serialized or serialize(t, 'return', false)
+      local s = serialized or serialize(t, 'return', false, serial_spec)
       if s then
         local sa = load(s)
         local f = io.open(fullpath, 'wb')
         if f and sa then 
-           f:write(string.dump(sa, true)) 
+           f:write(dump(sa, true)) 
            texio.write('(save cache: ' .. fullpath .. ')')
+            f:close()
         end
-        f:close()
       end
    end
 
-   save_cache = function (filename, t)
-      local fullpath = savepath .. '/' ..  filename .. '.lua'
-      local s = serialize(t, 'return', false)
+   local function save_cache(filename, t)
+      local fullpath = savepath .. '/' ..  filename .. '.lua.gz'
+      local s = serialize(t, 'return', false, serial_spec)
       if s then
-        local f = io.open(fullpath, 'w')
-        if f then 
-           f:write(s) 
-           texio.write('(save cache: ' .. fullpath .. ')')
-        end
-        f:close()
-        save_cache_luc(filename, t, s)
+         gzip.save(fullpath, s, 1)
+         texio.write('(save cache: ' .. fullpath .. ')')
+         save_cache_luc(filename, t, s)
       end
    end
 
-   local function load_cache_a (filename, outdate)
+   local function load_cache_a(filename, outdate, compressed)
       local result
       for _,v in pairs(path) do
         local fn = join(v, cache_dir, filename)
-        if isreadable(fn) then 
+        if isreadable(fn) then
            texio.write('(load cache: ' .. fn .. ')')
-           result = loadfile(fn)
-           result = result and result(); break
+           if compressed then
+             result = loadstring(gzip.load(fn))
+           else
+             result = loadfile(fn)
+           end
+           result = result and result()
+           break
         end
       end
       if (not result) or outdate(result) then 
@@ -302,17 +305,29 @@ do
       end
    end
    
-   load_cache = function (filename, outdate)
-      local r = load_cache_a(filename ..  luc_suffix, outdate)
+   local function load_cache(filename, outdate)
+      local r = load_cache_a(filename ..  luc_suffix, outdate, false)
       if r then 
         return r
       else
-         local r = load_cache_a(filename .. '.lua', outdate)
+         local r = load_cache_a(filename .. '.lua.gz', outdate, true)
         if r then save_cache_luc(filename, r) end -- update the precompiled cache
         return r
       end
    end
 
+   local function remove_file_if_exist(name)
+     if os.rename(name,name) then os.remove(name) end
+   end
+   local function remove_cache (filename)
+      local fullpath_wo_ext = savepath .. '/' ..  filename .. '.lu'
+      remove_file_if_exist(fullpath_wo_ext .. 'a')
+      remove_file_if_exist(fullpath_wo_ext .. 'a.gz')
+      remove_file_if_exist(fullpath_wo_ext .. 'b')
+      remove_file_if_exist(fullpath_wo_ext .. 'c')
+   end
+
+   ltjb.remove_cache = remove_cache
    ltjb.load_cache = load_cache
    ltjb.save_cache_luc = save_cache_luc
    ltjb.save_cache = save_cache
@@ -362,10 +377,10 @@ function ltjb.add_to_callback(name,fun,description,priority)
        "resetting exclusive callback: " .. name)
        luatexbase.reset_callback(name)
     end
-    local saved_callback={},ff,dd
+    local saved_callback={}
     for k,v in ipairs(luatexbase.callback_descriptions(name)) do
        if k >= priority then
-           ff,dd= luatexbase.remove_from_callback(name, v)
+           local ff,dd = luatexbase.remove_from_callback(name, v)
            saved_callback[#saved_callback+1]={ff,dd}
        end
     end