Replace tabs with spaces
This commit is contained in:
@@ -3,11 +3,11 @@ package data;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class Entry {
|
public class Entry {
|
||||||
public Date date;
|
public Date date;
|
||||||
public double value;
|
public double value;
|
||||||
|
|
||||||
public Entry(Date date, double value) {
|
public Entry(Date date, double value) {
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,15 +5,15 @@ import java.util.Date;
|
|||||||
|
|
||||||
public class Day extends Minute {
|
public class Day extends Minute {
|
||||||
|
|
||||||
public Day() {}
|
public Day() {}
|
||||||
|
|
||||||
public Date next() {
|
public Date next() {
|
||||||
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
calendar.add(Calendar.DAY_OF_MONTH, 1);
|
||||||
return get();
|
return get();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void adjust() {
|
protected void adjust() {
|
||||||
super.adjust();
|
super.adjust();
|
||||||
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,17 +4,17 @@ import java.util.Calendar;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class Hour extends Minute {
|
public class Hour extends Minute {
|
||||||
public static final int HOURS = 1;
|
public static final int HOURS = 1;
|
||||||
|
|
||||||
public Hour() {}
|
public Hour() {}
|
||||||
|
|
||||||
public Date next() {
|
public Date next() {
|
||||||
calendar.add(Calendar.HOUR, HOURS);
|
calendar.add(Calendar.HOUR, HOURS);
|
||||||
return get();
|
return get();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void adjust() {
|
protected void adjust() {
|
||||||
super.adjust();
|
super.adjust();
|
||||||
calendar.set(Calendar.MINUTE, 0);
|
calendar.set(Calendar.MINUTE, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package interval;
|
package interval;
|
||||||
|
|
||||||
public class Hour12 extends Hour {
|
public class Hour12 extends Hour {
|
||||||
public static final int HOURS = 12;
|
public static final int HOURS = 12;
|
||||||
|
|
||||||
public Hour12() {}
|
public Hour12() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package interval;
|
|||||||
|
|
||||||
|
|
||||||
public class Hour2 extends Hour {
|
public class Hour2 extends Hour {
|
||||||
public static final int HOURS = 2;
|
public static final int HOURS = 2;
|
||||||
|
|
||||||
public Hour2() {}
|
public Hour2() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package interval;
|
package interval;
|
||||||
|
|
||||||
public class Hour4 extends Hour {
|
public class Hour4 extends Hour {
|
||||||
public static final int HOURS = 4;
|
public static final int HOURS = 4;
|
||||||
|
|
||||||
public Hour4() {}
|
public Hour4() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package interval;
|
package interval;
|
||||||
|
|
||||||
public class Hour6 extends Hour {
|
public class Hour6 extends Hour {
|
||||||
public static final int HOURS = 6;
|
public static final int HOURS = 6;
|
||||||
|
|
||||||
public Hour6() {}
|
public Hour6() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,48 +7,48 @@ import java.util.Iterator;
|
|||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
public abstract class Interval implements Iterator<Date> {
|
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 Date endDate;
|
||||||
protected TimeZone timeZone;
|
protected TimeZone timeZone;
|
||||||
protected Calendar calendar;
|
protected Calendar calendar;
|
||||||
|
|
||||||
public Interval() {}
|
public Interval() {}
|
||||||
|
|
||||||
public Interval(Date startDate, Date endDate, String timeZoneID) {
|
public Interval(Date startDate, Date endDate, String timeZoneID) {
|
||||||
setTimeZone(timeZoneID);
|
setTimeZone(timeZoneID);
|
||||||
setDate(startDate, endDate);
|
setDate(startDate, endDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTimeZone(String timeZoneID) {
|
public void setTimeZone(String timeZoneID) {
|
||||||
timeZone = TimeZone.getTimeZone(timeZoneID);
|
timeZone = TimeZone.getTimeZone(timeZoneID);
|
||||||
calendar = new GregorianCalendar(TimeZone.getTimeZone(TIMEZONE));
|
calendar = new GregorianCalendar(TimeZone.getTimeZone(TIMEZONE));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Interval(Date startDate, Date endDate) {
|
public Interval(Date startDate, Date endDate) {
|
||||||
this(startDate, endDate, TIMEZONE);
|
this(startDate, endDate, TIMEZONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDate(Date startDate, Date endDate) {
|
public void setDate(Date startDate, Date endDate) {
|
||||||
if (timeZone == null) {
|
if (timeZone == null) {
|
||||||
setTimeZone(TIMEZONE);
|
setTimeZone(TIMEZONE);
|
||||||
}
|
}
|
||||||
calendar.setTime(startDate);
|
calendar.setTime(startDate);
|
||||||
adjust();
|
adjust();
|
||||||
this.endDate = endDate;
|
this.endDate = endDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void remove() {}
|
public void remove() {}
|
||||||
|
|
||||||
public Date get() {
|
public Date get() {
|
||||||
return calendar.getTime();
|
return calendar.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract Date next();
|
public abstract Date next();
|
||||||
|
|
||||||
public boolean hasNext() {
|
public boolean hasNext() {
|
||||||
return next().compareTo(endDate) < 0;
|
return next().compareTo(endDate) < 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void adjust();
|
protected abstract void adjust();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ import java.util.Calendar;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class Minute extends Interval {
|
public class Minute extends Interval {
|
||||||
public static final int MINUTES = 1;
|
public static final int MINUTES = 1;
|
||||||
|
|
||||||
public Date next() {
|
public Date next() {
|
||||||
calendar.add(Calendar.MINUTE, MINUTES);
|
calendar.add(Calendar.MINUTE, MINUTES);
|
||||||
return get();
|
return get();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void adjust() {
|
protected void adjust() {
|
||||||
calendar.set(Calendar.SECOND, 0);
|
calendar.set(Calendar.SECOND, 0);
|
||||||
calendar.set(Calendar.MILLISECOND, 0);
|
calendar.set(Calendar.MILLISECOND, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package interval;
|
package interval;
|
||||||
|
|
||||||
public class Minute15 extends Minute {
|
public class Minute15 extends Minute {
|
||||||
public static final int MINUTES = 15;
|
public static final int MINUTES = 15;
|
||||||
|
|
||||||
public Minute15() {}
|
public Minute15() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package interval;
|
package interval;
|
||||||
|
|
||||||
public class Minute30 extends Minute {
|
public class Minute30 extends Minute {
|
||||||
public static final int MINUTES = 30;
|
public static final int MINUTES = 30;
|
||||||
|
|
||||||
public Minute30() {}
|
public Minute30() {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ import java.util.Date;
|
|||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
|
|
||||||
public class Month extends Day {
|
public class Month extends Day {
|
||||||
public Month() {}
|
public Month() {}
|
||||||
|
|
||||||
public Date next() {
|
public Date next() {
|
||||||
calendar.add(Calendar.MONTH, 1);
|
calendar.add(Calendar.MONTH, 1);
|
||||||
return get();
|
return get();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void adjust() {
|
protected void adjust() {
|
||||||
super.adjust();
|
super.adjust();
|
||||||
calendar.set(Calendar.DAY_OF_MONTH, 1);
|
calendar.set(Calendar.DAY_OF_MONTH, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ import java.util.Calendar;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class Week extends Day {
|
public class Week extends Day {
|
||||||
public Week() {}
|
public Week() {}
|
||||||
|
|
||||||
public Date next() {
|
public Date next() {
|
||||||
calendar.add(Calendar.WEEK_OF_MONTH, 1);
|
calendar.add(Calendar.WEEK_OF_MONTH, 1);
|
||||||
return get();
|
return get();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void adjust() {
|
protected void adjust() {
|
||||||
super.adjust();
|
super.adjust();
|
||||||
calendar.set(Calendar.DAY_OF_WEEK, 2);
|
calendar.set(Calendar.DAY_OF_WEEK, 2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,284 +23,284 @@ import util.StringUtils;
|
|||||||
import data.Entry;
|
import data.Entry;
|
||||||
|
|
||||||
public class Model {
|
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 JedisPool pool;
|
||||||
protected Jedis jedis;
|
protected Jedis jedis;
|
||||||
protected Transaction transaction;
|
protected Transaction transaction;
|
||||||
|
|
||||||
protected int insertDataBatchCount, insertExtremesBatchStatementCount;
|
protected int insertDataBatchCount, insertExtremesBatchStatementCount;
|
||||||
protected PreparedStatement insertDataBatchStatement, insertExtremesBatchStatement;
|
protected PreparedStatement insertDataBatchStatement, insertExtremesBatchStatement;
|
||||||
|
|
||||||
//private Pipeline pipeline;
|
//private Pipeline pipeline;
|
||||||
|
|
||||||
public Model(Calendar calendar) {
|
public Model(Calendar calendar) {
|
||||||
this.calendar = calendar;
|
this.calendar = calendar;
|
||||||
pool = new JedisPool("localhost", 16379);
|
pool = new JedisPool("localhost", 16379);
|
||||||
jedis = pool.getResource();
|
jedis = pool.getResource();
|
||||||
jedis.select(1);
|
jedis.select(1);
|
||||||
//clear();
|
//clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clear() {
|
public void clear() {
|
||||||
Set<String> set = jedis.keys("*");
|
Set<String> set = jedis.keys("*");
|
||||||
if (set.size() > 0) {
|
if (set.size() > 0) {
|
||||||
jedis.del(set.toArray(new String[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 {
|
public Date getFirstEntryDate() throws SQLException {
|
||||||
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1));
|
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1));
|
||||||
long timestamp = 0;
|
long timestamp = 0;
|
||||||
if (list.size() > 0) {
|
if (list.size() > 0) {
|
||||||
timestamp = Long.valueOf(list.get(0));
|
timestamp = Long.valueOf(list.get(0));
|
||||||
}
|
}
|
||||||
calendar.setTimeInMillis(1000 * timestamp);
|
calendar.setTimeInMillis(1000 * timestamp);
|
||||||
return calendar.getTime();
|
return calendar.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date getLastEntryDate() throws SQLException {
|
public Date getLastEntryDate() throws SQLException {
|
||||||
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1).desc());
|
List<String> list = jedis.sort("data", new SortingParams().limit(1, 1).desc());
|
||||||
long timestamp = 0;
|
long timestamp = 0;
|
||||||
if (list.size() > 0) {
|
if (list.size() > 0) {
|
||||||
timestamp = Long.valueOf(list.get(0));
|
timestamp = Long.valueOf(list.get(0));
|
||||||
}
|
}
|
||||||
calendar.setTimeInMillis(1000 * timestamp);
|
calendar.setTimeInMillis(1000 * timestamp);
|
||||||
return calendar.getTime();
|
return calendar.getTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertInterval(String name) throws SQLException {
|
public void insertInterval(String name) throws SQLException {
|
||||||
Long id = jedis.incr("global:next-interval-id");
|
Long id = jedis.incr("global:next-interval-id");
|
||||||
String parameter = StringUtils.parameterize(name);
|
String parameter = StringUtils.parameterize(name);
|
||||||
jedis.set(String.format("interval:%d:name", id), name);
|
jedis.set(String.format("interval:%d:name", id), name);
|
||||||
jedis.set(String.format("interval:%s:id", parameter), String.valueOf(id));
|
jedis.set(String.format("interval:%s:id", parameter), String.valueOf(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
public int selectIntervalId(String name) throws Exception {
|
public int selectIntervalId(String name) throws Exception {
|
||||||
String parameter = StringUtils.parameterize(name);
|
String parameter = StringUtils.parameterize(name);
|
||||||
return Integer.valueOf(jedis.get(String.format("interval:%s:id", parameter)));
|
return Integer.valueOf(jedis.get(String.format("interval:%s:id", parameter)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public ArrayList<Entry> selectDataBetween(Date intervalStartDate, Date intervalEndDate) {
|
public ArrayList<Entry> selectDataBetween(Date intervalStartDate, Date intervalEndDate) {
|
||||||
// PreparedStatement selectStatement = connection.prepareStatement("select * from data where date between ? and ?");
|
// PreparedStatement selectStatement = connection.prepareStatement("select * from data where date between ? and ?");
|
||||||
// selectStatement.setLong(1, intervalStartDate.getTime() / 1000);
|
// selectStatement.setLong(1, intervalStartDate.getTime() / 1000);
|
||||||
// selectStatement.setLong(2, intervalEndDate.getTime() / 1000);
|
// selectStatement.setLong(2, intervalEndDate.getTime() / 1000);
|
||||||
// ResultSet resultSet = selectStatement.executeQuery();
|
// ResultSet resultSet = selectStatement.executeQuery();
|
||||||
long min = intervalStartDate.getTime() / 1000;
|
long min = intervalStartDate.getTime() / 1000;
|
||||||
long max = intervalEndDate.getTime() / 1000;
|
long max = intervalEndDate.getTime() / 1000;
|
||||||
String key = String.format("data:%s:%s", min, max);
|
String key = String.format("data:%s:%s", min, max);
|
||||||
|
|
||||||
Set<String> dateSet = jedis.zrangeByScore("data", min, max);
|
Set<String> dateSet = jedis.zrangeByScore("data", min, max);
|
||||||
|
|
||||||
String[] dateArray = dateSet.toArray(new String[0]);
|
String[] dateArray = dateSet.toArray(new String[0]);
|
||||||
ArrayList<Entry> entryList = new ArrayList<Entry>();
|
ArrayList<Entry> entryList = new ArrayList<Entry>();
|
||||||
if (dateArray.length == 0) {
|
if (dateArray.length == 0) {
|
||||||
return entryList;
|
return entryList;
|
||||||
}
|
}
|
||||||
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(dateArray));
|
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(dateArray));
|
||||||
List<String> valueList = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
|
List<String> valueList = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
|
||||||
|
|
||||||
Iterator<String> iterator = dateSet.iterator();
|
Iterator<String> iterator = dateSet.iterator();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
calendar.setTimeInMillis(1000 * Long.valueOf(iterator.next()));
|
calendar.setTimeInMillis(1000 * Long.valueOf(iterator.next()));
|
||||||
entryList.add(new Entry(calendar.getTime(), Double.valueOf(valueList.get(i++))));
|
entryList.add(new Entry(calendar.getTime(), Double.valueOf(valueList.get(i++))));
|
||||||
}
|
}
|
||||||
return entryList;
|
return entryList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertDataBatch(long date, double value) throws SQLException {
|
public void insertDataBatch(long date, double value) throws SQLException {
|
||||||
// if (pipeline == null) {
|
// if (pipeline == null) {
|
||||||
// pipeline = jedis.pipelined();
|
// pipeline = jedis.pipelined();
|
||||||
// pipeline.multi();
|
// pipeline.multi();
|
||||||
// }
|
// }
|
||||||
// if (insertDataBatchStatement == null) {
|
// if (insertDataBatchStatement == null) {
|
||||||
// insertDataBatchStatement = connection.prepareStatement("insert or ignore into data values(?, ?)");
|
// insertDataBatchStatement = connection.prepareStatement("insert or ignore into data values(?, ?)");
|
||||||
// insertDataBatchCount = 0;
|
// insertDataBatchCount = 0;
|
||||||
// }
|
// }
|
||||||
// setDate(insertDataBatchStatement, 1, date);
|
// setDate(insertDataBatchStatement, 1, date);
|
||||||
// insertDataBatchStatement.setDouble(2, value);
|
// insertDataBatchStatement.setDouble(2, value);
|
||||||
// insertDataBatchStatement.addBatch();
|
// insertDataBatchStatement.addBatch();
|
||||||
//
|
//
|
||||||
// if(++insertDataBatchCount % BATCH_SIZE == 0) {
|
// if(++insertDataBatchCount % BATCH_SIZE == 0) {
|
||||||
// insertDataBatchStatement.executeBatch();
|
// insertDataBatchStatement.executeBatch();
|
||||||
// System.out.println(insertDataBatchCount);
|
// System.out.println(insertDataBatchCount);
|
||||||
// }
|
// }
|
||||||
date /= 1000;
|
date /= 1000;
|
||||||
jedis.zadd(String.format("data"), date, String.valueOf(date));
|
jedis.zadd(String.format("data"), date, String.valueOf(date));
|
||||||
jedis.set(String.format("data:%s:value", date), String.valueOf(value));
|
jedis.set(String.format("data:%s:value", date), String.valueOf(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertDataBatchLast() throws SQLException {
|
public void insertDataBatchLast() throws SQLException {
|
||||||
//pipeline.exec();
|
//pipeline.exec();
|
||||||
//pipeline = null;
|
//pipeline = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertExtremesBatch(int id, Date date, Date firstDate, Date lastDate, int count, Double min, Double max) throws SQLException {
|
public void insertExtremesBatch(int id, Date date, Date firstDate, Date lastDate, int count, Double min, Double max) throws SQLException {
|
||||||
// if (pipeline == null) {
|
// if (pipeline == null) {
|
||||||
// pipeline = jedis.pipelined();
|
// pipeline = jedis.pipelined();
|
||||||
// pipeline.multi();
|
// pipeline.multi();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// HashMap<String, String> map = new HashMap<String, String>();
|
// HashMap<String, String> map = new HashMap<String, String>();
|
||||||
// map.put("first", String.valueOf(firstDate.getTime() / 1000));
|
// map.put("first", String.valueOf(firstDate.getTime() / 1000));
|
||||||
// map.put("last", String.valueOf(lastDate.getTime() / 1000));
|
// map.put("last", String.valueOf(lastDate.getTime() / 1000));
|
||||||
// map.put("count", String.valueOf(count));
|
// map.put("count", String.valueOf(count));
|
||||||
// map.put("min", String.valueOf(min));
|
// map.put("min", String.valueOf(min));
|
||||||
// map.put("max", String.valueOf(max));
|
// map.put("max", String.valueOf(max));
|
||||||
|
|
||||||
int dateInt = (int) date.getTime() / 1000;
|
int dateInt = (int) date.getTime() / 1000;
|
||||||
|
|
||||||
|
|
||||||
String key = String.format("extreme:%d:%d:", id, dateInt);
|
String key = String.format("extreme:%d:%d:", id, dateInt);
|
||||||
jedis.set(key + "first", String.valueOf(firstDate.getTime() / 1000));
|
jedis.set(key + "first", String.valueOf(firstDate.getTime() / 1000));
|
||||||
jedis.set(key + "last", String.valueOf(lastDate.getTime() / 1000));
|
jedis.set(key + "last", String.valueOf(lastDate.getTime() / 1000));
|
||||||
jedis.set(key + "count", String.valueOf(count));
|
jedis.set(key + "count", String.valueOf(count));
|
||||||
jedis.set(key + "min", String.valueOf(min));
|
jedis.set(key + "min", String.valueOf(min));
|
||||||
jedis.set(key + "max", String.valueOf(max));
|
jedis.set(key + "max", String.valueOf(max));
|
||||||
|
|
||||||
|
|
||||||
jedis.zadd(String.format("interval:%d", id), dateInt, String.valueOf(dateInt));
|
jedis.zadd(String.format("interval:%d", id), dateInt, String.valueOf(dateInt));
|
||||||
|
|
||||||
//jedis.hmset(String.format("extreme:%d:%d", id, date.getTime() / 1000), map);
|
//jedis.hmset(String.format("extreme:%d:%d", id, date.getTime() / 1000), map);
|
||||||
|
|
||||||
if(++insertExtremesBatchStatementCount % BATCH_SIZE == 0) {
|
if(++insertExtremesBatchStatementCount % BATCH_SIZE == 0) {
|
||||||
//pipeline.exec();
|
//pipeline.exec();
|
||||||
//pipeline.multi();
|
//pipeline.multi();
|
||||||
System.out.println(insertExtremesBatchStatementCount);
|
System.out.println(insertExtremesBatchStatementCount);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertExtremesBatchLast() throws SQLException {
|
public void insertExtremesBatchLast() throws SQLException {
|
||||||
//pipeline.exec();
|
//pipeline.exec();
|
||||||
//pipeline = null;
|
//pipeline = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void listData() throws SQLException {
|
protected void listData() throws SQLException {
|
||||||
// ResultSet resultSet = statement.executeQuery("select *,datetime(date, 'unixepoch') as fmt from data");
|
// ResultSet resultSet = statement.executeQuery("select *,datetime(date, 'unixepoch') as fmt from data");
|
||||||
// while (resultSet.next()) {
|
// while (resultSet.next()) {
|
||||||
// System.out.println("--------------------------");
|
// System.out.println("--------------------------");
|
||||||
// System.out.println("date = " + resultSet.getString("date"));
|
// System.out.println("date = " + resultSet.getString("date"));
|
||||||
// System.out.println("value = " + resultSet.getFloat("value"));
|
// System.out.println("value = " + resultSet.getFloat("value"));
|
||||||
// System.out.println("format = " + resultSet.getString("fmt"));
|
// System.out.println("format = " + resultSet.getString("fmt"));
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String getField(ResultSet resultSet, String columnLabel) throws SQLException {
|
protected String getField(ResultSet resultSet, String columnLabel) throws SQLException {
|
||||||
return resultSet.getString(columnLabel);
|
return resultSet.getString(columnLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void printField(ResultSet resultSet, String columnLabel) throws SQLException {
|
protected void printField(ResultSet resultSet, String columnLabel) throws SQLException {
|
||||||
System.out.printf("%s = %s\n", columnLabel, resultSet.getString(columnLabel));
|
System.out.printf("%s = %s\n", columnLabel, resultSet.getString(columnLabel));
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setDate(PreparedStatement statement, int parameterIndex, Date date) throws SQLException {
|
protected void setDate(PreparedStatement statement, int parameterIndex, Date date) throws SQLException {
|
||||||
setDate(statement, parameterIndex, date.getTime());
|
setDate(statement, parameterIndex, date.getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setDate(PreparedStatement statement, int parameterIndex, long date) throws SQLException {
|
protected void setDate(PreparedStatement statement, int parameterIndex, long date) throws SQLException {
|
||||||
statement.setLong(parameterIndex, date / 1000);
|
statement.setLong(parameterIndex, date / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResultSet getExtremes(long startDate, long endDate, String name) throws SQLException {
|
public ResultSet getExtremes(long startDate, long endDate, String name) throws SQLException {
|
||||||
// PreparedStatement selectStatement = connection.prepareStatement(
|
// 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 " +
|
// "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 " +
|
// "from extremes " +
|
||||||
// "left join data first on extremes.first = first.date " +
|
// "left join data first on extremes.first = first.date " +
|
||||||
// "left join data last on extremes.last = last.date " +
|
// "left join data last on extremes.last = last.date " +
|
||||||
// "where interval_id = (select id from intervals where name = ?) and extremes.date between ? and ?");
|
// "where interval_id = (select id from intervals where name = ?) and extremes.date between ? and ?");
|
||||||
// selectStatement.setString(1, name);
|
// selectStatement.setString(1, name);
|
||||||
// setDate(selectStatement, 2, startDate);
|
// setDate(selectStatement, 2, startDate);
|
||||||
// setDate(selectStatement, 3, endDate);
|
// setDate(selectStatement, 3, endDate);
|
||||||
// return selectStatement.executeQuery();
|
// return selectStatement.executeQuery();
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public String getJSON(long startDate, long endDate, String name) throws SQLException {
|
public String getJSON(long startDate, long endDate, String name) throws SQLException {
|
||||||
ResultSet resultSet = getExtremes(
|
ResultSet resultSet = getExtremes(
|
||||||
DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
|
DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
|
||||||
DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
|
DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
|
||||||
JSONArray jsonList = new JSONArray();
|
JSONArray jsonList = new JSONArray();
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
JSONArray jsonEntry = new JSONArray();
|
JSONArray jsonEntry = new JSONArray();
|
||||||
jsonEntry.add(1000 * resultSet.getLong("date"));
|
jsonEntry.add(1000 * resultSet.getLong("date"));
|
||||||
jsonEntry.add(resultSet.getDouble("first_value"));
|
jsonEntry.add(resultSet.getDouble("first_value"));
|
||||||
jsonEntry.add(resultSet.getDouble("min_value"));
|
jsonEntry.add(resultSet.getDouble("min_value"));
|
||||||
jsonEntry.add(resultSet.getDouble("max_value"));
|
jsonEntry.add(resultSet.getDouble("max_value"));
|
||||||
jsonEntry.add(resultSet.getDouble("last_value"));
|
jsonEntry.add(resultSet.getDouble("last_value"));
|
||||||
jsonList.add(jsonEntry);
|
jsonList.add(jsonEntry);
|
||||||
}
|
}
|
||||||
return jsonList.toJSONString();
|
return jsonList.toJSONString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void test(long startDate, long endDate, String name) throws SQLException {
|
public void test(long startDate, long endDate, String name) throws SQLException {
|
||||||
ResultSet resultSet = getExtremes(startDate, endDate, name);
|
ResultSet resultSet = getExtremes(startDate, endDate, name);
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
System.out.println("----------------");
|
System.out.println("----------------");
|
||||||
System.out.printf("# %d\n", resultSet.getRow());
|
System.out.printf("# %d\n", resultSet.getRow());
|
||||||
printField(resultSet, "date");
|
printField(resultSet, "date");
|
||||||
printField(resultSet, "date_first");
|
printField(resultSet, "date_first");
|
||||||
printField(resultSet, "count");
|
printField(resultSet, "count");
|
||||||
printField(resultSet, "min_value");
|
printField(resultSet, "min_value");
|
||||||
printField(resultSet, "max_value");
|
printField(resultSet, "max_value");
|
||||||
printField(resultSet, "first_value");
|
printField(resultSet, "first_value");
|
||||||
printField(resultSet, "last_value");
|
printField(resultSet, "last_value");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void test() {
|
public void test() {
|
||||||
/*Transaction transaction = jedis.multi();
|
/*Transaction transaction = jedis.multi();
|
||||||
Pipeline pipeline = jedis.pipelined();
|
Pipeline pipeline = jedis.pipelined();
|
||||||
Client client = jedis.getClient();*/
|
Client client = jedis.getClient();*/
|
||||||
|
|
||||||
// ZRANGEBYSCORE data 1375085460 1375088460
|
// ZRANGEBYSCORE data 1375085460 1375088460
|
||||||
// SORT data BY NOSORT GET data:*:value
|
// SORT data BY NOSORT GET data:*:value
|
||||||
|
|
||||||
int min = 1375085460;
|
int min = 1375085460;
|
||||||
int max = 1375088460;
|
int max = 1375088460;
|
||||||
String key = String.format("data:%s:%s", min, max);
|
String key = String.format("data:%s:%s", min, max);
|
||||||
|
|
||||||
Set<String> set = jedis.zrangeByScore("data", min, max);
|
Set<String> set = jedis.zrangeByScore("data", min, max);
|
||||||
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
|
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
|
||||||
List<String> list = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
|
List<String> list = jedis.sort(key, new SortingParams().nosort().get("data:*:value"));
|
||||||
for (String string : list) {
|
for (String string : list) {
|
||||||
System.out.println(string);
|
System.out.println(string);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, String> a = jedis.hgetAll("extreme:2");
|
Map<String, String> a = jedis.hgetAll("extreme:2");
|
||||||
System.out.println(a.get("date"));
|
System.out.println(a.get("date"));
|
||||||
//jedis.sort(date, new SortingParams().nosort().get("extreme:*"));
|
//jedis.sort(date, new SortingParams().nosort().get("extreme:*"));
|
||||||
|
|
||||||
|
|
||||||
//set = jedis.zrange("interval:1", 0, -1);
|
//set = jedis.zrange("interval:1", 0, -1);
|
||||||
|
|
||||||
min = 0;
|
min = 0;
|
||||||
max = Integer.MAX_VALUE;
|
max = Integer.MAX_VALUE;
|
||||||
key = String.format("interval:1:%s:%s", min, max);
|
key = String.format("interval:1:%s:%s", min, max);
|
||||||
|
|
||||||
set = jedis.zrangeByScore("interval:1", min, max);
|
set = jedis.zrangeByScore("interval:1", min, max);
|
||||||
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
|
jedis.sadd(SafeEncoder.encode(key), SafeEncoder.encodeMany(set.toArray(new String[0])));
|
||||||
|
|
||||||
list = jedis.sort(key, new SortingParams().nosort().get(new String[] {
|
list = jedis.sort(key, new SortingParams().nosort().get(new String[] {
|
||||||
"extreme:1:*:count",
|
"extreme:1:*:count",
|
||||||
"extreme:1:*:first"}));
|
"extreme:1:*:first"}));
|
||||||
//Iterator<String> iterator = set.iterator();
|
//Iterator<String> iterator = set.iterator();
|
||||||
for (String string : list) {
|
for (String string : list) {
|
||||||
System.out.println(string);
|
System.out.println(string);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,40 +5,40 @@ import java.util.GregorianCalendar;
|
|||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
public class DateUtils {
|
public class DateUtils {
|
||||||
public static final String TIMEZONE = "Europe/Amsterdam";
|
public static final String TIMEZONE = "Europe/Amsterdam";
|
||||||
protected static TimeZone timeZone;
|
protected static TimeZone timeZone;
|
||||||
protected static Calendar calendar;
|
protected static Calendar calendar;
|
||||||
|
|
||||||
public static void setTimeZone(String timeZone) {
|
public static void setTimeZone(String timeZone) {
|
||||||
DateUtils.timeZone = TimeZone.getTimeZone(timeZone);
|
DateUtils.timeZone = TimeZone.getTimeZone(timeZone);
|
||||||
calendar = new GregorianCalendar(DateUtils.timeZone);
|
calendar = new GregorianCalendar(DateUtils.timeZone);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static long makeTimestamp(int year, int month, int day, int hour, int minute, int second, int millisecond) {
|
public static long makeTimestamp(int year, int month, int day, int hour, int minute, int second, int millisecond) {
|
||||||
checkCalendar();
|
checkCalendar();
|
||||||
calendar.set(Calendar.YEAR, year);
|
calendar.set(Calendar.YEAR, year);
|
||||||
calendar.set(Calendar.MONTH, month - 1);
|
calendar.set(Calendar.MONTH, month - 1);
|
||||||
calendar.set(Calendar.DATE, day);
|
calendar.set(Calendar.DATE, day);
|
||||||
calendar.set(Calendar.HOUR_OF_DAY, hour);
|
calendar.set(Calendar.HOUR_OF_DAY, hour);
|
||||||
calendar.set(Calendar.MINUTE, minute);
|
calendar.set(Calendar.MINUTE, minute);
|
||||||
calendar.set(Calendar.SECOND, second);
|
calendar.set(Calendar.SECOND, second);
|
||||||
calendar.set(Calendar.MILLISECOND, millisecond);
|
calendar.set(Calendar.MILLISECOND, millisecond);
|
||||||
return calendar.getTimeInMillis();
|
return calendar.getTimeInMillis();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TimeZone getTimeZone() {
|
public static TimeZone getTimeZone() {
|
||||||
checkCalendar();
|
checkCalendar();
|
||||||
return timeZone;
|
return timeZone;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Calendar getCalendar() {
|
public static Calendar getCalendar() {
|
||||||
checkCalendar();
|
checkCalendar();
|
||||||
return calendar;
|
return calendar;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void checkCalendar() {
|
protected static void checkCalendar() {
|
||||||
if (calendar == null ) {
|
if (calendar == null ) {
|
||||||
setTimeZone(TIMEZONE);
|
setTimeZone(TIMEZONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,73 +12,73 @@ import java.net.URL;
|
|||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class FileUtils {
|
public class FileUtils {
|
||||||
public static final boolean REVERSE = false;
|
public static final boolean REVERSE = false;
|
||||||
|
|
||||||
public static String resourceToString(String name) throws IOException {
|
public static String resourceToString(String name) throws IOException {
|
||||||
File file = resourceToFile(name);
|
File file = resourceToFile(name);
|
||||||
Scanner scanner = new Scanner(file);
|
Scanner scanner = new Scanner(file);
|
||||||
String string = scanner.useDelimiter("\\A").next();
|
String string = scanner.useDelimiter("\\A").next();
|
||||||
scanner.close();
|
scanner.close();
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File resourceToFile(String name) throws IOException {
|
public static File resourceToFile(String name) throws IOException {
|
||||||
URL url = ClassLoader.getSystemClassLoader().getResource(name);
|
URL url = ClassLoader.getSystemClassLoader().getResource(name);
|
||||||
if (url != null) {
|
if (url != null) {
|
||||||
try {
|
try {
|
||||||
return new File(url.toURI());
|
return new File(url.toURI());
|
||||||
} catch (URISyntaxException e) {}
|
} catch (URISyntaxException e) {}
|
||||||
}
|
}
|
||||||
throw new IOException();
|
throw new IOException();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedReader resourceToReader(String name) throws IOException {
|
public static BufferedReader resourceToReader(String name) throws IOException {
|
||||||
return resourceToReader(name, REVERSE);
|
return resourceToReader(name, REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedReader resourceToReader(String name, boolean reverse) throws IOException {
|
public static BufferedReader resourceToReader(String name, boolean reverse) throws IOException {
|
||||||
File file = resourceToFile(name);
|
File file = resourceToFile(name);
|
||||||
return fileToReader(file, reverse);
|
return fileToReader(file, reverse);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedReader fileToBufferedReader(File file) throws FileNotFoundException {
|
public static BufferedReader fileToBufferedReader(File file) throws FileNotFoundException {
|
||||||
return fileToReader(file, REVERSE);
|
return fileToReader(file, REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static BufferedReader fileToReader(File file, boolean reverse) throws FileNotFoundException {
|
public static BufferedReader fileToReader(File file, boolean reverse) throws FileNotFoundException {
|
||||||
InputStream inputStream = fileToInputStream(file, reverse);
|
InputStream inputStream = fileToInputStream(file, reverse);
|
||||||
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
|
||||||
return new BufferedReader(inputStreamReader);
|
return new BufferedReader(inputStreamReader);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static InputStream fileToInputStream(File file) throws FileNotFoundException {
|
public static InputStream fileToInputStream(File file) throws FileNotFoundException {
|
||||||
return fileToInputStream(file, REVERSE);
|
return fileToInputStream(file, REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static InputStream fileToInputStream(File file, boolean reverse) throws FileNotFoundException {
|
public static InputStream fileToInputStream(File file, boolean reverse) throws FileNotFoundException {
|
||||||
InputStream inputStream;
|
InputStream inputStream;
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
inputStream = new ReverseLineInputStream(file);
|
inputStream = new ReverseLineInputStream(file);
|
||||||
} else {
|
} else {
|
||||||
inputStream = new FileInputStream(file);
|
inputStream = new FileInputStream(file);
|
||||||
}
|
}
|
||||||
return inputStream;
|
return inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static InputStream resourceToInputStream(String name) throws IOException {
|
public static InputStream resourceToInputStream(String name) throws IOException {
|
||||||
return resourceToInputStream(name, REVERSE);
|
return resourceToInputStream(name, REVERSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static InputStream resourceToInputStream(String name, boolean reverse) throws IOException {
|
public static InputStream resourceToInputStream(String name, boolean reverse) throws IOException {
|
||||||
if (reverse) {
|
if (reverse) {
|
||||||
File file = resourceToFile(name);
|
File file = resourceToFile(name);
|
||||||
return fileToInputStream(file, reverse);
|
return fileToInputStream(file, reverse);
|
||||||
} else {
|
} else {
|
||||||
return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
|
return ClassLoader.getSystemClassLoader().getResourceAsStream(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Scanner resourceToScanner(String string) throws IOException {
|
public static Scanner resourceToScanner(String string) throws IOException {
|
||||||
return new Scanner(resourceToReader(string));
|
return new Scanner(resourceToReader(string));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,108 +24,108 @@ import redis.clients.jedis.JedisPool;
|
|||||||
import redis.clients.jedis.exceptions.JedisDataException;
|
import redis.clients.jedis.exceptions.JedisDataException;
|
||||||
|
|
||||||
public class LuaUtils {
|
public class LuaUtils {
|
||||||
public static final boolean LOAD = true;
|
public static final boolean LOAD = true;
|
||||||
public static final List<String> EMPTY = new ArrayList<String>();
|
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 String[] LIBRARY_ARRAY = {"redis", "table", "string", "math", "debug", "cjson", "cmsgpack"};
|
||||||
public static final List<String> LIBRARY_LIST = Arrays.asList(LIBRARY_ARRAY);
|
public static final List<String> LIBRARY_LIST = Arrays.asList(LIBRARY_ARRAY);
|
||||||
|
|
||||||
protected static JedisPool jedisPool;
|
protected static JedisPool jedisPool;
|
||||||
protected static HashMap<String, String> hashMap = new HashMap<String, String>();
|
protected static HashMap<String, String> hashMap = new HashMap<String, String>();
|
||||||
|
|
||||||
public static void setPool(JedisPool pool) {
|
public static void setPool(JedisPool pool) {
|
||||||
LuaUtils.jedisPool = pool;
|
LuaUtils.jedisPool = pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object eval(String name, List<String> keys, List<String> args) throws IOException {
|
public static Object eval(String name, List<String> keys, List<String> args) throws IOException {
|
||||||
Jedis jedis = jedisPool.getResource();
|
Jedis jedis = jedisPool.getResource();
|
||||||
String hash = getHash(name, jedis);
|
String hash = getHash(name, jedis);
|
||||||
System.out.println(hash);
|
System.out.println(hash);
|
||||||
Object object = jedis.evalsha(hash, keys, args);
|
Object object = jedis.evalsha(hash, keys, args);
|
||||||
jedisPool.returnResource(jedis);
|
jedisPool.returnResource(jedis);
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object eval(String name, int keys, String... args) throws IOException {
|
public static Object eval(String name, int keys, String... args) throws IOException {
|
||||||
Jedis jedis = getResource();
|
Jedis jedis = getResource();
|
||||||
String hash = getHash(name, jedis);
|
String hash = getHash(name, jedis);
|
||||||
try {
|
try {
|
||||||
return jedis.evalsha(hash, keys, args);
|
return jedis.evalsha(hash, keys, args);
|
||||||
} catch (JedisDataException e) {
|
} catch (JedisDataException e) {
|
||||||
if (e.getMessage().startsWith("NOSCRIPT")) {
|
if (e.getMessage().startsWith("NOSCRIPT")) {
|
||||||
// Script not loaded correctly
|
// Script not loaded correctly
|
||||||
}
|
}
|
||||||
System.out.println(e);
|
System.out.println(e);
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
returnResource(jedis);
|
returnResource(jedis);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object eval(String name) throws IOException {
|
public static Object eval(String name) throws IOException {
|
||||||
return eval(name, 0);
|
return eval(name, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object eval(String string, List<String> params) throws IOException {
|
public static Object eval(String string, List<String> params) throws IOException {
|
||||||
return eval(string, EMPTY, params);
|
return eval(string, EMPTY, params);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String getHash(String name, Jedis jedis) throws IOException {
|
protected static String getHash(String name, Jedis jedis) throws IOException {
|
||||||
name = String.format("%s.lua", name);
|
name = String.format("%s.lua", name);
|
||||||
if (hashMap.containsKey(name)) {
|
if (hashMap.containsKey(name)) {
|
||||||
return hashMap.get(name);
|
return hashMap.get(name);
|
||||||
} else {
|
} else {
|
||||||
String lua = FileUtils.resourceToString(name);
|
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);
|
lua = resolveDepencencies(lua, libraryMap);
|
||||||
for (String library : libraryMap.keySet()) {
|
for (String library : libraryMap.keySet()) {
|
||||||
lua = String.format("local %s = {}\n%s", library, lua);
|
lua = String.format("local %s = {}\n%s", library, lua);
|
||||||
}
|
}
|
||||||
System.out.println("======");
|
System.out.println("======");
|
||||||
System.out.println(lua);
|
System.out.println(lua);
|
||||||
System.out.println("======");
|
System.out.println("======");
|
||||||
String hash = DigestUtils.sha1Hex(lua);
|
String hash = DigestUtils.sha1Hex(lua);
|
||||||
if (!jedis.scriptLoad(lua).equals(hash)) {
|
if (!jedis.scriptLoad(lua).equals(hash)) {
|
||||||
// Hashes don't match
|
// Hashes don't match
|
||||||
}
|
}
|
||||||
hashMap.put(name, hash);
|
hashMap.put(name, hash);
|
||||||
return hash;
|
return hash;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static String resolveDepencencies(String lua, Map<String, List<String>> libraryMap) throws IOException {
|
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(".")));
|
Pattern pattern = Pattern.compile(String.format("([a-z]+)%s([a-z]+)", Pattern.quote(".")));
|
||||||
Matcher matcher = pattern.matcher(lua);
|
Matcher matcher = pattern.matcher(lua);
|
||||||
String depencencyLua = "";
|
String depencencyLua = "";
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
String library = matcher.group(1);
|
String library = matcher.group(1);
|
||||||
if (!LIBRARY_LIST.contains(library)) {
|
if (!LIBRARY_LIST.contains(library)) {
|
||||||
if (!libraryMap.containsKey(library)) {
|
if (!libraryMap.containsKey(library)) {
|
||||||
libraryMap.put(library, new ArrayList<String>());
|
libraryMap.put(library, new ArrayList<String>());
|
||||||
}
|
}
|
||||||
List<String> methodList = libraryMap.get(library);
|
List<String> methodList = libraryMap.get(library);
|
||||||
String method = matcher.group(2);
|
String method = matcher.group(2);
|
||||||
if (!methodList.contains(method)) {
|
if (!methodList.contains(method)) {
|
||||||
String file = String.format("%s/%s.lua", library, method);
|
String file = String.format("%s/%s.lua", library, method);
|
||||||
System.out.println(file);
|
System.out.println(file);
|
||||||
String methodLua = FileUtils.resourceToString(file);
|
String methodLua = FileUtils.resourceToString(file);
|
||||||
methodList.add(method);
|
methodList.add(method);
|
||||||
String subDepencencyLua = resolveDepencencies(methodLua, libraryMap);
|
String subDepencencyLua = resolveDepencencies(methodLua, libraryMap);
|
||||||
if (depencencyLua.isEmpty()) {
|
if (depencencyLua.isEmpty()) {
|
||||||
depencencyLua = subDepencencyLua;
|
depencencyLua = subDepencencyLua;
|
||||||
} else if (!subDepencencyLua.isEmpty()) {
|
} else if (!subDepencencyLua.isEmpty()) {
|
||||||
depencencyLua = String.format("%s\n%s", depencencyLua, subDepencencyLua);
|
depencencyLua = String.format("%s\n%s", depencencyLua, subDepencencyLua);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (depencencyLua.isEmpty()) {
|
if (depencencyLua.isEmpty()) {
|
||||||
return lua;
|
return lua;
|
||||||
} else {
|
} else {
|
||||||
return String.format("%s\n%s", depencencyLua, lua);
|
return String.format("%s\n%s", depencencyLua, lua);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static void returnResource(Jedis jedis) {
|
protected static void returnResource(Jedis jedis) {
|
||||||
jedisPool.returnResource(jedis);
|
jedisPool.returnResource(jedis);
|
||||||
@@ -135,56 +135,56 @@ public class LuaUtils {
|
|||||||
return jedisPool.getResource();
|
return jedisPool.getResource();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Object insert(String table, Map<String, String> hash) throws IOException {
|
public static Object insert(String table, Map<String, String> hash) throws IOException {
|
||||||
List<String> params = new ArrayList<String>();
|
List<String> params = new ArrayList<String>();
|
||||||
for (final Entry<String, String> entry : hash.entrySet()) {
|
for (final Entry<String, String> entry : hash.entrySet()) {
|
||||||
params.add(entry.getKey());
|
params.add(entry.getKey());
|
||||||
params.add(entry.getValue());
|
params.add(entry.getValue());
|
||||||
params.add(StringUtils.parameterize(entry.getValue()));
|
params.add(StringUtils.parameterize(entry.getValue()));
|
||||||
}
|
}
|
||||||
params.add(0, table);
|
params.add(0, table);
|
||||||
return LuaUtils.eval("insert", params);
|
return LuaUtils.eval("insert", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static List<Map<String, String>> getAll(String table) throws IOException {
|
public static List<Map<String, String>> getAll(String table) throws IOException {
|
||||||
List<String> params = new ArrayList<String>();
|
List<String> params = new ArrayList<String>();
|
||||||
params.add(table);
|
params.add(table);
|
||||||
List<List<String>> listList = (List<List<String>>) LuaUtils.eval("getall", params);
|
List<List<String>> listList = (List<List<String>>) LuaUtils.eval("getall", params);
|
||||||
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
|
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
|
||||||
System.out.println(listList.size());
|
System.out.println(listList.size());
|
||||||
for (List<String> list : listList) {
|
for (List<String> list : listList) {
|
||||||
mapList.add(TypeUtils.listToMap(list));
|
mapList.add(TypeUtils.listToMap(list));
|
||||||
}
|
}
|
||||||
return mapList;
|
return mapList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static List<Map<String, String>> getSome(String... stringArray) throws IOException {
|
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<List<String>> listList = (List<List<String>>) LuaUtils.eval("getsome", Arrays.asList(stringArray));
|
||||||
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
|
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
|
||||||
for (List<String> list : listList) {
|
for (List<String> list : listList) {
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
for (int i = 1; i < stringArray.length; ++i) {
|
for (int i = 1; i < stringArray.length; ++i) {
|
||||||
map.put(stringArray[i], list.get(i - 1));
|
map.put(stringArray[i], list.get(i - 1));
|
||||||
}
|
}
|
||||||
mapList.add(map);
|
mapList.add(map);
|
||||||
}
|
}
|
||||||
return mapList;
|
return mapList;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static List<Map<String, String>> getSomeX(String... stringArray) throws IOException {
|
public static List<Map<String, String>> getSomeX(String... stringArray) throws IOException {
|
||||||
List<String> list = (List<String>) LuaUtils.eval("getsomex", Arrays.asList(stringArray));
|
List<String> list = (List<String>) LuaUtils.eval("getsomex", Arrays.asList(stringArray));
|
||||||
Iterator<String> iterator = list.iterator();
|
Iterator<String> iterator = list.iterator();
|
||||||
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
|
List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
for (int i = 1; i < stringArray.length; ++i) {
|
for (int i = 1; i < stringArray.length; ++i) {
|
||||||
map.put(stringArray[i], iterator.next());
|
map.put(stringArray[i], iterator.next());
|
||||||
}
|
}
|
||||||
mapList.add(map);
|
mapList.add(map);
|
||||||
}
|
}
|
||||||
return mapList;
|
return mapList;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,62 +7,62 @@ import java.io.InputStream;
|
|||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
|
|
||||||
public class ReverseLineInputStream extends InputStream {
|
public class ReverseLineInputStream extends InputStream {
|
||||||
RandomAccessFile randomAccessFile;
|
RandomAccessFile randomAccessFile;
|
||||||
|
|
||||||
long currentLineStart = -1;
|
long currentLineStart = -1;
|
||||||
long currentLineEnd = -1;
|
long currentLineEnd = -1;
|
||||||
long currentPos = -1;
|
long currentPos = -1;
|
||||||
long lastPosInFile = -1;
|
long lastPosInFile = -1;
|
||||||
|
|
||||||
public ReverseLineInputStream(File file) throws FileNotFoundException {
|
public ReverseLineInputStream(File file) throws FileNotFoundException {
|
||||||
randomAccessFile = new RandomAccessFile(file, "r");
|
randomAccessFile = new RandomAccessFile(file, "r");
|
||||||
currentLineStart = file.length();
|
currentLineStart = file.length();
|
||||||
currentLineEnd = file.length();
|
currentLineEnd = file.length();
|
||||||
lastPosInFile = file.length() - 1;
|
lastPosInFile = file.length() - 1;
|
||||||
currentPos = currentLineEnd;
|
currentPos = currentLineEnd;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void findPrevLine() throws IOException {
|
public void findPrevLine() throws IOException {
|
||||||
currentLineEnd = currentLineStart;
|
currentLineEnd = currentLineStart;
|
||||||
|
|
||||||
// No more lines, since at the beginning of the file
|
// No more lines, since at the beginning of the file
|
||||||
if (currentLineEnd == 0) {
|
if (currentLineEnd == 0) {
|
||||||
currentLineEnd = -1;
|
currentLineEnd = -1;
|
||||||
currentLineStart = -1;
|
currentLineStart = -1;
|
||||||
currentPos = -1;
|
currentPos = -1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
long filePointer = currentLineStart - 1;
|
long filePointer = currentLineStart - 1;
|
||||||
while (true) {
|
while (true) {
|
||||||
// At start of file so this is the first line in the file.
|
// At start of file so this is the first line in the file.
|
||||||
if (--filePointer < 0) {
|
if (--filePointer < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
randomAccessFile.seek(filePointer);
|
randomAccessFile.seek(filePointer);
|
||||||
int readByte = randomAccessFile.readByte();
|
int readByte = randomAccessFile.readByte();
|
||||||
|
|
||||||
// Ignore last LF in file. search back to find the previous LF.
|
// Ignore last LF in file. search back to find the previous LF.
|
||||||
if (readByte == 0xA && filePointer != lastPosInFile) {
|
if (readByte == 0xA && filePointer != lastPosInFile) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Start at pointer +1, after the LF found or at 0 the start of the file.
|
// Start at pointer +1, after the LF found or at 0 the start of the file.
|
||||||
currentLineStart = filePointer + 1;
|
currentLineStart = filePointer + 1;
|
||||||
currentPos = currentLineStart;
|
currentPos = currentLineStart;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int read() throws IOException {
|
public int read() throws IOException {
|
||||||
if (currentPos < currentLineEnd) {
|
if (currentPos < currentLineEnd) {
|
||||||
randomAccessFile.seek(currentPos++);
|
randomAccessFile.seek(currentPos++);
|
||||||
int readByte = randomAccessFile.readByte();
|
int readByte = randomAccessFile.readByte();
|
||||||
return readByte;
|
return readByte;
|
||||||
} else if (currentPos < 0) {
|
} else if (currentPos < 0) {
|
||||||
return -1;
|
return -1;
|
||||||
} else {
|
} else {
|
||||||
findPrevLine();
|
findPrevLine();
|
||||||
return read();
|
return read();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,15 +5,15 @@ import com.google.common.base.Joiner;
|
|||||||
import com.google.common.base.Splitter;
|
import com.google.common.base.Splitter;
|
||||||
|
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
protected static final char SEPARATOR = '-';
|
protected static final char SEPARATOR = '-';
|
||||||
|
|
||||||
public static String parameterize(String input) {
|
public static String parameterize(String input) {
|
||||||
return parameterize(input, SEPARATOR);
|
return parameterize(input, SEPARATOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String parameterize(String input, char separator) {
|
public static String parameterize(String input, char separator) {
|
||||||
Splitter splitter = Splitter.on(CharMatcher.JAVA_LETTER_OR_DIGIT.negate()).omitEmptyStrings();
|
Splitter splitter = Splitter.on(CharMatcher.JAVA_LETTER_OR_DIGIT.negate()).omitEmptyStrings();
|
||||||
Joiner joiner = Joiner.on(separator);
|
Joiner joiner = Joiner.on(separator);
|
||||||
return joiner.join(splitter.split(input)).toLowerCase();
|
return joiner.join(splitter.split(input)).toLowerCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
public class TypeUtils {
|
public class TypeUtils {
|
||||||
public static <T> Map<T,T> listToMap(List<T> list) {
|
public static <T> Map<T,T> listToMap(List<T> list) {
|
||||||
Map<T, T> map = new HashMap<T, T>();
|
Map<T, T> map = new HashMap<T, T>();
|
||||||
Iterator<T> iterator = list.iterator();
|
Iterator<T> iterator = list.iterator();
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
map.put(iterator.next(), iterator.next());
|
map.put(iterator.next(), iterator.next());
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,85 +15,85 @@ import util.FileUtils;
|
|||||||
import util.LuaUtils;
|
import util.LuaUtils;
|
||||||
|
|
||||||
public class Flight {
|
public class Flight {
|
||||||
private SimpleDateFormat inputSDF;
|
private SimpleDateFormat inputSDF;
|
||||||
private JedisPool pool;
|
private JedisPool pool;
|
||||||
|
|
||||||
public Flight() {
|
public Flight() {
|
||||||
inputSDF = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
|
inputSDF = new SimpleDateFormat("MM/dd/yyyy hh:mm a");
|
||||||
pool = new JedisPool("localhost", 16379);
|
pool = new JedisPool("localhost", 16379);
|
||||||
LuaUtils.setPool(pool);
|
LuaUtils.setPool(pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
new Flight().start();
|
new Flight().start();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void start() throws IOException, ParseException {
|
private void start() throws IOException, ParseException {
|
||||||
clear();
|
clear();
|
||||||
insert();
|
insert();
|
||||||
list();
|
list();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clear() {
|
private void clear() {
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
Set<String> set = jedis.keys("*");
|
Set<String> set = jedis.keys("*");
|
||||||
if (set.size() > 0) {
|
if (set.size() > 0) {
|
||||||
jedis.del(set.toArray(new String[0]));
|
jedis.del(set.toArray(new String[0]));
|
||||||
}
|
}
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insert() throws IOException {
|
private void insert() throws IOException {
|
||||||
// http://robots.thoughtbot.com/post/46335890055/redis-set-intersection-using-sets-to-filter-data
|
// http://robots.thoughtbot.com/post/46335890055/redis-set-intersection-using-sets-to-filter-data
|
||||||
Scanner lineScanner = FileUtils.resourceToScanner("flight.txt");
|
Scanner lineScanner = FileUtils.resourceToScanner("flight.txt");
|
||||||
lineScanner.nextLine();
|
lineScanner.nextLine();
|
||||||
while (lineScanner.hasNextLine()) {
|
while (lineScanner.hasNextLine()) {
|
||||||
Scanner scanner = new Scanner(lineScanner.nextLine());
|
Scanner scanner = new Scanner(lineScanner.nextLine());
|
||||||
HashMap<String,String> map = new HashMap<String,String>();
|
HashMap<String,String> map = new HashMap<String,String>();
|
||||||
scanner.useDelimiter("\t");
|
scanner.useDelimiter("\t");
|
||||||
scanner.nextInt(); // id
|
scanner.nextInt(); // id
|
||||||
String date = scanner.next();
|
String date = scanner.next();
|
||||||
long time;
|
long time;
|
||||||
try {
|
try {
|
||||||
time = inputSDF.parse(date).getTime() / 1000;
|
time = inputSDF.parse(date).getTime() / 1000;
|
||||||
map.put("departure_time", String.valueOf(time));
|
map.put("departure_time", String.valueOf(time));
|
||||||
map.put("airline", scanner.next());
|
map.put("airline", scanner.next());
|
||||||
map.put("departure", scanner.next());
|
map.put("departure", scanner.next());
|
||||||
map.put("arrival", scanner.next());
|
map.put("arrival", scanner.next());
|
||||||
long id = (Long) LuaUtils.insert("departures", map);
|
long id = (Long) LuaUtils.insert("departures", map);
|
||||||
System.out.println(id);
|
System.out.println(id);
|
||||||
|
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.zadd("departures:departure_time", time, String.valueOf(id));
|
jedis.zadd("departures:departure_time", time, String.valueOf(id));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
scanner.close();
|
scanner.close();
|
||||||
}
|
}
|
||||||
lineScanner.close();
|
lineScanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void list() throws IOException {
|
private void list() throws IOException {
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
System.out.println(jedis.hgetAll("departures:1"));
|
System.out.println(jedis.hgetAll("departures:1"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
|
|
||||||
List<Map<String, String>> listMap;
|
List<Map<String, String>> listMap;
|
||||||
|
|
||||||
listMap = LuaUtils.getSome("departures", "airline", "arrival");
|
listMap = LuaUtils.getSome("departures", "airline", "arrival");
|
||||||
for (Map<String, String> map : listMap) {
|
for (Map<String, String> map : listMap) {
|
||||||
System.out.println(map);
|
System.out.println(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
listMap = LuaUtils.getSomeX("departures", "airline", "arrival");
|
listMap = LuaUtils.getSomeX("departures", "airline", "arrival");
|
||||||
for (Map<String, String> map : listMap) {
|
for (Map<String, String> map : listMap) {
|
||||||
System.out.println(map);
|
System.out.println(map);
|
||||||
}
|
}
|
||||||
|
|
||||||
listMap = LuaUtils.getAll("departures");
|
listMap = LuaUtils.getAll("departures");
|
||||||
for (Map<String, String> map : listMap) {
|
for (Map<String, String> map : listMap) {
|
||||||
System.out.println(map);
|
System.out.println(map);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,121 +24,121 @@ import util.DateUtils;
|
|||||||
import util.FileUtils;
|
import util.FileUtils;
|
||||||
|
|
||||||
public class Relational {
|
public class Relational {
|
||||||
protected Model model;
|
protected Model model;
|
||||||
protected SimpleDateFormat inputSDF;
|
protected SimpleDateFormat inputSDF;
|
||||||
protected SimpleDateFormat outputSDF;
|
protected SimpleDateFormat outputSDF;
|
||||||
|
|
||||||
public Relational() throws SQLException {
|
public Relational() throws SQLException {
|
||||||
model = new Model(DateUtils.getCalendar());
|
model = new Model(DateUtils.getCalendar());
|
||||||
inputSDF = new SimpleDateFormat("yyyyMMdd HH:mm");
|
inputSDF = new SimpleDateFormat("yyyyMMdd HH:mm");
|
||||||
inputSDF.setTimeZone(DateUtils.getTimeZone());
|
inputSDF.setTimeZone(DateUtils.getTimeZone());
|
||||||
outputSDF = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
outputSDF = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
|
||||||
inputSDF.setTimeZone(DateUtils.getTimeZone());
|
inputSDF.setTimeZone(DateUtils.getTimeZone());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) throws FileNotFoundException {
|
public static void main(String[] args) throws FileNotFoundException {
|
||||||
try {
|
try {
|
||||||
new Relational().start();
|
new Relational().start();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void start() throws Exception {
|
public void start() throws Exception {
|
||||||
insertFromFile();
|
insertFromFile();
|
||||||
generateIntervals();
|
generateIntervals();
|
||||||
// model.test(
|
// model.test(
|
||||||
// DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
|
// DateUtils.makeTimestamp(2012, 1, 1, 14, 0, 0, 0),
|
||||||
// DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
|
// DateUtils.makeTimestamp(2013, 7, 1, 14, 0, 0, 0), "Day");
|
||||||
model.test();
|
model.test();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void insertFromFile() throws SQLException, IOException {
|
public void insertFromFile() throws SQLException, IOException {
|
||||||
Date endDate;
|
Date endDate;
|
||||||
boolean checkDate = ((endDate = model.getLastEntryDate()) != null);
|
boolean checkDate = ((endDate = model.getLastEntryDate()) != null);
|
||||||
System.out.println(endDate);
|
System.out.println(endDate);
|
||||||
JSONParser parser = new JSONParser();
|
JSONParser parser = new JSONParser();
|
||||||
BufferedReader bufferedReader = FileUtils.resourceToReader("pvoutput.debug", true);
|
BufferedReader bufferedReader = FileUtils.resourceToReader("pvoutput.debug", true);
|
||||||
String line;
|
String line;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while ((line = bufferedReader.readLine()) != null) {
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
Object object = null;
|
Object object = null;
|
||||||
try {
|
try {
|
||||||
JSONArray json = (JSONArray) parser.parse(line);
|
JSONArray json = (JSONArray) parser.parse(line);
|
||||||
object = parser.parse(json.get(3).toString());
|
object = parser.parse(json.get(3).toString());
|
||||||
} catch (ParseException e) {
|
} catch (ParseException e) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
HashMap<String, Object> map = (HashMap<String, Object>) object;
|
HashMap<String, Object> map = (HashMap<String, Object>) object;
|
||||||
String value = String.valueOf(map.get("v5"));
|
String value = String.valueOf(map.get("v5"));
|
||||||
if (!value.equals("null")) {
|
if (!value.equals("null")) {
|
||||||
try {
|
try {
|
||||||
Date date = inputSDF.parse(String.format("%s %s", map.get("d"), map.get("t")));
|
Date date = inputSDF.parse(String.format("%s %s", map.get("d"), map.get("t")));
|
||||||
if (checkDate && date.compareTo(endDate) < 0) {
|
if (checkDate && date.compareTo(endDate) < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (++i > 100) break;
|
if (++i > 100) break;
|
||||||
model.insertDataBatch(date.getTime(), Double.parseDouble(value));
|
model.insertDataBatch(date.getTime(), Double.parseDouble(value));
|
||||||
} catch (java.text.ParseException e) {
|
} catch (java.text.ParseException e) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
bufferedReader.close();
|
bufferedReader.close();
|
||||||
model.insertDataBatchLast();
|
model.insertDataBatchLast();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void generateIntervals() throws Exception {
|
public void generateIntervals() throws Exception {
|
||||||
Date startDate = model.getFirstEntryDate();
|
Date startDate = model.getFirstEntryDate();
|
||||||
Date endDate = model.getLastEntryDate();
|
Date endDate = model.getLastEntryDate();
|
||||||
|
|
||||||
for (Object object : ServiceLoader.load(interval.Interval.class)) {
|
for (Object object : ServiceLoader.load(interval.Interval.class)) {
|
||||||
Interval interval = (Interval) object;
|
Interval interval = (Interval) object;
|
||||||
interval.setDate(startDate, endDate);
|
interval.setDate(startDate, endDate);
|
||||||
processInterval(interval);
|
processInterval(interval);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void processInterval(Interval interval) throws Exception {
|
protected void processInterval(Interval interval) throws Exception {
|
||||||
String name = interval.getClass().getSimpleName();
|
String name = interval.getClass().getSimpleName();
|
||||||
model.insertInterval(name);
|
model.insertInterval(name);
|
||||||
int id = model.selectIntervalId(name);
|
int id = model.selectIntervalId(name);
|
||||||
|
|
||||||
System.out.printf("[%d] %s\n", id, name);
|
System.out.printf("[%d] %s\n", id, name);
|
||||||
System.out.println("========================================");
|
System.out.println("========================================");
|
||||||
|
|
||||||
do {
|
do {
|
||||||
Date intervalStartDate = interval.get();
|
Date intervalStartDate = interval.get();
|
||||||
Date intervalEndDate = interval.next();
|
Date intervalEndDate = interval.next();
|
||||||
|
|
||||||
ArrayList<Entry> entryList = model.selectDataBetween(intervalStartDate, intervalEndDate);
|
ArrayList<Entry> entryList = model.selectDataBetween(intervalStartDate, intervalEndDate);
|
||||||
|
|
||||||
int count = entryList.size();
|
int count = entryList.size();
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
System.out.printf("%s - %s (%d)\n", formatDate(intervalStartDate), formatDate(intervalEndDate), count);
|
System.out.printf("%s - %s (%d)\n", formatDate(intervalStartDate), formatDate(intervalEndDate), count);
|
||||||
ArrayList<Double> valueList = new ArrayList<Double>();
|
ArrayList<Double> valueList = new ArrayList<Double>();
|
||||||
for (Entry entry : entryList) {
|
for (Entry entry : entryList) {
|
||||||
System.out.printf("%s\t%f\n", formatDate(entry.date), entry.value);
|
System.out.printf("%s\t%f\n", formatDate(entry.date), entry.value);
|
||||||
valueList.add(entry.value);
|
valueList.add(entry.value);
|
||||||
}
|
}
|
||||||
model.insertExtremesBatch(
|
model.insertExtremesBatch(
|
||||||
id,
|
id,
|
||||||
intervalStartDate,
|
intervalStartDate,
|
||||||
entryList.get(0).date,
|
entryList.get(0).date,
|
||||||
entryList.get(count - 1).date,
|
entryList.get(count - 1).date,
|
||||||
count,
|
count,
|
||||||
Collections.min(valueList),
|
Collections.min(valueList),
|
||||||
Collections.max(valueList));
|
Collections.max(valueList));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (interval.hasNext());
|
while (interval.hasNext());
|
||||||
model.insertExtremesBatchLast();
|
model.insertExtremesBatchLast();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String formatDate(Date date) {
|
protected String formatDate(Date date) {
|
||||||
return outputSDF.format(date.getTime());
|
return outputSDF.format(date.getTime());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,31 +6,31 @@ import redis.clients.jedis.Jedis;
|
|||||||
|
|
||||||
public class Test1 {
|
public class Test1 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Jedis jedis = new Jedis("localhost", 16379);
|
Jedis jedis = new Jedis("localhost", 16379);
|
||||||
//jedis.select(0);
|
//jedis.select(0);
|
||||||
|
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
|
|
||||||
String value = jedis.get("foo");
|
String value = jedis.get("foo");
|
||||||
System.out.println(value);
|
System.out.println(value);
|
||||||
|
|
||||||
jedis.del("item");
|
jedis.del("item");
|
||||||
for (int i = 0; i < 10; ++i ) {
|
for (int i = 0; i < 10; ++i ) {
|
||||||
jedis.zadd(String.format("item"), 2*i, String.valueOf(Math.random()));
|
jedis.zadd(String.format("item"), 2*i, String.valueOf(Math.random()));
|
||||||
//jedis.sadd(String.format("items"), String.valueOf(i));
|
//jedis.sadd(String.format("items"), String.valueOf(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
Set<String> set = jedis.zrange("item", 0, -1);
|
Set<String> set = jedis.zrange("item", 0, -1);
|
||||||
for (String item : set) {
|
for (String item : set) {
|
||||||
System.out.println(item);
|
System.out.println(item);
|
||||||
}
|
}
|
||||||
System.out.println("-----------");
|
System.out.println("-----------");
|
||||||
set = jedis.zrangeByScore("item", 4, 7);
|
set = jedis.zrangeByScore("item", 4, 7);
|
||||||
for (String item : set) {
|
for (String item : set) {
|
||||||
System.out.println(item);
|
System.out.println(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
//jedis.close();
|
//jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,20 +10,20 @@ import redis.clients.jedis.JedisPool;
|
|||||||
import util.LuaUtils;
|
import util.LuaUtils;
|
||||||
|
|
||||||
public class Test2 {
|
public class Test2 {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
JedisPool pool = new JedisPool("localhost", 16379);
|
JedisPool pool = new JedisPool("localhost", 16379);
|
||||||
LuaUtils.setPool(pool);
|
LuaUtils.setPool(pool);
|
||||||
|
|
||||||
Object o1 = LuaUtils.getSome("departures", "airline", "arrival");
|
Object o1 = LuaUtils.getSome("departures", "airline", "arrival");
|
||||||
System.out.println(o1);
|
System.out.println(o1);
|
||||||
|
|
||||||
List<String> src = new ArrayList<String>();
|
List<String> src = new ArrayList<String>();
|
||||||
src.add("msgpack");
|
src.add("msgpack");
|
||||||
src.add("kumofs");
|
src.add("kumofs");
|
||||||
src.add("viver");
|
src.add("viver");
|
||||||
|
|
||||||
String arg = JSONValue.toJSONString(src);
|
String arg = JSONValue.toJSONString(src);
|
||||||
Object object = LuaUtils.eval("msgpack", 0, arg);
|
Object object = LuaUtils.eval("msgpack", 0, arg);
|
||||||
System.out.println(object);
|
System.out.println(object);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -7,38 +7,38 @@ import util.StringUtils;
|
|||||||
|
|
||||||
public class Test3 {
|
public class Test3 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println(StringUtils.parameterize("aDa halod823d!!@"));
|
System.out.println(StringUtils.parameterize("aDa halod823d!!@"));
|
||||||
|
|
||||||
JedisPool pool = new JedisPool("localhost", 16379);
|
JedisPool pool = new JedisPool("localhost", 16379);
|
||||||
Jedis jedis1 = pool.getResource();
|
Jedis jedis1 = pool.getResource();
|
||||||
jedis1.select(2);
|
jedis1.select(2);
|
||||||
|
|
||||||
jedis1.del("bla");
|
jedis1.del("bla");
|
||||||
jedis1.del("da");
|
jedis1.del("da");
|
||||||
|
|
||||||
Jedis jedis2 = pool.getResource();
|
Jedis jedis2 = pool.getResource();
|
||||||
|
|
||||||
Pipeline pipeline = jedis2.pipelined();
|
Pipeline pipeline = jedis2.pipelined();
|
||||||
pipeline.select(2);
|
pipeline.select(2);
|
||||||
for (int i = 0; i < 10000; ++i) {
|
for (int i = 0; i < 10000; ++i) {
|
||||||
pipeline.sadd("bla", String.valueOf(i));
|
pipeline.sadd("bla", String.valueOf(i));
|
||||||
if (i % 10 == 0) {
|
if (i % 10 == 0) {
|
||||||
jedis1.sadd("da", String.valueOf(i));
|
jedis1.sadd("da", String.valueOf(i));
|
||||||
}
|
}
|
||||||
if (i % 100 == 0) {
|
if (i % 100 == 0) {
|
||||||
System.out.println(jedis1.scard("bla"));
|
System.out.println(jedis1.scard("bla"));
|
||||||
System.out.println(jedis1.scard("da"));
|
System.out.println(jedis1.scard("da"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pipeline.sync();
|
pipeline.sync();
|
||||||
|
|
||||||
System.out.println(jedis1.scard("bla"));
|
System.out.println(jedis1.scard("bla"));
|
||||||
System.out.println(jedis1.scard("da"));
|
System.out.println(jedis1.scard("da"));
|
||||||
|
|
||||||
pool.returnResource(jedis1);
|
pool.returnResource(jedis1);
|
||||||
pool.returnResource(jedis2);
|
pool.returnResource(jedis2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,22 +8,22 @@ import redis.clients.jedis.ShardedJedis;
|
|||||||
|
|
||||||
public class Test4 {
|
public class Test4 {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
|
||||||
JedisShardInfo si = new JedisShardInfo("localhost", 16379);
|
JedisShardInfo si = new JedisShardInfo("localhost", 16379);
|
||||||
//si.setPassword("foobared");
|
//si.setPassword("foobared");
|
||||||
shards.add(si);
|
shards.add(si);
|
||||||
ShardedJedis jedis = new ShardedJedis(shards);
|
ShardedJedis jedis = new ShardedJedis(shards);
|
||||||
|
|
||||||
jedis.set("fooder", "bar");
|
jedis.set("fooder", "bar");
|
||||||
for (int i = 0; i < 100; ++i ) {
|
for (int i = 0; i < 100; ++i ) {
|
||||||
jedis.zadd(String.format("item"), i, String.valueOf(i));
|
jedis.zadd(String.format("item"), i, String.valueOf(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
//jedis.zrange(key, start, end)
|
//jedis.zrange(key, start, end)
|
||||||
String value = jedis.get("fooder");
|
String value = jedis.get("fooder");
|
||||||
System.out.println(value);
|
System.out.println(value);
|
||||||
|
|
||||||
//jedis.close();
|
//jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,31 +16,31 @@ import util.LuaUtils;
|
|||||||
|
|
||||||
public class Test5 {
|
public class Test5 {
|
||||||
|
|
||||||
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
|
public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
|
||||||
JedisPool pool = new JedisPool("localhost", 16379);
|
JedisPool pool = new JedisPool("localhost", 16379);
|
||||||
|
|
||||||
Jedis jedis = new Jedis("localhost", 16379);
|
Jedis jedis = new Jedis("localhost", 16379);
|
||||||
jedis.select(0);
|
jedis.select(0);
|
||||||
|
|
||||||
String lua1 = FileUtils.resourceToString("test1.lua");
|
String lua1 = FileUtils.resourceToString("test1.lua");
|
||||||
String lua2 = FileUtils.resourceToString("test2.lua");
|
String lua2 = FileUtils.resourceToString("test2.lua");
|
||||||
|
|
||||||
String lua1hash = DigestUtils.sha1Hex(lua1);
|
String lua1hash = DigestUtils.sha1Hex(lua1);
|
||||||
String lua2hash = DigestUtils.sha1Hex(lua2);
|
String lua2hash = DigestUtils.sha1Hex(lua2);
|
||||||
|
|
||||||
System.out.println(jedis.eval(lua1, 0));
|
System.out.println(jedis.eval(lua1, 0));
|
||||||
System.out.println(jedis.scriptLoad(lua1));
|
System.out.println(jedis.scriptLoad(lua1));
|
||||||
System.out.println(jedis.scriptLoad(lua2));
|
System.out.println(jedis.scriptLoad(lua2));
|
||||||
System.out.println(jedis.evalsha(lua1hash, 0));
|
System.out.println(jedis.evalsha(lua1hash, 0));
|
||||||
System.out.println(jedis.evalsha(lua2hash, 0));
|
System.out.println(jedis.evalsha(lua2hash, 0));
|
||||||
|
|
||||||
jedis.select(1);
|
jedis.select(1);
|
||||||
Model model = new Model(DateUtils.getCalendar());
|
Model model = new Model(DateUtils.getCalendar());
|
||||||
System.out.println(model.selectDataBetween(new Date(1372629840000L), new Date(1375076100000L)));
|
System.out.println(model.selectDataBetween(new Date(1372629840000L), new Date(1375076100000L)));
|
||||||
|
|
||||||
LuaUtils.setPool(pool);
|
LuaUtils.setPool(pool);
|
||||||
System.out.println(LuaUtils.eval("test3"));
|
System.out.println(LuaUtils.eval("test3"));
|
||||||
|
|
||||||
//jedis.close();
|
//jedis.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,22 +6,22 @@ import redis.clients.jedis.JedisPool;
|
|||||||
import util.LuaUtils;
|
import util.LuaUtils;
|
||||||
|
|
||||||
public class Test6 {
|
public class Test6 {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
JedisPool pool = new JedisPool("localhost", 16379);
|
JedisPool pool = new JedisPool("localhost", 16379);
|
||||||
LuaUtils.setPool(pool);
|
LuaUtils.setPool(pool);
|
||||||
|
|
||||||
Object o1 = LuaUtils.eval("returnnil");
|
Object o1 = LuaUtils.eval("returnnil");
|
||||||
System.out.println("1 returnnil: " + o1);
|
System.out.println("1 returnnil: " + o1);
|
||||||
|
|
||||||
Object o2 = LuaUtils.eval("returnint");
|
Object o2 = LuaUtils.eval("returnint");
|
||||||
System.out.println("2 returnint: " + o2);
|
System.out.println("2 returnint: " + o2);
|
||||||
|
|
||||||
Object o3 = LuaUtils.eval("returnstring");
|
Object o3 = LuaUtils.eval("returnstring");
|
||||||
System.out.println("3 returnstring: " + o3);
|
System.out.println("3 returnstring: " + o3);
|
||||||
|
|
||||||
Object o4 = LuaUtils.eval("returnset");
|
Object o4 = LuaUtils.eval("returnset");
|
||||||
System.out.println("4 returnset: " + o4);
|
System.out.println("4 returnset: " + o4);
|
||||||
|
|
||||||
// solves: https://github.com/xetorthio/jedis/issues/383
|
// solves: https://github.com/xetorthio/jedis/issues/383
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ import redis.clients.jedis.JedisPool;
|
|||||||
import util.LuaUtils;
|
import util.LuaUtils;
|
||||||
|
|
||||||
public class Test7 {
|
public class Test7 {
|
||||||
public static void main(String[] args) throws IOException {
|
public static void main(String[] args) throws IOException {
|
||||||
JedisPool pool = new JedisPool("localhost", 16379);
|
JedisPool pool = new JedisPool("localhost", 16379);
|
||||||
LuaUtils.setPool(pool);
|
LuaUtils.setPool(pool);
|
||||||
|
|
||||||
// http://blog.jupo.org/2013/06/12/bitwise-lua-operations-in-redis/
|
// http://blog.jupo.org/2013/06/12/bitwise-lua-operations-in-redis/
|
||||||
Object o1 = LuaUtils.eval("anon");
|
Object o1 = LuaUtils.eval("anon");
|
||||||
System.out.println(o1);
|
System.out.println(o1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user