1 /***********************************************************************
2  * Copyright (c) 2013-2025 General Atomics Integrated Intelligence, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Apache License, Version 2.0
5  * which accompanies this distribution and is available at
6  * https://www.apache.org/licenses/LICENSE-2.0
7  ***********************************************************************/
8 
9 package org.locationtech.geomesa.tools.data
10 
11 import org.geotools.api.data.DataStore
12 import org.geotools.api.feature.simple.SimpleFeatureType
13 import org.locationtech.geomesa.tools._
14 import org.locationtech.geomesa.tools.data.CreateSchemaCommand.{CreateSchemaParams, SchemaOptionsCommand}
15 import org.locationtech.geomesa.tools.utils.CLArgResolver
16 import org.locationtech.geomesa.utils.geotools.RichSimpleFeatureType.RichSimpleFeatureType
17 import org.locationtech.geomesa.utils.geotools.SimpleFeatureTypes
18 
19 import java.io.IOException
20 
21 trait CreateSchemaCommand[DS <: DataStore] extends DataStoreCommand[DS] with SchemaOptionsCommand {
22 
23   override val name = "create-schema"
24   override def params: CreateSchemaParams
25 
26   override def execute(): Unit = {
27     val sft = CLArgResolver.getSft(params.spec, params.featureName)
28     Option(params.dtgField).foreach(sft.setDtgField)
29     setBackendSpecificOptions(sft)
30     withDataStore(createSchema(_, sft))
31   }
32 
33   protected def createSchema(ds: DS, sft: SimpleFeatureType): Unit = {
34     lazy val sftString = SimpleFeatureTypes.encodeType(sft)
35     Command.user.info(s"Creating '${sft.getTypeName}' with spec '$sftString'. Just a few moments...")
36 
37     if (try { ds.getSchema(sft.getTypeName) == null } catch { case _: IOException => true }) {
38       ds.createSchema(sft)
39       if (try { ds.getSchema(sft.getTypeName) != null } catch { case _: IOException => false }) {
40         Command.user.info(s"Created schema '${sft.getTypeName}'")
41       } else {
42         Command.user.error(s"Could not create schema '${sft.getTypeName}'")
43       }
44     } else {
45       Command.user.error(s"Schema '${sft.getTypeName}' already exists in the data store")
46     }
47   }
48 }
49 
50 object CreateSchemaCommand {
51 
52   // @Parameters(commandDescription = "Create a GeoMesa feature type")
53   trait CreateSchemaParams extends RequiredFeatureSpecParam with OptionalTypeNameParam with OptionalDtgParam
54 
55   trait SchemaOptionsCommand {
56     protected def setBackendSpecificOptions(sft: SimpleFeatureType): Unit = {}
57   }
58 }
Line Stmt Id Pos Tree Symbol Tests Code
23 70913 1122 - 1137 Literal <nosymbol> "create-schema"
27 70915 1264 - 1282 Select org.locationtech.geomesa.tools.OptionalTypeNameParam.featureName CreateSchemaCommand.this.params.featureName
27 70914 1251 - 1262 Select org.locationtech.geomesa.tools.RequiredFeatureSpecParam.spec CreateSchemaCommand.this.params.spec
27 70916 1230 - 1283 Apply org.locationtech.geomesa.tools.utils.CLArgResolver.getSft org.locationtech.geomesa.tools.utils.CLArgResolver.getSft(CreateSchemaCommand.this.params.spec, CreateSchemaCommand.this.params.featureName)
28 70917 1295 - 1310 Select org.locationtech.geomesa.tools.OptionalDtgParam.dtgField CreateSchemaCommand.this.params.dtgField
28 70919 1288 - 1336 Apply scala.Option.foreach scala.Option.apply[String](CreateSchemaCommand.this.params.dtgField).foreach[Unit]({ <synthetic> val eta$0$1: org.locationtech.geomesa.utils.geotools.RichSimpleFeatureType.RichSimpleFeatureType = org.locationtech.geomesa.utils.geotools.RichSimpleFeatureType.RichSimpleFeatureType(sft); ((dtg: String) => eta$0$1.setDtgField(dtg)) })
28 70918 1320 - 1335 Apply org.locationtech.geomesa.utils.geotools.RichSimpleFeatureType.RichSimpleFeatureType.setDtgField eta$0$1.setDtgField(dtg)
29 70920 1341 - 1371 Apply org.locationtech.geomesa.tools.data.CreateSchemaCommand.SchemaOptionsCommand.setBackendSpecificOptions CreateSchemaCommand.this.setBackendSpecificOptions(sft)
30 70921 1390 - 1410 Apply org.locationtech.geomesa.tools.data.CreateSchemaCommand.createSchema CreateSchemaCommand.this.createSchema(x$1, sft)
30 70922 1376 - 1411 Apply org.locationtech.geomesa.tools.DataStoreCommand.withDataStore CreateSchemaCommand.this.withDataStore[Unit](((x$1: DS) => CreateSchemaCommand.this.createSchema(x$1, sft)))
37 70923 1665 - 1702 Apply java.lang.Object.== ds.getSchema(sft.getTypeName()).==(null)
37 70925 1736 - 1740 Literal <nosymbol> true
37 70924 1665 - 1702 Block java.lang.Object.== ds.getSchema(sft.getTypeName()).==(null)
37 70926 1736 - 1740 Block <nosymbol> true
37 70934 1744 - 2041 Block <nosymbol> { ds.createSchema(sft); if (try { ds.getSchema(sft.getTypeName()).!=(null) } catch { case (_: java.io.IOException) => false }) (if (org.locationtech.geomesa.tools.`package`.Command.user.underlying.isInfoEnabled()) org.locationtech.geomesa.tools.`package`.Command.user.underlying.info("Created schema \'{}\'", (sft.getTypeName(): AnyRef)) else (): Unit) else (if (org.locationtech.geomesa.tools.`package`.Command.user.underlying.isErrorEnabled()) org.locationtech.geomesa.tools.`package`.Command.user.underlying.error("Could not create schema \'{}\'", (sft.getTypeName(): AnyRef)) else (): Unit) }
38 70927 1752 - 1772 Apply org.geotools.api.data.DataAccess.createSchema ds.createSchema(sft)
39 70929 1789 - 1826 Block java.lang.Object.!= ds.getSchema(sft.getTypeName()).!=(null)
39 70928 1789 - 1826 Apply java.lang.Object.!= ds.getSchema(sft.getTypeName()).!=(null)
39 70931 1860 - 1865 Block <nosymbol> false
39 70930 1860 - 1865 Literal <nosymbol> false
40 70932 1879 - 1936 Typed <nosymbol> (if (org.locationtech.geomesa.tools.`package`.Command.user.underlying.isInfoEnabled()) org.locationtech.geomesa.tools.`package`.Command.user.underlying.info("Created schema \'{}\'", (sft.getTypeName(): AnyRef)) else (): Unit)
42 70933 1960 - 2027 Typed <nosymbol> (if (org.locationtech.geomesa.tools.`package`.Command.user.underlying.isErrorEnabled()) org.locationtech.geomesa.tools.`package`.Command.user.underlying.error("Could not create schema \'{}\'", (sft.getTypeName(): AnyRef)) else (): Unit)
45 70935 2055 - 2138 Typed <nosymbol> (if (org.locationtech.geomesa.tools.`package`.Command.user.underlying.isErrorEnabled()) org.locationtech.geomesa.tools.`package`.Command.user.underlying.error("Schema \'{}\' already exists in the data store", (sft.getTypeName(): AnyRef)) else (): Unit)
56 70936 2470 - 2472 Literal <nosymbol> ()