1 /**
2 * Copyright (c) 2020, Timothy Stack
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * * Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * * Neither the name of Timothy Stack nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "config.h"
31
32 #include "sql_util.hh"
33 #include "column_namer.hh"
34 #include "log_search_table.hh"
35
36 const static std::string LOG_MSG_INSTANCE = "log_msg_instance";
37 static auto instance_name = intern_string::lookup("log_msg_instance");
38 static auto instance_meta = logline_value_meta(
39 instance_name, value_kind_t::VALUE_INTEGER, 0);
40
log_search_table(pcrepp pattern,intern_string_t table_name)41 log_search_table::log_search_table(pcrepp pattern,
42 intern_string_t table_name)
43 : log_vtab_impl(table_name),
44 lst_regex(std::move(pattern)),
45 lst_instance(-1)
46 {
47 this->vi_supports_indexes = false;
48 this->get_columns_int(this->lst_cols);
49 }
50
get_columns_int(std::vector<vtab_column> & cols)51 void log_search_table::get_columns_int(std::vector<vtab_column> &cols)
52 {
53 column_namer cn;
54
55 cols.emplace_back(LOG_MSG_INSTANCE, SQLITE_INTEGER);
56 for (int lpc = 0; lpc < this->lst_regex.get_capture_count(); lpc++) {
57 std::string collator;
58 std::string colname;
59 int sqlite_type = SQLITE3_TEXT;
60
61 colname = cn.add_column(this->lst_regex.name_for_capture(lpc));
62 if (this->lst_regex.captures().size() ==
63 (size_t) this->lst_regex.get_capture_count()) {
64 auto iter = this->lst_regex.cap_begin() + lpc;
65 auto cap_re = this->lst_regex.get_pattern()
66 .substr(iter->c_begin, iter->length());
67 sqlite_type = guess_type_from_pcre(cap_re, collator);
68 switch (sqlite_type) {
69 case SQLITE_FLOAT:
70 this->lst_column_metas.emplace_back(
71 intern_string::lookup(colname),
72 value_kind_t::VALUE_FLOAT,
73 cols.size());
74 break;
75 case SQLITE_INTEGER:
76 this->lst_column_metas.emplace_back(
77 intern_string::lookup(colname),
78 value_kind_t::VALUE_INTEGER,
79 cols.size());
80 break;
81 default:
82 this->lst_column_metas.emplace_back(
83 intern_string::lookup(colname),
84 value_kind_t::VALUE_TEXT,
85 cols.size());
86 break;
87 }
88 }
89 cols.emplace_back(colname, sqlite_type, collator);
90 }
91 }
92
93 void
get_foreign_keys(std::vector<std::string> & keys_inout) const94 log_search_table::get_foreign_keys(std::vector<std::string> &keys_inout) const
95 {
96 log_vtab_impl::get_foreign_keys(keys_inout);
97 keys_inout.emplace_back("log_msg_instance");
98 }
99
next(log_cursor & lc,logfile_sub_source & lss)100 bool log_search_table::next(log_cursor &lc, logfile_sub_source &lss)
101 {
102 if (lc.lc_curr_line == -1_vl) {
103 this->lst_instance = -1;
104 }
105
106 lc.lc_curr_line = lc.lc_curr_line + 1_vl;
107 lc.lc_sub_index = 0;
108
109 if (lc.lc_curr_line == (int) lss.text_line_count()) {
110 return true;
111 }
112
113 auto cl = lss.at(lc.lc_curr_line);
114 auto lf = lss.find(cl);
115 auto lf_iter = lf->begin() + cl;
116
117 if (!lf_iter->is_message()) {
118 return false;
119 }
120
121 string_attrs_t sa;
122 std::vector<logline_value> line_values;
123
124 lf->read_full_message(lf_iter, this->lst_current_line);
125 lf->get_format()->annotate(cl, this->lst_current_line, sa, line_values,
126 false);
127 pcre_input pi(this->lst_current_line.get_data(),
128 0,
129 this->lst_current_line.length());
130
131 if (!this->lst_regex.match(this->lst_match_context, pi)) {
132 return false;
133 }
134
135 this->lst_instance += 1;
136
137 return true;
138 }
139
140 void
extract(std::shared_ptr<logfile> lf,uint64_t line_number,shared_buffer_ref & line,std::vector<logline_value> & values)141 log_search_table::extract(std::shared_ptr<logfile> lf, uint64_t line_number,
142 shared_buffer_ref &line,
143 std::vector<logline_value> &values)
144 {
145 values.emplace_back(instance_meta, this->lst_instance);
146 for (int lpc = 0; lpc < this->lst_regex.get_capture_count(); lpc++) {
147 auto cap = this->lst_match_context[lpc];
148 values.emplace_back(this->lst_column_metas[lpc], line,
149 line_range{cap->c_begin, cap->c_end});
150 }
151 }
152