本文主要是介绍2、FlashTsDB时序数据库-SDT算法(Java)实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
FlashTsDB时序数据库-SDT算法(Java)实现
FLashDB基于开源模式开发,地址:https://github.com/amon1991/flashdb
1、SDT原理
2、压缩算法
Point对象
public class Point implements Comparable<Point> {private long x; // 时间戳private double y; // 测点值// ... 此处省略set,get方法
}
SdtPeriod对象
public class SdtPeriod {private long bgTime; // 压缩段开始时间private double bgValue; // 压缩段开始值private long endTime; // 压缩段结束时间private double endValue;// 压缩段结束值private double gradient; // 梯度// ... 此处省略set,get方法}
压缩算法
/*** SDT algorithm compress function** @param originPoints 待压缩的点集* @param accuracyE 旋转门大小* @return*/
public List<SdtPeriod> sdtCompress(List<Point> originPoints, double accuracyE) {List<SdtPeriod> sdtPeriodList = new ArrayList<>();if (null != originPoints && originPoints.size() > 0) {SdtPoints sdtPoints = new SdtPoints();double upGate = -Double.MAX_VALUE;double downGate = Double.MAX_VALUE;for (int i = 0, size = originPoints.size(); i < size; i++) {Point currentPoint = originPoints.get(i);if (i == 0) {sdtPoints.setLastPoint(currentPoint);sdtPoints.setBeginPoint(currentPoint);} else {double nowUpGate = (currentPoint.getY() - sdtPoints.getBeginPoint().getY() - accuracyE) /(currentPoint.getX() - sdtPoints.getBeginPoint().getX());if (nowUpGate > upGate) {upGate = nowUpGate;}double nowDownGate = (currentPoint.getY() - sdtPoints.getBeginPoint().getY() + accuracyE) /(currentPoint.getX() - sdtPoints.getBeginPoint().getX());if (nowDownGate < downGate) {downGate = nowDownGate;}if (upGate > downGate) {// create new SdtPeriod(one)sdtPeriodList.add(structSdtPeriod(sdtPoints));// update gatesupGate = (currentPoint.getY() - sdtPoints.getLastPoint().getY() - accuracyE) /(currentPoint.getX() - sdtPoints.getLastPoint().getX());downGate = (currentPoint.getY() - sdtPoints.getLastPoint().getY() + accuracyE) /(currentPoint.getX() - sdtPoints.getLastPoint().getX());sdtPoints.setBeginPoint(sdtPoints.getLastPoint());}sdtPoints.setLastPoint(currentPoint);}if (i == size - 1) {// save last periodsdtPeriodList.add(structSdtPeriod(sdtPoints));}}}return sdtPeriodList;
}
3、解压算法
/*** SDT algorithm uncompress function** @param sdtPeriodList 压缩段集合* @param bgTime 返回点集开始时间* @param endTime 返回点集结束时间* @param interval 返回点集2点间隔* @return*/
public List<Point> sdtUnCompress(List<SdtPeriod> sdtPeriodList, long bgTime, long endTime, long interval) {List<Point> pointList = new ArrayList<>();if (null != sdtPeriodList && sdtPeriodList.size() > 0) {// recalculate bgTime and endTimelong globalBgTime = sdtPeriodList.get(0).getBgTime();long globalEndTime = sdtPeriodList.get(sdtPeriodList.size() - 1).getEndTime();if (bgTime > globalEndTime || endTime < globalBgTime) {return pointList;}if (bgTime < globalBgTime && endTime < globalEndTime) {while (bgTime < globalBgTime) {bgTime += interval;}}if (globalBgTime < bgTime && globalEndTime < endTime) {long tempEndTime = bgTime;while (tempEndTime < globalEndTime) {tempEndTime += interval;}endTime = tempEndTime - interval;}if (bgTime < globalBgTime && endTime > globalEndTime) {while (bgTime < globalBgTime) {bgTime += interval;}long tempEndTime = bgTime;while (tempEndTime < globalEndTime) {tempEndTime += interval;}endTime = tempEndTime - interval;}// final checkif (bgTime > globalEndTime || endTime < globalBgTime) {return pointList;}long interpolatingTime = bgTime;for (SdtPeriod sdtPeriod : sdtPeriodList) {long periodBgTime = sdtPeriod.getBgTime();double periodBgValue = sdtPeriod.getBgValue();long periodEndTime = sdtPeriod.getEndTime();double grad = sdtPeriod.getGradient();if (interpolatingTime >= periodBgTime && interpolatingTime <= periodEndTime) {while (interpolatingTime <= periodEndTime && interpolatingTime <= endTime) {Point point = new Point();point.setX(interpolatingTime);point.setY(periodBgValue + (interpolatingTime - periodBgTime) * grad);pointList.add(point);interpolatingTime += interval;}}if (interpolatingTime > endTime) {break;}}}return pointList;
}
这篇关于2、FlashTsDB时序数据库-SDT算法(Java)实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!