aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/idstring/OrderDocIdString.java
blob: 111be0110b58cbfa040dafd425e9a52c86c0f7f9 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.idstring;

import com.yahoo.collections.MD5;
import com.yahoo.text.Utf8;

import java.security.MessageDigest;

/**
 * Representation of groupdoc scheme in document IDs.
 *
 * @author <a href="mailto:humbe@yahoo-inc.com">H&aring;kon Humberset</a>
 */
public class OrderDocIdString extends IdString {
    String group;
    int widthBits;
    int divisionBits;
    long ordering;
    long location;

    /**
     * Create a groupdoc scheme object.
     * <code>groupdoc:&lt;namespace&gt;:&lt;group&gt;:&lt;namespaceSpecific&gt;</code>
     *
     * @param namespace The namespace of this document id.
     * @param group The groupname of this groupdoc id.
     * @param widthBits The number of bits used for the width of the data set
     * @param divisionBits The number of bits used for the smalles partitioning of the data set
     * @param ordering A value used to order documents of this type.
     * @param namespaceSpecific The namespace specific part.
     */
    public OrderDocIdString(String namespace, String group, int widthBits, int divisionBits, long ordering, String namespaceSpecific) {
        super(Scheme.orderdoc, namespace, namespaceSpecific);
        this.group = group;
        this.widthBits = widthBits;
        this.divisionBits = divisionBits;
        this.ordering = ordering;

        try {
            this.location = Long.parseLong(group);
        } catch (Exception foo) {
            location = 0;
            byte[] md5sum = MD5.md5.get().digest(Utf8.toBytes(group));
            for (int i=0; i<8; ++i) {
                location |= (md5sum[i] & 0xFFl) << (8*i);
            }
        }
    }

    /**
     * Get the location of this document id. The location is used for distribution
     * in clusters. For the orderdoc scheme, the location is a hash of the groupname or just the number specified.
     *
     * @return The 64 bit location.
     */
    public long getLocation() {
        return location;
    }

    public String getSchemeParameters() {
        return "(" + widthBits + "," + divisionBits + ")";
    }

    /** Get the scheme specific part. */
    public String getSchemeSpecific() {
        return group + ":" + ordering + ":";
    }

    public GidModifier getGidModifier() {
        GidModifier gm = new GidModifier();
        gm.usedBits = widthBits - divisionBits;
        long gidBits = (ordering << (64 - widthBits));
        gidBits = Long.reverse(gidBits);
        long gidMask = (0xFFFFFFFFFFFFFFFFl >>> (64 - gm.usedBits));
        gidBits &= gidMask;
        gm.value = gidBits;
        return gm;
    }

    @Override
    public boolean hasGroup() {
        return true;
    }

    /** @return Get the groupname of this id. */
    @Override
    public String getGroup() {
        return group;
    }

    @Override
    public boolean hasNumber() {
        return true;
    }

    @Override
    public long getNumber() {
        return location;
    }

    public long getUserId() {
        return location;
    }

    public int getWidthBits() {
        return widthBits;
    }

    public int getDivisionBits() {
        return divisionBits;
    }

    public long getOrdering() {
        return ordering;
    }
}