use of com.yahoo.document.datatypes.IntegerFieldValue in project vespa by vespa-engine.
the class VespaXMLUpdateReader method readArithmetic.
FieldUpdate readArithmetic(DocumentUpdate update, String type, Field f, FieldUpdate fu) throws XMLStreamException {
Double by = null;
for (int i = 0; i < reader.getAttributeCount(); i++) {
if ("by".equals(reader.getAttributeName(i).toString())) {
by = Double.parseDouble(reader.getAttributeValue(i));
}
}
if (by == null) {
throw newDeserializeException("Assignment update without \"by\" attribute");
}
FieldValue key = null;
do {
reader.next();
if (reader.getEventType() == XMLStreamReader.START_ELEMENT) {
if ("key".equals(reader.getName().toString())) {
if (f.getDataType() instanceof WeightedSetDataType) {
DataType nestedType = ((WeightedSetDataType) f.getDataType()).getNestedType();
key = nestedType.createFieldValue();
key.deserialize(this);
} else if (f.getDataType() instanceof MapDataType) {
key = ((MapDataType) f.getDataType()).getKeyType().createFieldValue();
key.deserialize(this);
} else if (f.getDataType() instanceof ArrayDataType) {
key = new IntegerFieldValue(Integer.parseInt(reader.getElementText()));
} else {
throw newDeserializeException("Key tag only applicable for weighted sets and maps");
}
skipToEnd("key");
} else {
throw newDeserializeException("\"" + reader.getName() + "\" not appropriate within " + type + " element.");
}
}
} while (reader.getEventType() != XMLStreamReader.END_ELEMENT);
if (key != null) {
if ("increment".equals(type)) {
fu.addValueUpdate(ValueUpdate.createIncrement(key, by));
}
if ("decrement".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDecrement(key, by));
}
if ("multiply".equals(type)) {
fu.addValueUpdate(ValueUpdate.createMultiply(key, by));
}
if ("divide".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDivide(key, by));
}
} else {
if ("increment".equals(type)) {
fu.addValueUpdate(ValueUpdate.createIncrement(by));
}
if ("decrement".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDecrement(by));
}
if ("multiply".equals(type)) {
fu.addValueUpdate(ValueUpdate.createMultiply(by));
}
if ("divide".equals(type)) {
fu.addValueUpdate(ValueUpdate.createDivide(by));
}
}
return fu;
}
use of com.yahoo.document.datatypes.IntegerFieldValue in project vespa by vespa-engine.
the class GetSearcherTestCase method testQueryPassThroughAndGet.
/* Test that a query will contain both document hits and hits from a searcher
* further down the chain, iff the searcher returns a DocumentHit.
*/
@Test
public void testQueryPassThroughAndGet() throws Exception {
Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:1234:foo"));
doc1.setFieldValue("name", new StringFieldValue("megacat"));
doc1.setFieldValue("description", new StringFieldValue("supercat"));
doc1.setFieldValue("fluffiness", new IntegerFieldValue(10000));
GetDocumentReply[] replies = new GetDocumentReply[] { new GetDocumentReply(doc1) };
DocumentSessionFactory factory = new DocumentSessionFactory(docType, null, false, replies);
GetSearcher searcher = new GetSearcher(new FeedContext(new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg), factory, docMan, new ClusterList(), new NullFeedMetric()));
DocumentHit backendHit = new DocumentHit(new Document(docType, new DocumentId("userdoc:kittens:5678:bar")), 5);
Chain<Searcher> searchChain = new Chain<>(searcher, new MockBackend(backendHit));
Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?query=flarn&id=userdoc:kittens:1234:foo"));
assertEquals(1, factory.messages.size());
assertEquals(2, result.hits().size());
assertNotNull(result.hits().get("userdoc:kittens:5678:bar"));
assertNotNull(result.hits().get("userdoc:kittens:1234:foo"));
}
use of com.yahoo.document.datatypes.IntegerFieldValue in project vespa by vespa-engine.
the class AccessesAnnotationTestCase method requireThatFieldsAreRestricted.
@Test
public void requireThatFieldsAreRestricted() {
DocumentType type = new DocumentType("album");
type.addField("title", DataType.STRING);
type.addField("artist", DataType.STRING);
type.addField("year", DataType.INT);
Document doc = new Document(type, new DocumentId("doc:map:test:1"));
MyDocProc docProc = new MyDocProc();
DocumentPut put = new DocumentPut(doc);
Document proxy = new Call(docProc).configDoc(docProc, put).getDocument();
proxy.setFieldValue("title", new StringFieldValue("foo"));
try {
proxy.setFieldValue("year", new IntegerFieldValue(69));
fail("Should have failed");
} catch (Exception e) {
System.out.println(e.getMessage());
assertTrue(e.getMessage().matches(".*not allowed.*"));
}
proxy.getFieldValue("title");
try {
proxy.getFieldValue("year");
fail("Should have failed");
} catch (Exception e) {
System.out.println(e.getMessage());
assertTrue(e.getMessage().matches(".*not allowed.*"));
}
}
use of com.yahoo.document.datatypes.IntegerFieldValue in project vespa by vespa-engine.
the class JsonReaderTestCase method testOldMapStringToArrayOfInt.
@Test
public final void testOldMapStringToArrayOfInt() throws IOException {
InputStream rawDoc = new ByteArrayInputStream(Utf8.toBytes("{\"put\": \"id:unittest:testMapStringToArrayOfInt::whee\"," + " \"fields\": { \"actualMapStringToArrayOfInt\": [" + "{ \"key\": \"bamse\", \"value\": [1, 2, 3] }" + "]}}"));
JsonReader r = new JsonReader(types, rawDoc, parserFactory);
DocumentParseInfo parseInfo = r.parseDocument().get();
DocumentType docType = r.readDocumentType(parseInfo.documentId);
DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId));
new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put);
Document doc = put.getDocument();
FieldValue f = doc.getFieldValue("actualMapStringToArrayOfInt");
assertSame(MapFieldValue.class, f.getClass());
MapFieldValue<?, ?> m = (MapFieldValue<?, ?>) f;
Array<?> a = (Array<?>) m.get(new StringFieldValue("bamse"));
assertEquals(3, a.size());
assertEquals(new IntegerFieldValue(1), a.get(0));
assertEquals(new IntegerFieldValue(2), a.get(1));
assertEquals(new IntegerFieldValue(3), a.get(2));
}
use of com.yahoo.document.datatypes.IntegerFieldValue in project vespa by vespa-engine.
the class VespaDocumentSerializerTestCase method get_serialized_size_uses_latest_serializer.
@Test
public void get_serialized_size_uses_latest_serializer() {
DocumentType docType = new DocumentType("my_type");
docType.addField("my_str", DataType.STRING);
docType.addField("my_int", DataType.INT);
Document doc = new Document(docType, "doc:scheme:");
doc.setFieldValue("my_str", new StringFieldValue("foo"));
doc.setFieldValue("my_int", new IntegerFieldValue(69));
GrowableByteBuffer buf = new GrowableByteBuffer();
doc.serialize(buf);
assertEquals(buf.position(), VespaDocumentSerializer42.getSerializedSize(doc));
}
Aggregations