aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/select/rule/IdNode.java
blob: 0b9398df3f7375cb2892bf98b892a84ee24573cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.select.rule;

import com.yahoo.document.DocumentId;
import com.yahoo.document.BucketIdFactory;
import com.yahoo.document.select.BucketSet;
import com.yahoo.document.select.Context;
import com.yahoo.document.select.Visitor;

/**
 * @author Simon Thoresen Hult
 */
public class IdNode implements ExpressionNode {

    private String field;
    private short widthBits = -1;
    private short divisionBits = -1;

    public IdNode() {
        // empty
    }

    public String getField() {
        return field;
    }

    public IdNode setField(String field) {
        this.field = field;
        return this;
    }

    public IdNode setWidthBits(short widthBits) {
        this.widthBits = widthBits;
        return this;
    }

    public IdNode setDivisionBits(short divisionBits) {
        this.divisionBits = divisionBits;
        return this;
    }

    @Override
    public BucketSet getBucketSet(BucketIdFactory factory) {
        return null;
    }

    @Override
    public Object evaluate(Context context) {
        DocumentId id = context.getDocumentOperation().getId();
        if (id == null) {
            throw new IllegalStateException("Document has no identifier.");
        }
        if (field == null) {
            return id.toString();
        } else if (field.equalsIgnoreCase("scheme")) {
            return id.getScheme().getType().toString();
        } else if (field.equalsIgnoreCase("namespace")) {
            return id.getScheme().getNamespace();
        } else if (field.equalsIgnoreCase("specific")) {
            return id.getScheme().getNamespaceSpecific();
        } else if (field.equalsIgnoreCase("group")) {
            if (id.getScheme().hasGroup()) {
                return id.getScheme().getGroup();
            }
            throw new IllegalStateException("Group identifier is null."); 
        } else if (field.equalsIgnoreCase("user")) {
            if (id.getScheme().hasNumber()) {
                return id.getScheme().getNumber();
            }
            throw new IllegalStateException("User identifier is null.");
        } else if (field.equalsIgnoreCase("type")) {
            if (id.getScheme().hasDocType()) {
                return id.getScheme().getDocType();
            }
            throw new IllegalStateException("Document id doesn't have doc type.");
        } else {
            throw new IllegalStateException("Identifier field '" + field + "' is not supported.");
        }
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    @Override
    public String toString() {
        return "id" + (field != null ? "." + field : "") + (widthBits != -1 ? "(" + widthBits + "," + divisionBits + ")" : "");
    }
}