2121#include < glog/logging.h>
2222#include < json2pb/pb_to_json.h>
2323
24+ #include < cstdint>
25+ #include < type_traits>
26+ #include < utility>
27+
2428#include " bvar/bvar.h"
29+ #include " exec/tablet_info.h"
2530#include " olap/tablet_schema.h"
2631#include " util/sha.h"
2732
@@ -39,6 +44,17 @@ static std::string get_key_signature(const std::string& origin) {
3944 return std::string {digest.digest ().data (), digest.digest ().length ()};
4045}
4146
47+ template <typename T>
48+ static void append_cache_key_value (std::string* key, T value) {
49+ static_assert (std::is_integral_v<T>);
50+ key->append (reinterpret_cast <const char *>(&value), sizeof (value));
51+ }
52+
53+ static void append_cache_key_string (std::string* key, const std::string& value) {
54+ append_cache_key_value (key, static_cast <uint64_t >(value.size ()));
55+ key->append (value);
56+ }
57+
4258std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert (const std::string& key) {
4359 std::string key_signature = get_key_signature (key);
4460 auto * lru_handle = lookup (key_signature);
@@ -64,6 +80,78 @@ std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std:
6480 return std::make_pair (lru_handle, tablet_schema_ptr);
6581}
6682
83+ std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert (
84+ const std::string& key, TabletSchemaSPtr tablet_schema) {
85+ auto * lru_handle = lookup (key);
86+ TabletSchemaSPtr tablet_schema_ptr;
87+ if (lru_handle) {
88+ auto * value = (CacheValue*)LRUCachePolicy::value (lru_handle);
89+ tablet_schema_ptr = value->tablet_schema ;
90+ g_tablet_schema_cache_hit_count << 1 ;
91+ } else {
92+ DCHECK (tablet_schema != nullptr );
93+ auto * value = new CacheValue;
94+ tablet_schema_ptr = std::move (tablet_schema);
95+ value->tablet_schema = tablet_schema_ptr;
96+ lru_handle = LRUCachePolicy::insert (key, value, tablet_schema_ptr->num_columns (),
97+ tablet_schema_ptr->mem_size (), CachePriority::NORMAL );
98+ g_tablet_schema_cache_count << 1 ;
99+ g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns ();
100+ }
101+ DCHECK (lru_handle != nullptr );
102+ return std::make_pair (lru_handle, tablet_schema_ptr);
103+ }
104+
105+ std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::lookup_schema (
106+ const std::string& key) {
107+ auto * lru_handle = lookup (key);
108+ if (lru_handle == nullptr ) {
109+ return {nullptr , nullptr };
110+ }
111+ auto * value = (CacheValue*)LRUCachePolicy::value (lru_handle);
112+ g_tablet_schema_cache_hit_count << 1 ;
113+ return {lru_handle, value->tablet_schema };
114+ }
115+
116+ std::string TabletSchemaCache::build_load_schema_cache_key (
117+ int64_t index_id, const OlapTableSchemaParam* table_schema_param,
118+ const TabletSchema& ori_tablet_schema, const OlapTableIndexSchema* index_schema) {
119+ DCHECK (table_schema_param != nullptr );
120+ std::string cache_key;
121+ cache_key.append (" load_schema_v2" );
122+ append_cache_key_value (&cache_key, index_id);
123+ append_cache_key_value (&cache_key, table_schema_param->table_id ());
124+ append_cache_key_value (&cache_key, table_schema_param->db_id ());
125+ append_cache_key_value (&cache_key, table_schema_param->version ());
126+
127+ TabletSchemaPB ori_schema_pb;
128+ ori_tablet_schema.to_schema_pb (&ori_schema_pb);
129+ append_cache_key_string (&cache_key,
130+ TabletSchema::deterministic_string_serialize (ori_schema_pb));
131+ if (ori_tablet_schema.num_variant_columns () > 0 ) {
132+ // Variant schemas carry path set info outside TabletSchemaPB, so do not share them
133+ // across different source TabletSchema objects unless that metadata is serialized.
134+ append_cache_key_value (&cache_key, reinterpret_cast <uintptr_t >(&ori_tablet_schema));
135+ }
136+
137+ std::string auto_increment_column;
138+ if (table_schema_param->is_partial_update ()) {
139+ auto_increment_column = table_schema_param->auto_increment_coulumn ();
140+ }
141+ append_cache_key_string (&cache_key, auto_increment_column);
142+
143+ const bool has_current_schema = index_schema != nullptr && !index_schema->columns .empty () &&
144+ index_schema->columns [0 ]->unique_id () >= 0 ;
145+ append_cache_key_value (&cache_key, has_current_schema);
146+ if (index_schema != nullptr ) {
147+ POlapTableIndexSchema index_schema_pb;
148+ index_schema->to_protobuf (&index_schema_pb);
149+ append_cache_key_string (&cache_key,
150+ TabletSchema::deterministic_string_serialize (index_schema_pb));
151+ }
152+ return get_key_signature (cache_key);
153+ }
154+
67155void TabletSchemaCache::release (Cache::Handle* lru_handle) {
68156 LRUCachePolicy::release (lru_handle);
69157}
0 commit comments