|  | 
|  | 1 | +package build.omnilink.evaldb | 
|  | 2 | + | 
|  | 3 | +import mill.* | 
|  | 4 | + | 
|  | 5 | +import scalasql.*, PostgresDialect.* | 
|  | 6 | +import mill.api.BuildCtx | 
|  | 7 | +import scala.util.Using | 
|  | 8 | +import java.time.LocalDateTime | 
|  | 9 | + | 
|  | 10 | +trait EvalDBTrait: | 
|  | 11 | +  def dbPath: os.SubPath | 
|  | 12 | +  def createTablesSQL: os.SubPath | 
|  | 13 | + | 
|  | 14 | +  final case class AutoCloseClient(client: DbClient, closeFn: () => Unit) | 
|  | 15 | +      extends AutoCloseable: | 
|  | 16 | +    export client.* | 
|  | 17 | +    def close(): Unit = | 
|  | 18 | +      closeFn() | 
|  | 19 | +  end AutoCloseClient | 
|  | 20 | + | 
|  | 21 | +  private lazy val fn = | 
|  | 22 | +    Class.forName("org.duckdb.DuckDBDriver") | 
|  | 23 | +    val fn = () => | 
|  | 24 | +      @scala.annotation.tailrec | 
|  | 25 | +      def retry(): AutoCloseClient = | 
|  | 26 | +        try | 
|  | 27 | +          val conn = java.sql.DriverManager.getConnection( | 
|  | 28 | +            s"jdbc:duckdb:${BuildCtx.workspaceRoot / dbPath}", | 
|  | 29 | +          ) | 
|  | 30 | +          val client = DbClient.Connection(conn) | 
|  | 31 | +          AutoCloseClient(client, conn.close) | 
|  | 32 | +        catch | 
|  | 33 | +          case ex: java.sql.SQLException => | 
|  | 34 | +            if ex.getMessage().contains("Could not set lock on file") | 
|  | 35 | +            then | 
|  | 36 | +              println(s"DuckDB file lock contention, backing off...") | 
|  | 37 | +              Thread.sleep(1000) | 
|  | 38 | +              retry() | 
|  | 39 | +            else throw ex | 
|  | 40 | +      end retry | 
|  | 41 | +      retry() | 
|  | 42 | +    end fn | 
|  | 43 | + | 
|  | 44 | +    Using.resource(fn()): conn => | 
|  | 45 | +      conn.getAutoCommitClientConnection | 
|  | 46 | +        .updateRaw(os.read(BuildCtx.workspaceRoot / createTablesSQL)) | 
|  | 47 | + | 
|  | 48 | +    fn | 
|  | 49 | +  end fn | 
|  | 50 | + | 
|  | 51 | +  def dbClient: () => AutoCloseClient = fn | 
|  | 52 | +end EvalDBTrait | 
|  | 53 | + | 
|  | 54 | +object EvalDB extends Module, EvalDBTrait: | 
|  | 55 | +  def createTablesSQL = os.sub / "omnilink" / "createTables.sql" | 
|  | 56 | +  def dbPath = os.sub / "omnilink" / "eval.duckdb" | 
|  | 57 | + | 
|  | 58 | +  given workspacePathMapper: TypeMapper[os.Path] = | 
|  | 59 | +    TypeMapper[String].bimap( | 
|  | 60 | +      _.relativeTo(BuildCtx.workspaceRoot).toString, | 
|  | 61 | +      os.Path(_, BuildCtx.workspaceRoot), | 
|  | 62 | +    ) | 
|  | 63 | + | 
|  | 64 | +  case class Config[T[_]]( | 
|  | 65 | +      id: T[String], | 
|  | 66 | +      expectedExperimentCount: T[Int], | 
|  | 67 | +  ) | 
|  | 68 | +  object Config extends Table[Config] | 
|  | 69 | + | 
|  | 70 | +  case class Experiment[T[_]]( | 
|  | 71 | +      configId: T[String], | 
|  | 72 | +      idx: T[Int], | 
|  | 73 | +      github: T[String], | 
|  | 74 | +      branch: T[String], | 
|  | 75 | +      specPath: T[os.Path], | 
|  | 76 | +      mcSpecPath: T[os.Path], | 
|  | 77 | +      mcConfigPath: T[os.Path], | 
|  | 78 | +      startTime: T[LocalDateTime], | 
|  | 79 | +      endTime: T[LocalDateTime], | 
|  | 80 | +  ) | 
|  | 81 | +  object Experiment extends Table[Experiment] | 
|  | 82 | + | 
|  | 83 | +  case class Trace[T[_]]( | 
|  | 84 | +      configId: T[String], | 
|  | 85 | +      experimentIdx: T[Int], | 
|  | 86 | +      id: T[Int], | 
|  | 87 | +      trace: T[geny.Bytes], | 
|  | 88 | +  ) | 
|  | 89 | +  object Trace extends Table[Trace] | 
|  | 90 | + | 
|  | 91 | +  case class GatherLog[T[_]]( | 
|  | 92 | +      configId: T[String], | 
|  | 93 | +      experimentIdx: T[Int], | 
|  | 94 | +      name: T[String], | 
|  | 95 | +      text: T[String], | 
|  | 96 | +  ) | 
|  | 97 | +  object GatherLog extends Table[GatherLog] | 
|  | 98 | + | 
|  | 99 | +  case class Validation[T[_]]( | 
|  | 100 | +      configId: T[String], | 
|  | 101 | +      experimentIdx: T[Int], | 
|  | 102 | +      logTxt: T[String], | 
|  | 103 | +      startTime: T[LocalDateTime], | 
|  | 104 | +      endTime: T[LocalDateTime], | 
|  | 105 | +      success: T[Boolean], | 
|  | 106 | +      counterExampleBin: T[Option[geny.Bytes]], | 
|  | 107 | +  ) | 
|  | 108 | +  object Validation extends Table[Validation] | 
|  | 109 | +end EvalDB | 
0 commit comments