Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Getting into REAL(M) business by dumping SQLite

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Dhrumil Shah
December 28, 2015

Getting into REAL(M) business by dumping SQLite

Presented this talk at DevFest Ahmedabad 2015 (#DevFestAhm). In this talk I have covered what is Realm? Why you should use Realm over SQLite and ORM with code snippets.

Dhrumil Shah

December 28, 2015

Transcript

51 slides
  1. Hello! I am Dhrumil Shah I work at Bugle Technologies

    Pvt. Ltd. & Co-organizer at GDG Ahmedabad. You can find me at: @dhuma1981 +DhrumilShah
  2. “

  3. BoilerPlate mess ▷ 1000’s of lines of boilerplate mess ▷

    Lot of string just to define a single table ▷ Tightly coupled schema ▷ RAW SQL Queries
  4. Nested Queries SELECT table1.col, table3.col FROM table1 JOIN table2 ON

    table1.primarykey = table2.foreignkey JOIN table3 ON table2.primarykey = table3.foreignkey
  5. 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
  6. ▷ SugarORM ▷ ORMLite ▷ CubBoard ▷ ActiveRecord ▷ Ollie

    ▷ DBFLow ORM Options ▷ DBQuery ▷ GreenDao ▷ EasyLiteORM ▷ RushORM ▷ SpringkilesBlack ▷ . . . .
  7. ▷ Easy setup ▷ Abstract SQLite boilerplate codes ▷ No

    direct dealing with SQL ▷ Object Mapping ORM Good Part
  8. ▷ 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
  9. Object Store SELECT table1.col, table3.col FROM table1 JOIN table2 ON

    table1.primarykey = table2. foreignkey JOIN table3 ON table2.primarykey = table3. foreignkey
  10. // 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
  11. 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<Person> queryResults = realm.allObjects(Person.class) queryResult.get(0) != queryResult.get(0) //different objects queryResult.get(0).equals(queryResult.get(0)) //same data Zero Copy
  12. ▷ 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
  13. Method and Size ▷ ~ 1750 method counts ▷ adds

    ~730 KB fat to apk ▷ install size less
  14. Realm Objects //Standard Java POJO from RealmObject public class Person

    extends RealmObject { @PrimaryKey private String personName; private int personAge; ….. …... //Getters and Setters }
  15. 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 }
  16. 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]"); } });
  17. ▷ 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
  18. Query Data // Build the query looking at all users:

    RealmQuery<User> query = realm.where(User.class); // Add query conditions: query.equalTo("name", "John"); query.or().equalTo("name", "Peter"); // Execute the query: RealmResults<User> result1 = query.findAll(); // Or alternatively do the same all at once (the "Fluent interface"): RealmResults<User> result2 = realm.where(User.class) .equalTo("name", "John") .or() .equalTo("name", "Peter") .findAll();
  19. Query Data // Queries use Builder pattern to build up

    the query conditions RealmResults<Dog> query = realm.where(Dog.class) .greaterThan("age", 8) .findAll(); // Queries are chainable RealmResults<Dog> allRex = query.where() .contains("name", "rex") .findAll();
  20. ▷ 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
  21. 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 }
  22. Link Queries // You can now link queries together RealmResults<Contact>

    contacts = realm.where(Contact. class).equalTo("emails.active", true).findAll();
  23. ▷ 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
  24. ▷ 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
  25. Mac App to browse data stored on realm files (there

    is a separate plugin for STETHO)
  26. 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

More Decks by Dhrumil Shah

Other Decks in Programming