aboutsummaryrefslogtreecommitdiffstats
path: root/vdslib/src/main/java/com/yahoo/vdslib/DocumentList.java
blob: 76d320102e388ff82f4837ab903dbddcffa9a8a6 (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
// 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.DocumentId;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.vespa.objects.Serializer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class DocumentList {

    protected DocumentList() { }

    /**
     * Creates a DocumentList from serialized form.
     *
     * @param docMan Documentmanager to use when deserializing
     * @param buffer the buffer to read from
     * @return a DocumentList instance
     */
    public static DocumentList create(DocumentTypeManager docMan, byte[] buffer) {
        return new BinaryDocumentList(docMan, buffer);
    }

    /**
     * Creates a DocumentList from a list of entries.
     * @param entries the entries to create a DocumentList from
     * @return a DocumentList instance
     * @see com.yahoo.vdslib.Entry
     */
    public static DocumentList create(List<Entry> entries) {
        return new DynamicDocumentList(entries);
    }

    /**
     * Creates a DocumentList containing a single entry.
     *
     * @param entry the entry to create a DocumentList from
     * @return a DocumentList instance
     * @see com.yahoo.vdslib.Entry
     */
    public static DocumentList create(Entry entry) {
        return new DynamicDocumentList(entry);
    }

    /**
     * Retrieves the specified Entry from the list.
     *
     * @param index the index of the Entry to return (0-based)
     * @return the entry at the specified position
     * @throws ArrayIndexOutOfBoundsException if index is &lt; 0 or &gt; size()
     * @throws com.yahoo.document.serialization.DeserializationException if the DocumentList is stored in binary form internally and deserialization fails
     */
    public abstract Entry get(int index) throws ArrayIndexOutOfBoundsException;

    /**
     * Returns the size of the list.
     *
     * @return the size of the list
     */
    public abstract int size();

    /**
     * Returns the byte size of the list. The value returned is exact if the list is stored in
     * binary form internally, otherwise it is approximate.
     *
     * @return the byte size of the list
     */
    public abstract int getApproxByteSize();

    /**
     * Serialize the list into the given buffer.
     *
     * @param buf the buffer to serialize into
     */
    public abstract void serialize(Serializer buf);

    /**
     * Test if a contains b
     *
     * @param list DocumentList contained
     * @return true if a contains b
     */
    public boolean containsAll(DocumentList list) {
        if( this.size() < list.size()) {
            return false;
        }

        Map<DocumentId, Integer> indexes = new HashMap<DocumentId, Integer>();
        for (int i=0; i<this.size(); ++i) {
            indexes.put(this.get(i).getDocumentOperation().getId(), i);
        }
        for (int i=0; i<list.size(); ++i) {
            Integer index = indexes.get(list.get(i).getDocumentOperation().getId());
            if (index == null ||
                    list.get(i).getTimestamp() != this.get(index).getTimestamp() ||
                    list.get(i).kind() != this.get(index).kind())
            {
                return false;
            }
        }
        return true;
    }

}