Slide 1

Slide 1 text

Getting into REAL (M) business by dumping SQLite

Slide 2

Slide 2 text

Hello! I am Dhrumil Shah I work at Bugle Technologies Pvt. Ltd. & Co-organizer at GDG Ahmedabad. You can find me at: @dhuma1981 +DhrumilShah

Slide 3

Slide 3 text

Slide 4

Slide 4 text

Why you should dump SQLite?

Slide 5

Slide 5 text

Typical Apps Requirement Lot of Data Tables W orks Offline Persist Locally

Slide 6

Slide 6 text

Problems :( More Data More Tables

Slide 7

Slide 7 text

Duplicate Data Normalization

Slide 8

Slide 8 text

Normalization More m:n

Slide 9

Slide 9 text

More m:n

Slide 10

Slide 10 text

BoilerPlate mess ▷ 1000’s of lines of boilerplate mess ▷ Lot of string just to define a single table ▷ Tightly coupled schema ▷ RAW SQL Queries

Slide 11

Slide 11 text

Nested Queries SELECT table1.col, table3.col FROM table1 JOIN table2 ON table1.primarykey = table2.foreignkey JOIN table3 ON table2.primarykey = table3.foreignkey

Slide 12

Slide 12 text

Nested Queries String query = “select” + table1.col + ”,” + table3.col + “FROM” + table1 + “JOIN” + table2 + “ON” + table1.primarykey + “=” + table2.foreignkey + “JOIN” + table3 + “ON” + table2.primarykey + “=” + table3.foreignkey

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Let’s abstract the problem Use ORM

Slide 15

Slide 15 text

▷ SugarORM ▷ ORMLite ▷ CubBoard ▷ ActiveRecord ▷ Ollie ▷ DBFLow ORM Options ▷ DBQuery ▷ GreenDao ▷ EasyLiteORM ▷ RushORM ▷ SpringkilesBlack ▷ . . . .

Slide 16

Slide 16 text

▷ Easy setup ▷ Abstract SQLite boilerplate codes ▷ No direct dealing with SQL ▷ Object Mapping ORM Good Part

Slide 17

Slide 17 text

▷ Slow, Most ORM uses reflections, read write performance is slow ▷ Not entirely SQL free, still need to write queries for certain task ▷ And as underlying is SQLite all the issues with database scaling. ORM Bad Part

Slide 18

Slide 18 text

What is Realm? Realm is an object database designed for mobile

Slide 19

Slide 19 text

“ Zero Copy Object Store

Slide 20

Slide 20 text

Object Store A E B D C VS

Slide 21

Slide 21 text

Object Store SELECT table1.col, table3.col FROM table1 JOIN table2 ON table1.primarykey = table2. foreignkey JOIN table3 ON table2.primarykey = table3. foreignkey

Slide 22

Slide 22 text

Object Store A E B D C Realm.getA().getB().getD()

Slide 23

Slide 23 text

“ Zero Copy

Slide 24

Slide 24 text

// create Cursor in order to parse our sqlite results Cursor cursor = sqliteDB.rawQuery("SELECT bookTitle FROM " + tableName, null); // if Cursor contains results if (cursor != null) { // move cursor to first row if (cursor.moveToFirst()) { do { // Get version from Cursor String bookName = cursor.getString(cursor.getColumnIndex("bookTitle")); } while (cursor.moveToNext()); } } What is Zero Copy

Slide 25

Slide 25 text

Zero Copy Src: Building Mobile DataBase by Christian Melchior http://www.slideshare.net/ChristianMelchior/realm- building-a-mobile-database#25

Slide 26

Slide 26 text

Person person = new Person() //Standard Java Person proxyperson = realm.copyToRealm(person) //Proxy object Person proxyperson = realm.createObject(Person.class) //Proxy object //Query Results are Lazy RealmResults queryResults = realm.allObjects(Person.class) queryResult.get(0) != queryResult.get(0) //different objects queryResult.get(0).equals(queryResult.get(0)) //same data Zero Copy

Slide 27

Slide 27 text

▷ Easy Setup ▷ Memory mapped files ▷ C++ core ▷ B+ trees ▷ Column Oriented ▷ Minimize IO overhead ▷ Cross platform ▷ Faster Read & writes ▷ Read > Writes ▷ Simple Querying interface Designed for Mobile

Slide 28

Slide 28 text

Benchmarking Benchmarking realm against other databases

Slide 29

Slide 29 text

Src: Building Mobile DataBase by Christian Melchior http://www.slideshare.net/ChristianMelchior/realm- building-a-mobile-database#25

Slide 30

Slide 30 text

Sample against 1,00,000 objects read and write

Slide 31

Slide 31 text

Let’s See some Code

Slide 32

Slide 32 text

Setup compile 'io.realm:realm-android: 0.86.1' Realm realm = Realm.getInstance(context)

Slide 33

Slide 33 text

Method and Size ▷ ~ 1750 method counts ▷ adds ~730 KB fat to apk ▷ install size less

Slide 34

Slide 34 text

Realm Objects //Standard Java POJO from RealmObject public class Person extends RealmObject { @PrimaryKey private String personName; private int personAge; ….. …... //Getters and Setters }

Slide 35

Slide 35 text

Writing Data //Standard Java extend person from RealmObject Person person = new Person() //call setter to set data …. //save data try{ realm.beginTransaction(); realm.copyToRealm(person); realm.commitTransaction(); }catch(Exception e){ realm.cancelTransaction(); // rollback data }

Slide 36

Slide 36 text

Writing Data Async Block //transaction block realm.executeTransaction(new Realm.Transaction() { @Override public void execute(Realm realm) { Person person = realm.createObject(Person.class); person.setName("John"); person.setEmail("[email protected]"); } });

Slide 37

Slide 37 text

▷ Supports Primary Key ▷ Supports primitive data types like int, long, boolean etc. ▷ Supports Box types like Integer, Long, String etc. ▷ Allows null value for non primitives types ▷ Supports Indexing Realm Object

Slide 38

Slide 38 text

Query Data // Build the query looking at all users: RealmQuery query = realm.where(User.class); // Add query conditions: query.equalTo("name", "John"); query.or().equalTo("name", "Peter"); // Execute the query: RealmResults result1 = query.findAll(); // Or alternatively do the same all at once (the "Fluent interface"): RealmResults result2 = realm.where(User.class) .equalTo("name", "John") .or() .equalTo("name", "Peter") .findAll();

Slide 39

Slide 39 text

Query Data // Queries use Builder pattern to build up the query conditions RealmResults query = realm.where(Dog.class) .greaterThan("age", 8) .findAll(); // Queries are chainable RealmResults allRex = query.where() .contains("name", "rex") .findAll();

Slide 40

Slide 40 text

▷ Conditions like lessThan, greaterThan, contains, beginsWith etc ▷ Modifiers for ignoring Cases ▷ Logical Operations OR AND ▷ Sorting Ascending, Descending ▷ Chaining Queries ▷ Aggregations like SUM, MIN, MAX, AVG ▷ Synchronous, Asynchronous operations Query Data Supports

Slide 41

Slide 41 text

Relationships // Any Realm Object can be linked together public class Email extends RealmObject { private String address; private boolean active; // ... setters and getters left out } public class Contact extends RealmObject { private String name; private Email email; // ... setters and getters left out }

Slide 42

Slide 42 text

Link Queries // You can now link queries together RealmResults contacts = realm.where(Contact. class).equalTo("emails.active", true).findAll();

Slide 43

Slide 43 text

▷ Direct support for Json to Object store ▷ Notification for database change like CP ▷ Encryption support in built ▷ Adapters for binding data with views ▷ Cross Platform, same data can work across iOS and Android ▷ Robust 3rd parties addons Other Features

Slide 44

Slide 44 text

Limitations

Slide 45

Slide 45 text

▷ Not Entirely NOSQL, need migration on schema Changes ▷ No support for auto incrementing primary keys ▷ Closed source code ▷ Mutable, thread-confined objects ▷ No support for realm objects across threads ▷ Slower write ▷ Increase method count and size of apk Limitations

Slide 46

Slide 46 text

Realm Browser

Slide 47

Slide 47 text

Mac App to browse data stored on realm files (there is a separate plugin for STETHO)

Slide 48

Slide 48 text

Who else is using Realm Is it production ready?

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Thanks! Any questions? You can find me at: @dhuma1981 +DhrumilShah [email protected]

Slide 51

Slide 51 text

Credits Special thanks to all the people who made and released these awesome resources for free: ▷ Realm by realm.io ▷ Christian Melchior from Realm Team ▷ Pranay Airan from GDG BlrDroid ▷ Presentation template by SlidesCarnival