Replace tabs with spaces

This commit is contained in:
2015-09-01 13:13:46 +01:00
parent 5023db3802
commit f35f33bb53
29 changed files with 959 additions and 959 deletions

View File

@@ -3,11 +3,11 @@ package data;
import java.util.Date;
public class Entry {
public Date date;
public double value;
public Date date;
public double value;
public Entry(Date date, double value) {
this.date = date;
this.value = value;
}
public Entry(Date date, double value) {
this.date = date;
this.value = value;
}
}

View File

@@ -5,15 +5,15 @@ import java.util.Date;
public class Day extends Minute {
public Day() {}
public Day() {}
public Date next() {
calendar.add(Calendar.DAY_OF_MONTH, 1);
return get();
}
public Date next() {
calendar.add(Calendar.DAY_OF_MONTH, 1);
return get();
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.HOUR_OF_DAY, 0);
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.HOUR_OF_DAY, 0);
}
}

View File

@@ -4,17 +4,17 @@ import java.util.Calendar;
import java.util.Date;
public class Hour extends Minute {
public static final int HOURS = 1;
public static final int HOURS = 1;
public Hour() {}
public Hour() {}
public Date next() {
calendar.add(Calendar.HOUR, HOURS);
return get();
}
public Date next() {
calendar.add(Calendar.HOUR, HOURS);
return get();
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.MINUTE, 0);
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.MINUTE, 0);
}
}

View File

@@ -1,7 +1,7 @@
package interval;
public class Hour12 extends Hour {
public static final int HOURS = 12;
public static final int HOURS = 12;
public Hour12() {}
public Hour12() {}
}

View File

@@ -2,7 +2,7 @@ package interval;
public class Hour2 extends Hour {
public static final int HOURS = 2;
public static final int HOURS = 2;
public Hour2() {}
public Hour2() {}
}

View File

@@ -1,7 +1,7 @@
package interval;
public class Hour4 extends Hour {
public static final int HOURS = 4;
public static final int HOURS = 4;
public Hour4() {}
public Hour4() {}
}

View File

@@ -1,7 +1,7 @@
package interval;
public class Hour6 extends Hour {
public static final int HOURS = 6;
public static final int HOURS = 6;
public Hour6() {}
public Hour6() {}
}

View File

@@ -7,48 +7,48 @@ import java.util.Iterator;
import java.util.TimeZone;
public abstract class Interval implements Iterator<Date> {
public static final String TIMEZONE = "Europe/Amsterdam";
public static final String TIMEZONE = "Europe/Amsterdam";
protected Date endDate;
protected TimeZone timeZone;
protected Calendar calendar;
protected Date endDate;
protected TimeZone timeZone;
protected Calendar calendar;
public Interval() {}
public Interval() {}
public Interval(Date startDate, Date endDate, String timeZoneID) {
setTimeZone(timeZoneID);
setDate(startDate, endDate);
}
public Interval(Date startDate, Date endDate, String timeZoneID) {
setTimeZone(timeZoneID);
setDate(startDate, endDate);
}
public void setTimeZone(String timeZoneID) {
timeZone = TimeZone.getTimeZone(timeZoneID);
calendar = new GregorianCalendar(TimeZone.getTimeZone(TIMEZONE));
}
public void setTimeZone(String timeZoneID) {
timeZone = TimeZone.getTimeZone(timeZoneID);
calendar = new GregorianCalendar(TimeZone.getTimeZone(TIMEZONE));
}
public Interval(Date startDate, Date endDate) {
this(startDate, endDate, TIMEZONE);
}
public void setDate(Date startDate, Date endDate) {
if (timeZone == null) {
setTimeZone(TIMEZONE);
}
calendar.setTime(startDate);
adjust();
this.endDate = endDate;
}
public Interval(Date startDate, Date endDate) {
this(startDate, endDate, TIMEZONE);
}
public void setDate(Date startDate, Date endDate) {
if (timeZone == null) {
setTimeZone(TIMEZONE);
}
calendar.setTime(startDate);
adjust();
this.endDate = endDate;
}
public void remove() {}
public void remove() {}
public Date get() {
return calendar.getTime();
}
public Date get() {
return calendar.getTime();
}
public abstract Date next();
public abstract Date next();
public boolean hasNext() {
return next().compareTo(endDate) < 0;
}
public boolean hasNext() {
return next().compareTo(endDate) < 0;
}
protected abstract void adjust();
protected abstract void adjust();
}

View File

@@ -4,15 +4,15 @@ import java.util.Calendar;
import java.util.Date;
public class Minute extends Interval {
public static final int MINUTES = 1;
public static final int MINUTES = 1;
public Date next() {
calendar.add(Calendar.MINUTE, MINUTES);
return get();
}
public Date next() {
calendar.add(Calendar.MINUTE, MINUTES);
return get();
}
protected void adjust() {
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
protected void adjust() {
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
}
}

View File

@@ -1,7 +1,7 @@
package interval;
public class Minute15 extends Minute {
public static final int MINUTES = 15;
public static final int MINUTES = 15;
public Minute15() {}
public Minute15() {}
}

View File

@@ -1,7 +1,7 @@
package interval;
public class Minute30 extends Minute {
public static final int MINUTES = 30;
public static final int MINUTES = 30;
public Minute30() {}
public Minute30() {}
}

View File

@@ -4,15 +4,15 @@ import java.util.Date;
import java.util.Calendar;
public class Month extends Day {
public Month() {}
public Month() {}
public Date next() {
calendar.add(Calendar.MONTH, 1);
return get();
}
public Date next() {
calendar.add(Calendar.MONTH, 1);
return get();
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.DAY_OF_MONTH, 1);
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.DAY_OF_MONTH, 1);
}
}

View File

@@ -4,15 +4,15 @@ import java.util.Calendar;
import java.util.Date;
public class Week extends Day {
public Week() {}
public Week() {}
public Date next() {
calendar.add(Calendar.WEEK_OF_MONTH, 1);
return get();
}
public Date next() {
calendar.add(Calendar.WEEK_OF_MONTH, 1);
return get();
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.DAY_OF_WEEK, 2);
}
protected void adjust() {
super.adjust();
calendar.set(Calendar.DAY_OF_WEEK, 2);
}
}

View File

