forked from olap4j/olap4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes the recording proxy cache to a more fancy one using hsqldb.
git-svn-id: https://olap4j.svn.sourceforge.net/svnroot/olap4j/trunk@422 c6a108a4-781c-0410-a6c6-c2d559e19af0
- Loading branch information
1 parent
7915295
commit 0f53c0a
Showing
5 changed files
with
207 additions
and
162 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
testsrc/org/olap4j/driver/xmla/cache/XmlaDatabaseCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
package org.olap4j.driver.xmla.cache; | ||
|
||
import java.net.URL; | ||
import java.sql.Connection; | ||
import java.sql.DriverManager; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Statement; | ||
import java.util.Map; | ||
|
||
import org.olap4j.impl.Base64; | ||
|
||
/** | ||
* This mock server cache is only used to save and load | ||
* runs of the XMLA driver as a database table. | ||
* @see Properties | ||
* @author LBoudreau | ||
*/ | ||
public class XmlaDatabaseCache implements XmlaOlap4jCache { | ||
|
||
private static Connection connection = null; | ||
private final static String CACHE_IDENT = "Panda steak!"; | ||
private Map<String, String> props; | ||
|
||
public static enum Properties { | ||
|
||
/** | ||
* Jdbc driver class to use. Defaults to | ||
* <code>org.hsqldb.jdbcDriver</code> | ||
*/ | ||
|
||
JDBC_DRIVER("org.hsqldb.jdbcDriver"), | ||
/** | ||
* Jdbc url to use. Defaults to | ||
* <code>jdbc:hsqldb:file:xmla-cache/xmla-cache-hsqldb</code> | ||
*/ | ||
JDBC_URL("jdbc:hsqldb:file:xmla-cache/xmla-cache-hsqldb"), | ||
|
||
/** | ||
* Jdbc username to use. Defaults to | ||
* <code>sa</code> | ||
*/ | ||
JDBC_USER("sa"), | ||
|
||
/** | ||
* Jdbc password to use. Defaults to | ||
* an empty string | ||
*/ | ||
JDBC_PASS(""), | ||
|
||
/** | ||
* Query to execute to insert elements. Defaults to | ||
* <code>insert into "cache" ("request", "response") values(?,?);</code> | ||
*/ | ||
QUERY_INSERT( | ||
"insert into \"cache\"(\"request\", \"response\") values(?,?);"), | ||
|
||
/** | ||
* Query to execute to select elements. Defaults to | ||
* <code>select "request", "response" from "cache" | ||
* where "request" = ?;</code> | ||
*/ | ||
QUERY_SELECT( | ||
"select \"request\", \"response\" from \"cache\" where" | ||
+ "\"request\" = ?;"), | ||
|
||
/** | ||
* Query to initialize the cache. Can be a batch of SQL. Defaults to | ||
* <code>create table "cache" ("request" | ||
* varchar, "response" varchar);</code> | ||
*/ | ||
QUERY_INIT_CACHE( | ||
"drop table \"cache\" if exists; create table \"cache\" (\"request\" varchar, \"response\" varchar);"), | ||
|
||
/** | ||
* Should the cache insert requests/responses. | ||
* defaults to false. | ||
*/ | ||
RECORD("false"), | ||
|
||
/** | ||
* Should the cache return cached responses. | ||
* defaults to false. | ||
*/ | ||
PLAY("false"), | ||
|
||
/** | ||
* Should the cache execute Properties.QUERY_INIT_CACHE. | ||
* defaults to false. | ||
*/ | ||
INIT("false"); | ||
|
||
private final String defaultValue; | ||
|
||
private Properties(String defaultValue) { | ||
this.defaultValue = defaultValue; | ||
} | ||
String getValueOrDefault(Map<String, String> props) { | ||
if (props.containsKey(this.name())) { | ||
return props.get(name()); | ||
} else { | ||
return this.defaultValue; | ||
} | ||
} | ||
} | ||
|
||
public void flushCache() { | ||
// no op | ||
} | ||
|
||
public byte[] get(String id, URL url, byte[] request) | ||
throws XmlaOlap4jInvalidStateException | ||
{ | ||
if (!Boolean.valueOf(Properties.PLAY.getValueOrDefault(props))) { | ||
return null; | ||
} | ||
try { | ||
final PreparedStatement stm = | ||
connection.prepareStatement( | ||
Properties.QUERY_SELECT.getValueOrDefault(props)); | ||
try { | ||
stm.setString( | ||
1, | ||
XmlaOlap4jShaEncoder.encodeSha1(new String(request))); | ||
stm.execute(); | ||
ResultSet rs = stm.getResultSet(); | ||
if (rs.next()) { | ||
return Base64.decode(rs.getString(2)); | ||
} else { | ||
return null; | ||
} | ||
} finally { | ||
stm.close(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void put(String id, URL url, byte[] request, byte[] response) | ||
throws XmlaOlap4jInvalidStateException | ||
{ | ||
if (!Boolean.valueOf(Properties.RECORD.getValueOrDefault(props))) { | ||
return; | ||
} | ||
try { | ||
final PreparedStatement stm = | ||
connection.prepareStatement( | ||
Properties.QUERY_INSERT.getValueOrDefault(props)); | ||
try { | ||
stm.setString( | ||
1, | ||
XmlaOlap4jShaEncoder.encodeSha1(new String(request))); | ||
stm.setString(2, Base64.encodeBytes(response)); | ||
stm.execute(); | ||
} finally { | ||
stm.close(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public String setParameters( | ||
Map<String, String> config, | ||
Map<String, String> props) | ||
{ | ||
this.props = props; | ||
if (connection == null) { | ||
try { | ||
Class.forName(Properties.JDBC_DRIVER.getValueOrDefault(props)); | ||
connection = | ||
DriverManager.getConnection( | ||
Properties.JDBC_URL.getValueOrDefault(props), | ||
Properties.JDBC_USER.getValueOrDefault(props), | ||
Properties.JDBC_PASS.getValueOrDefault(props)); | ||
if (Boolean.valueOf( | ||
Properties.INIT.getValueOrDefault(props))) | ||
{ | ||
final Statement stm = connection.createStatement(); | ||
try { | ||
stm.addBatch( | ||
Properties.QUERY_INIT_CACHE | ||
.getValueOrDefault(props)); | ||
stm.executeBatch(); | ||
} catch (SQLException e) { | ||
// no op | ||
} finally { | ||
stm.close(); | ||
} | ||
flushCache(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
return CACHE_IDENT; | ||
} | ||
} |
156 changes: 0 additions & 156 deletions
156
testsrc/org/olap4j/driver/xmla/cache/XmlaTextFileCache.java
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.