Skip to content

Commit 41c8a7a

Browse files
committed
2 parents 7d87336 + 1bf72e9 commit 41c8a7a

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.github.mauricio.async.db.mysql.blob
2+
3+
import java.io.{BufferedOutputStream, File, FileInputStream, FileOutputStream}
4+
import java.nio.ByteBuffer
5+
6+
import com.github.mauricio.async.db.mysql.ConnectionHelper
7+
import io.netty.buffer.Unpooled
8+
import org.specs2.mutable.{After, Specification}
9+
import org.specs2.specification.Scope
10+
11+
class LargeBlobSpec extends Specification with ConnectionHelper {
12+
13+
val create = """CREATE TEMPORARY TABLE t (
14+
| id BIGINT NOT NULL AUTO_INCREMENT,
15+
| theblob LONGBLOB NOT NULL,
16+
| PRIMARY KEY (id)
17+
|);""".stripMargin
18+
19+
val preparedInsert = "INSERT INTO t (theblob) VALUES (?)"
20+
// val select = "SELECT theblob FROM t WHERE ID=?"
21+
22+
"connection" should {
23+
24+
"handle large BLOBs from InputStream" in new BlobFile {
25+
26+
withConnection {
27+
connection =>
28+
executeQuery(connection, create)
29+
30+
val stream = new FileInputStream(blobFile)
31+
executePreparedStatement(connection, preparedInsert, stream)
32+
}
33+
34+
}
35+
36+
"handle BLOBs from ByteBuffer" in new BlobBuffer {
37+
38+
val preparedInsert = "INSERT INTO t (theblob) VALUES (?)"
39+
// val select = "SELECT theblob FROM t WHERE ID=?"
40+
41+
withConnection {
42+
connection =>
43+
executeQuery(connection, create)
44+
45+
executePreparedStatement(connection, preparedInsert, blobBuffer)
46+
}
47+
48+
}
49+
50+
"handle BLOBs from ByteBuf" in new BlobBuf {
51+
52+
val preparedInsert = "INSERT INTO t (theblob) VALUES (?)"
53+
// val select = "SELECT theblob FROM t WHERE ID=?"
54+
55+
withConnection {
56+
connection =>
57+
executeQuery(connection, create)
58+
59+
executePreparedStatement(connection, preparedInsert, blobBuf)
60+
}
61+
62+
}
63+
64+
}
65+
66+
}
67+
68+
trait BlobFile extends After {
69+
lazy val blobFile = {
70+
val file = File.createTempFile("blob1", null)
71+
val bos = new BufferedOutputStream(new FileOutputStream(file))
72+
0 to ((16 * 1024 * 1024)-1) foreach { n => bos.write(n & 128) }
73+
bos.close()
74+
file
75+
}
76+
77+
// lazy val outFile = File.createTempFile("blob2", null)
78+
79+
def after = {
80+
blobFile.delete()
81+
// outFile.delete()
82+
}
83+
}
84+
85+
trait BlobBuffer extends Scope {
86+
lazy val blobBuffer = {
87+
val array = new Array[Byte](1024)
88+
0 to (1024-1) foreach { n => array(n) = (n & 128).asInstanceOf[Byte] }
89+
ByteBuffer.wrap(array)
90+
}
91+
}
92+
93+
trait BlobBuf extends Scope {
94+
lazy val blobBuf = {
95+
val array = new Array[Byte](1024)
96+
0 to (1024-1) foreach { n => array(n) = (n & 128).asInstanceOf[Byte] }
97+
Unpooled.copiedBuffer(array)
98+
}
99+
}

0 commit comments

Comments
 (0)