|
1 | 1 | package ru.lionzxy.fastlogblock.io; |
2 | 2 |
|
3 | | -import ru.lionzxy.fastlogblock.FastLogBlock; |
4 | | -import ru.lionzxy.fastlogblock.config.LogConfig; |
| 3 | +import ru.lionzxy.fastlogblock.io.filesplitter.IFileSplitter; |
| 4 | +import ru.lionzxy.fastlogblock.io.log.LogWritter; |
| 5 | +import ru.lionzxy.fastlogblock.io.mappers.BlockMapper; |
5 | 6 | import ru.lionzxy.fastlogblock.io.mappers.NickMapper; |
| 7 | +import ru.lionzxy.fastlogblock.models.BlockChangeEventModel; |
6 | 8 |
|
7 | 9 | import java.io.File; |
8 | 10 | import java.io.IOException; |
| 11 | +import java.util.HashMap; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.concurrent.BlockingQueue; |
| 14 | +import java.util.concurrent.LinkedBlockingQueue; |
| 15 | +import java.util.concurrent.atomic.AtomicBoolean; |
9 | 16 |
|
10 | 17 | public class WriteRunnable implements Runnable { |
| 18 | + private final BlockingQueue<BlockChangeEventModel> eventQueue = new LinkedBlockingQueue<>(); |
| 19 | + private final Map<File, LogWritter> writterMap = new HashMap<>(); |
| 20 | + private final NickMapper nickMapper; |
| 21 | + private final BlockMapper blockMapper; |
| 22 | + private final IFileSplitter fileSplitter; |
| 23 | + private final AtomicBoolean withoutWork = new AtomicBoolean(true); |
11 | 24 |
|
12 | | - public WriteRunnable() throws IOException { |
13 | | - NickMapper nickMapper = new NickMapper(new File(FastLogBlock.fastLogBlock.logFolderFile, |
14 | | - LogConfig.nickToIntFilePath)); |
15 | | - //nickMapper.putNewUser(new ASCIString("LionZXY")); |
16 | | - nickMapper.sync(); |
| 25 | + public WriteRunnable(final IFileSplitter fileSplitter, final NickMapper nickMapper, final BlockMapper blockMapper) { |
| 26 | + this.nickMapper = nickMapper; |
| 27 | + this.blockMapper = blockMapper; |
| 28 | + this.fileSplitter = fileSplitter; |
17 | 29 | } |
18 | 30 |
|
19 | 31 | @Override |
20 | 32 | public void run() { |
| 33 | + while (!Thread.currentThread().isInterrupted()) { |
| 34 | + try { |
| 35 | + do { |
| 36 | + final BlockChangeEventModel event = eventQueue.take(); |
| 37 | + withoutWork.set(false); |
| 38 | + final File file = fileSplitter.getFileByPos(event.getBlockPos()); |
| 39 | + LogWritter writter = writterMap.get(file); |
| 40 | + if (writter == null) { |
| 41 | + writter = new LogWritter(file, blockMapper, nickMapper); |
| 42 | + writterMap.put(file, writter); |
| 43 | + } |
| 44 | + writter.putEvent(event); |
| 45 | + } while (!eventQueue.isEmpty()); |
21 | 46 |
|
| 47 | + writterMap.values().forEach(it -> { |
| 48 | + try { |
| 49 | + it.sync(); |
| 50 | + } catch (IOException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + }); |
| 54 | + withoutWork.set(true); |
| 55 | + } catch (Exception e) { |
| 56 | + e.printStackTrace(); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public void putEvent(BlockChangeEventModel blockChangeEventModel) { |
| 62 | + eventQueue.add(blockChangeEventModel); |
| 63 | + } |
| 64 | + |
| 65 | + public boolean isEmpty() { |
| 66 | + return eventQueue.isEmpty() && withoutWork.get(); |
22 | 67 | } |
23 | 68 | } |
0 commit comments