use of com.yahoo.slime.Cursor in project vespa by vespa-engine.
the class DocsumDefinitionTestCase method makeDocsum.
public static byte[] makeDocsum() {
Slime slime = new Slime();
Cursor docsum = slime.setObject();
docsum.setString("TOPIC", "Arts/Celebrities/Madonna");
docsum.setString("TITLE", "StudyOfMadonna.com - Interviews, Articles, Reviews, Quotes, Essays and more..");
docsum.setString("DYNTEASER", "dynamic teaser");
docsum.setLong("EXTINFOSOURCE", 1);
docsum.setLong("LANG1", 10);
docsum.setLong("WORDS", 352);
docsum.setLong("BYTES", 9190);
byte[] tmp = BinaryFormat.encode(slime);
ByteBuffer buf = ByteBuffer.allocate(tmp.length + 4);
buf.order(ByteOrder.LITTLE_ENDIAN);
buf.putInt(DocsumDefinitionSet.SLIME_MAGIC_ID);
buf.order(ByteOrder.BIG_ENDIAN);
buf.put(tmp);
return buf.array();
}
use of com.yahoo.slime.Cursor in project vespa by vespa-engine.
the class JsonRendererTestCase method testJsonObjects.
@Test
public void testJsonObjects() throws InterruptedException, ExecutionException, IOException, JSONException {
String expected = "{\n" + " \"root\": {\n" + " \"children\": [\n" + " {\n" + " \"fields\": {\n" + " \"inspectable\": {\n" + " \"a\": \"b\"\n" + " },\n" + " \"jackson\": {\n" + " \"Nineteen-eighty-four\": 1984\n" + " },\n" + " \"json producer\": {\n" + " \"long in structured\": 7809531904\n" + " },\n" + " \"org.json array\": [\n" + " true,\n" + " true,\n" + " false\n" + " ],\n" + " \"org.json object\": {\n" + " \"forty-two\": 42\n" + " }\n" + " },\n" + " \"id\": \"json objects\",\n" + " \"relevance\": 1.0\n" + " }\n" + " ],\n" + " \"fields\": {\n" + " \"totalCount\": 0\n" + " },\n" + " \"id\": \"toplevel\",\n" + " \"relevance\": 1.0\n" + " }\n" + "}\n";
Result r = newEmptyResult();
Hit h = new Hit("json objects");
JSONObject o = new JSONObject();
JSONArray a = new JSONArray();
ObjectMapper mapper = new ObjectMapper();
JsonNode j = mapper.createObjectNode();
JSONString s = new JSONString("{\"a\": \"b\"}");
Slime slime = new Slime();
Cursor c = slime.setObject();
c.setLong("long in structured", 7809531904L);
SlimeAdapter slimeInit = new SlimeAdapter(slime.get());
StructuredData struct = new StructuredData(slimeInit);
((ObjectNode) j).put("Nineteen-eighty-four", 1984);
o.put("forty-two", 42);
a.put(true);
a.put(true);
a.put(false);
h.setField("inspectable", s);
h.setField("jackson", j);
h.setField("json producer", struct);
h.setField("org.json array", a);
h.setField("org.json object", o);
r.hits().add(h);
String summary = render(r);
assertEqualJson(expected, summary);
}
use of com.yahoo.slime.Cursor in project vespa by vespa-engine.
the class BinaryFormat method encode.
private static void encode(Predicate predicate, Cursor out) {
if (predicate instanceof Conjunction) {
out.setLong(NODE_TYPE, TYPE_CONJUNCTION);
out = out.setArray(CHILDREN);
for (Predicate operand : ((Conjunction) predicate).getOperands()) {
encode(operand, out.addObject());
}
} else if (predicate instanceof Disjunction) {
out.setLong(NODE_TYPE, TYPE_DISJUNCTION);
out = out.setArray(CHILDREN);
for (Predicate operand : ((Disjunction) predicate).getOperands()) {
encode(operand, out.addObject());
}
} else if (predicate instanceof FeatureRange) {
FeatureRange range = (FeatureRange) predicate;
out.setLong(NODE_TYPE, TYPE_FEATURE_RANGE);
out.setString(KEY, range.getKey());
Long from = range.getFromInclusive();
if (from != null) {
out.setLong(RANGE_MIN, from);
}
Long to = range.getToInclusive();
if (to != null) {
out.setLong(RANGE_MAX, to);
}
Cursor p_out = out.setArray(HASHED_PARTITIONS);
for (RangePartition p : range.getPartitions()) {
p_out.addLong(PredicateHash.hash64(p.getLabel()));
}
p_out = out.setArray(HASHED_EDGE_PARTITIONS);
for (RangeEdgePartition p : range.getEdgePartitions()) {
Cursor obj = p_out.addObject();
obj.setLong(HASH, PredicateHash.hash64(p.getLabel()));
obj.setLong(VALUE, p.getValue());
obj.setLong(PAYLOAD, p.encodeBounds());
}
} else if (predicate instanceof FeatureSet) {
out.setLong(NODE_TYPE, TYPE_FEATURE_SET);
out.setString(KEY, ((FeatureSet) predicate).getKey());
out = out.setArray(SET);
for (String value : ((FeatureSet) predicate).getValues()) {
out.addString(value);
}
} else if (predicate instanceof Negation) {
out.setLong(NODE_TYPE, TYPE_NEGATION);
out = out.setArray(CHILDREN);
encode(((Negation) predicate).getOperand(), out.addObject());
} else if (predicate instanceof BooleanPredicate) {
out.setLong(NODE_TYPE, ((BooleanPredicate) predicate).getValue() ? TYPE_TRUE : TYPE_FALSE);
} else {
throw new UnsupportedOperationException(predicate.getClass().getName());
}
}
use of com.yahoo.slime.Cursor in project vespa by vespa-engine.
the class ApplicationApiHandler method recursiveRoot.
private HttpResponse recursiveRoot(HttpRequest request) {
Slime slime = new Slime();
Cursor tenantArray = slime.setArray();
for (Tenant tenant : controller.tenants().asList()) toSlime(tenantArray.addObject(), tenant, request, true);
return new SlimeJsonResponse(slime);
}
use of com.yahoo.slime.Cursor in project vespa by vespa-engine.
the class ApplicationApiHandler method authenticatedUser.
private HttpResponse authenticatedUser(HttpRequest request) {
String userIdString = request.getProperty("userOverride");
if (userIdString == null)
userIdString = getUserId(request).map(UserId::id).orElseThrow(() -> new ForbiddenException("You must be authenticated or specify userOverride"));
UserId userId = new UserId(userIdString);
List<Tenant> tenants = controller.tenants().asList(userId);
Slime slime = new Slime();
Cursor response = slime.setObject();
response.setString("user", userId.id());
Cursor tenantsArray = response.setArray("tenants");
for (Tenant tenant : tenants) tenantInTenantsListToSlime(tenant, request.getUri(), tenantsArray.addObject());
response.setBool("tenantExists", tenants.stream().map(Tenant::getId).anyMatch(id -> id.isTenantFor(userId)));
return new SlimeJsonResponse(slime);
}
Aggregations