aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/xml/DocprocOptionsBuilder.java
blob: e0dffca2bc526f587a8c3b85fac46d9aa2e3d344 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.vespa.model.container.docproc.ContainerDocproc;
import org.w3c.dom.Element;

/**
 * Extracted from DomDocProcClusterBuilder
 */
public class DocprocOptionsBuilder {
    public static ContainerDocproc.Options build(Element spec) {
        return new ContainerDocproc.Options(
                getMaxMessagesInQueue(spec),
                getSizeInMegabytes(spec.getAttribute("maxqueuebytesize")),
                getTime(spec.getAttribute("maxqueuewait")),
                getFactor(spec.getAttribute("maxconcurrentfactor")),
                getFactor(spec.getAttribute("documentexpansionfactor")),
                getInt(spec.getAttribute("containercorememory")));
    }

    private static Integer getInt(String integer) {
        return integer == null || integer.trim().isEmpty() ?
                null:
                Integer.parseInt(integer);
    }

    private static Double getFactor(String factor) {
        return factor == null || factor.trim().isEmpty() ?
                null :
                Double.parseDouble(factor);
    }


    private static Integer getMaxMessagesInQueue(Element spec) {
        // get max queue size (number of messages), if set
        Integer maxMessagesInQueue = null;
        if (spec.hasAttribute("maxmessagesinqueue")) {
            maxMessagesInQueue = Integer.valueOf(spec.getAttribute("maxmessagesinqueue"));
        }
        return maxMessagesInQueue;
    }

    private static Integer getSizeInMegabytes(String size) {
        if (size == null) {
            return null;
        }
        size = size.trim();
        if (size.isEmpty()) {
            return null;
        }

        Integer megabyteSize;
        if (size.endsWith("m")) {
            size = size.substring(0, size.length() - 1);
            megabyteSize = Integer.parseInt(size);
        } else if (size.endsWith("g")) {
            size = size.substring(0, size.length() - 1);
            megabyteSize = Integer.parseInt(size) * 1024;
        } else {
            throw new IllegalArgumentException("Heap sizes for docproc must be set to Xm or Xg, where X is an integer specifying megabytes or gigabytes, respectively.");
        }
        return megabyteSize;
    }

    private static Integer getTime(String intStr) {
        if (intStr == null) {
            return null;
        }
        intStr = intStr.trim();
        if (intStr.isEmpty()) {
            return null;
        }

        return 1000 * (int)Double.parseDouble(intStr);
    }
}