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

ys.artifact-orm.1.0.1.source-code.dao-redis.btl Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
<%
var packages = [];
array.addOnly(packages,"org.artifact.core.db.JedisDao");
array.addOnly(packages,"org.artifact.core.lang.StrPlus");
array.addOnly(packages,"redis.clients.jedis.Pipeline");
array.addOnly(packages,"org.artifact.core.context.bytebuf.IByteBuff");
array.addOnly(packages,"org.artifact.core.context.bytebuf.IByteBuffFactory");
array.addOnly(packages,"redis.clients.jedis.Jedis");
array.addOnly(packages,"java.util.List");
array.addOnly(packages,"java.util.Map");
array.addOnly(packages,"java.util.ArrayList");
array.addOnly(packages,"java.util.Set");
array.addOnly(packages,"org.artifact.core.lang.IServer");


array.addOnly(packages,templates['BeanTemplate'].package + "." + table.className);
%>
<%for(package in packages){%>
import ${package};
<%}%>
import static cn.hutool.core.util.ObjectUtil.equal;

public abstract class ${table.className+templates['DaoTemplate'].classSuffix} extends JedisDao<${table.className}>{
	<%include("dao-common.btl"){}%>


	protected String[] getIndexKey(${table.className} t){
		String[] indexKeys = new String[${table.indexs.~size}];
		<%
		for(index in table.indexs){
		var indexKey = "StrPlus.b(";
		for(column in index.columns){
			var ColumnName = uf(column.fieldName);
			var columnName = lf(column.fieldName);
			indexKey = indexKey + '"'+columnName+':", ' + 't.get'+ColumnName+'()';
			if(!columnLP.last){
				indexKey = indexKey + ',",",';
			}
		}
		indexKey = indexKey + ").e()";
		%>
		indexKeys[${indexLP.index-1}] = ${indexKey};
		<%}%>
		return indexKeys;
	}
	
	protected void addIndex(Pipeline p,${table.className} t){
		String[] keys = indexKeys.get(t.pk());
		if(keys == null){
			keys= getIndexKey(t);
		}
		<%
		for(index in table.indexs){
		%>
		<%if(index.unique){%>
		p.hset(toBytes(getTableName()+":unique"), toBytes(keys[${indexLP.index-1}]),toBytes(t.pk()));
		<%}else{%>
		p.sadd(toBytes(getTableName()+":" + keys[${indexLP.index-1}]), toBytes(t.pk()));
		<%}%>
		<%}%>
	}
	
	protected void removeIndex(Pipeline p,${table.className} t){
		String[] keys = indexKeys.get(t.pk());
		if(keys == null){
			keys= getIndexKey(t);
		}
		<%
		for(index in table.indexs){
		%>
		<%if(index.unique){%>
		p.hdel(toBytes(getTableName()+":unique"),toBytes(keys[${indexLP.index-1}]));
		<%}else{%>
		p.srem(toBytes(getTableName()+":" + keys[${indexLP.index-1}]), toBytes(t.pk()));
		<%}%>
		<%}%>
	}
	

	<%
	for(index in table.indexs){
	var methodSuffix = "";
	var parameter = "";
	var indexKey = "StrPlus.b(";
	var equals = "";
	
	for(column in index.columns){
		var ColumnName = uf(column.fieldName);
		var columnName = lf(column.fieldName);
		methodSuffix = methodSuffix + ColumnName;
		parameter = parameter + column.type + " " + columnName;
		indexKey = indexKey + '"'+columnName+':", ' + columnName;
		equals = equals + 'equal(t.get'+ColumnName+'(), '+columnName+')';
		if(!columnLP.last){
			parameter = parameter + " ,";
			indexKey = indexKey + ',",",';
			equals = equals + " && ";
		}
	}
	indexKey = indexKey + ").e()";
	%>
	<%if(index.unique){%>
	public ${table.className} findBy${methodSuffix}(${parameter}){
		return resultFirst(()->{
			Jedis jedis = getJedis();
			IByteBuffFactory factory = IServer.me().getContext().getByteBuffFactory();
			IByteBuff byteBuff = null;
			try{
				byte[] primaryKey = jedis.hget(toBytes(getTableName()+":unique"),toBytes(${indexKey}));
				if(primaryKey != null){
					byte[] data = jedis.hget(toBytes(getTableName()), primaryKey);
					if(data!=null){
						byteBuff = factory.wrap(data);
						Map map = byteBuff.readMap();
						if (map != null) {
							return createEntity().read(map);
						}				
					}
				}
			}finally {
				jedis.close();
			}
			return null;
		}, t -> {
			return ${equals};
		}) ;
	}
	
	<%}else{%>
	public List<${table.className}> findBy${methodSuffix}(${parameter}){
		return resultList(()->{
			List<${table.className}> list = new ArrayList<>();
			Jedis jedis = getJedis();
			IByteBuffFactory factory = IServer.me().getContext().getByteBuffFactory();
			IByteBuff byteBuff = null;
			try{
				Set primaryKeys = jedis.smembers(toBytes(getTableName() + ":" + ${indexKey}));
				for(byte[] primaryKey : primaryKeys){
					byte[] data = jedis.hget(toBytes(getTableName()), primaryKey);
					if(data!=null){
						byteBuff = factory.wrap(data);
						Map map = byteBuff.readMap();
						if (map != null) {
							list.add(createEntity().read(map));
						}
					}
				}
			} finally {
				jedis.close();
			}
			return list;
		}, t -> {
			return ${equals};
		}) ;
	}
	
	<%}%>
	<%}%>
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy