[intern] some cleanup

This commit is contained in:
Timothy Stack 2021-09-22 16:01:03 -07:00
parent d6535c40f6
commit b2f11ac27f
12 changed files with 115 additions and 48 deletions

View File

@ -32,6 +32,8 @@
#include "all_logs_vtab.hh"
#include "string_attr_type.hh"
static auto intern_lifetime = intern_string::get_table_lifetime();
all_logs_vtab::all_logs_vtab()
: log_vtab_impl(intern_string::lookup("all_logs")),
alv_value_meta(intern_string::lookup("log_format"),

View File

@ -33,10 +33,35 @@
#include <string.h>
#include <mutex>
#include "intern_string.hh"
const static int TABLE_SIZE = 4095;
static intern_string *TABLE[TABLE_SIZE];
struct intern_string::intern_table {
~intern_table() {
for (auto is : this->it_table) {
auto curr = is;
while (curr != nullptr) {
auto next = curr->is_next;
delete curr;
curr = next;
}
}
}
intern_string *it_table[TABLE_SIZE];
};
intern_table_lifetime intern_string::get_table_lifetime()
{
static intern_table_lifetime retval = std::make_shared<intern_table>();
return retval;
}
unsigned long
hash_str(const char *str, size_t len)
@ -61,22 +86,27 @@ const intern_string *intern_string::lookup(const char *str, ssize_t len) noexcep
}
h = hash_str(str, len) % TABLE_SIZE;
curr = TABLE[h];
while (curr != nullptr) {
if (curr->is_len == len && strncmp(curr->is_str, str, len) == 0) {
return curr;
{
static std::mutex table_mutex;
std::lock_guard<std::mutex> lk(table_mutex);
auto tab = get_table_lifetime();
curr = tab->it_table[h];
while (curr != nullptr) {
if (curr->is_str.size() == len && strncmp(curr->is_str.c_str(), str, len) == 0) {
return curr;
}
curr = curr->is_next;
}
curr = curr->is_next;
curr = new intern_string(str, len);
curr->is_next = tab->it_table[h];
tab->it_table[h] = curr;
return curr;
}
char *strcp = new char[len + 1];
memcpy(strcp, str, len);
strcp[len] = '\0';
curr = new intern_string(strcp, len);
curr->is_next = TABLE[h];
TABLE[h] = curr;
return curr;
}
const intern_string *intern_string::lookup(const string_fragment &sf) noexcept

View File

@ -136,7 +136,7 @@ struct string_fragment {
return *prefix == '\0';
}
string_fragment substr(int begin) {
string_fragment substr(int begin) const {
return string_fragment{
this->sf_string,
this->sf_begin + begin,
@ -162,7 +162,7 @@ struct string_fragment {
};
std::string to_string() const {
return std::string(this->data(), this->length());
return {this->data(), (size_t) this->length()};
}
void clear() {
@ -213,18 +213,6 @@ struct string_fragment {
int sf_end;
};
namespace fmt {
template<>
struct formatter<string_fragment> : formatter<string_view> {
template<typename FormatContext>
auto format(const string_fragment& sf, FormatContext &ctx)
{
return formatter<string_view>::format(
string_view{sf.data(), (size_t) sf.length()}, ctx);
}
};
}
inline bool operator<(const char *left, const string_fragment &right) {
int rc = strncmp(left, right.data(), right.length());
return rc < 0;
@ -244,23 +232,23 @@ public:
static const intern_string *lookup(const std::string &str) noexcept;
const char *get() const {
return this->is_str;
return this->is_str.c_str();
};
size_t size() const {
return this->is_len;
return this->is_str.size();
}
std::string to_string() const {
return std::string(this->is_str, this->is_len);
return this->is_str;
}
string_fragment to_string_fragment() const {
return string_fragment{this->is_str, 0, (int) this->is_len};
return string_fragment{this->is_str};
}
bool startswith(const char *prefix) const {
const char *curr = this->is_str;
const char *curr = this->is_str.data();
while (*prefix != '\0' && *prefix == *curr) {
prefix += 1;
@ -270,17 +258,21 @@ public:
return *prefix == '\0';
}
struct intern_table;
static std::shared_ptr<intern_table> get_table_lifetime();
private:
intern_string(const char *str, ssize_t len)
: is_next(nullptr), is_str(str), is_len(len) {
friend intern_table;
intern_string(const char *str, ssize_t len)
: is_next(nullptr), is_str(str, (size_t) len) {
}
intern_string *is_next;
const char *is_str;
ssize_t is_len;
std::string is_str;
};
using intern_table_lifetime = std::shared_ptr<intern_string::intern_table>;
class intern_string_t {
public:
using iterator = const char *;
@ -324,7 +316,7 @@ public:
}
size_t hash() const {
uintptr_t ptr = (uintptr_t) this->ist_interned_string;
auto ptr = (uintptr_t) this->ist_interned_string;
return ptr;
}
@ -369,6 +361,28 @@ private:
unsigned long hash_str(const char *str, size_t len);
namespace fmt {
template<>
struct formatter<string_fragment> : formatter<string_view> {
template<typename FormatContext>
auto format(const string_fragment &sf, FormatContext &ctx)
{
return formatter<string_view>::format(
string_view{sf.data(), (size_t) sf.length()}, ctx);
}
};
template<>
struct formatter<intern_string_t> : formatter<string_view> {
template<typename FormatContext>
auto format(const intern_string_t& is, FormatContext &ctx)
{
return formatter<string_view>::format(
string_view{is.get(), (size_t) is.size()}, ctx);
}
};
}
namespace std {
template <>
struct hash<const intern_string_t> {
@ -399,7 +413,7 @@ inline bool operator==(const string_fragment &left, const intern_string_t &right
namespace std {
inline string to_string(const string_fragment &s) {
return string(s.data(), s.length());
return {s.data(), (size_t) s.length()};
}
inline string to_string(const intern_string_t &s) {

View File

@ -106,9 +106,22 @@ void file_collection::regenerate_unique_file_names()
}
}
for (const auto &pair : this->fc_other_files) {
auto bn = ghc::filesystem::path(pair.first).filename().string();
if (bn.length() > this->fc_largest_path_length) {
this->fc_largest_path_length = bn.length();
switch (pair.second.ofd_format) {
case file_format_t::FF_UNKNOWN:
case file_format_t::FF_ARCHIVE:
case file_format_t::FF_SQLITE_DB: {
auto bn = ghc::filesystem::path(pair.first).filename().string();
if (bn.length() > this->fc_largest_path_length) {
this->fc_largest_path_length = bn.length();
}
break;
}
case file_format_t::FF_REMOTE: {
if (pair.first.length() > this->fc_largest_path_length) {
this->fc_largest_path_length = pair.first.length();
}
break;
}
}
}
}

View File

@ -53,13 +53,13 @@ struct formatter<file_format_t> : formatter<string_view> {
string_view name = "unknown";
switch (ff) {
case file_format_t::FF_SQLITE_DB:
name = "SQLite Database";
name = "\U0001F5C2 SQLite Database";
break;
case file_format_t::FF_ARCHIVE:
name = "Archive";
name = "\U0001F5C4 Archive";
break;
case file_format_t::FF_REMOTE:
name = "Remote";
name = "\U0001F5A5 Remote";
break;
default:
break;

View File

@ -158,6 +158,7 @@ using namespace std::literals::chrono_literals;
static bool initial_build = false;
static multimap<lnav_flags_t, string> DEFAULT_FILES;
static auto intern_lifetime = intern_string::get_table_lifetime();
struct lnav_data_t lnav_data;

View File

@ -67,6 +67,8 @@ using namespace std;
static const int MAX_CRASH_LOG_COUNT = 16;
static const auto STDIN_CAPTURE_RETENTION = 24h;
static auto intern_lifetime = intern_string::get_table_lifetime();
struct _lnav_config lnav_config;
struct _lnav_config rollback_lnav_config;
static struct _lnav_config lnav_default_config;

View File

@ -49,6 +49,7 @@
using namespace std;
static auto intern_lifetime = intern_string::get_table_lifetime();
string_attr_type logline::L_PREFIX("prefix");
string_attr_type logline::L_TIMESTAMP("timestamp");
string_attr_type logline::L_FILE("file");

View File

@ -40,7 +40,6 @@
#include "sql_util.hh"
#include "log_format.hh"
#include "log_vtab_impl.hh"
#include "lnav_util.hh"
#include "base/opt_util.hh"
using namespace std;

View File

@ -64,6 +64,7 @@ static void extract_metadata(const char *contents, size_t len, struct script_met
typedef map<intern_string_t, std::shared_ptr<external_log_format>> log_formats_map_t;
static auto intern_lifetime = intern_string::get_table_lifetime();
static log_formats_map_t LOG_FORMATS;
struct userdata {

View File

@ -40,6 +40,8 @@
using namespace std;
static auto intern_lifetime = intern_string::get_table_lifetime();
static struct log_cursor log_cursor_latest;
struct _log_vtab_data log_vtab_data;
@ -1073,10 +1075,10 @@ string log_vtab_manager::register_vtab(std::shared_ptr<log_vtab_impl> vi)
string log_vtab_manager::unregister_vtab(intern_string_t name)
{
string retval = "";
string retval;
if (this->vm_impls.find(name) == this->vm_impls.end()) {
retval = "unknown log line table -- " + name.to_string();
retval = fmt::format("unknown log line table -- {}", name);
}
else {
auto_mem<char, sqlite3_free> sql;

View File

@ -53,6 +53,8 @@
using namespace std;
static auto intern_lifetime = intern_string::get_table_lifetime();
static const size_t INDEX_RESERVE_INCREMENT = 1024;
Result<std::shared_ptr<logfile>, std::string> logfile::open(