|
| 1 | +package ru.lionzxy.fastlogblock.handlers; |
| 2 | + |
| 3 | +import ru.lionzxy.fastlogblock.config.LogConfig; |
| 4 | +import ru.lionzxy.fastlogblock.io.WriteRunnable; |
| 5 | +import ru.lionzxy.fastlogblock.io.filesplitter.IFileSplitter; |
| 6 | +import ru.lionzxy.fastlogblock.io.filesplitter.impl.BlockHashFileSplitter; |
| 7 | +import ru.lionzxy.fastlogblock.io.filesplitter.impl.SingleFileSplitter; |
| 8 | +import ru.lionzxy.fastlogblock.io.mappers.BlockMapper; |
| 9 | +import ru.lionzxy.fastlogblock.io.mappers.NickMapper; |
| 10 | +import ru.lionzxy.fastlogblock.models.BlockChangeEventModelWithWorld; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.io.IOException; |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.concurrent.BlockingQueue; |
| 17 | +import java.util.concurrent.Executor; |
| 18 | +import java.util.concurrent.LinkedBlockingQueue; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | + |
| 21 | +public class SplitterRunnable implements Runnable { |
| 22 | + private final BlockingQueue<BlockChangeEventModelWithWorld> eventQueue = new LinkedBlockingQueue<>(); |
| 23 | + private final WriteRunnable[] writeWorkers = new WriteRunnable[LogConfig.writeWorkersCount]; |
| 24 | + private final Map<File, WriteRunnable> fileToWriteWorker = new HashMap<>(); |
| 25 | + private final AtomicInteger counter = new AtomicInteger(0); |
| 26 | + |
| 27 | + private final IFileSplitter fileSplitter; |
| 28 | + private final NickMapper nickMapper; |
| 29 | + private final BlockMapper blockMapper; |
| 30 | + |
| 31 | + public SplitterRunnable() throws IOException { |
| 32 | + final File rootFile = new File(LogConfig.logFolderPath); |
| 33 | + |
| 34 | + switch (LogConfig.fileSplitterType) { |
| 35 | + case SINGLE: |
| 36 | + fileSplitter = new SingleFileSplitter(rootFile); |
| 37 | + break; |
| 38 | + default: |
| 39 | + case BLOCKHASH: |
| 40 | + fileSplitter = new BlockHashFileSplitter(rootFile); |
| 41 | + break; |
| 42 | + } |
| 43 | + this.nickMapper = new NickMapper(new File(rootFile, LogConfig.nickToIntFilePath)); |
| 44 | + this.blockMapper = new BlockMapper(new File(rootFile, LogConfig.blockToLongFilePath)); |
| 45 | + |
| 46 | + for (int i = 0; i < LogConfig.writeWorkersCount; i++) { |
| 47 | + writeWorkers[i] = new WriteRunnable(fileSplitter, nickMapper, blockMapper); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public void runWorkers(Executor executor) { |
| 52 | + for (int i = 0; i < LogConfig.writeWorkersCount; i++) { |
| 53 | + executor.execute(writeWorkers[i]); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + while (!Thread.currentThread().isInterrupted()) { |
| 60 | + try { |
| 61 | + processEvent(eventQueue.take()); |
| 62 | + nickMapper.sync(); |
| 63 | + blockMapper.sync(); |
| 64 | + } catch (Exception e) { |
| 65 | + e.printStackTrace(); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public void addEvent(BlockChangeEventModelWithWorld blockChangeEventModelWithWorld) { |
| 71 | + eventQueue.add(blockChangeEventModelWithWorld); |
| 72 | + } |
| 73 | + |
| 74 | + private void processEvent(BlockChangeEventModelWithWorld event) { |
| 75 | + final File file = fileSplitter.getFileByPosAndWorld(event.getBlockPos(), event.getWorld()); |
| 76 | + |
| 77 | + if (event.isIgnore()) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + WriteRunnable writeRunnable = fileToWriteWorker.get(file); |
| 82 | + if (writeRunnable != null) { |
| 83 | + writeRunnable.putEvent(event); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + if (counter.get() >= LogConfig.writeWorkersCount) { |
| 88 | + counter.set(0); |
| 89 | + } |
| 90 | + |
| 91 | + writeRunnable = writeWorkers[counter.getAndIncrement()]; |
| 92 | + fileToWriteWorker.put(file, writeRunnable); |
| 93 | + writeRunnable.putEvent(event); |
| 94 | + } |
| 95 | +} |
0 commit comments