objectbox - the new mobile database

27
greenrobot.org @greenrobot_de Markus Junginger, greenrobot.org, @greenrobot_de

Upload: greenrobot

Post on 21-Jan-2018

625 views

Category:

Software


0 download

TRANSCRIPT

Page 1: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

Markus Junginger, greenrobot.org, @greenrobot_de

Page 2: ObjectBox - The new Mobile Database

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

Page 3: ObjectBox - The new Mobile Database

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 ObjectORM

Page 4: ObjectBox - The new Mobile Database

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 styleNote note = noteDao.load(id);updateUi(note.getNote(), note.getDate());

Page 5: ObjectBox - The new Mobile Database

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

Page 6: ObjectBox - The new Mobile Database

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…“

Page 7: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

“Happy to announce…”

Page 8: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

OBJECTBO

X

Page 9: ObjectBox - The new Mobile Database

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

Page 10: ObjectBox - The new Mobile Database

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

Page 11: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

• Let‘s say you have a couple of objects…

ObjectBox API

Page 12: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

ObjectBox API

• Put objects in a box to store them

Page 13: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

ObjectBox API

• Get objects from a box to load them

Page 14: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

ObjectBox API

• Remove objects from a box to delete them

Page 15: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

ObjectBox API

• There is a box for each type of object

Circles Triangles

Page 16: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

ObjectBox API

• You get boxes from a box store

Circles Triangles Lines

Users Products Sales

Page 17: ObjectBox - The new Mobile Database

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();

Page 18: ObjectBox - The new Mobile Database

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

Page 19: ObjectBox - The new Mobile Database

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

Page 20: ObjectBox - The new Mobile Database

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;

}

Page 21: ObjectBox - The new Mobile Database

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)

Page 22: ObjectBox - The new Mobile Database

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

Page 23: ObjectBox - The new Mobile Database

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

Page 24: ObjectBox - The new Mobile Database

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

Page 25: ObjectBox - The new Mobile Database

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

Page 26: ObjectBox - The new Mobile Database

greenrobot.org

@greenrobot_de

Thank you!

[email protected]

Join/follow us:greenrobot.org/objectbox@greenrobot_de

Page 27: ObjectBox - The new Mobile Database

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/