All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.glowroot.central.repo.V09AgentRollupDao Maven / Gradle / Ivy

/*
 * Copyright 2017-2019 the original author or authors.
 *
 * Licensed 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.
 */
package org.glowroot.central.repo;

import java.util.HashMap;
import java.util.Map;

import com.datastax.driver.core.BoundStatement;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import org.checkerframework.checker.nullness.qual.Nullable;

import org.glowroot.central.util.Cache;
import org.glowroot.central.util.Cache.CacheLoader;
import org.glowroot.central.util.ClusterManager;
import org.glowroot.central.util.Session;

import static com.google.common.base.Preconditions.checkNotNull;

// this is needed as long as new v09 agents may connect in the future
public class V09AgentRollupDao {

    private static final String SINGLE_CACHE_KEY = "x";

    private final Session session;

    private final PreparedStatement insertPS;
    private final PreparedStatement readPS;

    private final Cache> agentRollupIdsCache;

    V09AgentRollupDao(Session session, ClusterManager clusterManager) throws Exception {
        this.session = session;

        session.createTableWithLCS("create table if not exists v09_agent_rollup (one int,"
                + " v09_agent_id varchar, v09_agent_rollup_id varchar, primary key (one,"
                + " v09_agent_id))");

        insertPS = session.prepare("insert into v09_agent_rollup (one, v09_agent_id,"
                + " v09_agent_rollup_id) values (1, ?, ?)");
        readPS = session.prepare(
                "select v09_agent_id, v09_agent_rollup_id from v09_agent_rollup where one = 1");

        agentRollupIdsCache = clusterManager.createSelfBoundedCache("v09AgentRollupIdCache",
                new AgentRollupIdCacheLoader());
    }

    public void store(String v09AgentId, String v09AgentRollupId) throws Exception {
        BoundStatement boundStatement = insertPS.bind();
        int i = 0;
        boundStatement.setString(i++, v09AgentId);
        boundStatement.setString(i++, v09AgentRollupId);
        session.write(boundStatement);
        agentRollupIdsCache.invalidate(SINGLE_CACHE_KEY);
    }

    public @Nullable String getV09AgentRollupId(String v09AgentId) throws Exception {
        return agentRollupIdsCache.get(SINGLE_CACHE_KEY).get(v09AgentId);
    }

    private class AgentRollupIdCacheLoader implements CacheLoader> {
        @Override
        public Map load(String key) throws Exception {
            BoundStatement boundStatement = readPS.bind();
            ResultSet results = session.read(boundStatement);
            Map agentRollupIds = new HashMap<>();
            for (Row row : results) {
                int i = 0;
                String v09AgentId = checkNotNull(row.getString(i++));
                String v09AgentRollupId = checkNotNull(row.getString(i++));
                agentRollupIds.put(v09AgentId, v09AgentRollupId);
            }
            return agentRollupIds;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy