Improve performance when importing database

pull/452/head^2
Alinson S. Xavier 7 years ago
parent 3581173193
commit 178061475e

@ -24,6 +24,8 @@ import android.support.annotation.*;
import com.google.auto.factory.*;
import org.isoron.uhabits.core.io.*;
import org.isoron.uhabits.core.models.ModelFactory;
import org.isoron.uhabits.core.models.sqlite.SQLModelFactory;
import org.isoron.uhabits.core.tasks.*;
import java.io.*;
@ -37,21 +39,25 @@ public class ImportDataTask implements Task
public static final int SUCCESS = 1;
int result;
private int result;
@NonNull
private final File file;
private GenericImporter importer;
private SQLModelFactory modelFactory;
@NonNull
private final Listener listener;
public ImportDataTask(@Provided @NonNull GenericImporter importer,
@Provided @NonNull ModelFactory modelFactory,
@NonNull File file,
@NonNull Listener listener)
{
this.importer = importer;
this.modelFactory = (SQLModelFactory) modelFactory;
this.listener = listener;
this.file = file;
}
@ -59,12 +65,15 @@ public class ImportDataTask implements Task
@Override
public void doInBackground()
{
modelFactory.db.beginTransaction();
try
{
if (importer.canHandle(file))
{
importer.importHabitsFromFile(file);
result = SUCCESS;
modelFactory.db.setTransactionSuccessful();
}
else
{
@ -76,6 +85,8 @@ public class ImportDataTask implements Task
result = FAILED;
e.printStackTrace();
}
modelFactory.db.endTransaction();
}
@Override

@ -246,17 +246,28 @@ public class Repository<T>
return fields;
}
private Field[] cacheFields = null;
@NonNull
private Field[] getFields()
{
if (cacheFields == null)
{
List<Field> fields = new ArrayList<>();
List<Pair<Field, Column>> columns = getFieldColumnPairs();
for (Pair<Field, Column> pair : columns) fields.add(pair.getLeft());
return fields.toArray(new Field[]{});
cacheFields = fields.toArray(new Field[]{});
}
return cacheFields;
}
private String[] cacheColumnNames = null;
@NonNull
private String[] getColumnNames()
{
if (cacheColumnNames == null)
{
List<String> names = new ArrayList<>();
List<Pair<Field, Column>> columns = getFieldColumnPairs();
@ -269,35 +280,64 @@ public class Repository<T>
names.add(cname);
}
return names.toArray(new String[]{});
cacheColumnNames = names.toArray(new String[]{});
}
return cacheColumnNames;
}
private String cacheTableName = null;
@NonNull
private String getTableName()
{
if (cacheTableName == null)
{
String name = getTableAnnotation().name();
if (name.isEmpty()) throw new RuntimeException("Table name is empty");
return name;
cacheTableName = name;
}
return cacheTableName;
}
private String cacheIdName = null;
@NonNull
private String getIdName()
{
if (cacheIdName == null)
{
String id = getTableAnnotation().id();
if (id.isEmpty()) throw new RuntimeException("Table id is empty");
return id;
cacheIdName = id;
}
return cacheIdName;
}
private Field cacheIdField = null;
@NonNull
private Field getIdField()
{
if (cacheIdField == null)
{
Field fields[] = getFields();
String idName = getIdName();
for (Field f : fields)
if (f.getName().equals(idName)) return f;
if (f.getName().equals(idName))
{
cacheIdField = f;
break;
}
if (cacheIdField == null)
throw new RuntimeException("Field not found: " + idName);
}
return cacheIdField;
}
@NonNull
private Table getTableAnnotation()
{

@ -33,7 +33,7 @@ import javax.inject.*;
*/
public class SQLModelFactory implements ModelFactory
{
private final Database db;
public final Database db;
@Inject
public SQLModelFactory(Database db)

Loading…
Cancel
Save