@@ -23,284 +23,284 @@ import util.StringUtils;
import data.Entry;
public class Model {
public static final int BATCH_SIZE = 10;
public static final int BATCH_SIZE = 10;
protected Calendar calendar;
protected Calendar calendar;
protected JedisPool pool;
protected Jedis jedis;
protected Transaction transaction;
protected JedisPool pool;
protected Jedis jedis;
protected Transaction transaction;
protected int insertDataBatchCount, insertExtremesBatchStatementCount;
protected PreparedStatement insertDataBatchStatement, insertExtremesBatchStatement;
protected int insertDataBatchCount, insertExtremesBatchStatementCount;
protected PreparedStatement insertDataBatchStatement, insertExtremesBatchStatement;
//private Pipeline pipeline;
//private Pipeline pipeline;
public Model(Calendar calendar) {
this.calendar = calendar;
pool = new JedisPool("localhost", 16379);
jedis = pool.getResource();
jedis.select(1);
//clear();
}
public Model(Calendar calendar) {
this.calendar = calendar;
pool = new JedisPool("localhost", 16379);
jedis = pool.getResource();
jedis.select(1);
//clear();
}
public void clear() {
Set<String> set = jedis.keys("*");
if (set.size() > 0) {
jedis.del(set.toArray(new String[0]));
}
}
public void clear() {
Set<String> set = jedis.keys("*");
if (set.size() > 0) {
jedis.del(set.toArray(new String[0]));
}
}
public void createDataTable() throws SQLException {
}
public void createDataTable() throws SQLException {
}
public void createIntervalsTable() throws SQLException {
}
public void createIntervalsTable() throws SQLException {
}
public void createExtremesTable() throws SQLException {
}
public void createExtremesTable() throws SQLException {
}
public Date getFirstEntryDate() throws SQLException {
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1));
long timestamp = 0;
if (list.size() > 0) {
timestamp = Long.valueOf(list.get(0));
}
calendar.setTimeInMillis(1000 * timestamp);
return calendar.getTime();
}
public Date getFirstEntryDate() throws SQLException {
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1));
long timestamp = 0;
if (list.size() > 0) {
timestamp = Long.valueOf(list.get(0));
}
calendar.setTimeInMillis(1000 * timestamp);
return calendar.getTime();
}
public Date getLastEntryDate() throws SQLException {
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1).desc());
long timestamp = 0;
if (list.size() > 0) {
timestamp = Long.valueOf(list.get(0));
}
calendar.setTimeInMillis(1000 * timestamp);
return calendar.getTime();
}
public Date getLastEntryDate() throws SQLException {
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1).desc());
long timestamp = 0;
if (list.size() > 0) {
timestamp = Long.valueOf(list.get(0));
}
calendar.setTimeInMillis(1000 * timestamp);
return calendar.getTime();
}
public void insertInterval(String name) throws SQLException {
Long id = jedis.incr("global:next-interval-id");
String parameter = StringUtils.parameterize(name);
jedis.set(String.format("interval:%d:name", id), name);
jedis.set(String.format("interval:%s:id", parameter), String.valueOf(id));
}
public void insertInterval(String name) throws SQLException {
Long id = jedis.incr("global:next-interval-id");
String parameter = StringUtils.parameterize(name);
jedis.set(String.format("interval:%d:name", id), name);
jedis.set(String.format("interval:%s:id", parameter), String.valueOf(id));
}
public int selectIntervalId(String name) throws Exception {
String parameter = StringUtils.parameterize(name);
return Integer.valueOf(jedis.get(String.format("interval:%s:id", parameter)));
}
public int selectIntervalId(String name) throws Exception {
String parameter = StringUtils.parameterize(name);
return Integer.valueOf(jedis.get(String.format("interval:%s:id", parameter)));
}
public ArrayList<Entry> selectDataBetween(Date intervalStartDate, Date intervalEndDate) {
// PreparedStatement selectStatement = connection.prepareStatement("select * from data where date between ? and ?");
// selectStatement.setLong(1, intervalStartDate.getTime() / 1000);
// selectStatement.setLong(2, intervalEndDate.getTime() / 1000);
// ResultSet resultSet = selectStatement.executeQuery();
long min = intervalStartDate.getTime() / 1000;
long max = intervalEndDate.getTime() / 1000;
String key = String.format("data:%s:%s", min, max);
Set<String> dateSet = jedis.zrangeByScore("data", min, max);
public ArrayList<Entry> selectDataBetween(Date intervalStartDate, Date intervalEndDate) {
// PreparedStatement selectStatement = connection.prepareStatement("select * from data where date between ? and ?");
// selectStatement.setLong(1, intervalStartDate.getTime() / 1000);
// selectStatement.setLong(2, intervalEndDate.getTime() / 1000);
// ResultSet resultSet = selectStatement.executeQuery();
long min = intervalStartDate.getTime() / 1000;
long max = intervalEndDate.getTime() / 1000;
String key = String.format("data:%s:%s", min, max);
Set<String> dateSet = jedis.zrangeByScore("data", min, max);
String[] dateArray = dateSet.toArray(new String[0]);
ArrayList<Entry> entryList = new ArrayList<Entry>();
if (dateArray.length == 0) {
return entryList;
}
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(dateArray));
List<String> valueList = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
Iterator<String> iterator = dateSet.iterator();
int i = 0;
while (iterator.hasNext()) {
calendar.setTimeInMillis(1000 * Long.valueOf(iterator.next()));
entryList.add(new Entry(calendar.getTime(), Double.valueOf(valueList.get(i++))));
}
return entryList;
}
String[] dateArray = dateSet.toArray(new String[0]);
ArrayList<Entry> entryList = new ArrayList<Entry>();
if (dateArray.length == 0) {
return entryList;
}
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(dateArray));
List<String> valueList = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
Iterator<String> iterator = dateSet.iterator();
int i = 0;
while (iterator.hasNext()) {
calendar.setTimeInMillis(1000 * Long.valueOf(iterator.next()));
entryList.add(new Entry(calendar.getTime(), Double.valueOf(valueList.get(i++))));
}
return entryList;
}
public void insertDataBatch(long date, double value) throws SQLException {
// if (pipeline == null) {
// pipeline = jedis.pipelined();
// pipeline.multi();
// }
// if (insertDataBatchStatement == null) {
// insertDataBatchStatement = connection.prepareStatement("insert or ignore into data values(?, ?)");
// insertDataBatchCount = 0;
// }
// setDate(insertDataBatchStatement, 1, date);
// insertDataBatchStatement.setDouble(2, value);
// insertDataBatchStatement.addBatch();
public void insertDataBatch(long date, double value) throws SQLException {
// if (pipeline == null) {
// pipeline = jedis.pipelined();
// pipeline.multi();
// }
// if (insertDataBatchStatement == null) {
// insertDataBatchStatement = connection.prepareStatement("insert or ignore into data values(?, ?)");
// insertDataBatchCount = 0;
// }
// setDate(insertDataBatchStatement, 1, date);
// insertDataBatchStatement.setDouble(2, value);
// insertDataBatchStatement.addBatch();
//
// if(++insertDataBatchCount % BATCH_SIZE == 0) {
// insertDataBatchStatement.executeBatch();
// System.out.println(insertDataBatchCount);
// }
date /= 1000;
jedis.zadd(String.format("data"), date, String.valueOf(date));
jedis.set(String.format("data:%s:value", date), String.valueOf(value));
}
// if(++insertDataBatchCount % BATCH_SIZE == 0) {
// insertDataBatchStatement.executeBatch();
// System.out.println(insertDataBatchCount);
// }
date /= 1000;
jedis.zadd(String.format("data"), date, String.valueOf(date));
jedis.set(String.format("data:%s:value", date), String.valueOf(value));
}
public void insertDataBatchLast() throws SQLException {
//pipeline.exec();
//pipeline = null;
}
public void insertDataBatchLast() throws SQLException {
//pipeline.exec();
//pipeline = null;
}
public void insertExtremesBatch(int id, Date date, Date firstDate, Date lastDate, int count, Double min, Double max) throws SQLException {
// if (pipeline == null) {
// pipeline = jedis.pipelined();
// pipeline.multi();
// }
// HashMap<String, String> map = new HashMap<String, String>();
// map.put("first", String.valueOf(firstDate.getTime() / 1000));
// map.put("last", String.valueOf(lastDate.getTime() / 1000));
// map.put("count", String.valueOf(count));
// map.put("min", String.valueOf(min));
// map.put("max", String.valueOf(max));
public void insertExtremesBatch(int id, Date date, Date firstDate, Date lastDate, int count, Double min, Double max) throws SQLException {
// if (pipeline == null) {
// pipeline = jedis.pipelined();
// pipeline.multi();
// }
// HashMap<String, String> map = new HashMap<String, String>();
// map.put("first", String.valueOf(firstDate.getTime() / 1000));
// map.put("last", String.valueOf(lastDate.getTime() / 1000));
// map.put("count", String.valueOf(count));
// map.put("min", String.valueOf(min));
// map.put("max", String.valueOf(max));
int dateInt = (int) date.getTime() / 1000;
String key = String.format("extreme:%d:%d:", id, dateInt);
jedis.set(key + "first", String.valueOf(firstDate.getTime() / 1000));
jedis.set(key + "last", String.valueOf(lastDate.getTime() / 1000));
jedis.set(key + "count", String.valueOf(count));
jedis.set(key + "min", String.valueOf(min));
jedis.set(key + "max", String.valueOf(max));
jedis.zadd(String.format("interval:%d", id), dateInt, String.valueOf(dateInt));
//jedis.hmset(String.format("extreme:%d:%d", id, date.getTime() / 1000), map);
int dateInt = (int) date.getTime() / 1000;
String key = String.format("extreme:%d:%d:", id, dateInt);
jedis.set(key + "first", String.valueOf(firstDate.getTime() / 1000));
jedis.set(key + "last", String.valueOf(lastDate.getTime() / 1000));
jedis.set(key + "count", String.valueOf(count));
jedis.set(key + "min", String.valueOf(min));
jedis.set(key + "max", String.valueOf(max));
jedis.zadd(String.format("interval:%d", id), dateInt, String.valueOf(dateInt));
//jedis.hmset(String.format("extreme:%d:%d", id, date.getTime() / 1000), map);
if(++insertExtremesBatchStatementCount % BATCH_SIZE == 0) {
//pipeline.exec();
//pipeline.multi();
System.out.println(insertExtremesBatchStatementCount);
}
}
public void insertExtremesBatchLast() throws SQLException {
//pipeline.exec();
//pipeline = null;
}
if(++insertExtremesBatchStatementCount % BATCH_SIZE == 0) {
//pipeline.exec();
//pipeline.multi();
System.out.println(insertExtremesBatchStatementCount);
}
}
public void insertExtremesBatchLast() throws SQLException {
//pipeline.exec();
//pipeline = null;
}
protected void listData() throws SQLException {
// ResultSet resultSet = statement.executeQuery("select *,datetime(date, 'unixepoch') as fmt from data");
// while (resultSet.next()) {
// System.out.println("--------------------------");
// System.out.println("date = " + resultSet.getString("date"));
// System.out.println("value = " + resultSet.getFloat("value"));
// System.out.println("format = " + resultSet.getString("fmt"));
// }
}
protected void listData() throws SQLException {
// ResultSet resultSet = statement.executeQuery("select *,datetime(date, 'unixepoch') as fmt from data");
// while (resultSet.next()) {
// System.out.println("--------------------------");
// System.out.println("date = " + resultSet.getString("date"));
// System.out.println("value = " + resultSet.getFloat("value"));
// System.out.println("format = " + resultSet.getString("fmt"));
// }
}
protected String getField(ResultSet resultSet, String columnLabel) throws SQLException {
return resultSet.getString(columnLabel);
}
protected void printField(ResultSet resultSet, String columnLabel) throws SQLException {
System.out.printf("%s = %s\n", columnLabel, resultSet.getString(columnLabel));
}
protected String getField(ResultSet resultSet, String columnLabel) throws SQLException {
return resultSet.getString(columnLabel);
}
protected void printField(ResultSet resultSet, String columnLabel) throws SQLException {
System.out.printf("%s = %s\n", columnLabel, resultSet.getString(columnLabel));
}
protected void setDate(PreparedStatement statement, int parameterIndex, Date date) throws SQLException {
setDate(statement, parameterIndex, date.getTime());
}
protected void setDate(PreparedStatement statement, int parameterIndex, Date date) throws SQLException {
setDate(statement, parameterIndex, date.getTime());
}
protected void setDate(PreparedStatement statement, int parameterIndex, long date) throws SQLException {
statement.setLong(parameterIndex, date / 1000);
}
protected void setDate(PreparedStatement statement, int parameterIndex, long date) throws SQLException {
statement.setLong(parameterIndex, date / 1000);
}
public ResultSet getExtremes(long startDate, long endDate, String name) throws SQLException {
// PreparedStatement selectStatement = connection.prepareStatement(
// "select extremes.date date, extremes.first date_first, extremes.last date_last, extremes.count count, extremes.min min_value, extremes.max max_value, first.value first_value, last.value last_value " +
// "from extremes " +
// "left join data first on extremes.first = first.date " +
// "left join data last on extremes.last = last.date " +
// "where interval_id = (select id from intervals where name = ?) and extremes.date between ? and ?");
// selectStatement.setString(1, name);
// setDate(selectStatement, 2, startDate);
// setDate(selectStatement, 3, endDate);
// return selectStatement.executeQuery();
return null;
}
@SuppressWarnings("unchecked")
public String getJSON(long startDate, long endDate, String name) throws SQLException {
ResultSet resultSet = getExtremes(
DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
JSONArray jsonList = new JSONArray();
while (resultSet.next()) {
JSONArray jsonEntry = new JSONArray();
jsonEntry.add(1000 * resultSet.getLong("date"));
jsonEntry.add(resultSet.getDouble("first_value"));
jsonEntry.add(resultSet.getDouble("min_value"));
jsonEntry.add(resultSet.getDouble("max_value"));
jsonEntry.add(resultSet.getDouble("last_value"));
jsonList.add(jsonEntry);
}
return jsonList.toJSONString();
}
public void test(long startDate, long endDate, String name) throws SQLException {
ResultSet resultSet = getExtremes(startDate, endDate, name);
while (resultSet.next()) {
System.out.println("----------------");
System.out.printf("# %d\n", resultSet.getRow());
printField(resultSet, "date");
printField(resultSet, "date_first");
printField(resultSet, "count");
printField(resultSet, "min_value");
printField(resultSet, "max_value");
printField(resultSet, "first_value");
printField(resultSet, "last_value");
}
}
public ResultSet getExtremes(long startDate, long endDate, String name) throws SQLException {
// PreparedStatement selectStatement = connection.prepareStatement(
// "select extremes.date date, extremes.first date_first, extremes.last date_last, extremes.count count, extremes.min min_value, extremes.max max_value, first.value first_value, last.value last_value " +
// "from extremes " +
// "left join data first on extremes.first = first.date " +
// "left join data last on extremes.last = last.date " +
// "where interval_id = (select id from intervals where name = ?) and extremes.date between ? and ?");
// selectStatement.setString(1, name);
// setDate(selectStatement, 2, startDate);
// setDate(selectStatement, 3, endDate);
// return selectStatement.executeQuery();
return null;
}
@SuppressWarnings("unchecked")
public String getJSON(long startDate, long endDate, String name) throws SQLException {
ResultSet resultSet = getExtremes(
DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
JSONArray jsonList = new JSONArray();
while (resultSet.next()) {
JSONArray jsonEntry = new JSONArray();
jsonEntry.add(1000 * resultSet.getLong("date"));
jsonEntry.add(resultSet.getDouble("first_value"));
jsonEntry.add(resultSet.getDouble("min_value"));
jsonEntry.add(resultSet.getDouble("max_value"));
jsonEntry.add(resultSet.getDouble("last_value"));
jsonList.add(jsonEntry);
}
return jsonList.toJSONString();
}
public void test(long startDate, long endDate, String name) throws SQLException {
ResultSet resultSet = getExtremes(startDate, endDate, name);
while (resultSet.next()) {
System.out.println("----------------");
System.out.printf("# %d\n", resultSet.getRow());
printField(resultSet, "date");
printField(resultSet, "date_first");
printField(resultSet, "count");
printField(resultSet, "min_value");
printField(resultSet, "max_value");
printField(resultSet, "first_value");
printField(resultSet, "last_value");
}
}
public void test() {
/*Transaction transaction = jedis.multi();
Pipeline pipeline = jedis.pipelined();
Client client = jedis.getClient();*/
// ZRANGEBYSCORE data 1375085460 1375088460
// SORT data BY NOSORT GET data:*:value
public void test() {
/*Transaction transaction = jedis.multi();
Pipeline pipeline = jedis.pipelined();
Client client = jedis.getClient();*/
// ZRANGEBYSCORE data 1375085460 1375088460
// SORT data BY NOSORT GET data:*:value
int min = 1375085460;
int max = 1375088460;
String key = String.format("data:%s:%s", min, max);
Set<String> set = jedis.zrangeByScore("data", min, max);
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
List<String> list = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
for (String string : list) {
System.out.println(string);
}
Map<String, String> a = jedis.hgetAll("extreme:2");
System.out.println(a.get("date"));
//jedis.sort(date, new SortingParams().nosort().get("extreme:*"));
int min = 1375085460;
int max = 1375088460;
String key = String.format("data:%s:%s", min, max);
Set<String> set = jedis.zrangeByScore("data", min, max);
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
List<String> list = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
for (String string : list) {
System.out.println(string);
}
Map<String, String> a = jedis.hgetAll("extreme:2");
System.out.println(a.get("date"));
//jedis.sort(date, new SortingParams().nosort().get("extreme:*"));
//set = jedis.zrange("interval:1", 0, -1);
min = 0;
max = Integer.MAX_VALUE;
key = String.format("interval:1:%s:%s", min, max);
set = jedis.zrangeByScore("interval:1", min, max);
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
list = jedis.sort(key, new SortingParams().nosort().get(new String[] {
"extreme:1:*:count",
"extreme:1:*:first"}));
//Iterator<String> iterator = set.iterator();
for (String string : list) {
System.out.println(string);
}
}
//set = jedis.zrange("interval:1", 0, -1);
min = 0;
max = Integer.MAX_VALUE;
key = String.format("interval:1:%s:%s", min, max);
set = jedis.zrangeByScore("interval:1", min, max);
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
list = jedis.sort(key, new SortingParams().nosort().get(new String[] {
"extreme:1:*:count",
"extreme:1:*:first"}));
//Iterator<String> iterator = set.iterator();
for (String string : list) {
System.out.println(string);
}
}
}

View File

@@ -5,40 +5,40 @@ import java.util.GregorianCalendar;
import java.util.TimeZone;
public class DateUtils {
public static final String TIMEZONE = "Europe/Amsterdam";
protected static TimeZone timeZone;
protected static Calendar calendar;
public static final String TIMEZONE = "Europe/Amsterdam";
protected static TimeZone timeZone;
protected static Calendar calendar;
public static void setTimeZone(String timeZone) {
DateUtils.timeZone = TimeZone.getTimeZone(timeZone);
calendar = new GregorianCalendar(DateUtils.timeZone);
}
public static void setTimeZone(String timeZone) {
DateUtils.timeZone = TimeZone.getTimeZone(timeZone);
calendar = new GregorianCalendar(DateUtils.timeZone);
}
public static long makeTimestamp(int year, int month, int day, int hour, int minute, int second, int millisecond) {
checkCalendar();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, millisecond);
return calendar.getTimeInMillis();
}
public static long makeTimestamp(int year, int month, int day, int hour, int minute, int second, int millisecond) {
checkCalendar();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month - 1);
calendar.set(Calendar.DATE, day);
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
calendar.set(Calendar.MILLISECOND, millisecond);
return calendar.getTimeInMillis();
}
public static TimeZone getTimeZone() {
checkCalendar();
return timeZone;
}
public static TimeZone getTimeZone() {
checkCalendar();
return timeZone;
}
public static Calendar getCalendar() {
checkCalendar();
return calendar;
}
public static Calendar getCalendar() {
checkCalendar();
return calendar;
}
protected static void checkCalendar() {
if (calendar == null ) {
setTimeZone(TIMEZONE);
}
}
protected static void checkCalendar() {
if (calendar == null ) {
setTimeZone(TIMEZONE);
}
}
}

