aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/main/java/com/yahoo/vdslib/BinaryEntry.java
blob: c11bb009a73350a0232dbe6cf2d92b20ac72782d (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
// 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.*;
import com.yahoo.document.serialization.DocumentDeserializer;
import com.yahoo.document.serialization.DocumentDeserializerFactory;
import com.yahoo.io.GrowableByteBuffer;

/**
 * An entry in serialized form.
 *
 * @author <a href="mailto:thomasg@yahoo-inc.com">Thomas Gundersen</a>, <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
class BinaryEntry extends Entry {
    private MetaEntry metaEntry;
    private byte[] buffer;
    private DocumentTypeManager docMan;

    /**
     * Creates an entry from serialized form.
     * @param docMan The documentmanager to use when deserializing.
     * @param buffer the buffer to read the entry from
     * @param entryIndex the index of the entry in the buffer
     */
    BinaryEntry(DocumentTypeManager docMan, byte[] buffer, int entryIndex) {
        this.buffer = buffer;
        metaEntry = new MetaEntry(buffer, 4 + entryIndex * MetaEntry.SIZE);
        this.docMan = docMan;
    }

    @Override
    public boolean valid() { return buffer != null; }

    @Override
    public boolean isRemoveEntry() { return (metaEntry.flags & MetaEntry.REMOVE_ENTRY) != 0; }

    @Override
    public boolean isBodyStripped() { return (metaEntry.flags & MetaEntry.BODY_STRIPPED) != 0; }

    @Override
    public boolean isUpdateEntry() { return (metaEntry.flags & MetaEntry.UPDATE_ENTRY) != 0; }

    @Override
    public long getTimestamp() { return metaEntry.timestamp; }

    @Override
    public DocumentOperation getDocumentOperation() {
        DocumentDeserializer buf = DocumentDeserializerFactory.create42(
                docMan,
                GrowableByteBuffer.wrap(buffer, metaEntry.headerPos, metaEntry.headerLen),
                (metaEntry.bodyLen > 0) ? GrowableByteBuffer.wrap(buffer, metaEntry.bodyPos, metaEntry.bodyLen) : null
        );

        DocumentOperation op;

        if ((metaEntry.flags & MetaEntry.UPDATE_ENTRY) != 0) {
            op = new DocumentUpdate(buf);
        } else if ((metaEntry.flags & MetaEntry.REMOVE_ENTRY) != 0) {
            op = new DocumentRemove(new Document(buf).getId());
        } else {
            op = new DocumentPut(new Document(buf));
            ((DocumentPut) op).getDocument().setLastModified(getTimestamp());

        }
        return op;
    }

    @Override
    public DocumentOperation getHeader() {
        DocumentDeserializer buf = DocumentDeserializerFactory.create42(docMan, GrowableByteBuffer.wrap(buffer, metaEntry.headerPos, metaEntry.headerLen));
        if ((metaEntry.flags & MetaEntry.UPDATE_ENTRY) != 0) {
	        return new DocumentUpdate(buf);
        } else if ((metaEntry.flags & MetaEntry.REMOVE_ENTRY) != 0) {
            return new DocumentRemove(new Document(buf).getId());
	    } else {
            DocumentPut op = new DocumentPut(new Document(buf));
            op.getDocument().setLastModified(getTimestamp());
            return op;
        }
    }

}