1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * 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 and
16 * limitations under the License.
17 */
18
19 package org.apache.giraph.ooc.persistence;
20
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24
25 /**
26 * Interface representing data accessor object (DAO) used as persistence layer
27 * in out-of-core mechanism.
28 * Note: any class implementing this interface should have one and only one
29 * constructor taking one and only one argument of type
30 * <code>ImmutableClassesGiraphConfiguration</code>
31 */
32 public interface OutOfCoreDataAccessor {
33 /** Initialize the DAO */
34 void initialize();
35
36 /** Shut down the DAO */
37 void shutdown();
38
39 /**
40 * @return the number of threads involved in data persistence
41 */
42 int getNumAccessorThreads();
43
44 /**
45 * Prepare a wrapper containing <code>DataInput</code> representation for a
46 * given thread involved in persistence for a given index chain for data.
47 *
48 * @param threadId id of the thread involved in persistence
49 * @param index index chain of the data to access the serialized data form
50 * @return the wrapper for <code>DataInput</code> representation of data
51 * @throws IOException
52 */
53 DataInputWrapper prepareInput(int threadId, DataIndex index)
54 throws IOException;
55
56 /**
57 * Prepare a wrapper containing <code>DataOutput</code> representation for a
58 * given thread involved in persistence for a given index chain for data.
59 *
60 * @param threadId id of the thread involved in persistence
61 * @param index index chain of the data to access the serialized data form
62 * @param shouldAppend whether the <code>DataOutput</code> should be used for
63 * appending to already existing data for the given index
64 * or the <code>DataOutput</code> should create new
65 * instance to store serialized data
66 * @return the wrapper for <code>DataOutput</code> representation of data
67 * @throws IOException
68 */
69 DataOutputWrapper prepareOutput(int threadId, DataIndex index,
70 boolean shouldAppend) throws IOException;
71
72 /**
73 * Whether the data for the given thread and index chain exists?
74 *
75 * @param threadId id of the thread involved in persistence
76 * @param index index chain used to access the data
77 * @return True if the data exists for the given index chain for the given
78 * thread, False otherwise
79 */
80 boolean dataExist(int threadId, DataIndex index);
81
82 /** Interface to wrap <code>DataInput</code> */
83 interface DataInputWrapper {
84 /**
85 * @return the <code>DataInput</code>, should return the same instance
86 * every time it's called (not start from the beginning)
87 */
88 DataInput getDataInput();
89
90 /**
91 * Finalize and close the <code>DataInput</code> used for persistence.
92 *
93 * @param deleteOnClose whether the source of <code>DataInput</code>
94 * should be deleted on closing/finalizing
95 * @return number of bytes read from <code>DataInput</code> since it was
96 * opened
97 */
98 long finalizeInput(boolean deleteOnClose);
99 }
100
101 /** Interface to warp <code>DataOutput</code> */
102 interface DataOutputWrapper {
103 /**
104 * @return the <code>DataOutput</code>
105 */
106 DataOutput getDataOutput();
107
108 /**
109 * Finalize and close the <code>DataOutput</code> used for persistence.
110 *
111 * @return number of bytes written to <code>DataOutput</code> since it was
112 * opened
113 */
114 long finalizeOutput();
115 }
116 }