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

com.logicbus.kvalue.xscript.zset.KVZRange Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.logicbus.kvalue.xscript.zset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.logicbus.kvalue.xscript.KVRowOperation;
import com.anysoft.util.Pair;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.logicbus.kvalue.core.KeyValueRow;
import com.logicbus.kvalue.core.SortedSetRow;

public class KVZRange extends KVRowOperation {

	protected String start = "0";
	protected String end = "150";
	protected String withscores = "false";
	protected String reverse = "false";
	protected String tag = "data";

	public KVZRange(String tag, Logiclet p) {
		super(tag, p);
	}

	@Override
	public void configure(Properties p) {
		super.configure(p);
		start = PropertiesConstants.getRaw(p, "start", start);
		end = PropertiesConstants.getRaw(p, "end", end);
		withscores = PropertiesConstants.getRaw(p, "withscores", withscores);
		reverse = PropertiesConstants.getRaw(p, "reverse", reverse);
		tag = PropertiesConstants.getRaw(p, "tag", tag);
	}

	@Override
	protected void onExecute(KeyValueRow row, Map root, Map current,
			LogicletContext ctx, ExecuteWatcher watcher) {

		if (row instanceof SortedSetRow) {
			SortedSetRow r = (SortedSetRow) row;
			if(getBoolean(ctx.transform(withscores), false)){
				List> l=r.rangeWithScores(getLong(ctx.transform(start), 0), getLong(ctx.transform(end), 150l),
						getBoolean(ctx.transform(reverse), false));
				
				List> result=new ArrayList>();
				if(null!=l&&l.size()>0){
					Iterator> ite=l.iterator();
					while(ite.hasNext()){
						Pair p=ite.next();
						Map map=new HashMap();
						map.put(p.key(), p.value());
						result.add(map);
					}
				}

				current.put(ctx.transform(tag), result);
			}else{
				current.put(ctx.transform(tag), r.range(getLong(ctx.transform(start), 0), getLong(ctx.transform(end), 150l),
						getBoolean(ctx.transform(reverse), false)));
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy