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

com.alogic.xscript.plugins.Hash Maven / Gradle / Ivy

There is a newer version: 1.6.17
Show newest version
package com.alogic.xscript.plugins;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alogic.xscript.AbstractLogiclet;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.util.MapProperties;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * Hash
 * 

* 计算指定字符串的hash值,并输出到指定变量 * * @author duanyy * * @since 1.6.6.14 * */ public class Hash extends AbstractLogiclet { protected String id = "$hash"; protected String value; protected long limit = Long.MAX_VALUE; public Hash(String tag, Logiclet p) { super(tag, p); } public void configure(Properties p){ super.configure(p); id = PropertiesConstants.getString(p,"id",id,true); limit = PropertiesConstants.getLong(p,"limit",limit,true); value = p.GetValue("value", "", false, true); } @Override protected void onExecute(Map root, Map current, LogicletContext ctx, ExecuteWatcher watcher) { if (StringUtils.isNotEmpty(id)){ MapProperties p = new MapProperties(current,ctx); ctx.SetValue(id, String.valueOf((hash(p.transform(value)) & Long.MAX_VALUE) % limit )); } } /** * MurMurHash算法实现 * @param key * @return Hash值 */ static protected Long hash(String key) { ByteBuffer buf = ByteBuffer.wrap(key.getBytes()); int seed = 0x1234ABCD; ByteOrder byteOrder = buf.order(); buf.order(ByteOrder.LITTLE_ENDIAN); long m = 0xc6a4a7935bd1e995L; int r = 47; long h = seed ^ (buf.remaining() * m); long k; while (buf.remaining() >= 8) { k = buf.getLong(); k *= m; k ^= k >>> r; k *= m; h ^= k; h *= m; } if (buf.remaining() > 0) { ByteBuffer finish = ByteBuffer.allocate(8).order( ByteOrder.LITTLE_ENDIAN); finish.put(buf).rewind(); h ^= finish.getLong(); h *= m; } h ^= h >>> r; h *= m; h ^= h >>> r; buf.order(byteOrder); return h; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy