栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号