[yajl] add a yajl_reset() so we do not have to do yajl_alloc for every line

This commit is contained in:
Timothy Stack 2014-10-29 06:42:45 -07:00
parent ccee7caa32
commit 6c1dfe3521
4 changed files with 30 additions and 15 deletions

View File

@ -566,16 +566,13 @@ bool external_log_format::scan(std::vector<logline> &dst,
shared_buffer_ref &sbr)
{
if (this->jlf_json) {
auto_mem<yajl_handle_t> handle(yajl_free);
yajlpp_parse_context &ypc = *(this->jlf_parse_context);
logline ll(offset, 0, 0, logline::LEVEL_INFO);
yajl_handle handle = this->jlf_yajl_handle.in();
json_log_userdata jlu(sbr);
bool retval = false;
handle = yajl_alloc(&this->jlf_parse_context->ypc_callbacks,
NULL,
this->jlf_parse_context.get());
yajl_config(handle, yajl_dont_validate_strings, 1);
yajl_reset(handle);
ypc.set_static_handler(json_log_handlers[0]);
ypc.ypc_userdata = &jlu;
ypc.ypc_ignore_unused = true;
@ -586,9 +583,9 @@ bool external_log_format::scan(std::vector<logline> &dst,
jlu.jlu_line_value = sbr.get_data();
jlu.jlu_line_size = sbr.length();
jlu.jlu_handle = handle;
if (yajl_parse(handle.in(),
if (yajl_parse(handle,
(const unsigned char *)sbr.get_data(), sbr.length()) == yajl_status_ok &&
yajl_complete_parse(handle.in()) == yajl_status_ok) {
yajl_complete_parse(handle) == yajl_status_ok) {
for (int lpc = 0; lpc < jlu.jlu_sub_line_count; lpc++) {
ll.set_sub_offset(lpc);
if (lpc > 0) {
@ -600,7 +597,7 @@ bool external_log_format::scan(std::vector<logline> &dst,
retval = true;
}
else {
unsigned char *msg = yajl_get_error(handle.in(), 1, (const unsigned char *)sbr.get_data(), sbr.length());
unsigned char *msg = yajl_get_error(handle, 1, (const unsigned char *)sbr.get_data(), sbr.length());
log_debug("bad line %s", msg);
}
@ -850,9 +847,9 @@ void external_log_format::get_subline(const logline &ll, shared_buffer_ref &sbr)
}
if (this->jlf_cached_offset != ll.get_offset()) {
auto_mem<yajl_handle_t> handle(yajl_free);
yajlpp_parse_context &ypc = *(this->jlf_parse_context);
view_colors &vc = view_colors::singleton();
yajl_handle handle = this->jlf_yajl_handle.in();
json_log_userdata jlu(sbr);
this->jlf_share_manager.invalidate_refs();
@ -861,10 +858,7 @@ void external_log_format::get_subline(const logline &ll, shared_buffer_ref &sbr)
this->jlf_line_offsets.clear();
this->jlf_line_attrs.clear();
handle = yajl_alloc(&this->jlf_parse_context->ypc_callbacks,
NULL,
this->jlf_parse_context.get());
yajl_config(handle, yajl_dont_validate_strings, 1);
yajl_reset(handle);
ypc.set_static_handler(json_log_rewrite_handlers[0]);
ypc.ypc_userdata = &jlu;
ypc.ypc_ignore_unused = true;
@ -877,10 +871,10 @@ void external_log_format::get_subline(const logline &ll, shared_buffer_ref &sbr)
jlu.jlu_handle = handle;
jlu.jlu_line_value = sbr.get_data();
yajl_status parse_status = yajl_parse(handle.in(),
yajl_status parse_status = yajl_parse(handle,
(const unsigned char *)sbr.get_data(), sbr.length());
if (parse_status == yajl_status_ok &&
yajl_complete_parse(handle.in()) == yajl_status_ok) {
yajl_complete_parse(handle) == yajl_status_ok) {
std::vector<logline_value>::iterator lv_iter;
std::vector<json_format_element>::iterator iter;
bool used_values[this->jlf_line_values.size()];
@ -1088,6 +1082,11 @@ void external_log_format::build(std::vector<std::string> &errors)
}
if (this->jlf_json) {
this->jlf_parse_context.reset(new yajlpp_parse_context(this->elf_name));
this->jlf_yajl_handle.reset(yajl_alloc(
&this->jlf_parse_context->ypc_callbacks,
NULL,
this->jlf_parse_context.get()));
yajl_config(this->jlf_yajl_handle.in(), yajl_dont_validate_strings, 1);
}
}

View File

@ -664,6 +664,7 @@ public:
elf_body_field(intern_string::lookup("body", -1)),
jlf_json(false),
jlf_cached_offset(-1),
jlf_yajl_handle(yajl_free),
elf_name(name) {
this->jlf_line_offsets.reserve(128);
};
@ -695,6 +696,11 @@ public:
if (this->jlf_json) {
this->jlf_parse_context.reset(new yajlpp_parse_context(this->elf_name));
this->jlf_yajl_handle.reset(yajl_alloc(
&this->jlf_parse_context->ypc_callbacks,
NULL,
this->jlf_parse_context.get()));
yajl_config(this->jlf_yajl_handle.in(), yajl_dont_validate_strings, 1);
this->jlf_cached_line.reserve(1024 * 1024);
}
else if (this->lf_fmt_lock != -1) {
@ -786,6 +792,7 @@ public:
std::vector<char> jlf_cached_line;
string_attrs_t jlf_line_attrs;
std::auto_ptr<yajlpp_parse_context> jlf_parse_context;
auto_mem<yajl_handle_t> jlf_yajl_handle;
private:
const std::string elf_name;

View File

@ -165,6 +165,8 @@ extern "C" {
*/
YAJL_API int yajl_config(yajl_handle h, yajl_option opt, ...);
YAJL_API void yajl_reset(yajl_handle handle);
/** free a parser handle */
YAJL_API void yajl_free(yajl_handle handle);

View File

@ -102,6 +102,13 @@ yajl_config(yajl_handle h, yajl_option opt, ...)
return rv;
}
void
yajl_reset(yajl_handle handle)
{
handle->bytesConsumed = 0;
yajl_bs_push(handle->stateStack, yajl_state_start);
}
void
yajl_free(yajl_handle handle)
{