3333package org .geppetto .datasources ;
3434
3535import java .util .ArrayList ;
36+ import java .util .LinkedHashMap ;
3637import java .util .List ;
3738
3839import org .geppetto .core .datasources .GeppettoDataSourceException ;
3940import org .geppetto .core .datasources .IDataSourceService ;
40- import org .geppetto .core .datasources .IQueryListener ;
4141import org .geppetto .core .datasources .QueryChecker ;
4242import org .geppetto .core .model .GeppettoModelAccess ;
4343import org .geppetto .core .services .AService ;
44+ import org .geppetto .model .datasources .CompoundRefQuery ;
4445import org .geppetto .model .datasources .DataSource ;
4546import org .geppetto .model .datasources .Query ;
4647import 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 ;
4949import org .geppetto .model .variables .Variable ;
5050import 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}
0 commit comments