ReverseIndexLoader.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package top.zhixinghe1.money.agg.cache;
  2. import com.github.benmanes.caffeine.cache.CacheLoader;
  3. import org.checkerframework.checker.nullness.qual.Nullable;
  4. import org.roaringbitmap.RoaringBitmap;
  5. import top.zhixinghe1.money.agg.Constant;
  6. import top.zhixinghe1.money.agg.entity.BufferedRandomAccessFile;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.io.RandomAccessFile;
  10. import java.nio.MappedByteBuffer;
  11. import java.nio.channels.FileChannel;
  12. import java.util.Map;
  13. public class ReverseIndexLoader implements CacheLoader<String, RoaringBitmap> {
  14. /**
  15. * 数据目录路径
  16. */
  17. private String dataPath;
  18. /**
  19. * 行索引
  20. */
  21. private Map<String, long[]> lineIndex;
  22. /**
  23. * 文件读写器
  24. */
  25. // private BufferedRandomAccessFile randomAccessFile;
  26. private RandomAccessFile randomAccessFile;
  27. private MappedByteBuffer mappedByteBuffer;
  28. public ReverseIndexLoader(String dataPath, Map<String, long[]> lineIndex) throws IOException {
  29. this.dataPath = dataPath;
  30. this.lineIndex = lineIndex;
  31. String filePath = String.join(File.separator, dataPath, Constant.WORD_REVERSE_INDEX_FILE);
  32. // randomAccessFile = new BufferedRandomAccessFile(filePath, "r", 10*1024*1024);
  33. randomAccessFile = new RandomAccessFile(filePath, "r");
  34. MappedByteBuffer mappedByteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, Integer.MAX_VALUE);
  35. }
  36. @Override
  37. public @Nullable RoaringBitmap load(String s) throws Exception {
  38. long[] basicInfo = lineIndex.get(s);
  39. // randomAccessFile.seek();
  40. // String lineContent = new String(randomAccessFile.readLine().getBytes("8859_1"), "UTF-8");
  41. // String lineContent = randomAccessFile.readUTF();
  42. // mappedByteBuffer.position((int) basicInfo[0]);
  43. byte[] buff = new byte[(int) basicInfo[2]];
  44. mappedByteBuffer.put(buff, (int)basicInfo[0], (int)basicInfo[2]);
  45. String lineContent = new String(buff);
  46. String[] split = lineContent.substring(lineContent.indexOf(",") + 1).split(",");
  47. int[] positionIntegers = new int[split.length];
  48. for (int i = 0; i < split.length; i++) {
  49. try {
  50. positionIntegers[i] = Integer.parseInt(split[i]);
  51. } catch (Exception e) {
  52. System.out.println("暂停");
  53. }
  54. }
  55. return RoaringBitmap.bitmapOf(positionIntegers);
  56. }
  57. }