1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */1819package org.apache.giraph.ooc.persistence;
2021import java.util.ArrayList;
22import java.util.List;
2324/**25 * Index chain used in out-of-core data accessor object (DAO) to access26 * serialized data.27 */28publicclassDataIndex {
29/** Chain of data indices */30privatefinal List<DataIndexEntry> indexList = new ArrayList<>(5);
3132/**33 * Add an index to the index chain34 *35 * @param entry the entry to add to the chain36 * @return the index chain itself37 */38publicDataIndex addIndex(DataIndexEntry entry) {
39 indexList.add(entry);
40returnthis;
41 }
4243/**44 * Remove/Pop the last index in the index chain45 *46 * @return the index chain itself47 */48publicDataIndex removeLastIndex() {
49 indexList.remove(indexList.size() - 1);
50returnthis;
51 }
5253/**54 * Create a copy of the existing DataIndex55 *56 * @return a copy of the existing index chain57 */58publicDataIndex copy() {
59DataIndex index = newDataIndex();
60for (DataIndexEntry entry : indexList) {
61 index.indexList.add(entry);
62 }
63return index;
64 }
6566 @Override
67publicboolean equals(Object obj) {
68if (!(obj instanceof DataIndex)) {
69return false;
70 }
71DataIndex dataIndex = (DataIndex) obj;
72return indexList.equals(dataIndex.indexList);
73 }
7475 @Override
76publicint hashCode() {
77return indexList.hashCode();
78 }
7980 @Override
81public String toString() {
82 StringBuffer sb = new StringBuffer();
83for (DataIndexEntry entry : indexList) {
84 sb.append(entry);
85 }
86return sb.toString();
87 }
8889/** Interface to unify different types of entries used as index chain */90publicinterfaceDataIndexEntry { }
9192/**93 * Different static types of index chain entry94 */95public enum TypeIndexEntryimplementsDataIndexEntry {
96/** The whole partition */97 PARTITION("_partition"),
98/** Partition vertices */99 PARTITION_VERTICES("_vertices"),
100/** Partition edges */101 PARTITION_EDGES("_edges"),
102/** Partition messages */103 MESSAGE("_messages"),
104/** Edges stored in edge store for a partition */105 EDGE_STORE("_edge_store"),
106/** Raw data buffer (refer to DiskBackedDataStore) */107 BUFFER("_buffer");
108109/** String realization of entry type */110privatefinal String name;
111112/**113 * Constructor114 *115 * @param name name of the type116 */117TypeIndexEntry(String name) {
118this.name = name;
119 }
120121public String getName() {
122return name;
123 }
124125 @Override
126public String toString() {
127return name;
128 }
129 }
130131/**132 * Class representing any index chain that depends on something with id.133 * Generally this is used for identifying indices in two types:134 * - Index entry based on superstep id ('S' and the superstep number)135 * - Index entry based on partition id ('P' and the partition id)136 */137publicstaticfinalclassNumericIndexEntryimplementsDataIndexEntry {
138/** Type of index */139privatefinalchar type;
140/** Id of the index associated with the specified type */141privatefinallong id;
142143/**144 * Constructor145 *146 * @param type type of index (for now 'S' for superstep, or 'P' for147 * partition)148 * @param id id of the index associated with the given type149 */150privateNumericIndexEntry(char type, long id) {
151this.type = type;
152this.id = id;
153 }
154155 @Override
156publicboolean equals(Object obj) {
157if (!(obj instanceof NumericIndexEntry)) {
158return false;
159 }
160NumericIndexEntry index = (NumericIndexEntry) obj;
161return index.type == type && index.id == id;
162 }
163164 @Override
165publicint hashCode() {
166int result = 17;
167 result = result * 37 + type;
168 result = result * 37 + (int) id;
169 result = result * 37 + (int) (id >> 32);
170return result;
171 }
172173 @Override
174public String toString() {
175return String.format("_%c%d", type, id);
176 }
177178/**179 * Create a data index entry for a given partition180 *181 * @param partitionId id of the partition182 * @return data index entry for a given partition183 */184publicstaticNumericIndexEntry createPartitionEntry(int partitionId) {
185returnnewNumericIndexEntry('P', partitionId);
186 }
187188/**189 * Create a data index entry for a given superstep190 *191 * @param superstep the superstep number192 * @return data index entry for a given superstep193 */194publicstaticNumericIndexEntry createSuperstepEntry(long superstep) {
195returnnewNumericIndexEntry('S', superstep);
196 }
197 }
198 }