blob: a1d3bc662ac48ad05264c9fa178f44414964f92e [file]
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "olap/tablet_schema_cache.h"
#include <gen_cpp/olap_file.pb.h>
#include "bvar/bvar.h"
#include "olap/tablet_schema.h"
bvar::Adder<int64_t> g_tablet_schema_cache_count("tablet_schema_cache_count");
bvar::Adder<int64_t> g_tablet_schema_cache_columns_count("tablet_schema_cache_columns_count");
namespace doris {
std::pair<Cache::Handle*, TabletSchemaSPtr> TabletSchemaCache::insert(const std::string& key) {
auto* lru_handle = cache()->lookup(key);
TabletSchemaSPtr tablet_schema_ptr;
if (lru_handle) {
auto* value = (CacheValue*)cache()->value(lru_handle);
value->last_visit_time = UnixMillis();
tablet_schema_ptr = value->tablet_schema;
} else {
auto* value = new CacheValue;
value->last_visit_time = UnixMillis();
tablet_schema_ptr = std::make_shared<TabletSchema>();
TabletSchemaPB pb;
pb.ParseFromString(key);
tablet_schema_ptr->init_from_pb(pb);
value->tablet_schema = tablet_schema_ptr;
auto deleter = [](const doris::CacheKey& key, void* value) {
auto* cache_value = (CacheValue*)value;
g_tablet_schema_cache_count << -1;
g_tablet_schema_cache_columns_count << -cache_value->tablet_schema->num_columns();
delete cache_value;
};
lru_handle = cache()->insert(key, value, tablet_schema_ptr->num_columns(), deleter,
CachePriority::NORMAL, 0);
g_tablet_schema_cache_count << 1;
g_tablet_schema_cache_columns_count << tablet_schema_ptr->num_columns();
}
DCHECK(lru_handle != nullptr);
return std::make_pair(lru_handle, tablet_schema_ptr);
}
void TabletSchemaCache::release(Cache::Handle* lru_handle) {
cache()->release(lru_handle);
}
} // namespace doris