Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/gtfs/CsvTableInserter.kt
Show All 13 Lines | BusTO - Data components | ||||
You should have received a copy of the GNU General Public License | You should have received a copy of the GNU General Public License | ||||
along with this program. If not, see <http://www.gnu.org/licenses/>. | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
package it.reyboz.bustorino.data.gtfs | package it.reyboz.bustorino.data.gtfs | ||||
import android.content.Context | import android.content.Context | ||||
import android.util.Log | import android.util.Log | ||||
import java.util.ArrayList | |||||
class CsvTableInserter( | class CsvTableInserter( | ||||
val tableName: String, context: Context | val tableName: String, context: Context | ||||
) { | ) { | ||||
private val database: GtfsDatabase = GtfsDatabase.getGtfsDatabase(context) | private val database: GtfsDatabase = GtfsDatabase.getGtfsDatabase(context) | ||||
private val dao: StaticGtfsDao = database.gtfsDao() | private val databaseDao: GtfsDBDao = database.gtfsDao() | ||||
private val elementsList: MutableList< in GtfsTable> = mutableListOf() | private val elementsList: MutableList< in GtfsTable> = mutableListOf() | ||||
private var stopsIDsPresent: HashSet<Int>? = null | private var stopsIDsPresent: HashSet<Int>? = null | ||||
private var tripsIDsPresent: HashSet<String>? = null | private var tripsIDsPresent: HashSet<String>? = null | ||||
private var countInsert = 0 | private var countInsert = 0 | ||||
init { | init { | ||||
if(tableName == "stop_times") { | if(tableName == "stop_times") { | ||||
stopsIDsPresent = dao.getAllStopsIDs().toHashSet() | stopsIDsPresent = databaseDao.getAllStopsIDs().toHashSet() | ||||
tripsIDsPresent = dao.getAllTripsIDs().toHashSet() | tripsIDsPresent = databaseDao.getAllTripsIDs().toHashSet() | ||||
Log.d(DEBUG_TAG, "num stop IDs present: "+ stopsIDsPresent!!.size) | Log.d(DEBUG_TAG, "num stop IDs present: "+ stopsIDsPresent!!.size) | ||||
Log.d(DEBUG_TAG, "num trips IDs present: "+ tripsIDsPresent!!.size) | Log.d(DEBUG_TAG, "num trips IDs present: "+ tripsIDsPresent!!.size) | ||||
} else if(tableName == "routes"){ | } else if(tableName == "routes"){ | ||||
dao.deleteAllRoutes() | databaseDao.deleteAllRoutes() | ||||
} | } | ||||
} | } | ||||
fun addElement(csvLineElements: Map<String,String>) { | fun addElement(csvLineElements: Map<String,String>) { | ||||
when(tableName){ | when(tableName){ | ||||
"stops" -> | "stops" -> | ||||
elementsList.add(GtfsStop(csvLineElements)) | elementsList.add(GtfsStop(csvLineElements)) | ||||
Show All 20 Lines | fun addElement(csvLineElements: Map<String,String>) { | ||||
} | } | ||||
} | } | ||||
if(elementsList.size >= MAX_ELEMENTS){ | if(elementsList.size >= MAX_ELEMENTS){ | ||||
//have to insert | //have to insert | ||||
if (tableName == "routes") | if (tableName == "routes") | ||||
dao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | databaseDao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | ||||
else | else | ||||
insertDataInDatabase() | insertDataInDatabase() | ||||
elementsList.clear() | elementsList.clear() | ||||
} | } | ||||
} | } | ||||
private fun insertDataInDatabase(){ | private fun insertDataInDatabase(){ | ||||
//Log.d(DEBUG_TAG, "Inserting batch of elements now, list size: "+elementsList.size) | //Log.d(DEBUG_TAG, "Inserting batch of elements now, list size: "+elementsList.size) | ||||
countInsert += elementsList.size | countInsert += elementsList.size | ||||
when(tableName){ | when(tableName){ | ||||
"stops" -> { | "stops" -> { | ||||
dao.insertStops(elementsList.filterIsInstance<GtfsStop>()) | databaseDao.insertStops(elementsList.filterIsInstance<GtfsStop>()) | ||||
} | } | ||||
"routes" -> dao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | "routes" -> databaseDao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | ||||
"calendar" -> dao.insertServices(elementsList.filterIsInstance<GtfsService>()) | "calendar" -> databaseDao.insertServices(elementsList.filterIsInstance<GtfsService>()) | ||||
"calendar_dates" -> dao.insertDates(elementsList.filterIsInstance<GtfsServiceDate>()) | "calendar_dates" -> databaseDao.insertDates(elementsList.filterIsInstance<GtfsServiceDate>()) | ||||
"trips" -> dao.insertTrips(elementsList.filterIsInstance<GtfsTrip>()) | "trips" -> databaseDao.insertTrips(elementsList.filterIsInstance<GtfsTrip>()) | ||||
"stop_times"-> dao.insertStopTimes(elementsList.filterIsInstance<GtfsStopTime>()) | "stop_times"-> databaseDao.insertStopTimes(elementsList.filterIsInstance<GtfsStopTime>()) | ||||
"shapes" -> dao.insertShapes(elementsList.filterIsInstance<GtfsShape>()) | "shapes" -> databaseDao.insertShapes(elementsList.filterIsInstance<GtfsShape>()) | ||||
} | } | ||||
///if(elementsList.size < MAX_ELEMENTS) | ///if(elementsList.size < MAX_ELEMENTS) | ||||
} | } | ||||
fun finishInsert(){ | fun finishInsert(){ | ||||
insertDataInDatabase() | insertDataInDatabase() | ||||
Log.d(DEBUG_TAG, "Inserted "+countInsert+" elements from "+tableName); | Log.d(DEBUG_TAG, "Inserted $countInsert elements from $tableName") | ||||
} | } | ||||
companion object{ | companion object{ | ||||
const val MAX_ELEMENTS = 5000 | const val MAX_ELEMENTS = 5000 | ||||
const val DEBUG_TAG="BusTO - TableInserter" | const val DEBUG_TAG="BusTO - TableInserter" | ||||
} | } | ||||
} | } | ||||
No newline at end of file | No newline at end of file |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register