Search in sources :

Example 1 with SeekableByteArrayInputStream

use of org.apache.accumulo.core.file.rfile.RFileTest.SeekableByteArrayInputStream in project accumulo by apache.

the class MultiLevelIndexTest method runTest.

private void runTest(int maxBlockSize, int num) throws IOException {
    AccumuloConfiguration aconf = DefaultConfiguration.getInstance();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FSDataOutputStream dos = new FSDataOutputStream(baos, new FileSystem.Statistics("a"));
    CachableBlockFile.Writer _cbw = new CachableBlockFile.Writer(PositionedOutputs.wrap(dos), "gz", CachedConfiguration.getInstance(), aconf);
    BufferedWriter mliw = new BufferedWriter(new Writer(_cbw, maxBlockSize));
    for (int i = 0; i < num; i++) mliw.add(new Key(String.format("%05d000", i)), i, 0, 0, 0);
    mliw.addLast(new Key(String.format("%05d000", num)), num, 0, 0, 0);
    ABlockWriter root = _cbw.prepareMetaBlock("root");
    mliw.close(root);
    root.close();
    _cbw.close();
    dos.close();
    baos.close();
    byte[] data = baos.toByteArray();
    SeekableByteArrayInputStream bais = new SeekableByteArrayInputStream(data);
    FSDataInputStream in = new FSDataInputStream(bais);
    CachableBlockFile.Reader _cbr = new CachableBlockFile.Reader(in, data.length, CachedConfiguration.getInstance(), aconf);
    Reader reader = new Reader(_cbr, RFile.RINDEX_VER_8);
    BlockRead rootIn = _cbr.getMetaBlock("root");
    reader.readFields(rootIn);
    rootIn.close();
    IndexIterator liter = reader.lookup(new Key("000000"));
    int count = 0;
    while (liter.hasNext()) {
        assertEquals(count, liter.nextIndex());
        assertEquals(count, liter.peek().getNumEntries());
        assertEquals(count, liter.next().getNumEntries());
        count++;
    }
    assertEquals(num + 1, count);
    while (liter.hasPrevious()) {
        count--;
        assertEquals(count, liter.previousIndex());
        assertEquals(count, liter.peekPrevious().getNumEntries());
        assertEquals(count, liter.previous().getNumEntries());
    }
    assertEquals(0, count);
    // go past the end
    liter = reader.lookup(new Key(String.format("%05d000", num + 1)));
    assertFalse(liter.hasNext());
    Random rand = new Random();
    for (int i = 0; i < 100; i++) {
        int k = rand.nextInt(num * 1000);
        int expected;
        if (k % 1000 == 0)
            // end key is inclusive
            expected = k / 1000;
        else
            expected = k / 1000 + 1;
        liter = reader.lookup(new Key(String.format("%08d", k)));
        IndexEntry ie = liter.next();
        assertEquals(expected, ie.getNumEntries());
    }
}
Also used : Reader(org.apache.accumulo.core.file.rfile.MultiLevelIndex.Reader) IndexEntry(org.apache.accumulo.core.file.rfile.MultiLevelIndex.IndexEntry) ABlockWriter(org.apache.accumulo.core.file.blockfile.ABlockWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IndexIterator(org.apache.accumulo.core.file.rfile.MultiLevelIndex.Reader.IndexIterator) BufferedWriter(org.apache.accumulo.core.file.rfile.MultiLevelIndex.BufferedWriter) BlockRead(org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile.BlockRead) Random(java.util.Random) FileSystem(org.apache.hadoop.fs.FileSystem) SeekableByteArrayInputStream(org.apache.accumulo.core.file.rfile.RFileTest.SeekableByteArrayInputStream) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) CachableBlockFile(org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream) BufferedWriter(org.apache.accumulo.core.file.rfile.MultiLevelIndex.BufferedWriter) ABlockWriter(org.apache.accumulo.core.file.blockfile.ABlockWriter) Writer(org.apache.accumulo.core.file.rfile.MultiLevelIndex.Writer) Key(org.apache.accumulo.core.data.Key) AccumuloConfiguration(org.apache.accumulo.core.conf.AccumuloConfiguration)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Random (java.util.Random)1 AccumuloConfiguration (org.apache.accumulo.core.conf.AccumuloConfiguration)1 Key (org.apache.accumulo.core.data.Key)1 ABlockWriter (org.apache.accumulo.core.file.blockfile.ABlockWriter)1 CachableBlockFile (org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile)1 BlockRead (org.apache.accumulo.core.file.blockfile.impl.CachableBlockFile.BlockRead)1 BufferedWriter (org.apache.accumulo.core.file.rfile.MultiLevelIndex.BufferedWriter)1 IndexEntry (org.apache.accumulo.core.file.rfile.MultiLevelIndex.IndexEntry)1 Reader (org.apache.accumulo.core.file.rfile.MultiLevelIndex.Reader)1 IndexIterator (org.apache.accumulo.core.file.rfile.MultiLevelIndex.Reader.IndexIterator)1 Writer (org.apache.accumulo.core.file.rfile.MultiLevelIndex.Writer)1 SeekableByteArrayInputStream (org.apache.accumulo.core.file.rfile.RFileTest.SeekableByteArrayInputStream)1 FSDataInputStream (org.apache.hadoop.fs.FSDataInputStream)1 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)1 FileSystem (org.apache.hadoop.fs.FileSystem)1