This project has retired. For details please refer to its Attic page.
GoraUtils xref
View Javadoc

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  package org.apache.giraph.io.gora.utils;
19  
20  import org.apache.gora.persistency.Persistent;
21  import org.apache.gora.query.Query;
22  import org.apache.gora.query.Result;
23  import org.apache.gora.query.impl.QueryBase;
24  import org.apache.gora.store.DataStore;
25  import org.apache.gora.store.DataStoreFactory;
26  import org.apache.gora.util.GoraException;
27  import org.apache.hadoop.conf.Configuration;
28  
29  /**
30   * Class used to handle the creation and querying of data stores through Gora.
31   */
32  public class GoraUtils {
33  
34    /**
35     * Attribute handling the specific class to be created.
36     */
37    private static Class<? extends DataStore> DATASTORECLASS;
38  
39    /**
40     * The default constructor is set to be private by default so that the
41     * class is not instantiated.
42     */
43    private GoraUtils() { /* private constructor */ }
44  
45    /**
46     * Creates a generic data store using the data store class.
47     * set using the class property
48     * @param conf Configuration
49     * @param <K> key class
50     * @param <T> value class
51     * @param keyClass key class used
52     * @param persistentClass persistent class used
53     * @return created data store
54     * @throws GoraException exception threw
55     */
56    @SuppressWarnings("unchecked")
57    public static <K, T extends Persistent> DataStore<K, T>
58    createDataStore(Configuration conf,
59        Class<K> keyClass, Class<T> persistentClass)
60      throws GoraException {
61      DataStoreFactory.createProps();
62      DataStore<K, T> dataStore =
63          DataStoreFactory.createDataStore((Class<? extends DataStore<K, T>>)
64                                            DATASTORECLASS,
65                                            keyClass, persistentClass,
66                                            conf);
67  
68      return dataStore;
69    }
70  
71    /**
72     * Creates a specific data store specified by.
73     * @param conf Configuration
74     * @param <K> key class
75     * @param <T> value class
76     * @param dataStoreClass  Defines the type of data store used.
77     * @param keyClass  Handles the key class to be used.
78     * @param persistentClass Handles the persistent class to be used.
79     * @return DataStore created using parameters passed.
80     * @throws GoraException  if an error occurs.
81     */
82    public static <K, T extends Persistent> DataStore<K, T>
83    createSpecificDataStore(Configuration conf,
84        Class<? extends DataStore> dataStoreClass,
85        Class<K> keyClass, Class<T> persistentClass) throws GoraException {
86      DATASTORECLASS = dataStoreClass;
87      return createDataStore(conf, keyClass, persistentClass);
88    }
89  
90    /**
91     * Performs a range query to Gora datastores
92     * @param <K> key class
93     * @param <T> value class
94     * @param pDataStore  data store being used.
95     * @param pStartKey start key for the range query.
96     * @param pEndKey end key for the range query.
97     * @return Result containing all results for the query.
98     */
99    public static <K, T extends Persistent> Result<K, T>
100   getRequest(DataStore<K, T> pDataStore, K pStartKey, K pEndKey) {
101     QueryBase query = getQuery(pDataStore, pStartKey, pEndKey);
102     return getRequest(pDataStore, query);
103   }
104 
105   /**
106    * Performs a query to Gora datastores
107    * @param pDataStore data store being used.
108    * @param query query executed over data stores.
109    * @param <K> key class
110    * @param <T> value class
111    * @return Result containing all results for the query.
112    */
113   public static <K, T extends Persistent> Result<K, T>
114   getRequest(DataStore<K, T> pDataStore, Query<K, T> query) {
115     return pDataStore.execute(query);
116   }
117 
118   /**
119    * Performs a range query to Gora datastores
120    * @param <K> key class
121    * @param <T> value class
122    * @param pDataStore  data store being used.
123    * @param pStartKey start key for the range query.
124    * @return  Result containing all results for the query.
125    */
126   public static <K, T extends Persistent> Result<K, T>
127   getRequest(DataStore<K, T> pDataStore, K pStartKey) {
128     return getRequest(pDataStore, pStartKey, null);
129   }
130 
131   /**
132    * Gets a query object to be used as a range query.
133    * @param pDataStore data store used.
134    * @param pStartKey range start key.
135    * @param pEndKey range end key.
136    * @param <K> key class
137    * @param <T> value class
138    * @return range query object.
139    */
140   public static <K, T extends Persistent> QueryBase
141   getQuery(DataStore pDataStore, K pStartKey, K pEndKey) {
142     QueryBase query = (QueryBase) pDataStore.newQuery();
143     query.setStartKey(pStartKey);
144     query.setEndKey(pEndKey);
145     return query;
146   }
147 
148   /**
149    * Gets a query object to be used as a simple get.
150    * @param pDataStore data store used.
151    * @param pStartKey range start key.
152    * @param <K> key class
153    * @param <T> value class
154    * @return query object.
155    */
156   public static <K, T extends Persistent> Query<K, T>
157   getQuery(DataStore<K, T> pDataStore, K pStartKey) {
158     Query<K, T> query = pDataStore.newQuery();
159     query.setStartKey(pStartKey);
160     query.setEndKey(null);
161     return query;
162   }
163 
164   /**
165    * Gets a query object to be used as a simple get.
166    * @param pDataStore data store used.
167    * @param <K> key class
168    * @param <T> value class
169    * @return query object.
170    */
171   public static <K, T extends Persistent> Query<K, T>
172   getQuery(DataStore<K, T> pDataStore) {
173     Query<K, T> query = pDataStore.newQuery();
174     query.setStartKey(null);
175     query.setEndKey(null);
176     return query;
177   }
178 }