Conditions | 13 |
Total Lines | 105 |
Code Lines | 66 |
Lines | 105 |
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.SparqlStep.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; |
||
42 | View Code Duplication | public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws KettleException { |
|
|
|||
43 | SparqlStepMeta meta = (SparqlStepMeta) smi; |
||
44 | SparqlStepData data = (SparqlStepData) sdi; |
||
45 | |||
46 | final Object[] row = getRow(); |
||
47 | |||
48 | if (first) { |
||
49 | first = false; |
||
50 | |||
51 | RowMetaInterface rowMeta = getInputRowMeta(row != null); |
||
52 | data.outputRowMeta = rowMeta.clone(); |
||
53 | |||
54 | // Adiciona os metadados do step atual |
||
55 | meta.getFields(data.outputRowMeta, getStepname(), null, null, this); |
||
56 | |||
57 | data.inputRowSize = rowMeta.size(); |
||
58 | |||
59 | // Obtem string de consulta e constroi o objeto consulta |
||
60 | String queryStr = SparqlStepUtils.toFullQueryString(meta.getPrefixes(), meta.getQueryString()); |
||
61 | try { |
||
62 | data.originalQuery = QueryFactory.create(queryStr); |
||
63 | } catch (QueryException e) { |
||
64 | // Se consulta for invalida nao pode continuar |
||
65 | throw new KettleException(e); |
||
66 | } |
||
67 | |||
68 | // Se nao usar SAX o execSelect() nao funciona |
||
69 | ARQ.set(ARQ.useSAX, true); |
||
70 | |||
71 | // Offset e Limit para Construct/select/describe quando limit nao |
||
72 | // especificado |
||
73 | if (!data.originalQuery.hasLimit() && (data.originalQuery.getQueryType() != Query.QueryTypeAsk) |
||
74 | && (data.originalQuery.getQueryType() != Query.QueryTypeDescribe)) { |
||
75 | // Consulta eh quebrada em varias usando OFFSET e LIMIT |
||
76 | data.offset = data.originalQuery.hasOffset() ? data.originalQuery.getOffset() : 0; |
||
77 | data.limit = 1000; |
||
78 | data.runAtOnce = false; |
||
79 | } else { |
||
80 | data.runAtOnce = true; |
||
81 | } |
||
82 | |||
83 | data.remainingTries = MAX_ATTEMPTS; |
||
84 | |||
85 | return true; |
||
86 | } |
||
87 | |||
88 | Query query = null; |
||
89 | if (data.runAtOnce) { |
||
90 | // Roda consulta num unico HTTP Request |
||
91 | query = data.originalQuery; |
||
92 | |||
93 | while (data.remainingTries > 0) { |
||
94 | // Tenta executar consulta ate MAX_ATTEMPTS vezes |
||
95 | try { |
||
96 | runQueryAndPutResults(query, meta, data, row); |
||
97 | |||
98 | setOutputDone(); |
||
99 | return false; // Nao ha mais resultados, ie, processRow() |
||
100 | // nao sera' chamado novamente |
||
101 | } catch (Throwable e) { |
||
102 | handleError(e, MAX_ATTEMPTS - data.remainingTries + 1); |
||
103 | } |
||
104 | |||
105 | data.remainingTries--; |
||
106 | } |
||
107 | } else { |
||
108 | // Cria consulta que representa o bloco atual |
||
109 | query = data.originalQuery.cloneQuery(); |
||
110 | query.setOffset(data.offset); |
||
111 | query.setLimit(data.limit); |
||
112 | |||
113 | while (data.remainingTries > 0) { |
||
114 | try { |
||
115 | int numRows = runQueryAndPutResults(query, meta, data, row); |
||
116 | |||
117 | if (numRows > 0) { |
||
118 | data.offset += data.limit; |
||
119 | data.remainingTries = MAX_ATTEMPTS; |
||
120 | return true; |
||
121 | } else { |
||
122 | setOutputDone(); |
||
123 | return false; |
||
124 | } |
||
125 | } catch (Throwable e) { |
||
126 | handleError(e, MAX_ATTEMPTS - data.remainingTries + 1); |
||
127 | } |
||
128 | |||
129 | data.remainingTries--; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | StringBuilder sb = new StringBuilder(); |
||
134 | sb.append("Todas as tentativas de executar a consulta falharam. "); |
||
135 | sb.append("Verifique conexão de rede e o SPARQL Endpoint.\n"); |
||
136 | sb.append("Endpoint: "); |
||
137 | sb.append(meta.getEndpointUri()); |
||
138 | sb.append('\n'); |
||
139 | sb.append("Grafo padrão: "); |
||
140 | sb.append(meta.getDefaultGraph()); |
||
141 | sb.append('\n'); |
||
142 | sb.append("Consulta:\n"); |
||
143 | sb.append(query.toString()); |
||
144 | sb.append('\n'); |
||
145 | |||
146 | throw new KettleException(sb.toString()); |
||
147 | } |
||
285 | } |