View File

@@ -12,73 +12,73 @@ import java.net.URL;
import java.util.Scanner;
public class FileUtils {
public static final boolean REVERSE = false;
public static String resourceToString(String name) throws IOException {
File file = resourceToFile(name);
Scanner scanner = new Scanner(file);
String string = scanner.useDelimiter("\\A").next();
scanner.close();
return string;
}
public static final boolean REVERSE = false;
public static String resourceToString(String name) throws IOException {
File file = resourceToFile(name);
Scanner scanner = new Scanner(file);
String string = scanner.useDelimiter("\\A").next();
scanner.close();
return string;
}
public static File resourceToFile(String name) throws IOException {
URL url = ClassLoader.getSystemClassLoader().getResource(name);
if (url != null) {
try {
return new File(url.toURI());
} catch (URISyntaxException e) {}
}
throw new IOException();
}
public static File resourceToFile(String name) throws IOException {
URL url = ClassLoader.getSystemClassLoader().getResource(name);
if (url != null) {
try {
return new File(url.toURI());
} catch (URISyntaxException e) {}
}
throw new IOException();
}
public static BufferedReader resourceToReader(String name) throws IOException {
return resourceToReader(name, REVERSE);
}
public static BufferedReader resourceToReader(String name) throws IOException {
return resourceToReader(name, REVERSE);
}
public static BufferedReader resourceToReader(String name, boolean reverse) throws IOException {
File file = resourceToFile(name);
return fileToReader(file, reverse);
}
public static BufferedReader resourceToReader(String name, boolean reverse) throws IOException {
File file = resourceToFile(name);
return fileToReader(file, reverse);
}
public static BufferedReader fileToBufferedReader(File file) throws FileNotFoundException {
return fileToReader(file, REVERSE);
}
public static BufferedReader fileToBufferedReader(File file) throws FileNotFoundException {
return fileToReader(file, REVERSE);
}
public static BufferedReader fileToReader(File file, boolean reverse) throws FileNotFoundException {
InputStream inputStream = fileToInputStream(file, reverse);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
return new BufferedReader(inputStreamReader);
}
public static BufferedReader fileToReader(File file, boolean reverse) throws FileNotFoundException {
InputStream inputStream = fileToInputStream(file, reverse);
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
return new BufferedReader(inputStreamReader);
}
public static InputStream fileToInputStream(File file) throws FileNotFoundException {
return fileToInputStream(file, REVERSE);
}
public static InputStream fileToInputStream(File file) throws FileNotFoundException {
return fileToInputStream(file, REVERSE);
}
public static InputStream fileToInputStream(File file, boolean reverse) throws FileNotFoundException {
InputStream inputStream;
if (reverse) {
inputStream = new ReverseLineInputStream(file);
} else {
inputStream = new FileInputStream(file);
}
return inputStream;
}
public static InputStream fileToInputStream(File file, boolean reverse) throws FileNotFoundException {
InputStream inputStream;
if (reverse) {
inputStream = new ReverseLineInputStream(file);
} else {
inputStream = new FileInputStream(file);
}
return inputStream;
}
public static InputStream resourceToInputStream(String name) throws IOException {
return resourceToInputStream(name, REVERSE);
}
public static InputStream resourceToInputStream(String name) throws IOException {
return resourceToInputStream(name, REVERSE);
}
public static InputStream resourceToInputStream(String name, boolean reverse) throws IOException {
if (reverse) {
File file = resourceToFile(name);
return fileToInputStream(file, reverse);
} else {
return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
}
}
public static InputStream resourceToInputStream(String name, boolean reverse) throws IOException {
if (reverse) {
File file = resourceToFile(name);
return fileToInputStream(file, reverse);
} else {
return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
}
}
public static Scanner resourceToScanner(String string) throws IOException {
return new Scanner(resourceToReader(string));
}
public static Scanner resourceToScanner(String string) throws IOException {
return new Scanner(resourceToReader(string));
}
}

