| Conditions | 13 |
| Total Lines | 114 |
| Code Lines | 66 |
| Lines | 114 |
| Ratio | 100 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like br.ufrj.ppgi.greco.kettle.GraphSparqlStep.processRow(StepMetaInterface,StepDataInterface) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package br.ufrj.ppgi.greco.kettle; |
||
| 62 | View Code Duplication | public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { |
|
|
|
|||
| 63 | GraphSparqlStepMeta meta = (GraphSparqlStepMeta) smi; |
||
| 64 | GraphSparqlStepData data = (GraphSparqlStepData) sdi; |
||
| 65 | |||
| 66 | // Obtem linha do fluxo de entrada |
||
| 67 | final Object[] row = getRow(); |
||
| 68 | |||
| 69 | if (first) { |
||
| 70 | // Executa apenas uma vez. Variavel first definida na superclasse |
||
| 71 | first = false; |
||
| 72 | |||
| 73 | // Obtem todas as colunas ate o step anterior. |
||
| 74 | // Chamar apenas apos chamar getRow() |
||
| 75 | RowMetaInterface rowMeta = getInputRowMeta(row != null); |
||
| 76 | data.outputRowMeta = rowMeta.clone(); |
||
| 77 | |||
| 78 | // Adiciona os metadados do step atual |
||
| 79 | meta.getFields(data.outputRowMeta, getStepname(), null, null, this); |
||
| 80 | |||
| 81 | data.inputRowSize = rowMeta.size(); |
||
| 82 | |||
| 83 | // Obtem string de consulta e constroi o objeto consulta |
||
| 84 | String queryStr = GraphSparqlStepUtils.toFullQueryString(meta.getPrefixes(), meta.getQueryString()); |
||
| 85 | try { |
||
| 86 | data.originalQuery = QueryFactory.create(queryStr); |
||
| 87 | } catch (QueryException e) { |
||
| 88 | // Se consulta for invalida nao pode continuar |
||
| 89 | throw new KettleException(e); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Se nao usar SAX o execSelect() nao funciona |
||
| 93 | ARQ.set(ARQ.useSAX, true); |
||
| 94 | |||
| 95 | // Offset e Limit para Construct/select/describe quando limit nao |
||
| 96 | // especificado |
||
| 97 | if (!data.originalQuery.hasLimit() && (data.originalQuery.getQueryType() != Query.QueryTypeAsk) |
||
| 98 | && (data.originalQuery.getQueryType() != Query.QueryTypeDescribe)) { |
||
| 99 | // Consulta eh quebrada em varias usando OFFSET e LIMIT |
||
| 100 | data.offset = data.originalQuery.hasOffset() ? data.originalQuery.getOffset() : 0; |
||
| 101 | data.limit = 1000; |
||
| 102 | data.runAtOnce = false; |
||
| 103 | } else { |
||
| 104 | data.runAtOnce = true; |
||
| 105 | } |
||
| 106 | |||
| 107 | data.remainingTries = MAX_ATTEMPTS; |
||
| 108 | |||
| 109 | return true; |
||
| 110 | } |
||
| 111 | |||
| 112 | Query query = null; |
||
| 113 | if (data.runAtOnce) { |
||
| 114 | // Roda consulta num unico HTTP Request |
||
| 115 | query = data.originalQuery; |
||
| 116 | |||
| 117 | while (data.remainingTries > 0) { |
||
| 118 | // Tenta executar consulta ate MAX_ATTEMPTS vezes |
||
| 119 | try { |
||
| 120 | runQueryAndPutResults(query, meta, data, row); |
||
| 121 | |||
| 122 | setOutputDone(); |
||
| 123 | return false; // Nao ha mais resultados, ie, processRow() |
||
| 124 | // nao sera' chamado novamente |
||
| 125 | } catch (Throwable e) { |
||
| 126 | handleError(e, MAX_ATTEMPTS - data.remainingTries + 1); |
||
| 127 | } |
||
| 128 | |||
| 129 | data.remainingTries--; |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | // Cria consulta que representa o bloco atual |
||
| 133 | query = data.originalQuery.cloneQuery(); |
||
| 134 | query.setOffset(data.offset); |
||
| 135 | query.setLimit(data.limit); |
||
| 136 | |||
| 137 | while (data.remainingTries > 0) { // Tenta executar este bloco ate' |
||
| 138 | // MAX_ATTEMPTS vezes |
||
| 139 | try { |
||
| 140 | int numRows = runQueryAndPutResults(query, meta, data, row); |
||
| 141 | |||
| 142 | if (numRows > 0) { // Este bloco de consulta rodou |
||
| 143 | data.offset += data.limit; |
||
| 144 | data.remainingTries = MAX_ATTEMPTS; |
||
| 145 | |||
| 146 | return true; |
||
| 147 | } else { // Nao ha mais resultados, ie, processRow() nao |
||
| 148 | // sera' |
||
| 149 | // chamado novamente |
||
| 150 | setOutputDone(); |
||
| 151 | return false; |
||
| 152 | } |
||
| 153 | } catch (Throwable e) { |
||
| 154 | handleError(e, MAX_ATTEMPTS - data.remainingTries + 1); |
||
| 155 | } |
||
| 156 | |||
| 157 | data.remainingTries--; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | // Nao funfou! |
||
| 162 | StringBuilder sb = new StringBuilder(); |
||
| 163 | sb.append("Todas as tentativas de executar a consulta falharam. "); |
||
| 164 | sb.append("Verifique conexão de rede e o SPARQL Endpoint.\n"); |
||
| 165 | sb.append("Endpoint: "); |
||
| 166 | sb.append(meta.getEndpointUri()); |
||
| 167 | sb.append('\n'); |
||
| 168 | sb.append("Grafo padrão: "); |
||
| 169 | sb.append(meta.getDefaultGraph()); |
||
| 170 | sb.append('\n'); |
||
| 171 | sb.append("Consulta:\n"); |
||
| 172 | sb.append(query.toString()); |
||
| 173 | sb.append('\n'); |
||
| 174 | |||
| 175 | throw new KettleException(sb.toString()); |
||
| 176 | } |
||
| 300 | } |