You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BaseDao.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.db;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import com.google.common.base.Preconditions;
  23. import com.google.common.collect.ImmutableList;
  24. import com.google.common.collect.Lists;
  25. import java.io.Serializable;
  26. import java.sql.Timestamp;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.Map;
  32. import javax.annotation.CheckForNull;
  33. import javax.annotation.Nullable;
  34. import org.apache.ibatis.session.ResultContext;
  35. import org.sonar.api.utils.System2;
  36. import org.sonar.api.utils.log.Logger;
  37. import org.sonar.api.utils.log.Loggers;
  38. import org.sonar.db.Dao;
  39. import org.sonar.db.DbSession;
  40. import org.sonar.db.Dto;
  41. import org.sonar.server.exceptions.NotFoundException;
  42. import org.sonar.server.search.DbSynchronizationHandler;
  43. import org.sonar.server.search.IndexDefinition;
  44. import org.sonar.server.search.action.DeleteKey;
  45. import org.sonar.server.search.action.DeleteNestedItem;
  46. import org.sonar.server.search.action.InsertDto;
  47. import org.sonar.server.search.action.RefreshIndex;
  48. import org.sonar.server.search.action.UpsertDto;
  49. import org.sonar.server.search.action.UpsertNestedItem;
  50. import static com.google.common.collect.Lists.newArrayList;
  51. import static com.google.common.collect.Maps.newHashMap;
  52. /**
  53. * naming convention for DAO
  54. * =========================
  55. * <p/>
  56. * The DAO manages a Business Domain for a set of DTO. There is a Main DTO (i.e. RuleDto)
  57. * that has a few nested/child DTOs. The DAO supports nested DTOs for 1-to-1 and 1-to-many
  58. * relations. Many-to-many relations are handled by their own DAO classes (i.e. ActiveRuleDao)
  59. * <p/>
  60. * Main DTO
  61. * -------------------------
  62. * <p/>
  63. * * GET Methods
  64. * - returns a single DTO
  65. * - DTO is fully loaded (no field will return null)
  66. * - returns null (and not empty)
  67. * - examples:
  68. * - RuleDto = ruleDao.getNullableByKey(dto.getKey());
  69. * <p/>
  70. * * FIND Methods
  71. * - returns a List of DTO.
  72. * - Returns an empty list id no match
  73. * - method name is FULLY-NOMINATIVE
  74. * - examples:
  75. * - List<RuleDto> rules findByQualityProfile(QualityProfile qprofile)
  76. * - List<RuleDto> rules findByQualityProfile(QualityProfileKey qprofileKey)
  77. * - List<RuleDto> rules findByQualityProfileAndCreatedAfter(QualityProfileKey qprofileKey, Date date)
  78. * <p/>
  79. * * CRUD Methods
  80. * - insert(DTO)
  81. * - udpate(DTO)
  82. * - delete(DTO)
  83. * <p/>
  84. * Nested DTO
  85. * -------------------------
  86. * <p/>
  87. * Some DAO implementations also manage nested DTO. RuleTag for example is managed by the RuleDao class
  88. * Nested DTO are accessible following a similar convention for the Main DTO:
  89. * <p/>
  90. * * GET Methods
  91. * - returns a single DTO
  92. * - DTO is fully loaded (no field will return null)
  93. * - returns null (and not empty)
  94. * - prefixed with DTO type
  95. * - examples:
  96. * - RuleTagDto = ruleDao.getTagByKey(tagDto.getKey());
  97. * <p/>
  98. * * FIND Methods
  99. * - returns a List of DTO.
  100. * - Returns an empty list id no match
  101. * - method name is FULLY-NOMINATIVE
  102. * - prefixed with DTO type
  103. * - examples:
  104. * - List<RuleTagDto> tags findRuleTagByRuleKey(RuleKey key)
  105. * - List<RuleTagDto> tags findRuleTagByRepositoryAndLanguage(RepositoryKey key, String language)
  106. * <p/>
  107. * * CRUD Methods are slightly different because they REQUIRE the main DTO to be valid
  108. * - Nested dto methods MUST have the Main DTO or it's key as param
  109. * - add
  110. * - remove
  111. * - update
  112. * - examples:
  113. * - RuleTagDto tag add(RuleTagDto tag, RuleKey key)
  114. * - RuleParamDto param add(RuleParamDto param, RuleDto rule)
  115. *
  116. * @param <MAPPER> iBatis Mapper class
  117. * @param <DTO> Produced DTO class from this dao
  118. * @param <KEY> DTO Key class
  119. */
  120. public abstract class BaseDao<MAPPER, DTO extends Dto<KEY>, KEY extends Serializable> implements DeprecatedDao<DTO,KEY>, Dao {
  121. private static final Logger LOGGER = Loggers.get(BaseDao.class);
  122. protected IndexDefinition indexDefinition;
  123. private Class<MAPPER> mapperClass;
  124. private System2 system2;
  125. protected boolean hasIndex() {
  126. return indexDefinition != null;
  127. }
  128. protected BaseDao(@Nullable IndexDefinition indexDefinition, Class<MAPPER> mapperClass, System2 system2) {
  129. this.mapperClass = mapperClass;
  130. this.indexDefinition = indexDefinition;
  131. this.system2 = system2;
  132. }
  133. protected BaseDao(Class<MAPPER> mapperClass, System2 system2) {
  134. this(null, mapperClass, system2);
  135. }
  136. public String getIndexType() {
  137. return indexDefinition != null ? this.indexDefinition.getIndexType() : null;
  138. }
  139. protected MAPPER mapper(DbSession session) {
  140. return session.getMapper(mapperClass);
  141. }
  142. @Override
  143. @CheckForNull
  144. public DTO getNullableByKey(DbSession session, KEY key) {
  145. return doGetNullableByKey(session, key);
  146. }
  147. @Override
  148. public DTO getByKey(DbSession session, KEY key) {
  149. DTO value = doGetNullableByKey(session, key);
  150. if (value == null) {
  151. throw new NotFoundException(String.format("Key '%s' not found", key));
  152. }
  153. return value;
  154. }
  155. public List<DTO> getByKeys(DbSession session, KEY... keys) {
  156. return getByKeys(session, ImmutableList.<KEY>copyOf(keys));
  157. }
  158. public List<DTO> getByKeys(DbSession session, Collection<KEY> keys) {
  159. if (keys.isEmpty()) {
  160. return Collections.emptyList();
  161. }
  162. List<DTO> components = newArrayList();
  163. List<List<KEY>> partitionList = Lists.partition(newArrayList(keys), 1000);
  164. for (List<KEY> partition : partitionList) {
  165. List<DTO> dtos = doGetByKeys(session, partition);
  166. components.addAll(dtos);
  167. }
  168. return components;
  169. }
  170. protected List<DTO> doGetByKeys(DbSession session, Collection<KEY> keys) {
  171. throw notImplemented(this);
  172. }
  173. @Override
  174. public DTO update(DbSession session, DTO item) {
  175. Date now = new Date(system2.now());
  176. update(session, item, now);
  177. return item;
  178. }
  179. @Override
  180. public DTO update(DbSession session, DTO item, DTO... others) {
  181. update(session, Lists.<DTO>asList(item, others));
  182. return item;
  183. }
  184. @Override
  185. public Collection<DTO> update(DbSession session, Collection<DTO> items) {
  186. Date now = new Date(system2.now());
  187. for (DTO item : items) {
  188. update(session, item, now);
  189. }
  190. return items;
  191. }
  192. private void update(DbSession session, DTO item, Date now) {
  193. try {
  194. item.setUpdatedAt(now);
  195. doUpdate(session, item);
  196. if (hasIndex()) {
  197. session.enqueue(new UpsertDto<DTO>(getIndexType(), item));
  198. }
  199. } catch (Exception e) {
  200. throw new IllegalStateException("Fail to update item in db: " + item, e);
  201. }
  202. }
  203. @Override
  204. public DTO insert(DbSession session, DTO item) {
  205. insert(session, item, new Date(system2.now()));
  206. return item;
  207. }
  208. @Override
  209. public Collection<DTO> insert(DbSession session, Collection<DTO> items) {
  210. Date now = new Date(system2.now());
  211. for (DTO item : items) {
  212. insert(session, item, now);
  213. }
  214. return items;
  215. }
  216. @Override
  217. public DTO insert(DbSession session, DTO item, DTO... others) {
  218. insert(session, Lists.<DTO>asList(item, others));
  219. return item;
  220. }
  221. private void insert(DbSession session, DTO item, Date now) {
  222. if (item.getCreatedAt() == null) {
  223. item.setCreatedAt(now);
  224. }
  225. item.setUpdatedAt(now);
  226. try {
  227. doInsert(session, item);
  228. if (hasIndex()) {
  229. session.enqueue(new UpsertDto<>(getIndexType(), item));
  230. }
  231. } catch (Exception e) {
  232. throw new IllegalStateException("Fail to insert item in db: " + item, e);
  233. }
  234. }
  235. @Override
  236. public void delete(DbSession session, DTO item) {
  237. deleteByKey(session, item.getKey());
  238. }
  239. @Override
  240. public void delete(DbSession session, DTO item, DTO... others) {
  241. delete(session, Lists.<DTO>asList(item, others));
  242. }
  243. @Override
  244. public void delete(DbSession session, Collection<DTO> items) {
  245. for (DTO item : items) {
  246. delete(session, item);
  247. }
  248. }
  249. @Override
  250. public void deleteByKey(DbSession session, KEY key) {
  251. Preconditions.checkNotNull(key, "Missing key");
  252. try {
  253. doDeleteByKey(session, key);
  254. if (hasIndex()) {
  255. session.enqueue(new DeleteKey<>(getIndexType(), key));
  256. }
  257. } catch (Exception e) {
  258. throw new IllegalStateException("Fail to delete item from db: " + key, e);
  259. }
  260. }
  261. protected final void enqueueUpdate(Object nestedItem, KEY key, DbSession session) {
  262. if (hasIndex()) {
  263. session.enqueue(new UpsertNestedItem<>(
  264. this.getIndexType(), key, nestedItem));
  265. }
  266. }
  267. public void enqueueDelete(Object nestedItem, KEY key, DbSession session) {
  268. if (hasIndex()) {
  269. session.enqueue(new DeleteNestedItem<>(
  270. this.getIndexType(), key, nestedItem));
  271. session.commit();
  272. }
  273. }
  274. public void enqueueInsert(Object nestedItem, KEY key, DbSession session) {
  275. if (hasIndex()) {
  276. this.enqueueUpdate(nestedItem, key, session);
  277. }
  278. }
  279. @VisibleForTesting
  280. public List<DTO> findAfterDate(final DbSession session, Date date, Map<String, String> params) {
  281. return session.selectList(getSynchronizeStatementFQN(), getSynchronizationParams(date, params));
  282. }
  283. @VisibleForTesting
  284. public List<DTO> findAfterDate(final DbSession session, Date date) {
  285. return findAfterDate(session, date, Collections.<String, String>emptyMap());
  286. }
  287. // Synchronization methods
  288. protected DbSynchronizationHandler getSynchronizationResultHandler(final DbSession session, Map<String, String> params) {
  289. return new DbSynchronizationHandler(session, params) {
  290. private int count = 0;
  291. @Override
  292. public void handleResult(ResultContext resultContext) {
  293. DTO dto = (DTO) resultContext.getResultObject();
  294. // session.enqueue(new UpsertDto<DTO>(getIndexType(), dto, false));
  295. getSession().enqueue(new InsertDto<>(getIndexType(), dto, false));
  296. count++;
  297. if (count % 100000 == 0) {
  298. LOGGER.info("Synchronized {} {}", count, getIndexType());
  299. }
  300. }
  301. @Override
  302. public void enqueueCollected() {
  303. // Do nothing in this case
  304. }
  305. };
  306. }
  307. protected Map<String, Object> getSynchronizationParams(@Nullable Date date, Map<String, String> params) {
  308. Map<String, Object> finalParams = newHashMap();
  309. if (date != null) {
  310. finalParams.put("date", new Timestamp(date.getTime()));
  311. }
  312. return finalParams;
  313. }
  314. @Override
  315. public void synchronizeAfter(final DbSession session) {
  316. this.synchronizeAfter(session, null, Collections.<String, String>emptyMap());
  317. }
  318. @Override
  319. public void synchronizeAfter(final DbSession session, @Nullable Date date) {
  320. this.synchronizeAfter(session, date, Collections.<String, String>emptyMap());
  321. }
  322. @Override
  323. public void synchronizeAfter(final DbSession session, @Nullable Date date, Map<String, String> params) {
  324. DbSynchronizationHandler handler = getSynchronizationResultHandler(session, params);
  325. session.select(getSynchronizeStatementFQN(), getSynchronizationParams(date, params), handler);
  326. handler.enqueueCollected();
  327. session.enqueue(new RefreshIndex(this.getIndexType()));
  328. session.commit();
  329. }
  330. private String getSynchronizeStatementFQN() {
  331. return mapperClass.getName() + "." + this.getSynchronizationStatementName();
  332. }
  333. @CheckForNull
  334. protected abstract DTO doGetNullableByKey(DbSession session, KEY key);
  335. protected String getSynchronizationStatementName() {
  336. return "selectAfterDate";
  337. }
  338. protected DTO doInsert(DbSession session, DTO item) {
  339. throw notImplemented(this);
  340. }
  341. protected DTO doUpdate(DbSession session, DTO item) {
  342. throw notImplemented(this);
  343. }
  344. protected void doDeleteByKey(DbSession session, KEY key) {
  345. throw notImplemented(this);
  346. }
  347. private static RuntimeException notImplemented(BaseDao baseDao) {
  348. throw new IllegalStateException("Not implemented yet for class [" + baseDao.getClass().getSimpleName() + "]");
  349. }
  350. }