mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 01:08:50 -06:00
Improve performance when importing database
This commit is contained in:
@@ -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,56 +246,96 @@ public class Repository<T>
|
|||||||
return fields;
|
return fields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Field[] cacheFields = null;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Field[] getFields()
|
private Field[] getFields()
|
||||||
{
|
{
|
||||||
List<Field> fields = new ArrayList<>();
|
if (cacheFields == null)
|
||||||
List<Pair<Field, Column>> columns = getFieldColumnPairs();
|
{
|
||||||
for (Pair<Field, Column> pair : columns) fields.add(pair.getLeft());
|
List<Field> fields = new ArrayList<>();
|
||||||
return fields.toArray(new Field[]{});
|
List<Pair<Field, Column>> columns = getFieldColumnPairs();
|
||||||
|
for (Pair<Field, Column> pair : columns) fields.add(pair.getLeft());
|
||||||
|
cacheFields = fields.toArray(new Field[]{});
|
||||||
|
}
|
||||||
|
|
||||||
|
return cacheFields;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String[] cacheColumnNames = null;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private String[] getColumnNames()
|
private String[] getColumnNames()
|
||||||
{
|
{
|
||||||
List<String> names = new ArrayList<>();
|
if (cacheColumnNames == null)
|
||||||
List<Pair<Field, Column>> columns = getFieldColumnPairs();
|
|
||||||
for (Pair<Field, Column> pair : columns)
|
|
||||||
{
|
{
|
||||||
String cname = pair.getRight().name();
|
List<String> names = new ArrayList<>();
|
||||||
if (cname.isEmpty()) cname = pair.getLeft().getName();
|
List<Pair<Field, Column>> columns = getFieldColumnPairs();
|
||||||
if (names.contains(cname))
|
for (Pair<Field, Column> pair : columns)
|
||||||
throw new RuntimeException("duplicated column : " + cname);
|
{
|
||||||
names.add(cname);
|
String cname = pair.getRight().name();
|
||||||
|
if (cname.isEmpty()) cname = pair.getLeft().getName();
|
||||||
|
if (names.contains(cname))
|
||||||
|
throw new RuntimeException("duplicated column : " + cname);
|
||||||
|
names.add(cname);
|
||||||
|
}
|
||||||
|
|
||||||
|
cacheColumnNames = names.toArray(new String[]{});
|
||||||
}
|
}
|
||||||
|
|
||||||
return names.toArray(new String[]{});
|
return cacheColumnNames;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String cacheTableName = null;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private String getTableName()
|
private String getTableName()
|
||||||
{
|
{
|
||||||
String name = getTableAnnotation().name();
|
if (cacheTableName == null)
|
||||||
if (name.isEmpty()) throw new RuntimeException("Table name is empty");
|
{
|
||||||
return name;
|
String name = getTableAnnotation().name();
|
||||||
|
if (name.isEmpty()) throw new RuntimeException("Table name is empty");
|
||||||
|
cacheTableName = name;
|
||||||
|
}
|
||||||
|
return cacheTableName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String cacheIdName = null;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private String getIdName()
|
private String getIdName()
|
||||||
{
|
{
|
||||||
String id = getTableAnnotation().id();
|
if (cacheIdName == null)
|
||||||
if (id.isEmpty()) throw new RuntimeException("Table id is empty");
|
{
|
||||||
return id;
|
String id = getTableAnnotation().id();
|
||||||
|
if (id.isEmpty()) throw new RuntimeException("Table id is empty");
|
||||||
|
cacheIdName = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
return cacheIdName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Field cacheIdField = null;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private Field getIdField()
|
private Field getIdField()
|
||||||
{
|
{
|
||||||
Field fields[] = getFields();
|
if (cacheIdField == null)
|
||||||
String idName = getIdName();
|
{
|
||||||
for (Field f : fields)
|
Field fields[] = getFields();
|
||||||
if (f.getName().equals(idName)) return f;
|
String idName = getIdName();
|
||||||
throw new RuntimeException("Field not found: " + idName);
|
for (Field f : fields)
|
||||||
|
if (f.getName().equals(idName))
|
||||||
|
{
|
||||||
|
cacheIdField = f;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cacheIdField == null)
|
||||||
|
throw new RuntimeException("Field not found: " + idName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return cacheIdField;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
Reference in New Issue
Block a user