ApplicationH2DAO.java 2.8 KB
Newer Older
P
add dao  
peng-yongsheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright 2017, OpenSkywalking Organization All rights reserved.
 *
 * 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.
 *
 * Project repository: https://github.com/OpenSkywalking/skywalking
 */

package org.skywalking.apm.collector.storage.h2.dao;

import java.util.HashMap;
import java.util.Map;
import org.skywalking.apm.collector.client.h2.H2Client;
import org.skywalking.apm.collector.client.h2.H2ClientException;
P
peng-yongsheng 已提交
25 26 27
import org.skywalking.apm.collector.core.data.Data;
import org.skywalking.apm.collector.storage.dao.IApplicationDAO;
import org.skywalking.apm.collector.storage.h2.base.dao.H2DAO;
P
peng-yongsheng 已提交
28
import org.skywalking.apm.collector.storage.base.sql.SqlBuilder;
P
peng-yongsheng 已提交
29 30
import org.skywalking.apm.collector.storage.table.register.ApplicationDataDefine;
import org.skywalking.apm.collector.storage.table.register.ApplicationTable;
P
add dao  
peng-yongsheng 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author peng-yongsheng, clevertension
 */
public class ApplicationH2DAO extends H2DAO implements IApplicationDAO {
    private final Logger logger = LoggerFactory.getLogger(ApplicationH2DAO.class);

    @Override
    public int getMaxApplicationId() {
        return getMaxId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
    }

    @Override
    public int getMinApplicationId() {
        return getMinId(ApplicationTable.TABLE, ApplicationTable.COLUMN_APPLICATION_ID);
    }

    @Override
P
peng-yongsheng 已提交
51 52 53 54
    public void save(Data data) {
        String id = ApplicationDataDefine.Application.INSTANCE.getId(data);
        int applicationId = ApplicationDataDefine.Application.INSTANCE.getApplicationId(data);
        String applicationCode = ApplicationDataDefine.Application.INSTANCE.getApplicationCode(data);
P
add dao  
peng-yongsheng 已提交
55 56 57
        H2Client client = getClient();

        Map<String, Object> source = new HashMap<>();
P
peng-yongsheng 已提交
58 59 60
        source.put(ApplicationTable.COLUMN_ID, id);
        source.put(ApplicationTable.COLUMN_APPLICATION_CODE, applicationCode);
        source.put(ApplicationTable.COLUMN_APPLICATION_ID, applicationId);
P
add dao  
peng-yongsheng 已提交
61 62 63 64 65 66 67 68 69 70

        String sql = SqlBuilder.buildBatchInsertSql(ApplicationTable.TABLE, source.keySet());
        Object[] params = source.values().toArray(new Object[0]);
        try {
            client.execute(sql, params);
        } catch (H2ClientException e) {
            logger.error(e.getMessage(), e);
        }
    }
}