Simple data storage facility

Write values with []= and read with [].

import linux::data_store # Needed for testing only

class A
    serialize

    var b = true
    var f = 1.234
end

var data_store = new DataStore
data_store["one"] = 1
data_store["str"] = "Some string"
data_store["a"] = new A

assert data_store["one"] == 1
assert data_store["str"] == "Some string"
assert data_store["a"].as(A).b
assert data_store["a"].as(A).f == 1.234
assert data_store["other"] == null

Set to null to clear a value.

data_store["one"] = null
assert data_store["one"] == null

Introduced properties

abstract fun [](key: String): nullable Object

app :: DataStore :: []

Get the object stored at key, or null if nothing is available
abstract fun []=(key: String, value: nullable Serializable)

app :: DataStore :: []=

Store value at key
fun db: nullable Sqlite3DB

app :: DataStore :: db

Database to use to implement the DataStore
fun db_file: String

app :: DataStore :: db_file

File path of the Sqlite3 DB file
fun db_table: String

app :: DataStore :: db_table

Sqlite3 table used
fun shared_preferences: SharedPreferences

app :: DataStore :: shared_preferences

The SharedPreferences used to implement the DataStore
protected fun shared_preferences=(shared_preferences: SharedPreferences)

app :: DataStore :: shared_preferences=

The SharedPreferences used to implement the DataStore
fun user_defaults: NSUserDefaults

app :: DataStore :: user_defaults

The NSUserDefaults used to implement DataStore
protected fun user_defaults=(user_defaults: NSUserDefaults)

app :: DataStore :: user_defaults=

The NSUserDefaults used to implement DataStore

Redefined properties

redef type SELF: DataStore

app $ DataStore :: SELF

Type of this instance, automatically specialized in every class
redef fun [](key: String): nullable Object

ios :: data_store $ DataStore :: []

Get the object stored at key, or null if nothing is available
redef fun [](key: String): nullable Object

linux :: data_store $ DataStore :: []

Get the object stored at key, or null if nothing is available
redef fun [](key: String): nullable Object

android :: data_store $ DataStore :: []

Get the object stored at key, or null if nothing is available
redef fun []=(key: String, value: nullable Serializable)

linux :: data_store $ DataStore :: []=

Store value at key
redef fun []=(key: String, value: nullable Serializable)

ios :: data_store $ DataStore :: []=

Store value at key
redef fun []=(key: String, value: nullable Serializable)

android :: data_store $ DataStore :: []=

Store value at key

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
abstract fun [](key: String): nullable Object

app :: DataStore :: []

Get the object stored at key, or null if nothing is available
abstract fun []=(key: String, value: nullable Serializable)

app :: DataStore :: []=

Store value at key
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
fun db: nullable Sqlite3DB

app :: DataStore :: db

Database to use to implement the DataStore
fun db_file: String

app :: DataStore :: db_file

File path of the Sqlite3 DB file
fun db_table: String

app :: DataStore :: db_table

Sqlite3 table used
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun shared_preferences: SharedPreferences

app :: DataStore :: shared_preferences

The SharedPreferences used to implement the DataStore
protected fun shared_preferences=(shared_preferences: SharedPreferences)

app :: DataStore :: shared_preferences=

The SharedPreferences used to implement the DataStore
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
fun user_defaults: NSUserDefaults

app :: DataStore :: user_defaults

The NSUserDefaults used to implement DataStore
protected fun user_defaults=(user_defaults: NSUserDefaults)

app :: DataStore :: user_defaults=

The NSUserDefaults used to implement DataStore
package_diagram app::DataStore DataStore core::Object Object app::DataStore->core::Object

Parents

interface Object

core :: Object

The root of the class hierarchy.

Class definitions

app $ DataStore
# Simple data storage facility
#
# Write values with `[]=` and read with `[]`.
# ~~~
# import linux::data_store # Needed for testing only
#
# class A
#     serialize
#
#     var b = true
#     var f = 1.234
# end
#
# var data_store = new DataStore
# data_store["one"] = 1
# data_store["str"] = "Some string"
# data_store["a"] = new A
#
# assert data_store["one"] == 1
# assert data_store["str"] == "Some string"
# assert data_store["a"].as(A).b
# assert data_store["a"].as(A).f == 1.234
# assert data_store["other"] == null
# ~~~
#
# Set to `null` to clear a value.
# ~~~
# data_store["one"] = null
# assert data_store["one"] == null
# ~~~
class DataStore

	# Get the object stored at `key`, or null if nothing is available
	fun [](key: String): nullable Object is abstract

	# Store `value` at `key`
	fun []=(key: String, value: nullable Serializable) is abstract
