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

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

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

Loading…
Cancel
Save