modules/core/dbutils/src/main/scripts/dbsetup/content-schema.xml | 9
modules/core/domain/pom.xml | 7
modules/core/domain/src/main/java/org/rhq/core/domain/drift/Snapshot.java | 73
+++++-
modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java | 112
++++++++++
4 files changed, 190 insertions(+), 11 deletions(-)
New commits:
commit e306c304d2929f9c9b52bf48a1458153131d46c6
Author: John Sanda <jsanda(a)redhat.com>
Date: Tue May 24 22:05:02 2011 -0400
Increasing the number and size of snapshots to verify lazy loading
I increased the number of snapshots to 25 with data files of 100 MB
which comes in at about 2.4 GB of data. When the test fetches the
Snapshots, it stores them in a list so they are held onto for the
duration of the test. If those blob fields were not getting lazy loaded,
at 2.4 GB of data, I have to think that an OutOfMemeoryError would be
getting thrown.
diff --git a/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
b/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
index c642e6b..3e85479 100644
--- a/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
+++ b/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
@@ -46,8 +46,8 @@ public class SnapshotTest extends AbstractEJB3Test {
@Test(groups = "integration.ejb3")
public void loadMultipleSnapshotsWithoutLoadingData() throws Exception {
- File dataFile = createDataFile("test_data.txt", 50);
- int numSnapshots = 10;
+ File dataFile = createDataFile("test_data.txt", 100);
+ int numSnapshots = 25;
List<Integer> snapshotIds = new ArrayList<Integer>();
getTransactionManager().begin();
commit be9289ebce5229bcfeacec4597fe49be23c1a5dd
Author: John Sanda <jsanda(a)redhat.com>
Date: Tue May 24 21:43:56 2011 -0400
Initial commit for snapshot JPA mappings and dbsetup changes
This commit is more of a prototype to figure out how to map and lazy load the
data and meta data files. The hibernate docs state that hibernate will lazy load
a field that is of type java.sql.Blob. SnapshotTest has a couple tests
to figure out a couple things.
First, I wanted to make sure Hibernate.createBlob works with both oracle and
postgresql. I have successfully run SnapshotTest against oracle 10g express and
against postgresql 8.4.
Secondly, I want to make sure loading a Snapshot object does not eagerly load a
Blob. The test loadMultipleSnapshotsWithoutLoadingData creates and loads 10
snapshots each having a data file 50 MB in size.
diff --git a/modules/core/dbutils/src/main/scripts/dbsetup/content-schema.xml
b/modules/core/dbutils/src/main/scripts/dbsetup/content-schema.xml
index 8cd2711..94fe4c3 100644
--- a/modules/core/dbutils/src/main/scripts/dbsetup/content-schema.xml
+++ b/modules/core/dbutils/src/main/scripts/dbsetup/content-schema.xml
@@ -645,6 +645,13 @@
</primaryKey>
</constraint>
</table>
-
+
+ <table name="RHQ_SNAPSHOT">
+ <column name="ID" default="sequence-only"
initial="10001" primarykey="true" required="true"
type="INTEGER"/>
+ <column name="DATA" type="LARGEOBJECT"
required="false"/>
+ <column name="DATA_SIZE" type="LONG"/>
+ <column name="CTIME" type="LONG"
required="true"/>
+ </table>
+
</dbsetup>
diff --git a/modules/core/domain/pom.xml b/modules/core/domain/pom.xml
index 6bfa045..42d28e1 100644
--- a/modules/core/domain/pom.xml
+++ b/modules/core/domain/pom.xml
@@ -185,6 +185,13 @@
</dependency>
<dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.4</version>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
diff --git a/modules/core/domain/src/main/java/org/rhq/core/domain/drift/Snapshot.java
b/modules/core/domain/src/main/java/org/rhq/core/domain/drift/Snapshot.java
index bdac7c8..6fee160 100644
--- a/modules/core/domain/src/main/java/org/rhq/core/domain/drift/Snapshot.java
+++ b/modules/core/domain/src/main/java/org/rhq/core/domain/drift/Snapshot.java
@@ -2,25 +2,78 @@ package org.rhq.core.domain.drift;
import java.io.InputStream;
import java.io.Serializable;
-import java.util.HashMap;
-import java.util.Map;
+import java.sql.Blob;
+import java.sql.SQLException;
+import javax.persistence.CascadeType;
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.JoinColumn;
+import javax.persistence.Lob;
+import javax.persistence.OneToOne;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "RHQ_SNAPSHOT")
+@SequenceGenerator(name = "SEQ", sequenceName =
"RHQ_SNAPSHOT_ID_SEQ")
public class Snapshot implements Serializable {
private static final long serialVersionUID = 1L;
- private SnapshotMetadata metadata = new SnapshotMetadata();
+ @Id
+ @Column(name = "ID")
+ @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ")
+ private int id;
+
+ //private SnapshotMetadata metadata = new SnapshotMetadata();
+
+ @Lob
+ @Column(name = "DATA")
+ private Blob data;
- private InputStream data;
+ @Column(name = "DATA_SIZE")
+ private long dataSize;
+
+ @Column(name = "CTIME")
+ private long ctime = System.currentTimeMillis();
+
+ public int getId() {
+ return id;
+ }
+
+// public SnapshotMetadata getMetadata() {
+// return metadata;
+// }
+//
+// public InputStream getData() {
+// return data;
+// }
+//
+// public void setData(InputStream data) {
+// this.data = data;
+// }
+
+ public InputStream getData() throws SQLException {
+ return data.getBinaryStream();
+ }
+
+ public void setData(Blob blob) {
+ this.data = blob;
+ }
- public SnapshotMetadata getMetadata() {
- return metadata;
+ public long getDataSize() {
+ return dataSize;
}
- public InputStream getData() {
- return data;
+ public void setDataSize(long size) {
+ dataSize = size;
}
- public void setData(InputStream data) {
- this.data = data;
+ public long getCtime() {
+ return ctime;
}
}
diff --git a/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
b/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
new file mode 100644
index 0000000..c642e6b
--- /dev/null
+++ b/modules/core/domain/src/test/java/org/rhq/core/domain/drift/SnapshotTest.java
@@ -0,0 +1,112 @@
+package org.rhq.core.domain.drift;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.net.URISyntaxException;
+import java.sql.Connection;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.print.URIException;
+
+import org.apache.commons.io.IOUtils;
+import org.hibernate.Hibernate;
+import org.hibernate.Session;
+import org.testng.annotations.Test;
+
+import org.rhq.core.domain.test.AbstractEJB3Test;
+
+import static org.apache.commons.io.IOUtils.toInputStream;
+
+public class SnapshotTest extends AbstractEJB3Test {
+
+ @Test(groups = "integration.ejb3")
+ public void loadSnapshotWithoutData() throws Exception {
+ String content = "This is a test";
+
+ getTransactionManager().begin();
+ Snapshot snapshot = new Snapshot();
+ snapshot.setDataSize(content.length());
+ snapshot.setData(Hibernate.createBlob(toInputStream(content),
content.length()));
+
+ getEntityManager().persist(snapshot);
+ getTransactionManager().commit();
+
+ getTransactionManager().begin();
+ snapshot = getEntityManager().find(Snapshot.class, snapshot.getId());
+ getTransactionManager().commit();
+
+ System.out.println("data size = " + snapshot.getDataSize());
+ }
+
+ @Test(groups = "integration.ejb3")
+ public void loadMultipleSnapshotsWithoutLoadingData() throws Exception {
+ File dataFile = createDataFile("test_data.txt", 50);
+ int numSnapshots = 10;
+ List<Integer> snapshotIds = new ArrayList<Integer>();
+
+ getTransactionManager().begin();
+ for (int i = 0; i < numSnapshots; ++i) {
+ Snapshot snapshot = new Snapshot();
+ snapshot.setDataSize(dataFile.length());
+ snapshot.setData(Hibernate.createBlob(new BufferedInputStream(new
FileInputStream(dataFile))));
+
+ getEntityManager().persist(snapshot);
+ snapshotIds.add(snapshot.getId());
+ }
+ getTransactionManager().commit();
+
+ getTransactionManager().begin();
+ List<Snapshot> snapshots = new ArrayList<Snapshot>();
+ for (Integer id : snapshotIds) {
+ snapshots.add(getEntityManager().find(Snapshot.class, id));
+ }
+ getTransactionManager().commit();
+ }
+
+ File snapshotDir() throws URISyntaxException {
+ File dir = new File(new File(getClass().getResource(".").toURI()),
"snapshots");
+ dir.mkdir();
+ return dir;
+ }
+
+ File workDir() throws URISyntaxException {
+ File dir = new File(new File(getClass().getResource(".").toURI()),
"work");
+ dir.mkdir();
+ return dir;
+ }
+
+ /**
+ * Creates a file in {@link #workDir()} that is filled with arbitrary data up to
<code>size</code> in megabytes.
+ *
+ * @param name The name of the file to create
+ * @param size The size of the file in megabytes
+ * @return The generated file
+ */
+ File createDataFile(String name, int size) throws Exception {
+ File file = new File(workDir(), name);
+
+ long oneMB = 1048576;
+ long sizeInBytes = size * oneMB;
+ int lineSize = 80; // size in bytes
+ long numLines = sizeInBytes / lineSize;
+
+ PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)));
+
+ for (int i = 0; i < numLines; ++i) {
+ StringBuilder line = new StringBuilder();
+ for (int j = 1; j < lineSize; ++j) {
+ line.append('x');
+ }
+ writer.println(line.toString());
+ }
+ writer.close();
+ return file;
+ }
+
+}
Show replies by date