Skip to content

Commit

Permalink
Straightened entity creation process.
Browse files Browse the repository at this point in the history
Signed-off-by: Ralph Gasser <[email protected]>
  • Loading branch information
ppanopticon committed Mar 21, 2021
1 parent 2aa201d commit e258b76
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ class MapDBColumn<T : Value>(override val path: Path, override val parent: Entit
* Initializes a new, empty [MapDBColumn]
*
* @param path The [Path] to the folder in which the [MapDBColumn] file should be stored.
* @param definition The [ColumnDef] that specifies the [MapDBColumn]
* @param column The [ColumnDef] that specifies the [MapDBColumn]
* @param config The [MapDBConfig] used to initialize the [MapDBColumn]
*/
fun initialize(path: Path, definition: ColumnDef<*>, config: MapDBConfig) {
if (Files.exists(path)) throw DatabaseException.InvalidFileException("Could not initialize index. A file already exists under $path.")
fun initialize(path: Path, column: ColumnDef<*>, config: MapDBConfig) {
if (Files.exists(path)) throw DatabaseException.InvalidFileException("Could not initialize column ${column.name}. A file already exists under $path.")
val store = config.store(path)
store.put(ColumnHeader(definition), ColumnHeader.Serializer)
store.put(ColumnHeader(column), ColumnHeader.Serializer)
store.commit()
store.close()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.vitrivr.cottontail.database.column.Column
import org.vitrivr.cottontail.database.column.ColumnDef
import org.vitrivr.cottontail.database.column.ColumnEngine
import org.vitrivr.cottontail.database.column.ColumnTx
import org.vitrivr.cottontail.database.column.mapdb.MapDBColumn
import org.vitrivr.cottontail.database.events.DataChangeEvent
import org.vitrivr.cottontail.database.general.AbstractTx
import org.vitrivr.cottontail.database.general.DBOVersion
Expand Down Expand Up @@ -75,27 +74,36 @@ class DefaultEntity(override val path: Path, override val parent: DefaultSchema)
fun initialize(name: Name.EntityName, path: Path, config: Config, columns: List<Pair<ColumnDef<*>, ColumnEngine>>): Path {
/* Prepare empty catalogue. */
val dataPath = path.resolve("entity_${name.simple}")
if (!Files.exists(dataPath)) {
Files.createDirectories(dataPath)
} else {
throw DatabaseException("Failed to create entity '$name'. Data directory '$dataPath' seems to be occupied.")
}
val catalogue = config.mapdb.db(dataPath.resolve(CATALOGUE_FILE))
if (Files.exists(dataPath)) throw DatabaseException.InvalidFileException("Failed to create entity '$name'. Data directory '$dataPath' seems to be occupied.")
Files.createDirectories(dataPath)

/* Prepare column references and column statistics. */
val entityHeader = EntityHeader(name = name.simple, columns = columns.map {
val colPath = dataPath.resolve("${it.first.name.simple}.col")
MapDBColumn.initialize(colPath, it.first, config.mapdb)
EntityHeader.ColumnRef(it.first.name.simple, it.second)
})
val entityStatistics = EntityStatistics()
columns.forEach { entityStatistics[it.first] = it.first.type.statistics() as ValueStatistics<Value> }

/* Write entity header + statistics entry to disk. */
catalogue.atomicVar(ENTITY_HEADER_FIELD, EntityHeader.Serializer).create().set(entityHeader)
catalogue.atomicVar(ENTITY_STATISTICS_FIELD, EntityStatistics.Serializer).create().set(entityStatistics)
catalogue.commit()
catalogue.close()
try {
val entityHeader = EntityHeader(name = name.simple, columns = columns.map {
val colPath = dataPath.resolve("${it.first.name.simple}.col")
it.second.create(colPath, it.first, config)
EntityHeader.ColumnRef(it.first.name.simple, it.second)
})
val entityStatistics = EntityStatistics()
columns.forEach { entityStatistics[it.first] = it.first.type.statistics() as ValueStatistics<Value> }

/* Write entity header + statistics entry to disk. */
config.mapdb.db(dataPath.resolve(CATALOGUE_FILE)).use {
it.atomicVar(ENTITY_HEADER_FIELD, EntityHeader.Serializer).create().set(entityHeader)
it.atomicVar(ENTITY_STATISTICS_FIELD, EntityStatistics.Serializer).create().set(entityStatistics)
it.commit()
it.close()
}
} catch (e: DBException) {
FileUtilities.deleteRecursively(dataPath) /* Cleanup. */
throw DatabaseException("Failed to create entity '$name' due to error in the underlying data store: {${e.message}")
} catch (e: IOException) {
FileUtilities.deleteRecursively(dataPath) /* Cleanup. */
throw DatabaseException("Failed to create entity '$name' due to an IO exception: {${e.message}")
} catch (e: Throwable) {
FileUtilities.deleteRecursively(dataPath) /* Cleanup. */
throw DatabaseException("Failed to create entity '$name' due to an unexpected error: {${e.message}")
}

return dataPath
}
Expand Down Expand Up @@ -566,7 +574,6 @@ class DefaultEntity(override val path: Path, override val parent: DefaultSchema)
this.snapshot.indexes.values.forEach { (this.context.getTx(it) as IndexTx).update(event) }
this.snapshot.statistics.consume(event)
this.context.signalEvent(event)

} catch (e: DatabaseException) {
this.status = TxStatus.ERROR
throw e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.vitrivr.cottontail.execution.TransactionContext
import org.vitrivr.cottontail.model.basics.Name
import org.vitrivr.cottontail.model.exceptions.DatabaseException
import org.vitrivr.cottontail.utilities.io.FileUtilities
import java.io.IOException
import java.nio.file.Files
import java.nio.file.Path
import java.util.*
Expand Down Expand Up @@ -247,20 +246,17 @@ class DefaultSchema(override val path: Path, override val parent: DefaultCatalog
throw DatabaseException.DuplicateColumnException(name, cols)
}

/* Initialize entity on disk and make it available to transaction. */
try {
/* Initialize entity on disk and make it available to transaction. */
val data = DefaultEntity.initialize(name, this@DefaultSchema.path, this@DefaultSchema.parent.config, columns.toList())
val entity = DefaultEntity(data, this@DefaultSchema)
this.snapshot.created[name] = entity
this.snapshot.entities[name] = entity
this.snapshot.dropped.remove(name)
return entity
} catch (e: DBException) {
} catch (e: DatabaseException) {
this.status = TxStatus.ERROR
throw DatabaseException("Failed to create entity '$name' due to error in the underlying data store: {${e.message}")
} catch (e: IOException) {
this.status = TxStatus.ERROR
throw DatabaseException("Failed to create entity '$name' due to an IO exception: {${e.message}")
throw e
}
}

Expand Down

0 comments on commit e258b76

Please sign in to comment.