Search in sources :

Example 11 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class QueryTestCase method packetToBytes.

public static byte[] packetToBytes(Packet packet) {
    try {
        ByteBuffer buffer = ByteBuffer.allocate(500);
        buffer.limit(0);
        packet.encode(buffer, 0);
        byte[] encoded = new byte[buffer.position()];
        buffer.rewind();
        buffer.get(encoded);
        return encoded;
    } catch (BufferTooSmallException e) {
        throw new RuntimeException(e);
    }
}
Also used : BufferTooSmallException(com.yahoo.fs4.BufferTooSmallException) ByteBuffer(java.nio.ByteBuffer)

Example 12 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class QueryTestCase method testPhraseEqualsPhraseWithPhraseSegment.

@Test
public void testPhraseEqualsPhraseWithPhraseSegment() throws BufferTooSmallException {
    Query query = new Query();
    PhraseItem p = new PhraseItem();
    PhraseSegmentItem ps = new PhraseSegmentItem("a b", false, false);
    ps.addItem(new WordItem("a"));
    ps.addItem(new WordItem("b"));
    p.addItem(ps);
    query.getModel().getQueryTree().setRoot(p);
    query.setTimeout(0);
    QueryPacket queryPacket = QueryPacket.create(query);
    ByteBuffer buffer1 = ByteBuffer.allocate(1024);
    queryPacket.encode(buffer1, 0);
    query = new Query();
    p = new PhraseItem();
    p.addItem(new WordItem("a"));
    p.addItem(new WordItem("b"));
    query.getModel().getQueryTree().setRoot(p);
    query.setTimeout(0);
    queryPacket = QueryPacket.create(query);
    assertNotNull(queryPacket);
    ByteBuffer buffer2 = ByteBuffer.allocate(1024);
    queryPacket.encode(buffer2, 0);
    byte[] encoded1 = new byte[buffer1.position()];
    buffer1.rewind();
    buffer1.get(encoded1);
    byte[] encoded2 = new byte[buffer2.position()];
    buffer2.rewind();
    buffer2.get(encoded2);
    assertEqualArrays(encoded2, encoded1);
}
Also used : Query(com.yahoo.search.Query) WordItem(com.yahoo.prelude.query.WordItem) PhraseSegmentItem(com.yahoo.prelude.query.PhraseSegmentItem) ByteBuffer(java.nio.ByteBuffer) PhraseItem(com.yahoo.prelude.query.PhraseItem) QueryPacket(com.yahoo.fs4.QueryPacket) Test(org.junit.Test)

Example 13 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketDecoderTestCase method testErrorPacket.

@Test
public void testErrorPacket() throws BufferTooSmallException {
    ByteBuffer b = ByteBuffer.allocate(100);
    b.putInt(0);
    b.putInt(203);
    b.putInt(1);
    b.putInt(37);
    b.putInt(5);
    b.put(new byte[] { (byte) 'n', (byte) 'a', (byte) 'l', (byte) 'l', (byte) 'e' });
    b.putInt(0, b.position() - 4);
    b.flip();
    DecodedPacket p = PacketDecoder.extractPacket(b);
    ErrorPacket e = (ErrorPacket) p.packet;
    assertEquals("nalle (37)", e.toString());
    assertEquals(203, e.getCode());
    assertEquals(37, e.getErrorCode());
    b = ByteBuffer.allocate(100);
    // warn if encoding support is added untested
    e.encode(b);
    b.position(0);
    assertEquals(4, b.getInt());
    assertEquals(203, b.getInt());
    assertFalse(b.hasRemaining());
}
Also used : ErrorPacket(com.yahoo.fs4.ErrorPacket) DecodedPacket(com.yahoo.fs4.PacketDecoder.DecodedPacket) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 14 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketDecoderTestCase method testTooSmallBufferForPacket.

/**
 * Test the case where the buffer is too small for the
 * packet
 */
@Test
public void testTooSmallBufferForPacket() {
    ByteBuffer data = ByteBuffer.allocate(10);
    data.put(queryResultPacketData, 0, 10);
    data.flip();
    try {
        PacketDecoder.extractPacket(data);
        fail();
    } catch (BufferTooSmallException e) {
    }
}
Also used : BufferTooSmallException(com.yahoo.fs4.BufferTooSmallException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 15 with BufferTooSmallException

use of com.yahoo.fs4.BufferTooSmallException in project vespa by vespa-engine.

the class PacketCacheTestCase method createCacheEntry.

/**
 * Creates a 64-byte packet in an array wrapped in a PacketWrapper
 */
private PacketWrapper createCacheEntry(CacheKey key) throws BufferTooSmallException {
    ByteBuffer data = ByteBuffer.allocate(length);
    data.put(queryResultPacketData);
    data.flip();
    BasicPacket[] content = new BasicPacket[] { PacketDecoder.extractPacket(data).packet };
    return new PacketWrapper(key, content);
}
Also used : BasicPacket(com.yahoo.fs4.BasicPacket) PacketWrapper(com.yahoo.prelude.fastsearch.PacketWrapper) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)15 Test (org.junit.Test)12 QueryPacket (com.yahoo.fs4.QueryPacket)6 Query (com.yahoo.search.Query)6 DecodedPacket (com.yahoo.fs4.PacketDecoder.DecodedPacket)5 BufferTooSmallException (com.yahoo.fs4.BufferTooSmallException)4 PacketDecoder (com.yahoo.fs4.PacketDecoder)4 BasicPacket (com.yahoo.fs4.BasicPacket)2 QueryResultPacket (com.yahoo.fs4.QueryResultPacket)2 ErrorPacket (com.yahoo.fs4.ErrorPacket)1 GetDocSumsPacket (com.yahoo.fs4.GetDocSumsPacket)1 PacketWrapper (com.yahoo.prelude.fastsearch.PacketWrapper)1 PhraseItem (com.yahoo.prelude.query.PhraseItem)1 PhraseSegmentItem (com.yahoo.prelude.query.PhraseSegmentItem)1 WordItem (com.yahoo.prelude.query.WordItem)1