switch back to MyISAM as InnoDB still creates a couple of problems. #3371
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / update / TreeIndexUpdater.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10 package eu.etaxonomy.cdm.database.update;
11
12 import org.apache.log4j.Logger;
13
14 import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
15 import eu.etaxonomy.cdm.database.ICdmDataSource;
16 import eu.etaxonomy.cdm.model.common.ITreeNode;
17
18 /**
19 * @author a.mueller
20 * @date 09.08.2013
21 *
22 */
23 public class TreeIndexUpdater extends AuditedSchemaUpdaterStepBase<TreeIndexUpdater> implements ISchemaUpdaterStep {
24 private static final Logger logger = Logger.getLogger(TreeIndexUpdater.class);
25
26 private String indexColumnName = "treeIndex";
27 private String treeIdColumnName;
28 private String parentIdColumnName = "parent_id";
29
30 public static final TreeIndexUpdater NewInstance(String stepName, String tableName, String treeIdColumnName, boolean includeAudTable){
31 return new TreeIndexUpdater(stepName, tableName, treeIdColumnName, null, includeAudTable);
32 }
33
34 public static final TreeIndexUpdater NewInstance(String stepName, String tableName, String treeIdColumnName, String indexColumnName, boolean includeAudTable){
35 return new TreeIndexUpdater(stepName, tableName, treeIdColumnName, indexColumnName, includeAudTable);
36 }
37
38
39 protected TreeIndexUpdater(String stepName, String tableName, String treeIdColumnName, String indexColumnName, boolean includeAudTable) {
40 super(stepName);
41 this.tableName = tableName;
42 this.treeIdColumnName = treeIdColumnName;
43 this.indexColumnName = indexColumnName == null ? this.indexColumnName : indexColumnName;
44 this.includeAudTable = includeAudTable;
45 }
46
47 @Override
48 protected boolean invokeOnTable(String tableName, ICdmDataSource datasource, IProgressMonitor monitor) {
49 try{
50 boolean result = true;
51
52 // String charType = "CHAR"; //TODO may depend on database type
53
54 //clean up nodes without classification //this should not happen with correct "delete" implementation
55 String sql = String.format(" DELETE FROM %s WHERE %s IS NULL ", tableName, treeIdColumnName);
56 datasource.executeUpdate(sql);
57
58 //... set all index entries to NULL
59 sql = String.format(" UPDATE %s SET %s = NULL", tableName, indexColumnName);
60 datasource.executeUpdate(sql);
61
62 //start
63 String separator = ITreeNode.separator;
64 String treePrefix = ITreeNode.treePrefix;
65 sql = String.format(" UPDATE %s tn " +
66 " SET tn.%s = CONCAT('%s%s', tn.%s, '%s', tn.id, '%s') " +
67 " WHERE tn.%s IS NULL AND tn.%s IS NOT NULL ",
68 tableName,
69 indexColumnName, separator, treePrefix, treeIdColumnName, separator, separator,
70 parentIdColumnName, treeIdColumnName);
71 datasource.executeUpdate(sql);
72
73 //width search index creation
74 String sqlCount = String.format(" SELECT count(*) as n " +
75 " FROM %s child INNER JOIN %s parent ON child.%s = parent.id " +
76 " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",
77 tableName, tableName, parentIdColumnName, indexColumnName, indexColumnName);
78
79 Long n;
80 do {
81
82 sql = String.format(" UPDATE %s child INNER JOIN %s parent ON child.%s = parent.id " +
83 " SET child.%s = CONCAT( parent.%s, child.id, '%s') " +
84 " WHERE parent.%s IS NOT NULL AND child.%s IS NULL ",
85 tableName, tableName, parentIdColumnName,
86 indexColumnName, indexColumnName, separator,
87 indexColumnName, indexColumnName);
88 datasource.executeUpdate(sql);
89
90 n = (Long)datasource.getSingleValue(sqlCount);
91 } while (n > 0) ;
92
93 sqlCount = String.format(" SELECT count(*) as n " +
94 " FROM %s " +
95 " WHERE %s IS NULL ", tableName, indexColumnName);
96 n = (Long)datasource.getSingleValue(sqlCount);
97 if (n > 0){
98 String message = "There are tree nodes with no tree index in %s. This indicates that there is a problem in the tree structure of 1 or more classifications.";
99 logger.error(String.format(message, tableName));
100 }
101
102 return result;
103 }catch(Exception e){
104 monitor.warning(e.getMessage(), e);
105 logger.error(e.getMessage());
106 return false;
107 }
108 }
109
110
111 }