1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.giraph.ooc.persistence;
20
21 import java.util.ArrayList;
22 import java.util.List;
23
24
25
26
27
28 public class DataIndex {
29
30 private final List<DataIndexEntry> indexList = new ArrayList<>(5);
31
32
33
34
35
36
37
38 public DataIndex addIndex(DataIndexEntry entry) {
39 indexList.add(entry);
40 return this;
41 }
42
43
44
45
46
47
48 public DataIndex removeLastIndex() {
49 indexList.remove(indexList.size() - 1);
50 return this;
51 }
52
53
54
55
56
57
58 public DataIndex copy() {
59 DataIndex index = new DataIndex();
60 for (DataIndexEntry entry : indexList) {
61 index.indexList.add(entry);
62 }
63 return index;
64 }
65
66 @Override
67 public boolean equals(Object obj) {
68 if (!(obj instanceof DataIndex)) {
69 return false;
70 }
71 DataIndex dataIndex = (DataIndex) obj;
72 return indexList.equals(dataIndex.indexList);
73 }
74
75 @Override
76 public int hashCode() {
77 return indexList.hashCode();
78 }
79
80 @Override
81 public String toString() {
82 StringBuffer sb = new StringBuffer();
83 for (DataIndexEntry entry : indexList) {
84 sb.append(entry);
85 }
86 return sb.toString();
87 }
88
89
90 public interface DataIndexEntry { }
91
92
93
94
95 public enum TypeIndexEntry implements DataIndexEntry {
96
97 PARTITION("_partition"),
98
99 PARTITION_VERTICES("_vertices"),
100
101 PARTITION_EDGES("_edges"),
102
103 MESSAGE("_messages"),
104
105 EDGE_STORE("_edge_store"),
106
107 BUFFER("_buffer");
108
109
110 private final String name;
111
112
113
114
115
116
117 TypeIndexEntry(String name) {
118 this.name = name;
119 }
120
121 public String getName() {
122 return name;
123 }
124
125 @Override
126 public String toString() {
127 return name;
128 }
129 }
130
131
132
133
134
135
136
137 public static final class NumericIndexEntry implements DataIndexEntry {
138
139 private final char type;
140
141 private final long id;
142
143
144
145
146
147
148
149
150 private NumericIndexEntry(char type, long id) {
151 this.type = type;
152 this.id = id;
153 }
154
155 @Override
156 public boolean equals(Object obj) {
157 if (!(obj instanceof NumericIndexEntry)) {
158 return false;
159 }
160 NumericIndexEntry index = (NumericIndexEntry) obj;
161 return index.type == type && index.id == id;
162 }
163
164 @Override
165 public int hashCode() {
166 int result = 17;
167 result = result * 37 + type;
168 result = result * 37 + (int) id;
169 result = result * 37 + (int) (id >> 32);
170 return result;
171 }
172
173 @Override
174 public String toString() {
175 return String.format("_%c%d", type, id);
176 }
177
178
179
180
181
182
183
184 public static NumericIndexEntry createPartitionEntry(int partitionId) {
185 return new NumericIndexEntry('P', partitionId);
186 }
187
188
189
190
191
192
193
194 public static NumericIndexEntry createSuperstepEntry(long superstep) {
195 return new NumericIndexEntry('S', superstep);
196 }
197 }
198 }