Java中排序后的(内存映射?)文件中的二进制搜索

Java中排序后的(内存映射?)文件中的二进制搜索,第1张

Java中排序后的(内存映射?)文件中的二进制搜索

我是一个 很大的 Java的风扇

MappedByteBuffers

像这样的情况。它正在迅速燃烧。下面是我为您整理的一个片段,该片段将缓冲区映射到文件,查找到中间,然后向后搜索换行符。这应该足以让您继续前进吗?

我有类似的代码(寻找,读,重复,直到完成)在我自己的应用程序,基准

java.io
针对流
MappedByteBuffer
在生产环境和贴在我的博客的结果(Geekomatic文章标签“的java.nio”)与原始数据,图表和所有。

两秒钟的总结? 基于

MappedByteBuffer
的实现速度提高了约275%。 YMMV。

为了处理大于〜2GB的文件,这是一个问题,因为强制转换和

.position(int pos)
,我精心设计了由
MappedByteBuffer
s
数组支持的分页算法。您需要使用64位系统才能处理大于2-4GB的文件,因为MBB使用 *** 作系统的虚拟内存系统来发挥其魔力。

public class StusMagicLargeFileReader  {    private static final long PAGE_SIZE = Integer.MAX_VALUE;    private List<MappedByteBuffer> buffers = new ArrayList<MappedByteBuffer>();    private final byte raw[] = new byte[1];    public static void main(String[] args) throws IOException {        File file = new File("/Users/stu/test.txt");        FileChannel fc = (new FileInputStream(file)).getChannel();         StusMagicLargeFileReader buffer = new StusMagicLargeFileReader(fc);        long position = file.length() / 2;        String candidate = buffer.getString(position--);        while (position >=0 && !candidate.equals('n'))  candidate = buffer.getString(position--);        //have newline position or start of file...do other stuff        }    StusMagicLargeFileReader(FileChannel channel) throws IOException {        long start = 0, length = 0;        for (long index = 0; start + length < channel.size(); index++) { if ((channel.size() / PAGE_SIZE) == index)     length = (channel.size() - index *  PAGE_SIZE) ; else     length = PAGE_SIZE; start = index * PAGE_SIZE; buffers.add(index, channel.map(READ_ONLY, start, length));        }        }    public String getString(long bytePosition) {        int page  = (int) (bytePosition / PAGE_SIZE);        int index = (int) (bytePosition % PAGE_SIZE);        raw[0] = buffers.get(page).get(index);        return new String(raw);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5586483.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存