Skip to content

Commit 83c32cf

Browse files
committed
Adding support for multiple queries
1 parent 129b383 commit 83c32cf

3 files changed

Lines changed: 285 additions & 84 deletions

File tree

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

Lines changed: 44 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,20 @@
3535
import java.util.ArrayList;
3636
import java.util.LinkedHashMap;
3737
import java.util.List;
38+
import java.util.Map;
3839

40+
import org.eclipse.emf.common.util.EList;
3941
import org.geppetto.core.datasources.GeppettoDataSourceException;
4042
import org.geppetto.core.datasources.IDataSourceService;
4143
import org.geppetto.core.datasources.QueryChecker;
4244
import org.geppetto.core.model.GeppettoModelAccess;
4345
import org.geppetto.core.services.AService;
44-
import org.geppetto.model.datasources.CompoundRefQuery;
4546
import org.geppetto.model.datasources.DataSource;
4647
import org.geppetto.model.datasources.Query;
4748
import org.geppetto.model.datasources.QueryResults;
48-
import org.geppetto.model.datasources.SimpleQuery;
49+
import org.geppetto.model.datasources.RunnableQuery;
50+
import org.geppetto.model.util.GeppettoModelTraversal;
51+
import org.geppetto.model.util.GeppettoVisitingException;
4952
import org.geppetto.model.variables.Variable;
5053
import org.geppetto.model.variables.VariablesFactory;
5154

@@ -73,7 +76,9 @@ public enum ConnectionType
7376

7477
private GeppettoModelAccess geppettoModelAccess;
7578

76-
private LinkedHashMap<String, QueryResults> cachedResults = new LinkedHashMap<String, QueryResults>();
79+
// Cache is shared
80+
private static Map<String, QueryResults> cachedResults = new LinkedHashMap<String, QueryResults>();
81+
private static Map<String, List<String>> cachedIds = new LinkedHashMap<String, List<String>>();
7782

7883
public ADataSourceService(String dataSourceTemplate)
7984
{
@@ -87,11 +92,47 @@ public void initialize(DataSource configuration, GeppettoModelAccess geppettoMod
8792
this.geppettoModelAccess = geppettoModelAccess;
8893
}
8994

95+
@Override
96+
public int getNumberOfResults(List<RunnableQuery> queries) throws GeppettoDataSourceException
97+
{
98+
try
99+
{
100+
ExecuteMultipleQueriesVisitor executeMultipleQueriesVisitor = new ExecuteMultipleQueriesVisitor(geppettoModelAccess, cachedResults, cachedIds);
101+
GeppettoModelTraversal.apply((EList) queries, executeMultipleQueriesVisitor);
102+
return executeMultipleQueriesVisitor.getCount();
103+
}
104+
catch(GeppettoVisitingException e)
105+
{
106+
throw new GeppettoDataSourceException(e);
107+
}
108+
}
109+
110+
@Override
111+
public QueryResults execute(List<RunnableQuery> queries) throws GeppettoDataSourceException
112+
{
113+
try
114+
{
115+
ExecuteMultipleQueriesVisitor executeMultipleQueriesVisitor = new ExecuteMultipleQueriesVisitor(geppettoModelAccess, cachedResults, cachedIds);
116+
GeppettoModelTraversal.apply((EList) queries, executeMultipleQueriesVisitor);
117+
return executeMultipleQueriesVisitor.getResults();
118+
}
119+
catch(GeppettoVisitingException e)
120+
{
121+
throw new GeppettoDataSourceException(e);
122+
}
123+
}
124+
125+
/**
126+
* @return
127+
*/
90128
protected GeppettoModelAccess getGeppettoModelAccess()
91129
{
92130
return geppettoModelAccess;
93131
}
94132

133+
/**
134+
* @return
135+
*/
95136
protected String getTemplate()
96137
{
97138
return dataSourceTemplate;
@@ -124,87 +165,6 @@ public List<Query> getAvailableQueries(Variable variable)
124165
return availableQueries;
125166
}
126167

127-
/*
128-
* (non-Javadoc)
129-
*
130-
* @see org.geppetto.core.model.QueryProvider#execute(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable, org.geppetto.core.model.QueryListener)
131-
*/
132-
@Override
133-
public QueryResults execute(Query query, Variable variable) throws GeppettoDataSourceException
134-
{
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-
}
148-
}
149-
150-
/**
151-
* @param key
152-
* @param results
153-
*/
154-
private void cache(String key, QueryResults results)
155-
{
156-
if(cachedResults.size() > 10)
157-
{
158-
cachedResults.remove(cachedResults.keySet().iterator().next());
159-
}
160-
cachedResults.put(key, results);
161-
162-
}
163-
164-
/*
165-
* (non-Javadoc)
166-
*
167-
* @see org.geppetto.core.model.QueryProvider#getNumberOfResults(org.geppetto.core.model.Query, org.geppetto.model.variables.Variable)
168-
*/
169-
@Override
170-
public int getNumberOfResults(Query query, Variable variable) throws GeppettoDataSourceException
171-
{
172-
int count = -1;
173-
if(query instanceof CompoundRefQuery)
174-
{
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-
runQueryVisitor.countOnly(true);
188-
runQueryVisitor.doSwitch(query);
189-
QueryResults results = runQueryVisitor.getResults();
190-
// 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
191-
count = results.getResults().size();
192-
193-
}
194-
}
195-
return count;
196-
}
197-
198-
/**
199-
* @param query
200-
* @param variable
201-
* @return
202-
*/
203-
private String getKey(Query query, Variable variable)
204-
{
205-
return query.getPath() + ":" + variable.getPath();
206-
}
207-
208168
/*
209169
* (non-Javadoc)
210170
*

0 commit comments

Comments
 (0)