Skip to content

Commit de5b7b2

Browse files
committed
Query count
1 parent 3503e97 commit de5b7b2

4 files changed

Lines changed: 75 additions & 101 deletions

File tree

src/main/java/org/geppetto/datasources/ADataSourceService.java

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@
3333
package org.geppetto.datasources;
3434

3535
import java.util.ArrayList;
36+
import java.util.LinkedHashMap;
3637
import java.util.List;
3738

3839
import org.geppetto.core.datasources.GeppettoDataSourceException;
3940
import org.geppetto.core.datasources.IDataSourceService;
40-
import org.geppetto.core.datasources.IQueryListener;
4141
import org.geppetto.core.datasources.QueryChecker;
4242
import org.geppetto.core.model.GeppettoModelAccess;
4343
import org.geppetto.core.services.AService;
44+
import org.geppetto.model.datasources.CompoundRefQuery;
4445
import org.geppetto.model.datasources.DataSource;
4546
import org.geppetto.model.datasources.Query;
4647
import org.geppetto.model.datasources.QueryResults;
47-
import org.geppetto.model.util.GeppettoModelTraversal;
48-
import org.geppetto.model.util.GeppettoVisitingException;
48+
import org.geppetto.model.datasources.SimpleQuery;
4949
import org.geppetto.model.variables.Variable;
5050
import org.geppetto.model.variables.VariablesFactory;
5151

@@ -59,7 +59,7 @@ public abstract class ADataSourceService extends AService implements IDataSource
5959
public abstract ConnectionType getConnectionType();
6060

