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

net.wicp.tams.common.redis.plugin.PluginCommon Maven / Gradle / Ivy

/*
 * **********************************************************************
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称:common
 * 项目描述:公共的工具集
 * 版权说明:本软件属andy.zhou([email protected])所有。
 * ***********************************************************************
 */
package net.wicp.tams.common.redis.plugin;

import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.tuple.Pair;

import net.wicp.tams.common.Conf;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.binlog.alone.binlog.bean.Rule;
import net.wicp.tams.common.binlog.alone.binlog.bean.RuleItem;
import net.wicp.tams.common.constant.DrdsPattern;

public class PluginCommon {
	public static String keyformate(String keyformart, DrdsPattern drdsPattern) {
		// String relakey =
		// StringUtil.trimSpace(rule.getItems().get(RuleItem.keyformart));
		Validate.isTrue(StringUtil.isNotNull(keyformart), "你需要配置key的存放规则");
		int num = keyformart.length() - keyformart.replaceAll("%s", "").length();
		if (num != 6 || num != 8) {// keyformart只能有3个%s(无tenant隔离),或4个%s(有tenant隔离),分别表示db/tb/id
			keyformart = keyformart.replace(":%s", "");
			keyformart = keyformart.replace("%s", "");
			keyformart = keyformart.replace("%s", "");
			if (drdsPattern != DrdsPattern.no) {
				keyformart += ":%s:%s:%s:%s";// 库名、表名、路由字段、主键字段
			} else {
				keyformart += ":%s:%s:%s";// 库名、表名、主键字段
			}
		}
		return keyformart;
	}

	/***
	 * 得到事件
	 * 
	 * @param context
	 * @return
	 */
//	public static Pair getChangeEvent(Map context) {
//		// JSONObject parseObject = JSONObject.parseObject(jsonstr);
//		String keyformate = keyformate(context.get(Conf.get("common.redis.msg.colname.keyformart")));
//		String retstr = String.format(keyformate, context.get(Conf.get("common.redis.msg.colname.db")),
//				context.get(Conf.get("common.redis.msg.colname.tb")),
//				context.get(Conf.get("common.redis.msg.colname.id")));
//		return Pair.of(retstr, OptType.valueOf(context.get(Conf.get("common.redis.msg.colname.opttype"))));
//	}

	/**
	 * 得到group和stream的key
	 * 
	 * @param db         库名
	 * @param tb         表名
	 * @param keyformart 注意已经过 keyformate处理好的key
	 * @return L:group key R:stream key
	 */
	public static Pair getControl(String db, String tb, DrdsPattern drdsPattern, String keyformart) {
		// String keyformateTrue = keyformate(keyformart);
		String groupkeyPre = keyformart.substring(0, keyformart.lastIndexOf(":"));
		if (drdsPattern != DrdsPattern.no) {
			groupkeyPre = groupkeyPre.substring(0, groupkeyPre.lastIndexOf(":"));
		}
		String keydemo = String.format(groupkeyPre, db, tb);// "id"随意,都是要放弃的值

		String groupkey = String.format("%s:%s:%s", keydemo, "control", "group");
		String streamkey = String.format("%s:%s:%s", keydemo, "control", "stream");
		return Pair.of(groupkey, streamkey);
	}

	/***
	 * 得到redis的配置
	 * 
	 * @param rule
	 * @return
	 */
	public static String initPoolConf(Rule rule) {
		String defaultDb = StringUtil.hasNull(rule.getItems().get(RuleItem.redisDb), "0");
		String returnpoolname = "default";
		if (!"0".equals(defaultDb)) {
			returnpoolname = "default_" + defaultDb;
			Conf.overProp(String.format("common.redis.redisserver.%s.defaultDb", returnpoolname), defaultDb);

		}
		return returnpoolname;
	}

//	public static Map packChangeEvent(String db, String tb, String id, String keyformart,
//			OptType opttype) {
//		Map msg = new HashMap();
//		msg.put(Conf.get("common.redis.msg.colname.db"), db);
//		msg.put(Conf.get("common.redis.msg.colname.tb"), tb);
//		msg.put(Conf.get("common.redis.msg.colname.id"), id);
//		msg.put(Conf.get("common.redis.msg.colname.opttype"), opttype.name());
//		msg.put(Conf.get("common.redis.msg.colname.keyformart"), keyformart);
//		return msg;
//	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy