Kripton Persistence Library — V. 4 is ready

Francesco Benincasa
6 min readMay 14, 2018

--

This post was originally posted on abubusoft.com.

Some time ago I wrote an Introduction article about Kripton Persistence Library.

After a lot of work, Kripton Persistence Library is finally arrived to version 4. There are many so many features to explain. Just remember that Kripton aim is uniform persistence task on Android platform.

As you’ll read from the new feature list, most of the work was done on ORM module, but other modules get some new features too. Let’s see new features and other stuff like bug fix and improvements.

New features

[KRIPTON-233] — GLOBAL: Modularization of Kripton Persistence Library

Kripton Persistence Library was remodelled allowing to separating persistence functionality in 3 main modules. There is a module for every kind of persistence:

  • com.abubusoft.kripton module: for basic persistence on the file system. It is based on FasterXML Jackson library that is included as its dependency. Thus, include this library in your project will increase size about 500 KByte. If you don’t need this kind of persistence, and you want to compact you APK is better to avoid to include it.
  • com.abubusoft.kripton-retrofit-converter: for Retrofit integration. It includes kripton module and Retrofit library
  • com.abubusoft.kripton-orm: for ORM functionality. It works alone, but if you need to embed in a column an entire bean, you need kripton module too.
  • com.abubusoft.kripton-shared-preferences: to work with Shared Preferences.

If you want it all, you can simply use com.abubusoft.kripton-android-library. Just remember that there are other modules:

  • com.abubusoft.kripton-processor: it contains Kripton annotation processor.
  • com.abubusoft.kripton-sqlite-test-library: used to test SQLite migration or generated data-source generic functionality
  • com.abubusoft.kripton-dataformat-cbor: for CBOR data format support.
  • com.abubusoft.kripton-dataformat-yaml: for YAML data format support.
  • com.abubusoft.kripton-dataformat-properties: for (Java) property support.
  • com.abubusoft.kripton-dataformat-smile: for Smile data format support.

[KRIPTON-251] — DATA-FORMAT: Add simply support for XML namespace

Kripton since version 4.0 allows specifying a namespace for an attribute. Suppose that you need to parse the following XML:

The Item associated Java class is so defined:

As you can see, thumbnail attribute has an associated namespace media defined with @BindXml#namespace attribute.

[KRIPTON-228] ORM: Add LiveData Support

With release 4, SQL Queries can now return LiveData object:

@BindDao(Person.class)
public interface DaoPerson {

@BindSqlSelect(where="name=${name}")
LiveData<List<Person>> select(String name);

}

This feature allows using Kripton with Architectural Component by Google.

[KRIPTON-240] ORM: Add support to Relationship

One of the key concepts in Kripton ORM is that every DAO can work with one kind of entity defined in data-source. Every entity can be bound to others with a relationship. Just as an example, let’s take two entities: Music Album and Song. One Music Album contains many Songs and there a One-2-Many relationship between them. Introducing @BindSqlRelation annotations allows to write the following declaration:

Without @BindSqlRelation, songs field in Album entity will be stored in table album (to avoid this, you can use @BindDisable annotation or @BindSqlColumn(enabled=false)). The use of @BindSqlRelation allows us to define child select for select included in DaoAlbum definition:

When DaoAlbum#selectAlbums is invoked, for each element found in the result set, Kripton runtime will invoke DaoSong#selectByAlbumId to fill Album#songs property.

[KRIPTON-234] ORM: Add configuration by @BindDataSourceOptions

@BindDataSourceoptions is used to define data-source options with annotation. There are two ways to customize data source creating: by code or by annotation. This annotation implements the second way. When data-source is created, usually at application startup, it is possible to define some features: if it is in-memory if DDL log is enabled, the populator and the migration-version tasks and so on.

To achieve the same result with @BindDataSourceOptions just define data source:

And the code for creating data source will become

[KRIPTON-238] ORM: Add global SQLiteTypeAdapter to data-source

Usually, to define a type adapter, you need to annotate attribute with specific type adapter. When you need to convert ALL field of a particular type, is more comfortable to define a global type adapter, specified in the data source definition.

In the above example, the Date2Long SqlTypeAdapter will be used in every attribute of the data model of PersonDataSource.

[KRIPTON-239] ORM: add the capability to specify for a column the type affinity

SQLite is a loosely typed database, so you can modify column type quite easily. If you need to change default column affinity for an attribute type, you can do it simply using @BindSqlColumn#columnAffinity.

[KRIPTON-254] ORM: Add annotation processor options: kripton.schemaLocation

Now you can specify in which folder Kripton shall create schema definition, simply specifying the annotation processor option kripton.schemaLocation.

[KRIPTON-250] ORM: Add populator to SQLiteUpdateTestDatabase#builder

Test and migration helper class for ORM module had a heavy refactoring and add a new feature: the capability to define a populator task executed when the database is created, despite its version.

[KRIPTON-237] SharedPrefs: Add @BindPreferenceAdapter

Since Version 4, Kripton allows defining a type adapter to SharedPreference binding too.

This annotation decorates a field to use a particular Shared-Preference Type Adapter to customize persistence on the Shared Preference mechanism. A type adapter must implement the PreferenceTypeAdapter interface. It has two parameter type: the first is the field type, the second is the type that we want to use as replacement and that will be used to store data into the Shared Preference. It implements two methods:

  • toJava: converts data retrieved from a Shared Preference element.
  • toData: converts a field into data to store into a Shared Preference element.

And the associated SQL type adapter:

This annotation is very useful when you need to persist a class that Kripton does not support directly for persistence or when you need to customize the storage mechanism on SharePreference.

Bug

  • [KRIPTON-230] — Insert from Select does not work
  • [KRIPTON-248] — Generate Async task had some problem during data source close operation
  • [KRIPTON-249] — DYNAMIC_WHERE did not work on explicit SELECT JQL queries

Improvements

  • [KRIPTON-160] — Add capability of add DYNAMIC_WHERE in extended query type (UPDATE-DELETE)
  • [KRIPTON-161] — Add capability of add DYNAMIC_ORDER in extended query type.
  • [KRIPTON-229] — Dao Subject: avoid to fire events if no elements is modified
  • [KRIPTON-241] — ORM: index refactoring: introducing @bindindex
  • [KRIPTON-247] — [BREAK COMPATIBILITY] @bindcolumn renamed in @bindsqlcolumn
  • [KRIPTON-252] — SQLite Test Suite refactoring
  • [KRIPTON-253] — [BREAK COMPATIBILITY] @bindtable renamed in @bindsqltype

Task

  • [KRIPTON-227] — Version alignment with maven version
  • [KRIPTON-231] — Remove SQLContext interface to AbstractDataSource
  • [KRIPTON-235] — rename BindTypeAdapter to TypeAdapter
  • [KRIPTON-236] — rename BindSqlTypeAdapter into SqlTypeAdapter
  • [KRIPTON-242] — ORM: removed generated build() method in datasource: it does not need anymore
  • [KRIPTON-243] — @bindcolumn#foreignKey renamed in parentEntity
  • [KRIPTON-244] — build and instance methods improvement: remove sync and implementation of inner sync
  • [KRIPTON-245] — @bindcolumn#foreignKey renamed in parentEntity
  • [KRIPTON-255] — [BREAK COMPATIBILITY] refactoring DataSource.instance() -> getInstance()
  • [KRIPTON-256] — [BREAK COMPATIBILITY] refactoring Dao.subject() -> getSubject()
  • [KRIPTON-257] — [BREAK COMPATIBILITY] KriptonLibrary.context() -> getContext()

And now?

Kripton latest version is 4.0.0-rc.18. I will take some days to do more tests. Wiki pages are already ported to version 4. Probably Kripton v.4 final release will be released in a couple of weeks.

Next step is to write more article and documentation about Kripton.

UPDATE: More article

Linked articles (constantly updated):

For sure the next articles will be about ORM version migration, Live Data integration and other stuff. If you are interested in a particular topic, let me know.

If you like Kripton and you think it’s a good library, please, give it a star on GitHub and give me feedback, thank you :).

For more information about Kripton Persistence Library:

Happy coding!

Francesco

--

--