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

com.opdar.gulosity.base.MysqlContext Maven / Gradle / Ivy

package com.opdar.gulosity.base;

import com.opdar.gulosity.event.binlog.TableMapEvent;
import com.opdar.gulosity.persistence.FilePersistence;
import com.opdar.gulosity.persistence.IPersistence;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 全局上下文
 * Created by Shey on 2016/8/25.
 */
public class MysqlContext {

    private static ConcurrentHashMap, Object> objects = new ConcurrentHashMap, Object>();
    private static ConcurrentHashMap tables = new ConcurrentHashMap();
    private static LinkedList rowCallbacks = new LinkedList();
    private static Map> tablesCache = new HashMap>();
    private static IPersistence persistence = new FilePersistence("./salve.dat");

    public static void setPersistence(IPersistence persistence) {
        MysqlContext.persistence = persistence;
    }

    public static IPersistence getPersistence() {
        return persistence;
    }

    public static  T get(Class eventCls) {
        return objects.containsKey(eventCls) ? (T) objects.get(eventCls) : null;
    }

    public static void addRowCallback(RowCallback callback) {
        if (callback != null) {
            rowCallbacks.add(callback);
        }
    }

    public static void putTableCache(Map> m) {
        tablesCache.putAll(m);
    }

    public static LinkedList getRowCallbacks() {
        return rowCallbacks;
    }

    public static  void add(T event) {
        objects.put(event.getClass(), event);
    }

    public static void addTable(TableMapEvent table) {
        String tableName = table.getSchemaName().concat(".").concat(table.getTableName());
        if (tablesCache.containsKey(tableName)) {
            LinkedList list = tablesCache.get(tableName);
            table.setColumnInfo(list);
        }
        tables.put(table.getTableId(), table);
    }

    public static TableMapEvent getTable(Long id) {
        return tables.get(id);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy