objectbox - the new mobile database
TRANSCRIPT
greenrobot.org
@greenrobot_de
Markus Junginger, greenrobot.org, @greenrobot_de
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
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
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());
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
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…“
greenrobot.org
@greenrobot_de
“Happy to announce…”
greenrobot.org
@greenrobot_de
OBJECTBO
X
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
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
greenrobot.org
@greenrobot_de
• Let‘s say you have a couple of objects…
ObjectBox API
greenrobot.org
@greenrobot_de
ObjectBox API
• Put objects in a box to store them
greenrobot.org
@greenrobot_de
ObjectBox API
• Get objects from a box to load them
greenrobot.org
@greenrobot_de
ObjectBox API
• Remove objects from a box to delete them
greenrobot.org
@greenrobot_de
ObjectBox API
• There is a box for each type of object
Circles Triangles
greenrobot.org
@greenrobot_de
ObjectBox API
• You get boxes from a box store
Circles Triangles Lines
Users Products Sales
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();
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
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
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;
}
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)
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
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
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
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
greenrobot.org
@greenrobot_de
Thank you!
Join/follow us:greenrobot.org/objectbox@greenrobot_de
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/