Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/gtfs/CsvTableInserter.kt
Show All 23 Lines | |||||
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 dao: StaticGtfsDao = database.gtfsDao() | ||||
private val elementsList: MutableList< in GtfsTable> = mutableListOf() | private val elementsList: MutableList< in GtfsTable> = mutableListOf() | ||||
private var stopsIDsPresent: HashSet<Int>? = null | |||||
private var tripsIDsPresent: HashSet<String>? = null | |||||
private var countInsert = 0 | |||||
init { | |||||
if(tableName == "stop_times") { | |||||
stopsIDsPresent = dao.getAllStopsIDs().toHashSet() | |||||
tripsIDsPresent = dao.getAllTripsIDs().toHashSet() | |||||
Log.d(DEBUG_TAG, "num stop IDs present: "+ stopsIDsPresent!!.size) | |||||
Log.d(DEBUG_TAG, "num trips IDs present: "+ tripsIDsPresent!!.size) | |||||
} else if(tableName == "routes"){ | |||||
dao.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)) | ||||
"routes" -> | "routes" -> | ||||
elementsList.add(GtfsRoute(csvLineElements)) | elementsList.add(GtfsRoute(csvLineElements)) | ||||
"calendar" -> | "calendar" -> | ||||
elementsList.add(GtfsService(csvLineElements)) | elementsList.add(GtfsService(csvLineElements)) | ||||
"calendar_dates" -> | "calendar_dates" -> | ||||
elementsList.add(GtfsServiceDate(csvLineElements)) | elementsList.add(GtfsServiceDate(csvLineElements)) | ||||
"trips" -> | "trips" -> | ||||
elementsList.add(GtfsTrip(csvLineElements)) | elementsList.add(GtfsTrip(csvLineElements)) | ||||
"shapes" -> | "shapes" -> | ||||
elementsList.add(GtfsShape(csvLineElements)) | elementsList.add(GtfsShape(csvLineElements)) | ||||
"stop_times" -> | "stop_times" -> { | ||||
elementsList.add(GtfsStopTime(csvLineElements)) | //filter stop | ||||
val stopTime = GtfsStopTime(csvLineElements) | |||||
/* | |||||
val stopOk = //tripsIDsPresent?.contains(stopTime.tripID) == true | |||||
(stopsIDsPresent?.contains(stopTime.stopID) == true)// && | |||||
// tripsIDsPresent?.contains(stopTime.tripID) == true) | |||||
if (stopOk) | |||||
*/ | |||||
elementsList.add(stopTime) | |||||
} | |||||
} | } | ||||
if(elementsList.size >= MAX_ELEMENTS){ | if(elementsList.size >= MAX_ELEMENTS){ | ||||
//have to insert | //have to insert | ||||
Log.d(DEBUG_TAG, "Inserting first batch of elements now, list size: "+elementsList.size) | |||||
if (tableName == "routes") | if (tableName == "routes") | ||||
dao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | dao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | ||||
else | else | ||||
insertDataInDatabase() | insertDataInDatabase() | ||||
elementsList.clear() | elementsList.clear() | ||||
} | } | ||||
} | } | ||||
fun insertDataInDatabase(){ | private fun insertDataInDatabase(){ | ||||
//Log.d(DEBUG_TAG, "Inserting batch of elements now, list size: "+elementsList.size) | |||||
countInsert += elementsList.size | |||||
when(tableName){ | when(tableName){ | ||||
"stops" -> dao.updateStops(elementsList.filterIsInstance<GtfsStop>()) | "stops" -> { | ||||
"routes" -> dao.clearAndInsertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | dao.insertStops(elementsList.filterIsInstance<GtfsStop>()) | ||||
} | |||||
"routes" -> dao.insertRoutes(elementsList.filterIsInstance<GtfsRoute>()) | |||||
"calendar" -> dao.insertServices(elementsList.filterIsInstance<GtfsService>()) | "calendar" -> dao.insertServices(elementsList.filterIsInstance<GtfsService>()) | ||||
"calendar_dates" -> dao.insertDates(elementsList.filterIsInstance<GtfsServiceDate>()) | "calendar_dates" -> dao.insertDates(elementsList.filterIsInstance<GtfsServiceDate>()) | ||||
"trips" -> dao.insertTrips(elementsList.filterIsInstance<GtfsTrip>()) | "trips" -> dao.insertTrips(elementsList.filterIsInstance<GtfsTrip>()) | ||||
"stop_times"-> dao.insertStopTimes(elementsList.filterIsInstance<GtfsStopTime>()) | "stop_times"-> dao.insertStopTimes(elementsList.filterIsInstance<GtfsStopTime>()) | ||||
"shapes" -> dao.insertShapes(elementsList.filterIsInstance<GtfsShape>()) | "shapes" -> dao.insertShapes(elementsList.filterIsInstance<GtfsShape>()) | ||||
} | } | ||||
///if(elementsList.size < MAX_ELEMENTS) | |||||
} | |||||
fun finishInsert(){ | |||||
insertDataInDatabase() | |||||
Log.d(DEBUG_TAG, "Inserted "+countInsert+" elements from "+tableName); | |||||
} | } | ||||
companion object{ | companion object{ | ||||
val MAX_ELEMENTS = 5000 | const val MAX_ELEMENTS = 5000 | ||||
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