@ -254,36 +254,7 @@ public class CacheImporter @Inject constructor(
) : List < Int > {
) : List < Int > {
return database . execute { connection ->
return database . execute { connection ->
prepare ( connection )
prepare ( connection )
addIndex ( connection , Index ( archive , index , buf , uncompressed ) )
val id = addIndex ( connection , Index ( archive , index , buf , uncompressed ) )
connection . prepareStatement (
"""
CREATE TEMPORARY TABLE tmp _groups (
group _id INTEGER NOT NULL ,
crc32 INTEGER NOT NULL ,
version INTEGER NOT NULL
) ON COMMIT DROP
""" .trimIndent()
) . use { stmt ->
stmt . execute ( )
}
connection . prepareStatement (
"""
INSERT INTO tmp _groups ( group _id , crc32 , version )
VALUES ( ? , ? , ? )
""" .trimIndent()
) . use { stmt ->
for ( entry in index ) {
stmt . setInt ( 1 , entry . id )
stmt . setInt ( 2 , entry . checksum )
stmt . setInt ( 3 , entry . version )
stmt . addBatch ( )
}
stmt . executeBatch ( )
}
/ *
/ *
* In order to defend against ( crc32 , version ) collisions , we only
* In order to defend against ( crc32 , version ) collisions , we only
@ -298,21 +269,22 @@ public class CacheImporter @Inject constructor(
* /
* /
connection . prepareStatement (
connection . prepareStatement (
"""
"""
SELECT t . group _id
SELECT ig . group _id
FROM tmp _groups t
FROM index _groups ig
LEFT JOIN master _index _valid _indexes i ON i . master _index _id = ? AND
LEFT JOIN master _index _valid _indexes i ON i . master _index _id = ? AND
i . archive _id = ?
i . archive _id = ?
LEFT JOIN index _groups ig ON ig . container _id = i . container _id AND ig . group _id = t . group _id AND
LEFT JOIN index _groups ig2 ON ig2 . container _id = i . container _id AND ig2 . group _id = ig . group _id AND
ig . crc32 = t . crc32 AND ig . version = t . version
ig2 . crc32 = ig . crc32 AND ig2 . version = ig . version
LEFT JOIN groups g ON g . archive _id = i . archive _id AND g . group _id = ig . group _id AND
LEFT JOIN groups g ON g . archive _id = i . archive _id AND g . group _id = ig2 . group _id AND
g . version = ig . version AND NOT g . version _truncated
g . version = ig2 . version AND NOT g . version _truncated
LEFT JOIN containers c ON c . id = g . container _id AND c . crc32 = ig . crc32
LEFT JOIN containers c ON c . id = g . container _id AND c . crc32 = ig2 . crc32
WHERE g . container _id IS NULL
WHERE ig . container _id = ? AND g . container _id IS NULL
ORDER BY t . group _id ASC
ORDER BY ig . group _id ASC
""" .trimIndent()
""" .trimIndent()
) . use { stmt ->
) . use { stmt ->
stmt . setObject ( 1 , previousMasterIndexId , Types . INTEGER )
stmt . setObject ( 1 , previousMasterIndexId , Types . INTEGER )
stmt . setInt ( 2 , archive )
stmt . setInt ( 2 , archive )
stmt . setLong ( 3 , id )
stmt . executeQuery ( ) . use { rows ->
stmt . executeQuery ( ) . use { rows ->
val groups = mutableListOf < Int > ( )
val groups = mutableListOf < Int > ( )
@ -582,7 +554,7 @@ public class CacheImporter @Inject constructor(
}
}
}
}
private fun addIndex ( connection : Connection , index : Index ) {
private fun addIndex ( connection : Connection , index : Index ) : Long {
val containerId = addGroup ( connection , index )
val containerId = addGroup ( connection , index )
val savepoint = connection . setSavepoint ( )
val savepoint = connection . setSavepoint ( )
@ -607,7 +579,7 @@ public class CacheImporter @Inject constructor(
} catch ( ex : SQLException ) {
} catch ( ex : SQLException ) {
if ( ex . sqlState == PSQLState . UNIQUE _VIOLATION . state ) {
if ( ex . sqlState == PSQLState . UNIQUE _VIOLATION . state ) {
connection . rollback ( savepoint )
connection . rollback ( savepoint )
return @addIndex
return @addIndex containerId
}
}
throw ex
throw ex
}
}
@ -679,6 +651,8 @@ public class CacheImporter @Inject constructor(
stmt . executeBatch ( )
stmt . executeBatch ( )
}
}
return containerId
}
}
private fun prepare ( connection : Connection ) {
private fun prepare ( connection : Connection ) {