aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/prelude/cache/QueryCacheKey.java
blob: d885422ce57ddbf7021eb1dddc1e6539760c2edb (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.cache;

import com.yahoo.search.Query;

public class QueryCacheKey {
    private Query query;
    private int offset;
    private int hits;

    public QueryCacheKey(Query query) {
        this.query = query;
        this.offset = query.getOffset();
        this.hits = query.getHits();
    }

    public boolean equals(Object key) {
        if (key==null) {
            return false;
        }
        if (query==null) {
            return false;
        }
        if (key instanceof QueryCacheKey) {
            QueryCacheKey ckey = (QueryCacheKey)key;
            boolean res = equalQueryWith(ckey) && equalPathWith(ckey);
            return res;
        }
        return false;
    }

    private boolean equalQueryWith(QueryCacheKey other) {
        return query.equals(other.getQuery());
    }

    private boolean equalPathWith(QueryCacheKey other) {
        if (other == null) return false;
        if (other.getQuery() == null) return false;

        return query.getHttpRequest().getUri().getPath().equals(other.getQuery().getHttpRequest().getUri().getPath());
    }

    public int getHits() {
        return hits;
    }

    public int getOffset() {
        return offset;
    }

    public Query getQuery() {
        return query;
    }

    public void setQuery(Query newQuery) {
        query = newQuery;
    }

    public String toString() {
        if (query==null) {
            return super.toString();
        }
        return query.toString();
    }

    public int hashCode() {
        if (query==null) {
            return super.hashCode();
        }
        int ret = query.hashCode();
        return ret;
    }
}