use of com.yahoo.vespa.objects.BufferSerializer in project vespa by vespa-engine.
the class QueryPacket method encodeBody.
public void encodeBody(ByteBuffer buffer) {
queryPacketData = new QueryPacketData();
final int relativeZero = buffer.position();
boolean sendSessionKey = query.getGroupingSessionCache() || query.getRanking().getQueryCache();
int featureFlag = getFeatureInt(sendSessionKey);
buffer.putInt(featureFlag);
ignoreableOffset = buffer.position() - relativeZero;
IntegerCompressor.putCompressedPositiveNumber(getOffset(), buffer);
IntegerCompressor.putCompressedPositiveNumber(getHits(), buffer);
// Safety to avoid sending down 0 or negative number
buffer.putInt(Math.max(1, (int) query.getTimeLeft()));
ignoreableSize = buffer.position() - relativeZero - ignoreableOffset;
buffer.putInt(getFlagInt());
int startOfFieldToSave = buffer.position();
Item.putString(query.getRanking().getProfile(), buffer);
queryPacketData.setRankProfile(buffer, startOfFieldToSave);
if ((featureFlag & QF_PROPERTIES) != 0) {
startOfFieldToSave = buffer.position();
query.encodeAsProperties(buffer, true);
queryPacketData.setPropertyMaps(buffer, startOfFieldToSave);
}
if ((featureFlag & QF_SORTSPEC) != 0) {
int sortSpecLengthPosition = buffer.position();
buffer.putInt(0);
int sortSpecLength = query.getRanking().getSorting().encode(buffer);
buffer.putInt(sortSpecLengthPosition, sortSpecLength);
}
if ((featureFlag & QF_GROUPSPEC) != 0) {
List<Grouping> groupingList = GroupingExecutor.getGroupingList(query);
BufferSerializer gbuf = new BufferSerializer(new GrowableByteBuffer());
gbuf.putInt(null, groupingList.size());
for (Grouping g : groupingList) {
g.serialize(gbuf);
}
gbuf.getBuf().flip();
byte[] blob = new byte[gbuf.getBuf().limit()];
gbuf.getBuf().get(blob);
buffer.putInt(blob.length);
buffer.put(blob);
}
sessionOffset = buffer.position() - relativeZero;
if (sendSessionKey) {
Utf8String key = query.getSessionId(true).asUtf8String();
sessionSize = key.getByteLength();
buffer.putInt(key.getByteLength());
buffer.put(key.getBytes());
}
if ((featureFlag & QF_LOCATION) != 0) {
startOfFieldToSave = buffer.position();
int locationLengthPosition = buffer.position();
buffer.putInt(0);
int locationLength = query.getRanking().getLocation().encode(buffer);
buffer.putInt(locationLengthPosition, locationLength);
queryPacketData.setLocation(buffer, startOfFieldToSave);
}
startOfFieldToSave = buffer.position();
int stackItemPosition = buffer.position();
// Number of stack items written below
buffer.putInt(0);
int stackLengthPosition = buffer.position();
buffer.putInt(0);
int stackPosition = buffer.position();
int stackItemCount = query.encode(buffer);
int stackLength = buffer.position() - stackPosition;
buffer.putInt(stackItemPosition, stackItemCount);
buffer.putInt(stackLengthPosition, stackLength);
queryPacketData.setQueryStack(buffer, startOfFieldToSave);
}
use of com.yahoo.vespa.objects.BufferSerializer in project vespa by vespa-engine.
the class VdsVisitor method mergeGroupingMaps.
private void mergeGroupingMaps(Map<Integer, byte[]> newGroupingMap) {
if (log.isLoggable(LogLevel.SPAM)) {
log.log(LogLevel.SPAM, "mergeGroupingMaps: newGroupingMap = " + newGroupingMap);
}
for (Integer key : newGroupingMap.keySet()) {
byte[] value = newGroupingMap.get(key);
Grouping newGrouping = new Grouping();
if (log.isLoggable(LogLevel.SPAM)) {
log.log(LogLevel.SPAM, "Received group with key " + key + " and size " + value.length);
}
BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer(ByteBuffer.wrap(value)));
newGrouping.deserialize(buf);
if (buf.getBuf().hasRemaining()) {
throw new IllegalArgumentException("Failed deserializing grouping. There are still data left. Position = " + buf.position() + ", limit = " + buf.getBuf().limit());
}
synchronized (groupingMap) {
if (groupingMap.containsKey(key)) {
Grouping grouping = groupingMap.get(key);
grouping.merge(newGrouping);
} else {
groupingMap.put(key, newGrouping);
}
}
}
}
use of com.yahoo.vespa.objects.BufferSerializer in project vespa by vespa-engine.
the class AggregationTestCase method serializeDeserialize1.
// --------------------------------------------------------------------------------
//
// Everything below this point is helper functions.
//
// --------------------------------------------------------------------------------
private static Identifiable serializeDeserialize1(Identifiable a) {
BufferSerializer buf = new BufferSerializer(new GrowableByteBuffer());
a.serializeWithId(buf);
buf.flip();
Identifiable b = Identifiable.create(buf);
assertEquals(a.getClass(), b.getClass());
assertEquals(buf.getBuf().hasRemaining(), false);
Identifiable c = b.clone();
assertEquals(b.getClass(), c.getClass());
BufferSerializer bb = new BufferSerializer(new GrowableByteBuffer());
BufferSerializer cb = new BufferSerializer(new GrowableByteBuffer());
b.serializeWithId(bb);
c.serializeWithId(cb);
assertEquals(bb.getBuf().limit(), cb.getBuf().limit());
assertEquals(bb.position(), cb.position());
bb.getBuf().flip();
cb.getBuf().flip();
for (int i = 0; i < bb.getBuf().limit(); i++) {
assertEquals(bb.getBuf().get(), cb.getBuf().get());
}
return b;
}
use of com.yahoo.vespa.objects.BufferSerializer in project vespa by vespa-engine.
the class GroupTestCase method requireThatDeserializeResolvesAggregationRef.
@Test
public void requireThatDeserializeResolvesAggregationRef() {
Group group = new Group();
AggregationResult res = new MinAggregationResult();
group.addOrderBy(new AggregationRefNode(res), true);
BufferSerializer buf = new BufferSerializer();
group.serializeWithId(buf);
buf.flip();
group = (Group) Identifiable.create(buf);
assertEquals(1, group.getOrderByExpressions().size());
AggregationRefNode ref = (AggregationRefNode) group.getOrderByExpressions().get(0);
assertEquals(0, ref.getIndex());
assertEquals(res, ref.getExpression());
assertNotSame(res, ref.getExpression());
}
use of com.yahoo.vespa.objects.BufferSerializer in project vespa by vespa-engine.
the class GroupingTestCase method requireThatValidAccessorsWork.
@Test
public void requireThatValidAccessorsWork() {
byte[] arr = new byte[1024];
BufferSerializer buf = new BufferSerializer(arr);
Grouping grouping = new Grouping();
grouping.serializeWithId(buf);
buf.flip();
assertEquals(1, arr[VALID_BYTE_INDEX]);
arr[VALID_BYTE_INDEX] = 0;
Grouping other = (Grouping) Grouping.create(buf);
assertFalse(other.valid());
assertEquals(grouping, grouping.clone());
assertSerialize(grouping);
}
Aggregations