com.j256.ormlite.android.apptools
Class OrmLiteConfigUtil

java.lang.Object
  extended by com.j256.ormlite.android.apptools.OrmLiteConfigUtil

public class OrmLiteConfigUtil
extends Object

Database configuration file helper class that is used to write a configuration file into the raw resource sub-directory to speed up DAO creation.

With help from the user list and especially Ian Dees, we discovered that calls to annotation methods in Android are _very_ expensive because Method.equals() was doing a huge toString(). This was causing folks to see 2-3 seconds startup time when configuring 10-15 DAOs because of 1000s of calls to @DatabaseField methods. See this Android bug report.

I added this utility class which writes a configuration file into the raw resource "res/raw" directory inside of your project containing the table and field names and associated details. This file can then be loaded into the DaoManager with the help of the OrmLiteSqliteOpenHelper.OrmLiteSqliteOpenHelper(android.content.Context, String, android.database.sqlite.SQLiteDatabase.CursorFactory, int, int) constructor. This means that you can configure your classes _without_ any runtime calls to annotations. It seems significantly faster.

WARNING: Although this is fast, the big problem is that you have to remember to regenerate the config file whenever you edit one of your database classes. There is no way that I know of to do this automagically.

Author:
graywatson

Field Summary
protected static int maxFindSourceLevel
          Maximum recursion level while we are looking for source files.
protected static String RAW_DIR_NAME
          Raw directory name that we are looking for.
protected static String RESOURCE_DIR_NAME
          Resource directory name that we are looking for.
 
Constructor Summary
OrmLiteConfigUtil()
           
 
Method Summary
protected static File findRawDir(File dir)
          Look for the resource-directory in the current directory or the directories above.
static void main(String[] args)
          A call through to writeConfigFile(String) taking the file name from the single command line argument.
static void writeConfigFile(File configFile)
          Finds the annotated classes in the current directory or below and writes a configuration file.
static void writeConfigFile(File configFile, Class<?>[] classes)
          Write a configuration file with the configuration for classes.
static void writeConfigFile(OutputStream outputStream, Class<?>[] classes)
          Write a configuration file to an output stream with the configuration for classes.
static void writeConfigFile(String fileName)
          Finds the annotated classes in the current directory or below and writes a configuration file to the file-name in the raw folder.
static void writeConfigFile(String fileName, Class<?>[] classes)
          Writes a configuration fileName in the raw directory with the configuration for classes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RESOURCE_DIR_NAME

protected static final String RESOURCE_DIR_NAME
Resource directory name that we are looking for.

See Also:
Constant Field Values

RAW_DIR_NAME

protected static final String RAW_DIR_NAME
Raw directory name that we are looking for.

See Also:
Constant Field Values

maxFindSourceLevel

protected static int maxFindSourceLevel
Maximum recursion level while we are looking for source files.

Constructor Detail

OrmLiteConfigUtil

public OrmLiteConfigUtil()
Method Detail

main

public static void main(String[] args)
                 throws Exception
A call through to writeConfigFile(String) taking the file name from the single command line argument.

Throws:
Exception

writeConfigFile

public static void writeConfigFile(String fileName)
                            throws SQLException,
                                   IOException
Finds the annotated classes in the current directory or below and writes a configuration file to the file-name in the raw folder.

Throws:
SQLException
IOException

writeConfigFile

public static void writeConfigFile(File configFile)
                            throws SQLException,
                                   IOException
Finds the annotated classes in the current directory or below and writes a configuration file.

Throws:
SQLException
IOException

writeConfigFile

public static void writeConfigFile(String fileName,
                                   Class<?>[] classes)
                            throws SQLException,
                                   IOException
Writes a configuration fileName in the raw directory with the configuration for classes.

Throws:
SQLException
IOException

writeConfigFile

public static void writeConfigFile(File configFile,
                                   Class<?>[] classes)
                            throws SQLException,
                                   IOException
Write a configuration file with the configuration for classes.

Throws:
SQLException
IOException

writeConfigFile

public static void writeConfigFile(OutputStream outputStream,
                                   Class<?>[] classes)
                            throws SQLException,
                                   IOException
Write a configuration file to an output stream with the configuration for classes.

Throws:
SQLException
IOException

findRawDir

protected static File findRawDir(File dir)
Look for the resource-directory in the current directory or the directories above. Then look for the raw-directory underneath the resource-directory.



This content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.