aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/main/java/com/yahoo/vdslib/Entry.java
blob: 2ce3822f5a9ea0f8b41600e550fd6791f769d5ef (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vdslib;

import com.yahoo.document.DocumentOperation;
import com.yahoo.document.DocumentRemove;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.DocumentUpdate;

/**
 * Represents a document operation in a DocumentList, which can currently be
 * PUT, REMOVE and UPDATE.
 *
 * @author <a href="mailto:thomasg@yahoo-inc.com">Thomas Gundersen</a>, <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public abstract class Entry {

    protected Entry() { }

    /**
     * Creates a new entry from serialized form.
     *
     * @param docMan Documentmanager to use when deserializing
     * @param buffer the buffer to read the entry from
     * @param entryIndex the index of the entry in the buffer
     * @return an Entry reading from the buffer
     */
    public static Entry create(DocumentTypeManager docMan, byte[] buffer, int entryIndex) {
        return new BinaryEntry(docMan, buffer, entryIndex);
    }

    /**
     * Creates a new entry from a document operation.
     *
     * @param op the document in the entry
     * @param bodyStripped true if the document contains only header fields
     * @return an Entry for this document
     */
    public static Entry create(DocumentOperation op, boolean bodyStripped) {
        return new DynamicEntry(op, bodyStripped);
    }

    /**
     * Creates a new entry from a document operation.
     *
     * @param op the document in the entry
     * @return an Entry for this document
     */
    public static Entry create(DocumentOperation op) {
        return create(op, false);
    }
    /**
     * Creates a new entry from a document remove operation.
     *
     * @param doc the document in the entry
     * @return an Entry for this document
     */
    public static Entry create(DocumentRemove doc) {
        return new DynamicEntry(doc);
    }

    /**
     * Creates a new entry from a document update operation.
     *
     * @param doc the document update in the entry
     * @return an Entry for this document update
     */
    public static Entry create(DocumentUpdate doc) {
        return new DynamicEntry(doc);
    }

    /**
     * Entries in iterators gotten from DocumentList::end() are invalid.
     * @return true if valid
     */
    public abstract boolean valid();

    /**
     * Returns true if the Document represented by this entry has been removed from persistent storage.
     *
     * @return true if the Document has been removed
     */
    public abstract boolean isRemoveEntry();

    /**
     * Returns true if the Document represented by this entry has gotten its body fields stripped
     * away (note: the body fields might still be stored in persistent storage).
     *
     * @return true if the Document only has header fields
     */
    public abstract boolean isBodyStripped();

    /**
     * Returns true if this entry represents a document update operation.
     *
     * @return true if this is a document update operation
     */
    public abstract boolean isUpdateEntry();


    public int kind(){
        if (isRemoveEntry()) {
            return 0; //REMOVE
        }
        if (isUpdateEntry()) {
            return 2; //UPDATE
        }
        return 1; // PUT
    }

    /**
     * Returns the timestamp (last modified) of this entry, from persistent storage.
     * @return the last modified timestamp of this entry
     */
    public abstract long getTimestamp();

    /**
     * Returns the DocumentPut or DocumentUpdate operation in this entry.
     *
     * @return the DocumentOperation in this entry.
     */
    public abstract DocumentOperation getDocumentOperation();

    /**
     * Returns the Document header (if this is a DocumentPut or a DocumentRemove operation), otherwise
     * a DocumentUpdate operation.
     *
     * @return a DocumentPut operation containing a Document with only the header fields present
     * @throws RuntimeException if deserialization fails, or if this is a DocumentUpdate operation
     */
    public abstract DocumentOperation getHeader();

    @Override
    public boolean equals(Object obj) {
        if ( this == obj ) {
            return true;
        }
        if (!(obj instanceof Entry)) {
            return false;
        }
        Entry entry = (Entry) obj;
        return this.getDocumentOperation().getId().equals(entry.getDocumentOperation().getId()) &&
                this.getTimestamp() == entry.getTimestamp() &&
                this.kind() == entry.kind() &&
                this.isBodyStripped() == entry.isBodyStripped() &&
                this.valid() == entry.valid();
    }

    @Override
    public int hashCode() {
        int res = 31;
        res = 31 * res + getDocumentOperation().getId().hashCode();
        res = (int) (31 * res + getTimestamp());
        res = 31 * res + kind()*31;
        res = 31 * res + (isBodyStripped() ? 17 : 249);
        res = 31 * res + (valid() ? 333 : 31);

        return res;
    }
}