View File

@@ -24,108 +24,108 @@ import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisDataException;
public class LuaUtils {
public static final boolean LOAD = true;
public static final List<String> EMPTY = new ArrayList<String>();
public static final String[] LIBRARY_ARRAY = {"redis", "table", "string", "math", "debug", "cjson", "cmsgpack"};
public static final List<String> LIBRARY_LIST = Arrays.asList(LIBRARY_ARRAY);
public static final boolean LOAD = true;
public static final List<String> EMPTY = new ArrayList<String>();
public static final String[] LIBRARY_ARRAY = {"redis", "table", "string", "math", "debug", "cjson", "cmsgpack"};
public static final List<String> LIBRARY_LIST = Arrays.asList(LIBRARY_ARRAY);
protected static JedisPool jedisPool;
protected static HashMap<String, String> hashMap = new HashMap<String, String>();
protected static JedisPool jedisPool;
protected static HashMap<String, String> hashMap = new HashMap<String, String>();
public static void setPool(JedisPool pool) {
LuaUtils.jedisPool = pool;
}
public static void setPool(JedisPool pool) {
LuaUtils.jedisPool = pool;
}
public static Object eval(String name, List<String> keys, List<String> args) throws IOException {
Jedis jedis = jedisPool.getResource();
String hash = getHash(name, jedis);
System.out.println(hash);
Object object = jedis.evalsha(hash, keys, args);
jedisPool.returnResource(jedis);
return object;
}
public static Object eval(String name, List<String> keys, List<String> args) throws IOException {
Jedis jedis = jedisPool.getResource();
String hash = getHash(name, jedis);
System.out.println(hash);
Object object = jedis.evalsha(hash, keys, args);
jedisPool.returnResource(jedis);
return object;
}
public static Object eval(String name, int keys, String... args) throws IOException {
Jedis jedis = getResource();
String hash = getHash(name, jedis);
try {
return jedis.evalsha(hash, keys, args);
} catch (JedisDataException e) {
if (e.getMessage().startsWith("NOSCRIPT")) {
// Script not loaded correctly
}
System.out.println(e);
throw e;
} finally {
returnResource(jedis);
}
}
public static Object eval(String name, int keys, String... args) throws IOException {
Jedis jedis = getResource();
String hash = getHash(name, jedis);
try {
return jedis.evalsha(hash, keys, args);
} catch (JedisDataException e) {
if (e.getMessage().startsWith("NOSCRIPT")) {
// Script not loaded correctly
}
System.out.println(e);
throw e;
} finally {
returnResource(jedis);
}
}
public static Object eval(String name) throws IOException {
return eval(name, 0);
}
public static Object eval(String name) throws IOException {
return eval(name, 0);
}
public static Object eval(String string, List<String> params) throws IOException {
return eval(string, EMPTY, params);
}
protected static String getHash(String name, Jedis jedis) throws IOException {
name = String.format("%s.lua", name);
if (hashMap.containsKey(name)) {
return hashMap.get(name);
} else {
String lua = FileUtils.resourceToString(name);
public static Object eval(String string, List<String> params) throws IOException {
return eval(string, EMPTY, params);
}
protected static String getHash(String name, Jedis jedis) throws IOException {
name = String.format("%s.lua", name);
if (hashMap.containsKey(name)) {
return hashMap.get(name);
} else {
String lua = FileUtils.resourceToString(name);
Map<String, List<String>> libraryMap = new HashMap<String, List<String>>();
Map<String, List<String>> libraryMap = new HashMap<String, List<String>>();
lua = resolveDepencencies(lua, libraryMap);
for (String library : libraryMap.keySet()) {
lua = String.format("local %s = {}\n%s", library, lua);
}
System.out.println("======");
System.out.println(lua);
System.out.println("======");
String hash = DigestUtils.sha1Hex(lua);
if (!jedis.scriptLoad(lua).equals(hash)) {
// Hashes don't match
}
hashMap.put(name, hash);
return hash;
}
}
lua = resolveDepencencies(lua, libraryMap);
for (String library : libraryMap.keySet()) {
lua = String.format("local %s = {}\n%s", library, lua);
}
System.out.println("======");
System.out.println(lua);
System.out.println("======");
String hash = DigestUtils.sha1Hex(lua);
if (!jedis.scriptLoad(lua).equals(hash)) {
// Hashes don't match
}
hashMap.put(name, hash);
return hash;
}
}
protected static String resolveDepencencies(String lua, Map<String, List<String>> libraryMap) throws IOException {
Pattern pattern = Pattern.compile(String.format("([a-z]+)%s([a-z]+)", Pattern.quote(".")));
Matcher matcher = pattern.matcher(lua);
String depencencyLua = "";
while (matcher.find()) {
String library = matcher.group(1);
if (!LIBRARY_LIST.contains(library)) {
if (!libraryMap.containsKey(library)) {
libraryMap.put(library, new ArrayList<String>());
}
List<String> methodList = libraryMap.get(library);
String method = matcher.group(2);
if (!methodList.contains(method)) {
String file = String.format("%s/%s.lua", library, method);
System.out.println(file);
String methodLua = FileUtils.resourceToString(file);
methodList.add(method);
String subDepencencyLua = resolveDepencencies(methodLua, libraryMap);
if (depencencyLua.isEmpty()) {
depencencyLua = subDepencencyLua;
} else if (!subDepencencyLua.isEmpty()) {
depencencyLua = String.format("%s\n%s", depencencyLua, subDepencencyLua);
}
}
}
}
if (depencencyLua.isEmpty()) {
return lua;
} else {
return String.format("%s\n%s", depencencyLua, lua);
}
}
protected static String resolveDepencencies(String lua, Map<String, List<String>> libraryMap) throws IOException {
Pattern pattern = Pattern.compile(String.format("([a-z]+)%s([a-z]+)", Pattern.quote(".")));
Matcher matcher = pattern.matcher(lua);
String depencencyLua = "";
while (matcher.find()) {
String library = matcher.group(1);
if (!LIBRARY_LIST.contains(library)) {
if (!libraryMap.containsKey(library)) {
libraryMap.put(library, new ArrayList<String>());
}
List<String> methodList = libraryMap.get(library);
String method = matcher.group(2);
if (!methodList.contains(method)) {
String file = String.format("%s/%s.lua", library, method);
System.out.println(file);
String methodLua = FileUtils.resourceToString(file);
methodList.add(method);
String subDepencencyLua = resolveDepencencies(methodLua, libraryMap);
if (depencencyLua.isEmpty()) {
depencencyLua = subDepencencyLua;
} else if (!subDepencencyLua.isEmpty()) {
depencencyLua = String.format("%s\n%s", depencencyLua, subDepencencyLua);
}
}
}
}
if (depencencyLua.isEmpty()) {
return lua;
} else {
return String.format("%s\n%s", depencencyLua, lua);
}
}
protected static void returnResource(Jedis jedis) {
jedisPool.returnResource(jedis);
@@ -135,56 +135,56 @@ public class LuaUtils {
return jedisPool.getResource();
}
public static Object insert(String table, Map<String, String> hash) throws IOException {
List<String> params = new ArrayList<String>();
for (final Entry<String, String> entry : hash.entrySet()) {
params.add(entry.getKey());
params.add(entry.getValue());
params.add(StringUtils.parameterize(entry.getValue()));
}
params.add(0, table);
return LuaUtils.eval("insert", params);
}
public static Object insert(String table, Map<String, String> hash) throws IOException {
List<String> params = new ArrayList<String>();
for (final Entry<String, String> entry : hash.entrySet()) {
params.add(entry.getKey());
params.add(entry.getValue());
params.add(StringUtils.parameterize(entry.getValue()));
}
params.add(0, table);
return LuaUtils.eval("insert", params);
}
@SuppressWarnings("unchecked")
public static List<Map<String, String>> getAll(String table) throws IOException {
List<String> params = new ArrayList<String>();
params.add(table);
List<List<String>> listList = (List<List<String>>) LuaUtils.eval("getall", params);
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
System.out.println(listList.size());
for (List<String> list : listList) {
mapList.add(TypeUtils.listToMap(list));
}
return mapList;
}
@SuppressWarnings("unchecked")
public static List<Map<String, String>> getAll(String table) throws IOException {
List<String> params = new ArrayList<String>();
params.add(table);
List<List<String>> listList = (List<List<String>>) LuaUtils.eval("getall", params);
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
System.out.println(listList.size());
for (List<String> list : listList) {
mapList.add(TypeUtils.listToMap(list));
}
return mapList;
}
@SuppressWarnings("unchecked")
public static List<Map<String, String>> getSome(String... stringArray) throws IOException {
List<List<String>> listList = (List<List<String>>) LuaUtils.eval("getsome", Arrays.asList(stringArray));
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
for (List<String> list : listList) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i < stringArray.length; ++i) {
map.put(stringArray[i], list.get(i - 1));
}
mapList.add(map);
}
return mapList;
}
@SuppressWarnings("unchecked")
public static List<Map<String, String>> getSome(String... stringArray) throws IOException {
List<List<String>> listList = (List<List<String>>) LuaUtils.eval("getsome", Arrays.asList(stringArray));
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
for (List<String> list : listList) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i < stringArray.length; ++i) {
map.put(stringArray[i], list.get(i - 1));
}
mapList.add(map);
}
return mapList;
}
@SuppressWarnings("unchecked")
public static List<Map<String, String>> getSomeX(String... stringArray) throws IOException {
List<String> list = (List<String>) LuaUtils.eval("getsomex", Arrays.asList(stringArray));
Iterator<String> iterator = list.iterator();
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
while (iterator.hasNext()) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i < stringArray.length; ++i) {
map.put(stringArray[i], iterator.next());
}
mapList.add(map);
}
return mapList;
}
@SuppressWarnings("unchecked")
public static List<Map<String, String>> getSomeX(String... stringArray) throws IOException {
List<String> list = (List<String>) LuaUtils.eval("getsomex", Arrays.asList(stringArray));
Iterator<String> iterator = list.iterator();
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
while (iterator.hasNext()) {
Map<String, String> map = new HashMap<String, String>();
for (int i = 1; i < stringArray.length; ++i) {
map.put(stringArray[i], iterator.next());
}
mapList.add(map);
}
return mapList;
}
}

