Visual Analytics with Tableau & Couchbase: Couchbase Connect 2015
Introduction to Couchbase Mobile: Couchbase Connect 2014
-
Upload
couchbase -
Category
Data & Analytics
-
view
678 -
download
3
description
Transcript of Introduction to Couchbase Mobile: Couchbase Connect 2014
Introduction to Couchbase LiteTraun Leyden | Couchbase Lite Android Developer,
Couchbase
©2014 Couchbase, Inc.
Here’s what I’m going to cover:
Overview of the problem
A demo of Couchbase Lite
An overview of Couchbase Lite
A deep dive into Couchbase Lite core concepts
Resources
Q&A
Overview
2
Where should you store your mobile app data?
Local Only Remote Only Local and Remote
©2014 Couchbase, Inc.
Implications of Data Location choice
4
Data
Location
Network
Requirement
Application
Availability
Application
Responsiveness
Enabling
Technology
Remote
Only
Always
Connected
Sometimes
AvailableVariable
REST Services
(JSON)
Local
Only
Never
Connected
Always
Available
Always
FastSQLite
Local
& Remote
Sometimes
Connected
Always
Available
Always
Fast?
1 2 3 4 5
©2014 Couchbase, Inc.
Narrowing down Data Location options
5
Data
Location
Network
RequirementApplication Availability
Application
Responsiveness
Remote
Only
Local
Only
(maybe in 90s)
Local
& Remote
©2014 Couchbase, Inc.
Narrowing down Data Location options
6
Data
Location
Network
RequirementApplication Availability
Application
Responsiveness
Remote
Only
Always
Connected
(unrealistic)
Sometimes
Available
(bad)
Variable
(bad)
Local
Only
(maybe in 90s)
Local
& Remote
Sometimes
Connected
Always
Available
Always
Fast
©2014 Couchbase, Inc.
Requirements:
Always fast — because the network is taken out of the critical path
Always available — for the same reason
Data location constraint:
Local & Remote.
Local so that the application is always available and fast
Remote so that it can be shared with other users
Requirements recap
7
©2014 Couchbase, Inc.
Requirement simplification: treat the local data as “read-only” cached copy.
For example, a Twitter app
Remote data is pulled and stored locally in SQLite
The app shows the data from SQLite
No network — no problem
Writes (new Tweets) require an internet connection
Once the write is pushed to the server, it is stored locally
Simple enough to build from scratch
Building an app that meets the requirements
8
©2014 Couchbase, Inc.
But what if you need to build an Evernote clone rather than a Twitter app?
New requirements:
The user can add / edit / delete notes while completely offline
If online, but network is slow, modifying notes must still be fast
The user can even edit the same note on two different offline devices
and ...
Adding a requirement: user writes to local data store
9
©2014 Couchbase, Inc.
Adding a requirement: user writes to local data store
10
Your boss wants it done by the end of this sprint!
©2014 Couchbase, Inc.
Why is this hard?
The “local read only” storage no longer works
You no longer have a single master — it’s now a multi-master system
Deletes are tricky
Conflicts are tricky
You’ll need to build a full-fledged “sync engine”
Adding a requirement: user writes to local data store
11
You probably know what’s coming next …
We’ve already built this for you
Demo
Couchbase Lite Overview
©2014 Couchbase, Inc.
Couchbase Lite Architecture
14
Sync Gateway
Couchbase
Lite
Couchbase
Lite
App
Lite
App
Lite
HTTP
Sync
©2014 Couchbase, Inc.
Couchbase Lite Architecture
15
Peer to Peer
HTTP
Sync
Couchbase
Lite
Couchbase
Lite
AppLite
AppLite
©2014 Couchbase, Inc.
Supported Couchbase Lite Platforms
Native iOS / OSX
Native Android
Native Java (Linux, OSX, Windows)
Native C# -> Native iOS / Android / Windows Phone via Xamarin
Phonegap
Titanium
Couchbase Lite Architecture
16
(100% Open Source — Apache 2)
©2014 Couchbase, Inc.
Couchbase Lite core functionality
Document CRUD
Attachments
Views / Queries (MapReduce)
Replication
Pluggable “change listener” to monitor DB events
REST API (PhoneGap)
Couchbase Lite Overview
17
©2014 Couchbase, Inc.
Couchbase Lite extended functionality - Part I
REST API Listener (iOS, Android)
Javascript Based Views (iOS, Android)
Couchbase Lite Overview
18
©2014 Couchbase, Inc.
Couchbase Lite extended functionality, Part II
Model layer (iOS)
Full Text Indexing (iOS)
Geoqueries (iOS)
Database-level encryption via SQLCipher (iOS)
Couchbase Lite Overview
19
Noteworthy on the horizon ...
Experimental branch w/ ForestDB backend (iOS)
Couchbase Lite Deep Dive
CRUD
©2014 Couchbase, Inc.
Modeling data in JSON is different than SQL
You can have arbitrarily nested structure in a document
Contact addresses could be included as an array
You can gain some “atomicity” between fields by putting them in the same document
There are no joins — if a doc contains an ID of another doc, a separate lookup would be needed
Couchbase Lite Deep Dive
21
©2014 Couchbase, Inc.
JSON Document
22
Sample JSON document
{
"_id":"[email protected]",
"_rev":"1-238",
"type":"User",
"addresses":[
{
"address_type":"home",
"street":"Easy St"
},
{
"address_type":"work",
"street":"Mission St"
}
]
}
_id can be specified or auto-assigned a UUID. Must be unique in database.
_rev is special metadata used by the system to track revisions + conflicts
“type” is a common convention to specify “object type”
©2014 Couchbase, Inc.
CRUD - Creating a new document
You pass it an NSDictionary (iOS) or a Map (Java) that represents your JSON Document
The Map can contain Lists and other Maps
Everything must be serializable into primitive types
Couchbase Lite Deep Dive
23
©2014 Couchbase, Inc.
CRUD - Understanding Attachments
A document can have any number of attachments
Typical example: Profile document with image of user attached
Efficient sync behavior: attachment data only transferred when the attachment has changed
Common question: sync priority? Ans: not yet
Couchbase Lite Deep Dive
24
©2014 Couchbase, Inc.
CRUD - Editing Documents
Your edit attempt may fail!
Pass the entire new Map object, with the previous _rev ID field
It does a “Compare and Set”, and if the previous _rev ID field is different than what you passed, the operation will fail
How can this happen? New revision of doc is pulled while user is editing a UI form.
Couchbase Lite Deep Dive
25
©2014 Couchbase, Inc.
CRUD - Deletions are new special Revision
A deletion adds a new “Tombstone” revision
This revision is sync’d like any other revision
If a doc has a tombstone revision, Couchbase Lite pretends its not there.
If needed, you can actually retain any information you like in the Tombstone revision
Clean up happens by a process called Compaction
Couchbase Lite Deep Dive
26
©2014 Couchbase, Inc.
CRUD - A few “fringe” features to be aware of
Document purging allows you to delete all traces of a document locally, without affecting any other clients
Local Documents allow you to create a document that is never sync’d
Couchbase Lite Deep Dive
27
©2014 Couchbase, Inc.
Revisions and Conflicts
28
Imagine a document editable by Alice and Bob
Alice creates Rev 3-21ab of document while offline
Bob creates Rev 3-42cc of document while offline
Alice and Bob both go online and devices sync
On both devices, Couchbase Lite deterministically chooses Rev 3-21ab as the winner and deletes Rev 3-42cc
As of Rev 4-45cb, the conflict is resolved
Rev
1-34ad
Rev
2-63fc
Rev
4-10ac
Rev
4-45cb
Rev
5-42bb
Tombstone
Rev
3-21ab
Rev
3-42ccConflict
©2014 Couchbase, Inc.
CRUD - A few more notes about conflicts
The system will “auto-resolve” conflicts in a deterministic manner
Non-deletions always chosen over deletions
Branches with more revisions win over others
All else equal, lexicographic compare of revision Id digest (the non-numeric part of the revision)
It’s not possible for your app to “intercept” this logic
However, your app can retroactively choose a different conflict resolution, possibly doing application specific merge
Couchbase Lite Deep Dive
29
Couchbase Lite Deep Dive
Views + Queries
©2014 Couchbase, Inc.
Views
Purpose: The primary way of “getting data out” of DB, aside from fetching single doc by ID
Harder to grasp than SQL
As the developer, you are responsible for building low level indexes
No joins — Some queries can be awkward
Typically, a TableView / ListAdapter will correspond to a single Couchbase Lite View
A map function is required for every view
An optional reduce function can be specified — a typical use case would be for “stats”
Typically written in native code (Java / ObjC), but there is an option to write in Javascript
Couchbase Lite Deep Dive
31
©2014 Couchbase, Inc.
Couchbase Lite Deep Dive
32
Suppose you have the following docs:
{ "id": "Bob", "age": 22 }
{ "id": "Mary", "age": 52 }
{ "id": "Joe", "age": 28 }
{ "id": "Bill", "age": 15 }
{ "id": "Alice", "age": 20 }
And want to show usernames of all
users between 20 and 30, sorted
ascending by age:
Name
Alice (20)
Bob (22)
Joe (28)
©2014 Couchbase, Inc.
Couchbase Lite Deep Dive
33
Map Function Generated View Index
map(doc) {
# eg, "Alice (20)"
value = "%s (%d)" % (doc.id, doc.age)
# we use the age for the key
key = doc.age
# emit key and value into view index
emit(key, value)
}
Key Value (DocID)
15 Bill (15) Bill
20 Alice (20) Alice
22 Bob (22) Bob
28 Joe (28) Joe
52 Mary (52) Mary
Query Generated View Index
©2014 Couchbase, Inc.
Couchbase Lite Deep Dive
34
query.startKey = 20
query.endKey = 30
Only these
three rows fall
in key range of
query
X
X
Key Value (DocID)
15 Bill (15) Bill
20 Alice (20) Alice
22 Bob (22) Bob
28 Joe (28) Joe
52 Mary (52) Mary
©2014 Couchbase, Inc.
Final notes on Views and Queries
Can emit more than one row into index for single document
Keys can be arrays rather than single value
Queries support “grouping”
A LiveQuery runs persistently and streams new results
A view index is updated lazily when queried, but running a LiveQuery on it will keep it constantly up to date
The map function must always produce same output given same input —cannot depend on current time for example
Couchbase Lite Deep Dive
35
Couchbase Lite Deep Dive
Replications
©2014 Couchbase, Inc.
Couchbase Lite Deep Dive
37
Push and Pull are independent
©2014 Couchbase, Inc.
Sync Gateway
Couchbase
Lite
(Device A)
Push
Couchbase
Lite
(Device B)
Pull Pull
©2014 Couchbase, Inc.
Couchbase Lite Deep Dive
38
One Shot vs. Continuous
©2014 Couchbase, Inc.
Started
Running
Stopped
One Shot
Started
Running Idle
Continuous
©2014 Couchbase, Inc.
Couchbase Lite Deep Dive
39
Callback API for app to be notified of change events
Continuous replications use HTTP longpoll (Java/.NET) or Websockets to open socket to Sync Gateway Changes Feed
A “slice” of the database can be replicated by using Channels
On Android, a background replication will continue to run as long as not in Deep Sleep
On iOS, it’s possible to use push notifications to trigger replication, or have scheduled background trigger to run periodically
Couchbase Lite Resources
©2014 Couchbase, Inc.
Couchbase Lite Resources
41
Downloads: http://mobile.couchbase.com
Documentation: http://developer.couchbase.com
Twitter: @CouchbaseMobile
Google Group (active!): http://groups.google.com/group/mobile-couchbase/
Github Repos:
https://github.com/couchbase/couchbase-lite-ios
https://github.com/couchbase/couchbase-lite-android
https://github.com/couchbase/couchbase-lite-net
https://github.com/couchbaselabs/ToDoLite-Android
https://github.com/couchbaselabs/ToDoLite-iOS
Thanks for watching! Questions?