summaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/select/rule/IdNode.java
blob: 75b4890af8064d816c5f46ceb91bb0fcf640c837 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2016 Yahoo Inc. 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.*;
import com.yahoo.document.idstring.*;

/**
 * @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
 */
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;
    }

    public short getWidthBits() {
        return widthBits;
    }

    public short getDivisionBits() {
        return divisionBits;
    }

    // Inherit doc from ExpressionNode.
    public BucketSet getBucketSet(BucketIdFactory factory) {
        return null;
    }

    public OrderingSpecification getOrdering(int ordering) {
        return null;
    }

    // Inherit doc from ExpressionNode.
    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 if (field.equalsIgnoreCase("order")) {
            if (id.getScheme() instanceof OrderDocIdString) {
                OrderDocIdString ods = (OrderDocIdString)id.getScheme();
                if (ods.getWidthBits() == widthBits && ods.getDivisionBits() == divisionBits) {
                    return ods.getOrdering();
                }
            }
        } else{
            throw new IllegalStateException("Identifier field '" + field + "' is not supported.");
        }
        return null;
    }

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

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