<commit_before>package com.github.tslamic.dn

import android.content.Context
import android.content.SharedPreferences
import android.database.sqlite.SQLiteDatabase
import java.io.File
import java.io.InputStream
import java.io.OutputStream

class DatabaseImpl(val context: Context) : Database {
  val prefs: SharedPreferences = context.getSharedPreferences("__dn__", Context.MODE_PRIVATE)
  val database: File = context.getDatabasePath("dn.db")
  val version: Long = 2L

  override fun copyFromAssets() {
    val key = "version"
    if (!database.isValid() || version != prefs.getLong(key, 0)) {
      val src = context.assets.open("dn.db")
      copy(src, database.outputStream(), key, version)
    }
  }

  override fun instance(): SQLiteDatabase? {
    if (database.isValid()) {
      return SQLiteDatabase.openDatabase(database.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
    }
    return null
  }

  private fun copy(src: InputStream, dst: OutputStream, key: String, version: Long) {
    src.use { s ->
      dst.use { d ->
        if (s.copyTo(d) > 0) {
          prefs.edit().putLong(key, version).apply()
        }<commit_msg>Check and create db dir if necessary<commit_after>package com.github.tslamic.dn

import android.content.Context
import android.content.SharedPreferences
import android.database.sqlite.SQLiteDatabase
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream

class DatabaseImpl(val context: Context) : Database {
  val prefs: SharedPreferences = context.getSharedPreferences("__dn__", Context.MODE_PRIVATE)
  val database: File = context.getDatabasePath("dn.db")
  val version: Long = 2L

  override fun copyFromAssets() {
    val key = "version"
    if (!database.isValid() || version != prefs.getLong(key, 0)) {
      if (!database.parentFile.exists() && !database.parentFile.mkdir()) {
        throw IOException("Couldn't create db dir")
      }
      val src = context.assets.open("dn.db")
      copy(src, database.outputStream(), key, version)
    }
  }

  override fun instance(): SQLiteDatabase? {
    if (database.isValid()) {
      return SQLiteDatabase.openDatabase(database.absolutePath, null, SQLiteDatabase.OPEN_READONLY)
    }
    return null
  }

  private fun copy(src: InputStream, dst: OutputStream, key: String, version: Long) {
    src.use { s ->
      dst.use { d ->
        if (s.copyTo(d) > 0) {
          prefs.edit().putLong(key, version).apply()
        }
{'fraction_non_alphanumeric': 0.12139667072675599, 'fraction_numerical': 0.00243605359317905, 'mean_word_length': 3.6490566037735848, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7316636', 'n_tokens_mistral': 746, 'n_tokens_neox': 732, 'n_words': 178}