aboutsummaryrefslogtreecommitdiffstats
path: root/persistence/src/main/java/com/yahoo/persistence/spi/Selection.java
blob: 4bcf75fa322cd5a8dd02ec69e959880184a51406 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.persistence.spi;

import com.yahoo.document.Document;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.select.DocumentSelector;
import com.yahoo.document.select.Result;
import com.yahoo.document.select.parser.ParseException;

import java.util.Set;

/**
 * Class used when iterating to represent a selection of entries to be returned.
 *
 * This class is likely to be replaced by a more generic selection AST in the near future.
 */
public class Selection {
    DocumentSelector documentSelection = null;
    long fromTimestamp = 0;
    long toTimestamp = Long.MAX_VALUE;
    Set<Long> timestampSubset = null;

    public Selection(String documentSelection, long fromTimestamp, long toTimestamp) throws ParseException {
        this.documentSelection = new DocumentSelector(documentSelection);
        this.fromTimestamp = fromTimestamp;
        this.toTimestamp = toTimestamp;
    }

    public Selection(Set<Long> timestampSubset) {
        this.timestampSubset = timestampSubset;
    }

    public boolean requiresFields() {
        return documentSelection != null;
    }

    public Set<Long> getTimestampSubset() {
        return timestampSubset;
    }

    /**
     * Returns true if the entry matches the selection criteria given.
     */
    public boolean match(Document doc, long timestamp) {
        if (timestamp < fromTimestamp) {
            return false;
        }

        if (timestamp > toTimestamp) {
            return false;
        }

        if (timestampSubset != null && !timestampSubset.contains(timestamp)) {
            return false;
        }

        if (documentSelection != null && doc != null && !documentSelection.accepts(new DocumentPut(doc)).equals(Result.TRUE)) {
            return false;
        }

        return true;
    }

    /**
     * Returns true if the entry matches the timestamp ranges/subsets specified in the selection.
     */
    public boolean match(long timestamp) {
        return match(null, timestamp);
    }
}