Skip to content

Commit

Permalink
[Fts] improve and clean up FTS code.
Browse files Browse the repository at this point in the history
  • Loading branch information
agrosner committed Feb 11, 2021
1 parent b18e84f commit 680b03a
Show file tree
Hide file tree
Showing 9 changed files with 220 additions and 221 deletions.
26 changes: 26 additions & 0 deletions core/src/main/kotlin/com/dbflow5/annotation/Fts.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.dbflow5.annotation

import kotlin.reflect.KClass

/**
* Description: Creates a class using the SQLITE FTS3 [https://www.sqlite.org/fts3.html]
*/
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class Fts3


/**
* Description: Creates a class using the SQLITE FTS4 [https://www.sqlite.org/fts3.html]
*/
@Retention(AnnotationRetention.SOURCE)
@Target(AnnotationTarget.CLASS)
annotation class Fts4(
/**
* Optionally points to a content table that fills this FTS4 with content.
* The content option allows FTS4 to forego storing the text being indexed and
* results in significant space savings.
*/
val contentTable: KClass<*> = Any::class
)

8 changes: 0 additions & 8 deletions core/src/main/kotlin/com/dbflow5/annotation/Fts3.kt

This file was deleted.

17 changes: 0 additions & 17 deletions core/src/main/kotlin/com/dbflow5/annotation/Fts4.kt

This file was deleted.

8 changes: 4 additions & 4 deletions lib/src/main/kotlin/com/dbflow5/query/Method.kt
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ inline fun <reified T : Any> offsets() = Method("offsets", tableName<T>())
* @param start - the start match text.
* @param end - the end match text
* @param ellipses
* @param ftsTableColumnNumber - The FTS table column number to extract the returned fragments of
* @param index - The FTS table column number to extract the returned fragments of
* text from. Columns are numbered from left to right starting with zero.
* A negative value indicates that the text may be extracted from any column.
* @param approximateTokens - The absolute value of this integer argument is used as the
Expand All @@ -277,13 +277,13 @@ inline fun <reified T : Any> snippet(
start: String? = null,
end: String? = null,
ellipses: String? = null,
ftsTableColumnNumber: Int? = null,
index: Int? = null,
approximateTokens: Int? = null,
): Method {
val args = listOfNotNull(tableName<T>(), start, end, ellipses, ftsTableColumnNumber, approximateTokens)
val args = listOfNotNull(tableName<T>(), start, end, ellipses, index, approximateTokens)
.map {
if (it is String) propertyString("'${it}'")
else propertyString<Any>(it.toString())
}.toTypedArray()
return Method("snippet", *args)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class CreationQueryMethod(private val tableDefinition: TableDefinition) : Method
add("(")
// FTS4 uses column names directly.
add(tableDefinition.columnDefinitions.joinToString { it.columnName.quote() })
tableDefinition.ftS4Behavior?.addContentTableCode(tableDefinition.columnDefinitions.isNotEmpty(), this)
tableDefinition.ftsBehavior?.addContentTableCode(tableDefinition.columnDefinitions.isNotEmpty(), this)
add(")")
}.S
};\n")
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,36 @@ import com.squareup.javapoet.ClassName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.TypeName