View File

@@ -7,62 +7,62 @@ import java.io.InputStream;
import java.io.RandomAccessFile;
public class ReverseLineInputStream extends InputStream {
RandomAccessFile randomAccessFile;
RandomAccessFile randomAccessFile;
long currentLineStart = -1;
long currentLineEnd = -1;
long currentPos = -1;
long lastPosInFile = -1;
long currentLineStart = -1;
long currentLineEnd = -1;
long currentPos = -1;
long lastPosInFile = -1;
public ReverseLineInputStream(File file) throws FileNotFoundException {
randomAccessFile = new RandomAccessFile(file, "r");
currentLineStart = file.length();
currentLineEnd = file.length();
lastPosInFile = file.length() - 1;
currentPos = currentLineEnd;
}
public ReverseLineInputStream(File file) throws FileNotFoundException {
randomAccessFile = new RandomAccessFile(file, "r");
currentLineStart = file.length();
currentLineEnd = file.length();
lastPosInFile = file.length() - 1;
currentPos = currentLineEnd;
}
public void findPrevLine() throws IOException {
currentLineEnd = currentLineStart;
public void findPrevLine() throws IOException {
currentLineEnd = currentLineStart;
// No more lines, since at the beginning of the file
if (currentLineEnd == 0) {
currentLineEnd = -1;
currentLineStart = -1;
currentPos = -1;
return;
}
// No more lines, since at the beginning of the file
if (currentLineEnd == 0) {
currentLineEnd = -1;
currentLineStart = -1;
currentPos = -1;
return;
}
long filePointer = currentLineStart - 1;
while (true) {
// At start of file so this is the first line in the file.
if (--filePointer < 0) {
break;
}
long filePointer = currentLineStart - 1;
while (true) {
// At start of file so this is the first line in the file.
if (--filePointer < 0) {
break;
}
randomAccessFile.seek(filePointer);
int readByte = randomAccessFile.readByte();
randomAccessFile.seek(filePointer);
int readByte = randomAccessFile.readByte();
// Ignore last LF in file. search back to find the previous LF.
if (readByte == 0xA && filePointer != lastPosInFile) {
break;
}
}
// Start at pointer +1, after the LF found or at 0 the start of the file.
currentLineStart = filePointer + 1;
currentPos = currentLineStart;
}
// Ignore last LF in file. search back to find the previous LF.
if (readByte == 0xA && filePointer != lastPosInFile) {
break;
}
}
// Start at pointer +1, after the LF found or at 0 the start of the file.
currentLineStart = filePointer + 1;
currentPos = currentLineStart;
}
public int read() throws IOException {
if (currentPos < currentLineEnd) {
randomAccessFile.seek(currentPos++);
int readByte = randomAccessFile.readByte();
return readByte;
} else if (currentPos < 0) {
return -1;
} else {
findPrevLine();
return read();
}
}
public int read() throws IOException {
if (currentPos < currentLineEnd) {
randomAccessFile.seek(currentPos++);
int readByte = randomAccessFile.readByte();
return readByte;
} else if (currentPos < 0) {
return -1;
} else {
findPrevLine();
return read();
}
}
}

