Skip to content

Commit b380e95

Browse files
committed
Use JSON instead of Yajl to improve performance of store/load files
Signed-off-by: Shizuo Fujita <[email protected]>
1 parent b9fc438 commit b380e95

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

lib/fluent/plugin/formatter_out_file.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
require 'fluent/plugin/formatter'
1818
require 'fluent/time'
19-
require 'yajl'
19+
require 'json'
2020

2121
module Fluent
2222
module Plugin
@@ -46,7 +46,7 @@ def format(tag, time, record)
4646
header = ''
4747
header << "#{@timef.format(time)}#{@delimiter}" if @output_time
4848
header << "#{tag}#{@delimiter}" if @output_tag
49-
"#{header}#{Yajl.dump(record)}#{@newline}"
49+
"#{header}#{JSON.generate(record)}#{@newline}"
5050
end
5151
end
5252
end

lib/fluent/plugin/storage_local.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
require 'fluent/plugin/storage'
2020

2121
require 'fileutils'
22-
require 'yajl'
22+
require 'json'
2323

2424
module Fluent
2525
module Plugin
@@ -90,7 +90,7 @@ def configure(conf)
9090
log.warn "detect empty plugin storage file during startup. Ignored: #{@path}"
9191
return
9292
end
93-
data = Yajl::Parser.parse(data)
93+
data = JSON.parse(data)
9494
raise Fluent::ConfigError, "Invalid contents (not object) in plugin storage file: '#{@path}'" unless data.is_a?(Hash)
9595
rescue => e
9696
log.error "failed to read data from plugin storage file", path: @path, error: e
@@ -114,7 +114,7 @@ def load
114114
return unless File.exist?(@path)
115115
begin
116116
json_string = File.open(@path, 'r:utf-8'){ |io| io.read }
117-
json = Yajl::Parser.parse(json_string)
117+
json = JSON.parse(json_string)
118118
unless json.is_a?(Hash)
119119
log.error "broken content for plugin storage (Hash required: ignored)", type: json.class
120120
log.debug "broken content", content: json_string
@@ -130,7 +130,11 @@ def save
130130
return if @on_memory
131131
tmp_path = @path + '.tmp.' + Fluent::UniqueId.hex(Fluent::UniqueId.generate)
132132
begin
133-
json_string = Yajl::Encoder.encode(@store, pretty: @pretty_print)
133+
if @pretty_print
134+
json_string = JSON.pretty_generate(@store)
135+
else
136+
json_string = JSON.generate(@store)
137+
end
134138
File.open(tmp_path, 'w:utf-8', @mode) { |io| io.write json_string; io.fsync }
135139
File.rename(tmp_path, @path)
136140
rescue => e

0 commit comments

Comments
 (0)