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

ObjectBox

greenrobot
January 24, 2017

 ObjectBox

ObjectBox is a new superfast mobile database. Learn about how you can develop faster apps with ObjectBox. It is much simpler than SQLite.

greenrobot

January 24, 2017

Transcript

27 slides
  1. greenrobot.org @greenrobot_de Some History: the path to ObjectBox • SQLite

    Available since 2000 THE standard for mobile databases SQL, rows, and columns • ORMs Bridge the gap between objects and tables • Object and NoSQL Databases Getting popular on mobile
  2. greenrobot.org @greenrobot_de Object/Relational Mapping (ORM) • ORMs have a higher

    level of abstraction Read & write objects TABLE generation Building queries etc. Database Java Object Java Object Java Object ORM
  3. greenrobot.org @greenrobot_de Example: SQLite vs. ORM String[] columns = {

    "note", "date_changed" }; String[] idArgs = { String.valueOf(id) }; SQLiteCursor cursor = (SQLiteCursor) db.query("notes", columns, "_id=?", idArgs, null, null, "note"); try { if (cursor.getCount() != 1) { throw new Exception("Unexpected count: " +cursor.getCount()); } cursor.moveToNext(); String note = cursor.getString(0); String date = cursor.getString(1); updateUi(note, date); } finally { cursor.close(); } // ORM style Note note = noteDao.load(id); updateUi(note.getNote(), note.getDate());
  4. greenrobot.org @greenrobot_de greenDAO: ORM by greenrobot • Fastest ORM on

    Android • Started in 2011 • Open Source • First Android ORM with code generation For best performance Avoid reflection (super slow in Android) • “Lessons learnt” and inspiration for ObjectBox
  5. greenrobot.org @greenrobot_de ORM/SQL Limitations • SQLite SQL, unreliable performance, complex,

    sub-optimal APIs especially for objects • Column based databases Splitting up objects and putting them together • No innovations, no performance gains Look at the server side (NoSQL, et al.) • „So, if we designed a database today, how…“
  6. greenrobot.org @greenrobot_de What is ObjectBox? • It‘s based on the

    learnings from greenDAO Objects first, replace everything else • A new database Not build on top of SQLite, NoSQL all through • Android first, cross platform Already runs on Linux, Windows, FreeBSD (macOS and iOS will follow) • It‘s pretty fast
  7. greenrobot.org @greenrobot_de ObjectBox Guiding Principles • Performance Prefer C/C++, Java

    on Android is slow No complicated middle layers • Keep it simple Reduce everything to the minimum Less stuff  better performance
  8. greenrobot.org @greenrobot_de ObjectBox API • You get boxes from a

    box store Circles Triangles Lines Users Products Sales
  9. greenrobot.org @greenrobot_de ObjectBox API: Example Code Box<Note> box = boxStore.boxFor(Note.class);

    box.put(newNote); List<Note> notes = box.getAll(); box.remove(oldNote1, oldNote2); long numberOfNotes = box.count(); Query<Note> query = box.query() .startsWith(Text, "TODO").build(); Note firstNote = query.findFirst();
  10. greenrobot.org @greenrobot_de ObjectBox Objects (aka Entities) • @Entity • An

    object is an object is an object (POJO) • No threading constraints • No hidden costs for accessing data No „live properties“ hitting the DB on getters
  11. greenrobot.org @greenrobot_de ObjectBox Put Semantics • There is no insert,

    update, updateOrInsert, insertOrReplace, … • There is just put • Semantics depend on the ID value of an object 0: new ID assigned, new object is put != 0: An existing object is replaced • It‘s OK to mix new and existing object for put
  12. greenrobot.org @greenrobot_de Object Relations: Code example @Entity public class Order

    { @Id long id; long customerId; @Relation Customer customer; } @Entity public class Customer { @Id long id; // References the ID property in the *Order* entity @Relation(idProperty = "customerId") List<Order> orders; }
  13. greenrobot.org @greenrobot_de ObjectBox Queries • Generated meta classes for entities

    JPA 2 style meta classes: „MyEntity_“ QueryBuilder references entity properties • QueryBuilder Collects all criteria and initial values QueryBuilder builds Query objects • Query has various find methods • Aggregates (sum, min, max, avg)
  14. greenrobot.org @greenrobot_de @Generated Code • ObjectBox has 100% transparent code

    gen. No byte code transformation, no surprises • Some code is generated into the entity Your code and gen. code co-exist in same file • @Generated marker with a content hash Changes are detected (& usually not required) • @Keep overrides @Generated Puts you in charge
  15. greenrobot.org @greenrobot_de Unit Testing and Databases • Claim: Mocking out

    a DB is a waste of time Testing logic including a DB is a great unit integration test • Android instrumentation tests: slow & hassle • Robolectric tests: slow • Testing with ObjectBox: Plain Java & JUnit No dexing / booting up  just instant testing
  16. greenrobot.org @greenrobot_de ObjectBox Migrations • ObjectBox keeps track of entities

    & properties Needs to assign IDs internally etc. Stored in objectbox-models/default.json • Adding and removing just works • Renames: use “refID” from default.json Stable ID – apply before refactorings @Entity(refId = 1234) class AnyNameYouWant
  17. greenrobot.org @greenrobot_de dao-compat • A compatibility layer on top of

    ObjectBox • Emulates greenDAO APIs • For existing apps using greenDAO Easy switch to ObjectBox • For new apps using ObjectBox For the still undecided
  18. greenrobot.org @greenrobot_de Copyright & License Terms Copyright © 2017 greenrobot

    / ObjectBox / Markus Junginger Attribution-ShareAlike 4.0 http://creativecommons.org/licenses/by-sa/4.0/

More Decks by greenrobot

Other Decks in Programming