View File

@@ -5,15 +5,15 @@ import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
public class StringUtils {
protected static final char SEPARATOR = '-';
public static String parameterize(String input) {
return parameterize(input, SEPARATOR);
}
protected static final char SEPARATOR = '-';
public static String parameterize(String input) {
return parameterize(input, SEPARATOR);
}
public static String parameterize(String input, char separator) {
Splitter splitter = Splitter.on(CharMatcher.JAVA_LETTER_OR_DIGIT.negate()).omitEmptyStrings();
Joiner joiner = Joiner.on(separator);
return joiner.join(splitter.split(input)).toLowerCase();
}
public static String parameterize(String input, char separator) {
Splitter splitter = Splitter.on(CharMatcher.JAVA_LETTER_OR_DIGIT.negate()).omitEmptyStrings();
Joiner joiner = Joiner.on(separator);
return joiner.join(splitter.split(input)).toLowerCase();
}
}

View File

@@ -6,12 +6,12 @@ import java.util.List;
import java.util.Map;
public class TypeUtils {
public static <T> Map<T,T> listToMap(List<T> list) {
Map<T, T> map = new HashMap<T, T>();
Iterator<T> iterator = list.iterator();
while (iterator.hasNext()) {
map.put(iterator.next(), iterator.next());
}
return map;
}
public static <T> Map<T,T> listToMap(List<T> list) {
Map<T, T> map = new HashMap<T, T>();
Iterator<T> iterator = list.iterator();
while (iterator.hasNext()) {
map.put(iterator.next(), iterator.next());
}
return map;
}
}

View File

@@ -15,85 +15,85 @@ import util.FileUtils;
import util.LuaUtils;
public class Flight {
private SimpleDateFormat inputSDF;
private JedisPool pool;
private SimpleDateFormat inputSDF;
private JedisPool pool;
public Flight() {
inputSDF = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
}
public Flight() {
inputSDF = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
}
public static void main(String[] args) throws Exception {
new Flight().start();
}
public static void main(String[] args) throws Exception {
new Flight().start();
}
private void start() throws IOException, ParseException {
clear();
insert();
list();
}
private void start() throws IOException, ParseException {
clear();
insert();
list();
}
private void clear() {
Jedis jedis = pool.getResource();
Set<String> set = jedis.keys("*");
if (set.size() > 0) {
jedis.del(set.toArray(new String[0]));
}
pool.returnResource(jedis);
}
private void clear() {
Jedis jedis = pool.getResource();
Set<String> set = jedis.keys("*");
if (set.size() > 0) {
jedis.del(set.toArray(new String[0]));
}
pool.returnResource(jedis);
}
private void insert() throws IOException {
// http://robots.thoughtbot.com/post/46335890055/redis-set-intersection-using-sets-to-filter-data
Scanner lineScanner = FileUtils.resourceToScanner("flight.txt");
lineScanner.nextLine();
while (lineScanner.hasNextLine()) {
Scanner scanner = new Scanner(lineScanner.nextLine());
HashMap<String,String> map = new HashMap<String,String>();
scanner.useDelimiter("\t");
scanner.nextInt(); // id
String date = scanner.next();
long time;
try {
time = inputSDF.parse(date).getTime() / 1000;
map.put("departure_time", String.valueOf(time));
map.put("airline", scanner.next());
map.put("departure", scanner.next());
map.put("arrival", scanner.next());
long id = (Long) LuaUtils.insert("departures", map);
System.out.println(id);
private void insert() throws IOException {
// http://robots.thoughtbot.com/post/46335890055/redis-set-intersection-using-sets-to-filter-data
Scanner lineScanner = FileUtils.resourceToScanner("flight.txt");
lineScanner.nextLine();
while (lineScanner.hasNextLine()) {
Scanner scanner = new Scanner(lineScanner.nextLine());
HashMap<String,String> map = new HashMap<String,String>();
scanner.useDelimiter("\t");
scanner.nextInt(); // id
String date = scanner.next();
long time;
try {
time = inputSDF.parse(date).getTime() / 1000;
map.put("departure_time", String.valueOf(time));
map.put("airline", scanner.next());
map.put("departure", scanner.next());
map.put("arrival", scanner.next());
long id = (Long) LuaUtils.insert("departures", map);
System.out.println(id);
Jedis jedis = pool.getResource();
jedis.zadd("departures:departure_time", time, String.valueOf(id));
pool.returnResource(jedis);
} catch (ParseException e) {
e.printStackTrace();
}
scanner.close();
}
lineScanner.close();
}
Jedis jedis = pool.getResource();
jedis.zadd("departures:departure_time", time, String.valueOf(id));
pool.returnResource(jedis);
} catch (ParseException e) {
e.printStackTrace();
}
scanner.close();
}
lineScanner.close();
}
private void list() throws IOException {
Jedis jedis = pool.getResource();
System.out.println(jedis.hgetAll("departures:1"));
pool.returnResource(jedis);
private void list() throws IOException {
Jedis jedis = pool.getResource();
System.out.println(jedis.hgetAll("departures:1"));
pool.returnResource(jedis);
List<Map<String, String>> listMap;
List<Map<String, String>> listMap;
listMap = LuaUtils.getSome("departures", "airline", "arrival");
for (Map<String, String> map : listMap) {
System.out.println(map);
}
listMap = LuaUtils.getSome("departures", "airline", "arrival");
for (Map<String, String> map : listMap) {
System.out.println(map);
}
listMap = LuaUtils.getSomeX("departures", "airline", "arrival");
for (Map<String, String> map : listMap) {
System.out.println(map);
}
listMap = LuaUtils.getAll("departures");
for (Map<String, String> map : listMap) {
System.out.println(map);
}
}
listMap = LuaUtils.getSomeX("departures", "airline", "arrival");
for (Map<String, String> map : listMap) {
System.out.println(map);
}
listMap = LuaUtils.getAll("departures");
for (Map<String, String> map : listMap) {
System.out.println(map);
}
}
}

