Skip to content

Instantly share code, notes, and snippets.

@taichi
Created April 18, 2011 06:39
Show Gist options
  • Save taichi/924907 to your computer and use it in GitHub Desktop.
Save taichi/924907 to your computer and use it in GitHub Desktop.
MapReduceKeyFilter code generator for Riak.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RiakFilterCodeGenerator {
public static void main(String[] args) {
String[][] oneargs = { { "greater_than", "greaterThan" },
{ "less_than", "lessThan" },
{ "greater_than_eq", "greaterThanEq" },
{ "less_than_eq", "lessThanEq" }, { "neq", "notEqual" },
{ "eq", "equal" } };
// create(oneargs);
// oneArgs(oneargs);
String[][] onlycreate = { { "between", "between" },
{ "matches", "matches" }, { "set_member", "setMember" },
{ "similar_to", "similarTo" }, { "starts_with", "startsWith" },
{ "ends_with", "endsWith" }, { "and", "and" }, { "or", "or" },
{ "not", "not" } };
// create(onlycreate);
setMember();
}
static void create(String[][] src) {
for (String[] ary : src) {
System.out.println(create(ary[0], ary[1]));
}
}
static final String createTemplate = " public static ArrayNode ${method}(ArrayNode container) { return create(\"${function}\", container); }";
public static String create(String function, String method) {
Map<String, String> context = new HashMap<String, String>();
context.put("function", function);
context.put("method", method);
return replace(createTemplate, context);
}
static final String oneArgsTemplate = " public static MapReduceKeyFilter ${method}(final ${type} value) { return new MapReduceKeyFilter() { @Override public void appendTo(ArrayNode json) { ArrayNode node = InternalFunctions.${method}(json); node.add(value); } }; }";
static void oneArgs(String[][] src) {
for (String[] ary : src) {
List<String> codes = oneArgs(ary[0], ary[1]);
for (String s : codes) {
System.out.println(s);
}
}
}
static List<String> oneArgs(String function, String method) {
List<String> result = new ArrayList<String>();
String[] types = { "String", "int", "long", "double", "float" };
for (String s : types) {
Map<String, String> context = new HashMap<String, String>();
context.put("function", function);
context.put("method", method);
context.put("type", s);
result.add(replace(oneArgsTemplate, context));
}
return result;
}
static final String setMemberTemplate = " /**\n"
+ "* Tests that the input is contained in the set given as the arguments.\n"
+ "*/ public static MapReduceKeyFilter setMember(final ${type}[] members) { return new MapReduceKeyFilter() { @Override public void appendTo(ArrayNode json) { ArrayNode node = InternalFunctions.setMember(json); for (${type} member : members) { node.add(member); } } }; }";
static void setMember() {
String[] types = { "String", "int", "long", "double", "float" };
for (String s : types) {
Map<String, String> context = new HashMap<String, String>();
context.put("type", s);
System.out.println(replace(setMemberTemplate, context));
}
}
public static String replace(String template, Map<String, String> context) {
String result = "";
if (isEmpty(template) == false) {
Pattern p = Pattern.compile("\\$\\{[^\\$\\{\\}]*\\}");
StringBuffer stb = new StringBuffer(template);
Matcher m = p.matcher(stb);
int index = 0;
while ((index < stb.length()) && m.find(index)) {
String s = m.group();
String v = toString(context.get(s.substring(2, s.length() - 1)));
index = m.start() + v.length();
stb.replace(m.start(), m.end(), v);
m = p.matcher(stb);
}
result = stb.toString();
}
return result;
}
public static String toString(Object s) {
return toString(s, "");
}
public static String toString(Object s, String r) {
return isEmpty(s) ? r : s.toString();
}
public static boolean isEmpty(Object s) {
return (s == null) || (s.toString().length() < 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment