forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericDao.java
More file actions
executable file
·280 lines (223 loc) · 8.13 KB
/
GenericDao.java
File metadata and controls
executable file
·280 lines (223 loc) · 8.13 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.utils.db;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.naming.ConfigurationException;
import com.cloud.utils.Pair;
/**
* a uniform method for persisting and finding db entities.
**/
public interface GenericDao<T, ID extends Serializable> {
/**
*/
static final String REMOVED_COLUMN = "cloud_removed";
static final String REMOVED = "removed";
/**
* This column can be used if the table wants to track creation time.
*/
static final String CREATED_COLUMN = "created";
/**
*/
static final String XID_COLUMN = "xid";
/**
* Look for an entity bean using the database id. Does not lock the row.
* @param id database unique id for the entity bean.
* @return entity bean.
**/
T findById(ID id);
T findByIdIncludingRemoved(ID id);
T findById(ID id, boolean fresh);
// Finds one unique VO using uuid
T findByUuid(String uuid);
// Finds one unique VO using uuid including removed entities
T findByUuidIncludingRemoved(String uuid);
/**
* @return VO object ready to be used for update. It won't have any fields filled in.
*/
T createForUpdate();
SearchBuilder<T> createSearchBuilder();
<K> GenericSearchBuilder<T, K> createSearchBuilder(Class<K> clazz);
T createForUpdate(ID id);
/**
* Returns a SearchCriteria object that can be used to build search conditions.
*
* @return SearchCriteria
*/
SearchCriteria<T> createSearchCriteria();
/**
* lock the rows that matched the search criteria and filter. This method needs
* to be called within a transaction.
*
* @param sc SearchCriteria containing the different search conditions
* @param filter Filter containing limits etc
* @param exclusive exclusive or share lock
* @return List<T> list of entity beans
*/
List<T> lockRows(SearchCriteria<T> sc, Filter filter, boolean exclusive);
/**
* lock 1 of the return set. This method needs to be run within a
* transaction or else it's useless.
* @param sc
* @param exclusive
* @return T if found and locked. null if not.
*/
T lockOneRandomRow(SearchCriteria<T> sc, boolean exclusive);
/**
* Find and lock the row for update.
* @param id id
* @param exclusive is this a read share lock or exclusive lock?
* @return T
*/
T lockRow(ID id, Boolean exclusive);
/**
* Acquires a database wide lock on the id of the entity. This ensures
* that only one is being used. The timeout is the configured default.
*
* @param id id of the entity to acquire an lock on.
* @return object if acquired; null if not. If null, you need to call findById to see if it is actually not found.
*/
T acquireInLockTable(ID id);
/**
* Acquires a database wide lock on the id of the entity. This ensures
* that only one is being used. The timeout is the configured default.
*
* @param id id of the entity to acquire an lock on.
* @param seconds time to wait for the lock.
* @return entity if the lock is acquired; null if not.
*/
T acquireInLockTable(ID id, int seconds);
/**
* releases the lock acquired in the acquire method call.
* @param id id of the entity to release the lock on.
* @return true if it is released. false if not or not found.
*/
boolean releaseFromLockTable(final ID id);
boolean update(ID id, T entity);
/**
* Look for all active rows.
* @return list of entity beans.
*/
List<T> listAll();
/**
* Look for all active rows.
* @param filter filter to limit the results
* @return list of entity beans.
*/
List<T> listAll(Filter filter);
/**
* Search for the entity beans
* @param sc
* @param filter
* @return list of entity beans.
*/
List<T> search(SearchCriteria<T> sc, Filter filter);
/**
* Search for the entity beans using the sql SQL_CACHE option
* @param sc
* @param filter
* @param enable_query_cache
* @return list of entity beans.
*/
List<T> search(SearchCriteria<T> sc, Filter filter, final boolean enable_query_cache);
List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache);
List<T> searchIncludingRemoved(SearchCriteria<T> sc, final Filter filter, final Boolean lock, final boolean cache, final boolean enable_query_cache);
/**
* Customized search with SearchCritiria
* @param sc
* @param filter
* @return list of entity beans.
*/
public <M> List<M> customSearchIncludingRemoved(SearchCriteria<M> sc, Filter filter);
/**
* Retrieves the entire table.
* @return collection of entity beans.
**/
List<T> listAllIncludingRemoved();
/**
* Retrieves the entire table.
* @param filter filter to limit the returns.
* @return collection of entity beans.
**/
List<T> listAllIncludingRemoved(Filter filter);
/**
* Persist the entity bean. The id field of the entity is updated with
* the new id.
* @param entity the bean to persist.
* @return The persisted version of the object. A null is returned if
* there's no primary key specified in the VO object.
**/
T persist(T entity);
/**
* remove the entity bean. This will call delete automatically if
* the entity bean does not have a removed field.
* @param id
* @return true if removed.
*/
boolean remove(ID id);
/**
* Remove based on the search criteria. This will delete if the VO object
* does not have a REMOVED column.
* @param sc search criteria to match
* @return rows removed.
*/
int remove(SearchCriteria<T> sc);
/**
* Expunge actually delete the row even if it's REMOVED.
* @param id
* @return true if removed.
*/
boolean expunge(ID id);
/**
* remove the entity bean specified by the search criteria
* @param sc
* @return number of rows deleted
*/
int expunge(final SearchCriteria<T> sc);
/**
* expunge the removed rows.
*/
void expunge();
public <K> K getNextInSequence(Class<K> clazz, String name);
/**
* Configure.
* @param name name of the dao.
* @param params params if any are specified.
* @return true if config is good. false if not.
*/
boolean configure(String name, Map<String, Object> params) throws ConfigurationException;
<M> List<M> customSearch(SearchCriteria<M> sc, Filter filter);
boolean lockInLockTable(String id);
boolean lockInLockTable(String id, int seconds);
boolean unlockFromLockTable(String id);
public <K> K getRandomlyIncreasingNextInSequence(Class<K> clazz, String name);
<K> SearchCriteria2 createSearchCriteria2(Class<K> resultType);
SearchCriteria2 createSearchCriteria2();
public T findOneBy(final SearchCriteria<T> sc);
/**
* @return
*/
Class<T> getEntityBeanType();
public int getRegionId();
/**
* @param sc
* @param filter
* @return
*/
Pair<List<T>, Integer> searchAndCount(SearchCriteria<T> sc, Filter filter);
}