Search in sources :

Example 36 with Cursor

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();
}
Also used : Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor) ByteBuffer(java.nio.ByteBuffer)

Example 37 with Cursor

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);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) StructuredData(com.yahoo.search.result.StructuredData) JSONArray(org.json.JSONArray) JsonNode(com.fasterxml.jackson.databind.JsonNode) JSONString(com.yahoo.prelude.hitfield.JSONString) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor) Result(com.yahoo.search.Result) FastHit(com.yahoo.prelude.fastsearch.FastHit) Hit(com.yahoo.search.result.Hit) JSONObject(org.json.JSONObject) SlimeAdapter(com.yahoo.data.access.slime.SlimeAdapter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JSONString(com.yahoo.prelude.hitfield.JSONString) Test(org.junit.Test)

Example 38 with Cursor

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());
    }
}
Also used : Cursor(com.yahoo.slime.Cursor)

Example 39 with Cursor

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);
}
Also used : Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) SlimeJsonResponse(com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor)

Example 40 with Cursor

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);
}
Also used : AlreadyExistsException(com.yahoo.vespa.hosted.controller.AlreadyExistsException) EndpointStatus(com.yahoo.vespa.hosted.controller.api.application.v4.model.EndpointStatus) Inject(com.google.inject.Inject) URISyntaxException(java.net.URISyntaxException) SlimeJsonResponse(com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse) Scanner(java.util.Scanner) DeploymentJobs(com.yahoo.vespa.hosted.controller.application.DeploymentJobs) ConfigServerException(com.yahoo.vespa.hosted.controller.api.integration.configserver.ConfigServerException) RegionName(com.yahoo.config.provision.RegionName) TenantName(com.yahoo.config.provision.TenantName) ResourceResponse(com.yahoo.vespa.hosted.controller.restapi.ResourceResponse) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) ZoneId(com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneId) ClusterUtilization(com.yahoo.vespa.hosted.controller.application.ClusterUtilization) Duration(java.time.Duration) Map(java.util.Map) LogLevel(com.yahoo.log.LogLevel) Path(com.yahoo.vespa.hosted.controller.restapi.Path) JobStatus(com.yahoo.vespa.hosted.controller.application.JobStatus) GitRevision(com.yahoo.vespa.hosted.controller.api.application.v4.model.GitRevision) ClusterCost(com.yahoo.vespa.hosted.controller.application.ClusterCost) BadRequestException(javax.ws.rs.BadRequestException) DeployOptions(com.yahoo.vespa.hosted.controller.api.application.v4.model.DeployOptions) URI(java.net.URI) DeploymentCost(com.yahoo.vespa.hosted.controller.application.DeploymentCost) ScrewdriverBuildJob(com.yahoo.vespa.hosted.controller.api.application.v4.model.ScrewdriverBuildJob) Exceptions(com.yahoo.yolean.Exceptions) AthenzDomain(com.yahoo.vespa.athenz.api.AthenzDomain) ImmutableSet(com.google.common.collect.ImmutableSet) Inspector(com.yahoo.slime.Inspector) NotExistsException(com.yahoo.vespa.hosted.controller.NotExistsException) ApplicationVersion(com.yahoo.vespa.hosted.controller.application.ApplicationVersion) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) ErrorResponse(com.yahoo.vespa.hosted.controller.restapi.ErrorResponse) RestartAction(com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.RestartAction) Property(com.yahoo.vespa.hosted.controller.api.identifiers.Property) ApplicationView(com.yahoo.vespa.serviceview.bindings.ApplicationView) Objects(java.util.Objects) ZmsException(com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsException) List(java.util.List) Principal(java.security.Principal) AthenzPrincipal(com.yahoo.vespa.athenz.api.AthenzPrincipal) NotAuthorizedException(javax.ws.rs.NotAuthorizedException) Optional(java.util.Optional) Deployment(com.yahoo.vespa.hosted.controller.application.Deployment) HttpResponse(com.yahoo.container.jdisc.HttpResponse) Controller(com.yahoo.vespa.hosted.controller.Controller) Joiner(com.google.common.base.Joiner) Log(com.yahoo.vespa.hosted.controller.api.integration.configserver.Log) AthenzClientFactory(com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzClientFactory) GitRepository(com.yahoo.vespa.hosted.controller.api.identifiers.GitRepository) ApplicationName(com.yahoo.config.provision.ApplicationName) AthenzUser(com.yahoo.vespa.athenz.api.AthenzUser) Version(com.yahoo.component.Version) ApplicationId(com.yahoo.config.provision.ApplicationId) PropertyId(com.yahoo.vespa.hosted.controller.api.identifiers.PropertyId) RefeedAction(com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.RefeedAction) DeploymentId(com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId) Slime(com.yahoo.slime.Slime) AthenzIdentity(com.yahoo.vespa.athenz.api.AthenzIdentity) IOUtils(com.yahoo.io.IOUtils) NToken(com.yahoo.vespa.athenz.api.NToken) Level(java.util.logging.Level) DeploymentMetrics(com.yahoo.vespa.hosted.controller.application.DeploymentMetrics) ApplicationResource(com.yahoo.vespa.hosted.controller.api.application.v4.ApplicationResource) SlimeUtils(com.yahoo.vespa.config.SlimeUtils) Change(com.yahoo.vespa.hosted.controller.application.Change) TenantId(com.yahoo.vespa.hosted.controller.api.identifiers.TenantId) GitBranch(com.yahoo.vespa.hosted.controller.api.identifiers.GitBranch) ServiceInfo(com.yahoo.vespa.hosted.controller.api.application.v4.model.configserverbindings.ServiceInfo) SetBouncerPassthruHeaderFilter(com.yahoo.vespa.hosted.controller.restapi.filter.SetBouncerPassthruHeaderFilter) EnvironmentResource(com.yahoo.vespa.hosted.controller.api.application.v4.EnvironmentResource) TenantResource(com.yahoo.vespa.hosted.controller.api.application.v4.TenantResource) Application(com.yahoo.vespa.hosted.controller.Application) ActivateResult(com.yahoo.vespa.hosted.controller.api.ActivateResult) Cursor(com.yahoo.slime.Cursor) StringResponse(com.yahoo.vespa.hosted.controller.restapi.StringResponse) ForbiddenException(javax.ws.rs.ForbiddenException) Hostname(com.yahoo.vespa.hosted.controller.api.identifiers.Hostname) Environment(com.yahoo.config.provision.Environment) GitCommit(com.yahoo.vespa.hosted.controller.api.identifiers.GitCommit) HttpRequest(com.yahoo.container.jdisc.HttpRequest) SourceRevision(com.yahoo.vespa.hosted.controller.application.SourceRevision) IOException(java.io.IOException) MessageResponse(com.yahoo.vespa.hosted.controller.restapi.MessageResponse) ApplicationPackage(com.yahoo.vespa.hosted.controller.application.ApplicationPackage) LoggingRequestHandler(com.yahoo.container.jdisc.LoggingRequestHandler) User(com.yahoo.vespa.hosted.controller.api.integration.organization.User) UserId(com.yahoo.vespa.hosted.controller.api.identifiers.UserId) RotationStatus(com.yahoo.vespa.hosted.controller.api.integration.routing.RotationStatus) DeploymentSpec(com.yahoo.config.application.api.DeploymentSpec) DayOfWeek(java.time.DayOfWeek) ScrewdriverId(com.yahoo.vespa.hosted.controller.api.identifiers.ScrewdriverId) Collections(java.util.Collections) InputStream(java.io.InputStream) ForbiddenException(javax.ws.rs.ForbiddenException) Tenant(com.yahoo.vespa.hosted.controller.api.Tenant) SlimeJsonResponse(com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse) UserId(com.yahoo.vespa.hosted.controller.api.identifiers.UserId) Slime(com.yahoo.slime.Slime) Cursor(com.yahoo.slime.Cursor)

Aggregations

Cursor (com.yahoo.slime.Cursor)112 Slime (com.yahoo.slime.Slime)79 Test (org.junit.Test)33 SlimeJsonResponse (com.yahoo.vespa.hosted.controller.restapi.SlimeJsonResponse)19 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 DefParser (com.yahoo.config.codegen.DefParser)15 InnerCNode (com.yahoo.config.codegen.InnerCNode)15 StringReader (java.io.StringReader)15 IOException (java.io.IOException)9 ApplicationId (com.yahoo.config.provision.ApplicationId)8 JsonFormat (com.yahoo.slime.JsonFormat)8 Application (com.yahoo.vespa.hosted.controller.Application)6 List (java.util.List)6 Map (java.util.Map)6 Inspector (com.yahoo.slime.Inspector)5 SlimeUtils (com.yahoo.vespa.config.SlimeUtils)5 Ignore (org.junit.Ignore)5 Version (com.yahoo.component.Version)4 RegionName (com.yahoo.config.provision.RegionName)4 TenantName (com.yahoo.config.provision.TenantName)4