View File

@@ -24,121 +24,121 @@ import util.DateUtils;
import util.FileUtils;
public class Relational {
protected Model model;
protected SimpleDateFormat inputSDF;
protected SimpleDateFormat outputSDF;
protected Model model;
protected SimpleDateFormat inputSDF;
protected SimpleDateFormat outputSDF;
public Relational() throws SQLException {
model = new Model(DateUtils.getCalendar());
inputSDF = new SimpleDateFormat("yyyyMMdd HH:mm");
inputSDF.setTimeZone(DateUtils.getTimeZone());
outputSDF = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
inputSDF.setTimeZone(DateUtils.getTimeZone());
}
public Relational() throws SQLException {
model = new Model(DateUtils.getCalendar());
inputSDF = new SimpleDateFormat("yyyyMMdd HH:mm");
inputSDF.setTimeZone(DateUtils.getTimeZone());
outputSDF = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
inputSDF.setTimeZone(DateUtils.getTimeZone());
}
public static void main(String[] args) throws FileNotFoundException {
try {
new Relational().start();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
try {
new Relational().start();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() throws Exception {
insertFromFile();
generateIntervals();
// model.test(
// DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
// DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
model.test();
}
public void start() throws Exception {
insertFromFile();
generateIntervals();
// model.test(
// DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
// DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
model.test();
}
public void insertFromFile() throws SQLException, IOException {
Date endDate;
boolean checkDate = ((endDate = model.getLastEntryDate()) != null);
System.out.println(endDate);
JSONParser parser = new JSONParser();
BufferedReader bufferedReader = FileUtils.resourceToReader("pvoutput.debug", true);
String line;
int i = 0;
while ((line = bufferedReader.readLine()) != null) {
Object object = null;
try {
JSONArray json = (JSONArray) parser.parse(line);
object = parser.parse(json.get(3).toString());
} catch (ParseException e) {
continue;
}
@SuppressWarnings("unchecked")
HashMap<String, Object> map = (HashMap<String, Object>) object;
String value = String.valueOf(map.get("v5"));
if (!value.equals("null")) {
try {
Date date = inputSDF.parse(String.format("%s %s", map.get("d"), map.get("t")));
if (checkDate && date.compareTo(endDate) < 0) {
break;
}
if (++i > 100) break;
model.insertDataBatch(date.getTime(), Double.parseDouble(value));
} catch (java.text.ParseException e) {
continue;
}
}
}
bufferedReader.close();
model.insertDataBatchLast();
}
public void insertFromFile() throws SQLException, IOException {
Date endDate;
boolean checkDate = ((endDate = model.getLastEntryDate()) != null);
System.out.println(endDate);
JSONParser parser = new JSONParser();
BufferedReader bufferedReader = FileUtils.resourceToReader("pvoutput.debug", true);
String line;
int i = 0;
while ((line = bufferedReader.readLine()) != null) {
Object object = null;
try {
JSONArray json = (JSONArray) parser.parse(line);
object = parser.parse(json.get(3).toString());
} catch (ParseException e) {
continue;
}
@SuppressWarnings("unchecked")
HashMap<String, Object> map = (HashMap<String, Object>) object;
String value = String.valueOf(map.get("v5"));
if (!value.equals("null")) {
try {
Date date = inputSDF.parse(String.format("%s %s", map.get("d"), map.get("t")));
if (checkDate && date.compareTo(endDate) < 0) {
break;
}
if (++i > 100) break;
model.insertDataBatch(date.getTime(), Double.parseDouble(value));
} catch (java.text.ParseException e) {
continue;
}
}
}
bufferedReader.close();
model.insertDataBatchLast();
}
public void generateIntervals() throws Exception {
Date startDate = model.getFirstEntryDate();
Date endDate = model.getLastEntryDate();
public void generateIntervals() throws Exception {
Date startDate = model.getFirstEntryDate();
Date endDate = model.getLastEntryDate();
for (Object object : ServiceLoader.load(interval.Interval.class)) {
Interval interval = (Interval) object;
interval.setDate(startDate, endDate);
processInterval(interval);
}
}
for (Object object : ServiceLoader.load(interval.Interval.class)) {
Interval interval = (Interval) object;
interval.setDate(startDate, endDate);
processInterval(interval);
}
}
protected void processInterval(Interval interval) throws Exception {
String name = interval.getClass().getSimpleName();
model.insertInterval(name);
int id = model.selectIntervalId(name);
protected void processInterval(Interval interval) throws Exception {
String name = interval.getClass().getSimpleName();
model.insertInterval(name);
int id = model.selectIntervalId(name);
System.out.printf("[%d] %s\n", id, name);
System.out.println("========================================");
System.out.printf("[%d] %s\n", id, name);
System.out.println("========================================");
do {
Date intervalStartDate = interval.get();
Date intervalEndDate = interval.next();
do {
Date intervalStartDate = interval.get();
Date intervalEndDate = interval.next();
ArrayList<Entry> entryList = model.selectDataBetween(intervalStartDate, intervalEndDate);
int count = entryList.size();
if (count > 0) {
System.out.printf("%s - %s (%d)\n", formatDate(intervalStartDate), formatDate(intervalEndDate), count);
ArrayList<Double> valueList = new ArrayList<Double>();
for (Entry entry : entryList) {
System.out.printf("%s\t%f\n", formatDate(entry.date), entry.value);
valueList.add(entry.value);
}
model.insertExtremesBatch(
id,
intervalStartDate,
entryList.get(0).date,
entryList.get(count - 1).date,
count,
Collections.min(valueList),
Collections.max(valueList));
}
}
while (interval.hasNext());
model.insertExtremesBatchLast();
}
ArrayList<Entry> entryList = model.selectDataBetween(intervalStartDate, intervalEndDate);
int count = entryList.size();
if (count > 0) {
System.out.printf("%s - %s (%d)\n", formatDate(intervalStartDate), formatDate(intervalEndDate), count);
ArrayList<Double> valueList = new ArrayList<Double>();
for (Entry entry : entryList) {
System.out.printf("%s\t%f\n", formatDate(entry.date), entry.value);
valueList.add(entry.value);
}
model.insertExtremesBatch(
id,
intervalStartDate,
entryList.get(0).date,
entryList.get(count - 1).date,
count,
Collections.min(valueList),
Collections.max(valueList));
}
}
while (interval.hasNext());
model.insertExtremesBatchLast();
}
protected String formatDate(Date date) {
return outputSDF.format(date.getTime());
}
protected String formatDate(Date date) {
return outputSDF.format(date.getTime());
}
}

View File

@@ -6,31 +6,31 @@ import redis.clients.jedis.Jedis;
public class Test1 {
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost", 16379);
//jedis.select(0);
jedis.set("foo", "bar");
public static void main(String[] args) {
Jedis jedis = new Jedis("localhost", 16379);
//jedis.select(0);
jedis.set("foo", "bar");
String value = jedis.get("foo");
System.out.println(value);
jedis.del("item");
for (int i = 0; i < 10; ++i ) {
jedis.zadd(String.format("item"), 2*i, String.valueOf(Math.random()));
//jedis.sadd(String.format("items"), String.valueOf(i));
}
Set<String> set = jedis.zrange("item", 0, -1);
for (String item : set) {
System.out.println(item);
}
System.out.println("-----------");
set = jedis.zrangeByScore("item", 4, 7);
for (String item : set) {
System.out.println(item);
}
//jedis.close();
}
String value = jedis.get("foo");
System.out.println(value);
jedis.del("item");
for (int i = 0; i < 10; ++i ) {
jedis.zadd(String.format("item"), 2*i, String.valueOf(Math.random()));
//jedis.sadd(String.format("items"), String.valueOf(i));
}
Set<String> set = jedis.zrange("item", 0, -1);
for (String item : set) {
System.out.println(item);
}
System.out.println("-----------");
set = jedis.zrangeByScore("item", 4, 7);
for (String item : set) {
System.out.println(item);
}
//jedis.close();
}
}

View File

@@ -10,20 +10,20 @@ import redis.clients.jedis.JedisPool;
import util.LuaUtils;
public class Test2 {
public static void main(String[] args) throws IOException {
JedisPool pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
public static void main(String[] args) throws IOException {
JedisPool pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
Object o1 = LuaUtils.getSome("departures", "airline", "arrival");
System.out.println(o1);
Object o1 = LuaUtils.getSome("departures", "airline", "arrival");
System.out.println(o1);
List<String> src = new ArrayList<String>();
src.add("msgpack");
src.add("kumofs");
src.add("viver");
List<String> src = new ArrayList<String>();
src.add("msgpack");
src.add("kumofs");
src.add("viver");
String arg = JSONValue.toJSONString(src);
Object object = LuaUtils.eval("msgpack", 0, arg);
System.out.println(object);
}
String arg = JSONValue.toJSONString(src);
Object object = LuaUtils.eval("msgpack", 0, arg);
System.out.println(object);
}
}

View File

@@ -7,38 +7,38 @@ import util.StringUtils;
public class Test3 {
public static void main(String[] args) {
System.out.println(StringUtils.parameterize("aDa halod823d!!@"));
public static void main(String[] args) {
System.out.println(StringUtils.parameterize("aDa halod823d!!@"));
JedisPool pool = new JedisPool("localhost", 16379);
Jedis jedis1 = pool.getResource();
jedis1.select(2);
jedis1.del("bla");
jedis1.del("da");
JedisPool pool = new JedisPool("localhost", 16379);
Jedis jedis1 = pool.getResource();
jedis1.select(2);
jedis1.del("bla");
jedis1.del("da");
Jedis jedis2 = pool.getResource();
Pipeline pipeline = jedis2.pipelined();
pipeline.select(2);
for (int i = 0; i < 10000; ++i) {
pipeline.sadd("bla", String.valueOf(i));
if (i % 10 == 0) {
jedis1.sadd("da", String.valueOf(i));
}
if (i % 100 == 0) {
System.out.println(jedis1.scard("bla"));
System.out.println(jedis1.scard("da"));
}
}
Jedis jedis2 = pool.getResource();
Pipeline pipeline = jedis2.pipelined();
pipeline.select(2);
for (int i = 0; i < 10000; ++i) {
pipeline.sadd("bla", String.valueOf(i));
if (i % 10 == 0) {
jedis1.sadd("da", String.valueOf(i));
}
if (i % 100 == 0) {
System.out.println(jedis1.scard("bla"));
System.out.println(jedis1.scard("da"));
}
}
pipeline.sync();
pipeline.sync();
System.out.println(jedis1.scard("bla"));
System.out.println(jedis1.scard("da"));
pool.returnResource(jedis1);
pool.returnResource(jedis2);
}
System.out.println(jedis1.scard("bla"));
System.out.println(jedis1.scard("da"));
pool.returnResource(jedis1);
pool.returnResource(jedis2);
}
}

View File

@@ -8,22 +8,22 @@ import redis.clients.jedis.ShardedJedis;
public class Test4 {
public static void main(String[] args) {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo("localhost", 16379);
//si.setPassword("foobared");
shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards);
jedis.set("fooder", "bar");
for (int i = 0; i < 100; ++i ) {
jedis.zadd(String.format("item"), i, String.valueOf(i));
}
public static void main(String[] args) {
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
JedisShardInfo si = new JedisShardInfo("localhost", 16379);
//si.setPassword("foobared");
shards.add(si);
ShardedJedis jedis = new ShardedJedis(shards);
jedis.set("fooder", "bar");
for (int i = 0; i < 100; ++i ) {
jedis.zadd(String.format("item"), i, String.valueOf(i));
}
//jedis.zrange(key, start, end)
String value = jedis.get("fooder");
System.out.println(value);
//jedis.close();
}
//jedis.zrange(key, start, end)
String value = jedis.get("fooder");
System.out.println(value);
//jedis.close();
}
}

View File

@@ -16,31 +16,31 @@ import util.LuaUtils;
public class Test5 {
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
JedisPool pool = new JedisPool("localhost", 16379);
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
JedisPool pool = new JedisPool("localhost", 16379);
Jedis jedis = new Jedis("localhost", 16379);
jedis.select(0);
String lua1 = FileUtils.resourceToString("test1.lua");
String lua2 = FileUtils.resourceToString("test2.lua");
Jedis jedis = new Jedis("localhost", 16379);
jedis.select(0);
String lua1 = FileUtils.resourceToString("test1.lua");
String lua2 = FileUtils.resourceToString("test2.lua");
String lua1hash = DigestUtils.sha1Hex(lua1);
String lua2hash = DigestUtils.sha1Hex(lua2);
System.out.println(jedis.eval(lua1, 0));
System.out.println(jedis.scriptLoad(lua1));
System.out.println(jedis.scriptLoad(lua2));
System.out.println(jedis.evalsha(lua1hash, 0));
System.out.println(jedis.evalsha(lua2hash, 0));
String lua1hash = DigestUtils.sha1Hex(lua1);
String lua2hash = DigestUtils.sha1Hex(lua2);
System.out.println(jedis.eval(lua1, 0));
System.out.println(jedis.scriptLoad(lua1));
System.out.println(jedis.scriptLoad(lua2));
System.out.println(jedis.evalsha(lua1hash, 0));
System.out.println(jedis.evalsha(lua2hash, 0));
jedis.select(1);
Model model = new Model(DateUtils.getCalendar());
System.out.println(model.selectDataBetween(new Date(1372629840000L), new Date(1375076100000L)));
LuaUtils.setPool(pool);
System.out.println(LuaUtils.eval("test3"));
//jedis.close();
}
jedis.select(1);
Model model = new Model(DateUtils.getCalendar());
System.out.println(model.selectDataBetween(new Date(1372629840000L), new Date(1375076100000L)));
LuaUtils.setPool(pool);
System.out.println(LuaUtils.eval("test3"));
//jedis.close();
}
}

