1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
| @Slf4j public abstract class AbstractLineShapeMmapContainer implements ILineShapeMmapContainer {
protected ConcurrentHashMap<String, Optional<LineShapeMQEntity>> lastLineShapeMap = new ConcurrentHashMap<>(); protected ConcurrentHashMap<String, Tuple2<Integer, BitSet>> lineShapeMap = new ConcurrentHashMap<>();
protected Integer pointNumber;
public static final Integer OPENING_POINT = 570;
protected MmapContainer mmapContainer;
protected String calcDuration;
protected Long totalPage;
protected String fileName;
protected String market;
@Resource protected MongoTemplate mongoTemplate;
@Resource protected ValueConfig valueConfig;
public MmapContainer getMmapContainer() { return mmapContainer; }
@PostConstruct @SneakyThrows public void init() { if (LineShapeMQKeyConstants.dailyCalcDuration.equals(calcDuration)) { pointNumber = 1; } else if (LineShapeMQKeyConstants.min120CalcDuration.equals(calcDuration)) { pointNumber = 4; } else if (LineShapeMQKeyConstants.min60CalcDuration.equals(calcDuration)) { pointNumber = 7; } else if (LineShapeMQKeyConstants.min15CalcDuration.equals(calcDuration)) { pointNumber = 28; } else if (LineShapeMQKeyConstants.min5CalcDuration.equals(calcDuration)) { pointNumber = 78; }
mmapContainer = new MmapContainer(fileName, totalPage * pointNumber);
if (FileUtil.exist(new File(fileName))) { mmapContainer.init(); reload(); } else { mmapContainer.init(); } }
public void reload() { log.info("LineShapeMmapContainer reload start:{} ", System.currentTimeMillis()); for (int i = 0; i < totalPage * pointNumber; i++) { byte[] read = mmapContainer.read(i); if (read.length == 0) { continue; } LineShapeMQEntity lineShapeMQDomain = JsonUtils.toBean(new String(read), LineShapeMQEntity.class); String stockCode = lineShapeMQDomain.getStockKline().getCode(); lineShapeMap.computeIfAbsent(stockCode, s -> Tuple.tuple(lineShapeMap.size(), new BitSet(Math.toIntExact(pointNumber)))); Integer point = getPoint(lineShapeMQDomain.getStockKline().getTime()); if (point < 0) { continue; } Tuple2<Integer, BitSet> tuple = lineShapeMap.get(stockCode); tuple.getV2().set(point); } log.info("LineShapeMmapContainer reload end:{} ", System.currentTimeMillis()); }
public void clear() { lineShapeMap.clear(); lastLineShapeMap.clear(); mmapContainer.clear(); mmapContainer.init(); }
public Tuple2<Boolean, byte[]> calTraceAndWrite(LineShapeMQEntity lineShapeMQDomain) { LineShapeMQEntity lastLineShapeMQDomain; Integer point = getPoint(lineShapeMQDomain.getStockKline().getTime()); if (point < 0) { return Tuple.tuple(false, new byte[0]); } int lastPoint = point - 1; if (lastPoint >= 0) { Tuple2<Integer, BitSet> tuple = lineShapeMap.get(lineShapeMQDomain.getStockKline().getCode()); if (tuple == null) { lastLineShapeMQDomain = getYesterdayLast(lineShapeMQDomain.getStockKline().getCode(), lineShapeMQDomain.getDurationEnum()); } else { byte[] read = new byte[0]; try { read = mmapContainer.read(tuple.getV1() * pointNumber + lastPoint); } catch (Exception e) { log.error("写文件读上一条形态数据失败,code:{}, v1:{},pointNumber:{},lastPoint:{}", lineShapeMQDomain.getStockKline().getCode(), tuple.getV1(), pointNumber, lastPoint); throw e; } if (read.length > 0) { lastLineShapeMQDomain = JsonUtils.toBean(new String(read), LineShapeMQEntity.class); } else { lastLineShapeMQDomain = null; } } } else { lastLineShapeMQDomain = getYesterdayLast(lineShapeMQDomain.getStockKline().getCode(), lineShapeMQDomain.getDurationEnum()); } TraceTriggerHelper.traceTrigger(lastLineShapeMQDomain, lineShapeMQDomain); Boolean needPush = TraceTriggerHelper.needPush(lastLineShapeMQDomain, lineShapeMQDomain); return Tuple.tuple(needPush, write(lineShapeMQDomain)); }
public byte[] write(LineShapeMQEntity lineShapeMQDomain) { String stockCode = lineShapeMQDomain.getStockKline().getCode(); lineShapeMap.computeIfAbsent(stockCode, s -> Tuple.tuple(lineShapeMap.size(), new BitSet(Math.toIntExact(pointNumber)))); Integer point = getPoint(lineShapeMQDomain.getStockKline().getTime()); if (point < 0 || point >= pointNumber) { return new byte[0]; } Tuple2<Integer, BitSet> tuple = lineShapeMap.get(stockCode); tuple.getV2().set(point); byte[] jsonBytes = JsonUtils.toJsonBytes(lineShapeMQDomain);
mmapContainer.write(tuple.getV1() * pointNumber + point, jsonBytes); return jsonBytes; }
public List<LineShapeMQEntity> getByStockCode(String stockCode) { return getByStockCode(stockCode, true); }
public List<LineShapeMQEntity> getByStockCode(String stockCode, boolean validData) { List<LineShapeMQEntity> result = new ArrayList<>(); if (!lineShapeMap.containsKey(stockCode)) { return new ArrayList<>(); }
Tuple2<Integer, BitSet> tuple = lineShapeMap.get(stockCode); if (validData) { for (int i = tuple.getV2().nextSetBit(0); i >= 0; i = tuple.getV2().nextSetBit(i + 1)) { byte[] read = mmapContainer.read(tuple.getV1() * pointNumber + i); if (read.length > 0) { LineShapeMQEntity lineShapeMQDomain = JsonUtils.toBean(new String(read), LineShapeMQEntity.class); result.add(lineShapeMQDomain); if (!lineShapeMQDomain.getStockKline().getCode().equals(stockCode)) { log.error("Mmap 查询数据越界 {}", JsonUtils.toString(lineShapeMQDomain)); } } } } else { int n = 0; while (n < pointNumber) { byte[] read = mmapContainer.read(tuple.getV1() * pointNumber + n); if (read.length > 0) { LineShapeMQEntity lineShapeMQDomain = JsonUtils.toBean(new String(read), LineShapeMQEntity.class); result.add(lineShapeMQDomain); } else { result.add(null); } n++; } } return result; }
@Override public LineShapeMQEntity getByStockCode(String stockCode, Long time) { if (Boolean.FALSE.equals(hasThisPoint(stockCode, time))) { return null; } Integer point = getPoint(time); Tuple2<Integer, BitSet> tuple = lineShapeMap.get(stockCode); byte[] read = mmapContainer.read(tuple.getV1() * pointNumber + point); LineShapeMQEntity result = null; if (read.length > 0) { result = JsonUtils.toBean(new String(read), LineShapeMQEntity.class); } return result; }
public Boolean hasThisPoint(String stockCode, Long time) { int point = getPoint(time); if (point < 0) { return false; } Tuple2<Integer, BitSet> tuple = lineShapeMap.get(stockCode); if (tuple == null) { return false; } else { return tuple.getV2().get(point); } }
public Integer getPoint(Long time) { ZoneId timeZoneId; if (market.equals("hk")) { timeZoneId = ZoneOffset.ofHours(8); } else { timeZoneId = ZoneId.of("America/New_York"); } LocalDateTime localDateTime = LocalDateTimeUtil.of(time, timeZoneId); if (localDateTime.getSecond() == 0) { localDateTime = localDateTime.minusSeconds(1); } int minuteFromOpening;
minuteFromOpening = localDateTime.getHour() * 60 + localDateTime.getMinute() - OPENING_POINT; int point; if (LineShapeMQKeyConstants.dailyCalcDuration.equals(calcDuration)) { point = 0; return point; } else if (LineShapeMQKeyConstants.min120CalcDuration.equals(calcDuration)) { point = (int) Math.floor((double) minuteFromOpening / 120); } else if (LineShapeMQKeyConstants.min60CalcDuration.equals(calcDuration)) { point = (int) Math.floor((double) minuteFromOpening / 60); } else if (LineShapeMQKeyConstants.min15CalcDuration.equals(calcDuration)) { point = (int) Math.floor((double) minuteFromOpening / 15); } else if (LineShapeMQKeyConstants.min5CalcDuration.equals(calcDuration)) { point = (int) Math.floor((double) minuteFromOpening / 5); } else { throw new IllegalArgumentException("calcDuration 类型错误"); } if (point < 0 || point >= pointNumber) { return -1; } return point; }
public List<LineShapeMQEntity> getAllData() { List<LineShapeMQEntity> result = new ArrayList<>(); for (String stockCode : lineShapeMap.keySet()) { Tuple2<Integer, BitSet> tuple = lineShapeMap.get(stockCode); for (int i = 0; i < pointNumber; i++) { byte[] read = mmapContainer.read(tuple.getV1() * pointNumber + i); if (read.length > 0) { LineShapeMQEntity lineShapeMQDomain = JsonUtils.toBean(new String(read), LineShapeMQEntity.class); result.add(lineShapeMQDomain); } } } return result; }
}
|