end
lib/app/data_store.nit:56,1--93,3

ios :: data_store $ DataStore
redef class DataStore

	# The `NSUserDefaults` used to implement `DataStore`
	var user_defaults = new NSUserDefaults.standard_user_defaults is lazy

	redef fun [](key)
	do
		var nsstr = user_defaults.string_for_key(key.to_nsstring)

		if nsstr.address_is_null then return null

		# TODO report errors
		var deserializer = new JsonDeserializer(nsstr.to_s)
		var deserialized = deserializer.deserialize

		var errors = deserializer.errors
		if errors.not_empty then
			# An update may have broken the versioning compatibility
			print_error "{class_name} error at deserialization: {errors.join(", ")}"
			return null # Let's be safe
		end

		return deserialized
	end

	redef fun []=(key, value)
	do
		var nsobject: NSString

		if value == null then
			nsobject = new NSString.nil
		else
			var serialized_string = new StringWriter
			var serializer = new JsonSerializer(serialized_string)
			serializer.serialize(value)

			# TODO report errors
			nsobject = serialized_string.to_s.to_nsstring
		end

		user_defaults.set_object(nsobject, key.to_nsstring)
	end
end
lib/ios/data_store.nit:22,1--64,3

linux :: data_store $ DataStore
redef class DataStore

	# File path of the Sqlite3 DB file
	fun db_file: String do return "data_store.db"

	# Sqlite3 table used
	fun db_table: String do return "data_store"

	private var db_cache: nullable Sqlite3DB = null

	# Database to use to implement the `DataStore`
	fun db: nullable Sqlite3DB
	do
		var db = db_cache
		if db != null then return db

		# Find DB path
		var config_home = xdg_basedir.config_home.to_s
		var config_dir = config_home.join_path(sys.program_name.basename)
		if not config_dir.file_exists then config_dir.mkdir
		var db_path = config_dir.join_path(db_file)

		# Open DB connection
		db = new Sqlite3DB.open(db_path)
		if not db.is_open then
			print_error "Data store unavaible, cannot load/save data. (at '{db_path}' with '{db.error or else "unknown"}')"
			return null
		end

		# Create DB table
		db.create_table "IF NOT EXISTS {db_table} (key TEXT PRIMARY KEY, value TEXT)"

		db_cache = db
		return db
	end

	redef fun [](key)
	do
		# Get DB
		var db = self.db
		if db == null then return null

		# Prepare SELECT statement
		var stmt = db.select("* FROM {db_table} WHERE key == {key.to_sql_string}")
		if stmt == null then return null

		# Execute statment
		for row in stmt do
			# Get from statement
			var serialized = row[1].to_s
			stmt.close

			# Deserialize
			var deserializer = new JsonDeserializer(serialized)
			var deserialized = deserializer.deserialize

			var errors = deserializer.errors
			if errors.not_empty then
				# An update may have broken the versioning compatibility
				print_error "{class_name} error at deserialization: {errors.join(", ")}"
				return null # Let's be safe
			end

			return deserialized
		end

		stmt.close
		return null
	end

	redef fun []=(key, value)
	do
		# Get DB
		var db = self.db
		if db == null then return

		# Serialize
		var stream = new StringWriter
		var serializer = new JsonSerializer(stream)
		serializer.serialize value
		var serialized = stream.to_s

		# Save in DB
		db.execute "BEGIN TRANSACTION"
		db.insert "OR REPLACE INTO {db_table} VALUES({key.to_sql_string}, {serialized.to_sql_string})"
		db.execute "COMMIT"
	end
end
lib/linux/data_store.nit:25,1--112,3

android :: data_store $ DataStore
redef class DataStore

	# The `SharedPreferences` used to implement the `DataStore`
	var shared_preferences = new SharedPreferences.privately(app, "data_store") is lazy

	redef fun [](key) do return shared_preferences[key]

	redef fun []=(key, value) do shared_preferences[key] = value
end
lib/android/data_store.nit:25,1--33,3