6161
public abstract IQueryResponseProcessor getQueryResponseProcessor();
62-
62+
6363
public enum ConnectionType
6464
{
6565
GET, POST
@@ -73,6 +73,8 @@ public enum ConnectionType
7373

7474
private GeppettoModelAccess geppettoModelAccess;
7575

76+
private LinkedHashMap<String, QueryResults> cachedResults = new LinkedHashMap<String, QueryResults>();
77+
7678
public ADataSourceService(String dataSourceTemplate)
7779
{
7880
this.dataSourceTemplate = dataSourceTemplate;
@@ -103,8 +105,6 @@ protected DataSource getConfiguration()
103105
return configuration;
104106
}
105107

106-
107-
108108
/*
109109
* (non-Javadoc)
110110
*
@@ -130,26 +130,37 @@ public List<Query> getAvailableQueries(Variable variable)
130130
* @see org.geppetto.core.model.QueryProvider#execute(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryListener)
131131
*/
132132
@Override
133-
public QueryResults execute(Query query, Variable variable, IQueryListener listener) throws GeppettoDataSourceException
133+
public QueryResults execute(Query query, Variable variable) throws GeppettoDataSourceException
134134
{
135-
ExecuteQueryVisitor runQueryVisitor = new ExecuteQueryVisitor(variable, getGeppettoModelAccess());
136-
runQueryVisitor.doSwitch(query);
137-
return runQueryVisitor.getResults();
135+
String key = getKey(query, variable);
136+
if(cachedResults.containsKey(key))
137+
{
138+
return cachedResults.get(key);
139+
}
140+
else
141+
{
142+
ExecuteQueryVisitor runQueryVisitor = new ExecuteQueryVisitor(variable, getGeppettoModelAccess());
143+
runQueryVisitor.doSwitch(query);
144+
QueryResults results = runQueryVisitor.getResults();
145+
cache(key, results);
146+
return results;
147+
}
138148
}
139149

140-
/*
141-
* (non-Javadoc)
142-
*
143-
* @see org.geppetto.core.model.QueryProvider#getNumberOfResults(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryResults)
150+
/**
151+
* @param key
152+
* @param results
144153
*/
145-
@Override
146-
public int getNumberOfResults(Query query, Variable variable, QueryResults results) throws GeppettoDataSourceException
154+
private void cache(String key, QueryResults results)
147155
{
148-
// TODO Auto-generated method stub
149-
return 0;
156+
if(cachedResults.size() > 10)
157+
{
158+
cachedResults.remove(cachedResults.keySet().iterator().next());
159+
}
160+
cachedResults.put(key, results);
161+
150162
}
151163

152-
153164
/*
154165
* (non-Javadoc)
155166
*
@@ -158,32 +169,37 @@ public int getNumberOfResults(Query query, Variable variable, QueryResults resul
158169
@Override
159170
public int getNumberOfResults(Query query, Variable variable) throws GeppettoDataSourceException
160171
{
161-
Query fetchVariableQuery = getConfiguration().getFetchVariableQuery();
162-
ExecuteQueryVisitor runQueryVisitor = new ExecuteQueryVisitor(variable, getGeppettoModelAccess());
163-
try
172+
int count = -1;
173+
if(query instanceof CompoundRefQuery)
164174
{
165-
GeppettoModelTraversal.applyDirectChildren(fetchVariableQuery, runQueryVisitor);
175+
SimpleQuery simpleQuery = (SimpleQuery) ((CompoundRefQuery) query).getQueryChain().get(0);
176+
ExecuteQueryVisitor runQueryVisitor = new ExecuteQueryVisitor(variable, getGeppettoModelAccess());
177+
if(simpleQuery.getCountQuery() != null && !simpleQuery.getCountQuery().isEmpty())
178+
{
179+
runQueryVisitor.countOnly(true);
180+
// Assumption: in a compound query the number of results is dictated only by the first simple query, any further processing or querying will not increase the number of results.
181+
// If this will change this algorithm has to change (and become a visitor)
182+
runQueryVisitor.doSwitch(simpleQuery);
183+
count = runQueryVisitor.getCount();
184+
}
185+
else
186+
{
187+
// There is no query specified, we run everything and we cache it so if it will be actually run later one we'll already have the results
188+
count = execute(query, variable).getResults().size();
166189

190+
}
167191
}
168-
catch(GeppettoVisitingException e)
169-
{
170-
throw new GeppettoDataSourceException(e);
171-
}
172-
173-
return runQueryVisitor.getCount();
192+
return count;
174193
}
175-
176-
/*
177-
* (non-Javadoc)
178-
*
179-
* @see org.geppetto.core.model.QueryProvider#execute(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryResults,
180-
* org.geppetto.core.model.QueryListener)
194+
195+
/**
196+
* @param query
197+
* @param variable
198+
* @return
181199
*/
182-
@Override
183-
public QueryResults execute(Query query, Variable variable, QueryResults results, IQueryListener listener) throws GeppettoDataSourceException
200+
private String getKey(Query query, Variable variable)
184201
{
185-
// TODO Auto-generated method stub
186-
return null;
202+
return query.getPath() + ":" + variable.getPath();
187203
}
188204

189205
/*
@@ -213,6 +229,4 @@ public void registerGeppettoService() throws Exception
213229
// Nothing to do here
214230
}
215231

216-
217-
218232
}

src/main/java/org/geppetto/datasources/DummyDataSourceService.java

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@
3333
package org.geppetto.datasources;
3434

3535
import org.geppetto.core.datasources.GeppettoDataSourceException;
36-
import org.geppetto.core.datasources.IDataSourceService;
37-
import org.geppetto.core.datasources.IQueryListener;
3836
import org.geppetto.datasources.neo4j.Neo4jResponseProcessor;
3937
import org.geppetto.model.GeppettoLibrary;
4038
import org.geppetto.model.datasources.DataSource;
4139
import org.geppetto.model.datasources.DataSourceLibraryConfiguration;
42-
import org.geppetto.model.datasources.Query;
43-
import org.geppetto.model.datasources.QueryResults;
4440
import org.geppetto.model.types.ImportType;
4541
import org.geppetto.model.types.TypesFactory;
4642
import org.geppetto.model.variables.Variable;
@@ -50,63 +46,14 @@
5046
* @author matteocantarelli
5147
*
5248
*/
53-
public class DummyDataSourceService extends ADataSourceService implements IDataSourceService
49+
public class DummyDataSourceService extends ADataSourceService
5450
{
5551

5652
public DummyDataSourceService()
5753
{
5854
super("");
5955
}
6056

61-
/*
62-
* (non-Javadoc)
63-
*
64-
* @see org.geppetto.core.model.QueryProvider#getNumberOfResults(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable)
65-
*/
66-
@Override
67-
public int getNumberOfResults(Query query, Variable variable) throws GeppettoDataSourceException
68-
{
69-
// TODO Auto-generated method stub
70-
return 0;
71-
}
72-
73-
/*
74-
* (non-Javadoc)
75-
*
76-
* @see org.geppetto.core.model.QueryProvider#getNumberOfResults(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryResults)
77-
*/
78-
@Override
79-
public int getNumberOfResults(Query query, Variable variable, QueryResults results) throws GeppettoDataSourceException
80-
{
81-
// TODO Auto-generated method stub
82-
return 0;
83-
}
84-
85-
/*
86-
* (non-Javadoc)
87-
*
88-
* @see org.geppetto.core.model.QueryProvider#execute(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryListener)
89-
*/
90-
@Override
91-
public QueryResults execute(Query query, Variable variable, IQueryListener listener) throws GeppettoDataSourceException
92-
{
93-
// TODO Auto-generated method stub
94-
return null;
95-
}
96-
97-
/*
98-
* (non-Javadoc)
99-
*
100-
* @see org.geppetto.core.model.QueryProvider#execute(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryResults,
101-
* org.geppetto.core.model.QueryListener)
102-
*/
103-
@Override
104-
public QueryResults execute(Query query, Variable variable, QueryResults results, IQueryListener listener) throws GeppettoDataSourceException
105-
{
106-
// TODO Auto-generated method stub
107-
return null;
108-
}
109-
11057
/*
11158
* (non-Javadoc)
11259
*

src/main/java/org/geppetto/datasources/ExecuteQueryVisitor.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@
6060
import org.geppetto.model.variables.Variable;
6161

6262
/**
63-
* @author matteocantarelli Matteo TODO: I really want to move this to the datasource bundle...
63+
* @author matteocantarelli
6464
*/
6565
public class ExecuteQueryVisitor extends DatasourcesSwitch<Object>
6666
{
6767

6868
private static final String ID = "ID";
6969

70-
private boolean count; // true if we want to execute a count
70+
private boolean count = false; // true if we want to execute a count
7171

7272
private QueryResults results = null;
7373

@@ -77,6 +77,8 @@ public class ExecuteQueryVisitor extends DatasourcesSwitch<Object>
7777

7878
private GeppettoModelAccess geppettoModelAccess;
7979

80+
private int resultsCount = -1;
81+
8082
public ExecuteQueryVisitor(Variable variable, GeppettoModelAccess geppettoModelAccess)
8183
{
8284
this.variable = variable;
@@ -223,10 +225,11 @@ public Object caseSimpleQuery(SimpleQuery query)
223225
*/
224226
private void processResponse(String response, ADataSourceService dataSourceService) throws GeppettoDataSourceException
225227
{
226-
// TODO Maybe split in two different visitors?
227228
if(count)
228229
{
229-
// TODO update the count
230+
Map<String, Object> responseMap = JSONUtility.getAsMap(response);
231+
results = dataSourceService.getQueryResponseProcessor().processResponse(responseMap);
232+
// TODO How to get the count if it is actually specified?
230233
}
231234
else
232235
{
@@ -321,8 +324,16 @@ public QueryResults getResults()
321324

322325
public int getCount()
323326
{
324-
// TODO Auto-generated method stub
325-
return 0;
327+
return resultsCount;
328+
}
329+
330+
/**
331+
* @param countOnly
332+
*/
333+
public void countOnly(boolean countOnly)
334+
{
335+
count = countOnly;
336+
326337
}
327338

328339
}

src/main/java/org/geppetto/datasources/aberowl/AberOWLDataSourceService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,6 @@ public IQueryResponseProcessor getQueryResponseProcessor()
8383
return queryResponseProcessor;
8484
}
8585

86+
87+
8688
}

0 commit comments

Comments
 (0)