View File

@@ -6,22 +6,22 @@ import redis.clients.jedis.JedisPool;
import util.LuaUtils;
public class Test6 {
public static void main(String[] args) throws IOException {
JedisPool pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
Object o1 = LuaUtils.eval("returnnil");
System.out.println("1 returnnil: " + o1);
Object o2 = LuaUtils.eval("returnint");
System.out.println("2 returnint: " + o2);
Object o3 = LuaUtils.eval("returnstring");
System.out.println("3 returnstring: " + o3);
public static void main(String[] args) throws IOException {
JedisPool pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
Object o1 = LuaUtils.eval("returnnil");
System.out.println("1 returnnil: " + o1);
Object o2 = LuaUtils.eval("returnint");
System.out.println("2 returnint: " + o2);
Object o3 = LuaUtils.eval("returnstring");
System.out.println("3 returnstring: " + o3);
Object o4 = LuaUtils.eval("returnset");
System.out.println("4 returnset: " + o4);
Object o4 = LuaUtils.eval("returnset");
System.out.println("4 returnset: " + o4);
// solves: https://github.com/xetorthio/jedis/issues/383
}
// solves: https://github.com/xetorthio/jedis/issues/383
}
}

View File

@@ -6,12 +6,12 @@ import redis.clients.jedis.JedisPool;
import util.LuaUtils;
public class Test7 {
public static void main(String[] args) throws IOException {
JedisPool pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
public static void main(String[] args) throws IOException {
JedisPool pool = new JedisPool("localhost", 16379);
LuaUtils.setPool(pool);
// http://blog.jupo.org/2013/06/12/bitwise-lua-operations-in-redis/
Object o1 = LuaUtils.eval("anon");
System.out.println(o1);
}
// http://blog.jupo.org/2013/06/12/bitwise-lua-operations-in-redis/
Object o1 = LuaUtils.eval("anon");
System.out.println(o1);
}
}