Browse Source

Started work on database integration. Cleaned up caching of thumbnails and improved performance of the thumb path method.

master
Drew Short 11 years ago
parent
commit
0fcad44e4e
  1. 23
      pom.xml
  2. 5
      src/main/resources/application.conf
  3. 34
      src/main/resources/hibernate.cfg.xml
  4. 23
      src/main/scala/com/sothr/imagetools/image/ImageService.scala
  5. 5
      src/test/resources/application.conf
  6. 7
      todo

23
pom.xml

@ -26,9 +26,6 @@
</pluginRepositories>
<properties>
<project.version.major>0</project.version.major>
<project.version.minor>1</project.version.minor>
<project.version.tag>DEV</project.version.tag>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdk.version>1.7</jdk.version>
<lib.junit.version>3.8.1</lib.junit.version>
@ -46,6 +43,8 @@
<lib.jtransforms.version>2.4.0</lib.jtransforms.version>
<lib.typesafe-config.version>1.2.0</lib.typesafe-config.version>
<lib.thumbnailator.version>[0.4, 0.5)</lib.thumbnailator.version>
<lib.h2database.version>1.3.175</lib.h2database.version>
<lib.hibernate.version>4.3.0.Final</lib.hibernate.version>
</properties>
<dependencies>
@ -136,6 +135,21 @@
<artifactId>akka-slf4j_2.10</artifactId>
<version>${lib.akka.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${lib.h2database.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${lib.hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${lib.hibernate.version}</version>
</dependency>
</dependencies>
<build>
@ -337,6 +351,9 @@
<phase>process-resources</phase>
<configuration>
<tasks>
<copy todir="${basedir}/src/test/resources/hibernate">
<fileset dir="${basedir}/src/main/resources/hibernate" includes="**/*" />
</copy>
<copy file="${project.build.directory}/version.info" toFile="${basedir}/version.info" overwrite="true" />
<copy file="${project.build.directory}/name.info" toFile="${basedir}/name.info" overwrite="true" />
<copy file="${project.build.directory}/LICENSE" toFile="${basedir}/LICENSE" overwrite="true" />

5
src/main/resources/application.conf

@ -52,4 +52,9 @@ app {
//Size of the thumbnail to generate and store
size = 128
}
//Default Database Settings
database {
location = ".cache/database.db"
inMemory = false
}
}

34
src/main/resources/hibernate.cfg.xml

@ -0,0 +1,34 @@
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- a SessionFactory instance listed as /jndi/name -->
<session-factory
name="java:hibernate/SessionFactory">
<!-- properties -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.datasource">jdbc:h2:.cache/database.db</property>
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<property name="show_sql">false</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JTATransactionFactory
</property>
<property name="jta.UserTransaction">java:comp/UserTransaction</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- mapping files -->
<!--<mapping resource="hibernate/"/>-->
<!-- cache settings -->
<!--<class-cache class="org.hibernate.auction.Item" usage="read-write"/>
<class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
<collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>-->
</session-factory>
</hibernate-configuration>

23
src/main/scala/com/sothr/imagetools/image/ImageService.scala

@ -14,7 +14,6 @@ import com.sothr.imagetools.util.{PropertiesEnum, PropertiesService}
object ImageService extends Logging {
val imageCache = AppConfig.cacheManager.getCache("images")
val thumbnailCache = AppConfig.cacheManager.getCache("thumbnails")
private def lookupImage(file:File):Image = {
var image:Image = null
@ -45,7 +44,7 @@ object ImageService extends Logging {
val bufferedImage = ImageIO.read(file)
val hashes = HashService.getImageHashes(bufferedImage, file.getAbsolutePath)
var thumbnailPath = lookupThumbnailPath(hashes.md5)
if (thumbnailPath == null) thumbnailPath = saveThumbnail(hashes.md5, getThumbnail(bufferedImage, hashes.md5))
if (thumbnailPath == null) thumbnailPath = getThumbnail(bufferedImage, hashes.md5)
val imageSize = { (bufferedImage.getWidth, bufferedImage.getHeight) }
val image = new Image(file.getAbsolutePath, thumbnailPath, imageSize, hashes)
debug(s"Created image: $image")
@ -61,8 +60,9 @@ object ImageService extends Logging {
def calculateThumbPath(md5:String):String = {
//break the path down into 4 char parts
val split:List[String] = md5.grouped(4).toList
var path:String = PropertiesService.get(PropertiesEnum.ThumbnailDirectory.toString) + PropertiesService.get(PropertiesEnum.ThumbnailSize.toString) + "/"
for (seg <- split) path += seg + "/"
var dirPath = ""
for (seg <- split) dirPath += seg + "/"
var path:String = s"${PropertiesService.get(PropertiesEnum.ThumbnailDirectory.toString)}${PropertiesService.get(PropertiesEnum.ThumbnailSize.toString)}/$dirPath"
try {
val dir = new File(path)
if (!dir.exists()) dir.mkdirs()
@ -76,13 +76,6 @@ object ImageService extends Logging {
def lookupThumbnailPath(md5:String):String = {
var thumbPath:String = null
if (md5 != null) {
//get from memory cache if possible
try {
if (thumbnailCache.isKeyInCache(md5)) thumbPath = thumbnailCache.get(md5).getObjectValue.asInstanceOf[String]
} catch {
case npe:NullPointerException => error(s"Error grabbing \'$md5\' from cache. thumbPath: \'$thumbPath\'", npe)
}
//get from datastore if possible
//check for the actual file
val checkPath = calculateThumbPath(md5)
if (new File(checkPath).exists) thumbPath = checkPath
@ -108,14 +101,6 @@ object ImageService extends Logging {
path
}
def saveThumbnail(md5:String, thumbnailPath:String):String = {
if (md5 == null || thumbnailPath == null) error(s"Error trying to save a md5: $md5 path: $thumbnailPath")
//save to cache
thumbnailCache.put(new Element(md5, thumbnailPath))
//save to datastore
thumbnailPath
}
/**
* Get the raw data for an image
*/

5
src/test/resources/application.conf

@ -52,4 +52,9 @@ app {
//Size of the thumbnail to generate and store
size = 128
}
//Default Database Settings
database {
location = ".cache/database.db"
inMemory = false
}
}

7
todo

@ -7,3 +7,10 @@ Add functionality to ImageService
Improve functionality of the PropertiesService
-Getters and Setters for sepcific values with proper cascading and type awareness
-Nicer debugging?
Hibernate - H2 intergration
- setup hibernate
- setup h2
- setup C3P0 connection pool
- transfer configuration files
- autoload configuration files?
- map DTO's
Loading…
Cancel
Save