/**
* Description:
*/
class FTS4Behavior(
val contentTable: TypeName,
private val databaseTypeName: TypeName,
private val elementName: String,
private val manager: ProcessorManager) {
interface FtsBehavior {

val manager: ProcessorManager
val elementName: String

fun validateColumnDefinition(columnDefinition: ColumnDefinition) {
if (columnDefinition.type != ColumnDefinition.Type.RowId
&& columnDefinition.columnName != "rowid"
&& columnDefinition.elementTypeName.isOneOf(Int::class, Long::class)) {
&& columnDefinition.columnName != "rowid"
&& columnDefinition.elementTypeName.isOneOf(Int::class, Long::class)) {
manager.logError("FTS4 Table of type $elementName can only have a single primary key named \"rowid\" of type rowid that is an Int or Long type.")
} else if (columnDefinition.elementTypeName != ClassName.get(String::class.java)) {
manager.logError("FTS4 Table of type $elementName must only contain String columns")
}
}

fun addContentTableCode(addComma: Boolean, codeBlock: CodeBlock.Builder) {

}
}

/**
* Description:
*/
class FTS4Behavior(
val contentTable: TypeName,
private val databaseTypeName: TypeName,
override val elementName: String,
override val manager: ProcessorManager) : FtsBehavior {

override fun addContentTableCode(addComma: Boolean, codeBlock: CodeBlock.Builder) {
val contentTableDefinition = manager.getTableDefinition(databaseTypeName, contentTable)
contentTableDefinition?.let { tableDefinition ->
if (addComma) {
Expand All @@ -39,17 +49,5 @@ class FTS4Behavior(
}

class FTS3Behavior(
private val elementName: String,
private val manager: ProcessorManager) {

fun validateColumnDefinition(columnDefinition: ColumnDefinition) {
if (columnDefinition.type != ColumnDefinition.Type.RowId
&& columnDefinition.columnName != "rowid"
&& columnDefinition.elementTypeName.isOneOf(Int::class, Long::class)) {
manager.logError("FTS4 Table of type $elementName can only have a single primary key named \"rowid\" of type rowid that is an Int or Long type.")
} else if (columnDefinition.elementTypeName != ClassName.get(String::class.java)) {
manager.logError("FTS4 Table of type $elementName must only contain String columns")
}
}

}
override val elementName: String,
override val manager: ProcessorManager) : FtsBehavior
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import org.junit.Test
/**
* Description:
*/
class Fts4ModelTest : BaseUnitTest() {
class FtsModelTest : BaseUnitTest() {

@Test
fun validate_fts4_created() {
Expand Down Expand Up @@ -80,4 +80,4 @@ class Fts4ModelTest : BaseUnitTest() {
" and cool elsewhere, [minimum] [temperature] 17-20oC. Cold...")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.dbflow5.annotation.ColumnIgnore
import com.dbflow5.annotation.ConflictAction
import com.dbflow5.annotation.ForeignKey
import com.dbflow5.annotation.ForeignKeyAction
import com.dbflow5.annotation.Fts3
import com.dbflow5.annotation.Fts4
import com.dbflow5.annotation.ManyToMany
import com.dbflow5.annotation.PrimaryKey
Expand Down Expand Up @@ -236,9 +237,9 @@ class Dog : BaseModel() {

@Table(database = TestDatabase::class)
data class Currency(@PrimaryKey(autoincrement = true) var id: Long = 0,
@Column @Unique var symbol: String? = null,
@Column var shortName: String? = null,
@Column @Unique var name: String = "") // nullability of fields are respected. We will not assign a null value to this field.
@Column @Unique var symbol: String? = null,
@Column var shortName: String? = null,
@Column @Unique var name: String = "") // nullability of fields are respected. We will not assign a null value to this field.

inline class Password(val value: String)
inline class Email(val value: String)
Expand Down Expand Up @@ -266,6 +267,9 @@ class UniqueModel(@PrimaryKey var id: String = "",
@Unique(uniqueGroups = [1]) var name: String = "",
@ForeignKey @Unique(uniqueGroups = [1]) var model: TypeConverterModel? = null)

@Table(database = TestDatabase::class)
@Fts3
class Fts3Model(var name: String = "")

@Table(database = TestDatabase::class)
class Fts4Model(
Expand All @@ -275,4 +279,4 @@ class Fts4Model(

@Table(database = TestDatabase::class)
@Fts4(contentTable = Fts4Model::class)
class Fts4VirtualModel2(var name: String = "")
class Fts4VirtualModel2(var name: String = "")

0 comments on commit 680b03a

Please sign in to comment.