| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package top.zhixinghe1.money.agg.cache;
- import com.github.benmanes.caffeine.cache.CacheLoader;
- import org.checkerframework.checker.nullness.qual.Nullable;
- import org.roaringbitmap.RoaringBitmap;
- import top.zhixinghe1.money.agg.Constant;
- import top.zhixinghe1.money.agg.entity.BufferedRandomAccessFile;
- import java.io.File;
- import java.io.IOException;
- import java.io.RandomAccessFile;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.FileChannel;
- import java.util.Map;
- public class ReverseIndexLoader implements CacheLoader<String, RoaringBitmap> {
- /**
- * 数据目录路径
- */
- private String dataPath;
- /**
- * 行索引
- */
- private Map<String, long[]> lineIndex;
- /**
- * 文件读写器
- */
- // private BufferedRandomAccessFile randomAccessFile;
- private RandomAccessFile randomAccessFile;
- private MappedByteBuffer mappedByteBuffer;
- public ReverseIndexLoader(String dataPath, Map<String, long[]> lineIndex) throws IOException {
- this.dataPath = dataPath;
- this.lineIndex = lineIndex;
- String filePath = String.join(File.separator, dataPath, Constant.WORD_REVERSE_INDEX_FILE);
- // randomAccessFile = new BufferedRandomAccessFile(filePath, "r", 10*1024*1024);
- randomAccessFile = new RandomAccessFile(filePath, "r");
- MappedByteBuffer mappedByteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, Integer.MAX_VALUE);
- }
- @Override
- public @Nullable RoaringBitmap load(String s) throws Exception {
- long[] basicInfo = lineIndex.get(s);
- // randomAccessFile.seek();
- // String lineContent = new String(randomAccessFile.readLine().getBytes("8859_1"), "UTF-8");
- // String lineContent = randomAccessFile.readUTF();
- // mappedByteBuffer.position((int) basicInfo[0]);
- byte[] buff = new byte[(int) basicInfo[2]];
- mappedByteBuffer.put(buff, (int)basicInfo[0], (int)basicInfo[2]);
- String lineContent = new String(buff);
- String[] split = lineContent.substring(lineContent.indexOf(",") + 1).split(",");
- int[] positionIntegers = new int[split.length];
- for (int i = 0; i < split.length; i++) {
- try {
- positionIntegers[i] = Integer.parseInt(split[i]);
- } catch (Exception e) {
- System.out.println("暂停");
- }
- }
- return RoaringBitmap.bitmapOf(positionIntegers);
- }
- }
|