49
relaxing with couchdb will leinweber [email protected] acts_as_conference 2009 1

Relaxing With CouchDB

Embed Size (px)

DESCRIPTION

My presentation on using CouchDB with Ruby on Rails for acts_as_conference 2009

Citation preview

Page 1: Relaxing With CouchDB

relaxing with couchdbwill leinweber

[email protected]

acts_as_conference 20091

Page 2: Relaxing With CouchDB

2

Page 3: Relaxing With CouchDB

this talk: why & how

3

Page 4: Relaxing With CouchDB

what is couchdb?

4

Page 5: Relaxing With CouchDB

document databaseno schema

5

Page 6: Relaxing With CouchDB

erlangconcurrentscalable

6

Page 7: Relaxing With CouchDB

built for the webrestfuljson

7

Page 8: Relaxing With CouchDB

json{ "title": "A Brief History of Slinkies", "chapters": [{ "title": "Sorta like a spring", "text": "Round and metal..." },{ "title": "Stairs", "text": "They can go down, but not up" }],

"_id": "4b859...", "_rev": "3280991488" }

8

Page 9: Relaxing With CouchDB

multi version concurrency controlno locking

locking mvccwrite reads

9

Page 10: Relaxing With CouchDB

add onlynever in a bad state

10

Page 11: Relaxing With CouchDB

incremental replicationeventual consistencywinning documents

11

Page 12: Relaxing With CouchDB

couchdb only appsjavascript + html

12

Page 13: Relaxing With CouchDB

multiple databases for a single app

13

Page 14: Relaxing With CouchDB

integrate with full text search

14

Page 15: Relaxing With CouchDB

file attachment

15

Page 16: Relaxing With CouchDB

views(not queries)

16

Page 17: Relaxing With CouchDB

stored as design documents

17

Page 18: Relaxing With CouchDB

view serverjavascript (spidermonkey)ruby and python too

18

Page 19: Relaxing With CouchDB

mapreduce (optional)

19

Page 20: Relaxing With CouchDB

goes through each documentvery slow

20

Page 21: Relaxing With CouchDB

map stepemits keys value pairs

21

Page 22: Relaxing With CouchDB

persisted indexkeeps track of changes

22

Page 23: Relaxing With CouchDB

keep your views fresh

23

Page 24: Relaxing With CouchDB

http benefits

24

Page 25: Relaxing With CouchDB

cacheableload balancers

25

Page 26: Relaxing With CouchDB

easy interfaceunderstand and implement

26

Page 27: Relaxing With CouchDB

getting started

27

Page 28: Relaxing With CouchDB

install couch from svn headget erlang, spidermonkey, icu

28

Page 29: Relaxing With CouchDB

gem install jchris-couchrestlightweight api wrapper

29

Page 30: Relaxing With CouchDB

db = CouchRest.database!("http://localhost:5984/books")response = db.save(:title => "recipes") # =>

{"rev"=>"2351730874", "id"=>"07cb62...", "ok"=>true}

doc = db.get response["id"] # => {"title"=>"recipes","_id"=>"07cb62...", "_rev"=>"2351730874"}

30

Page 31: Relaxing With CouchDB

$ curl http://localhost:5984/books/07cb6232593b61dd022d1c05b1c7deac{"_id":"07cb6232593b61dd022d1c05b1c7deac","_rev":"2351730874","title":"recipes"}

31

Page 32: Relaxing With CouchDB

doc["title"] = "cook book"doc.save # => truedb.get response["id"] # => {"title"=>"cook book",

"_id"=>"07cb623...", "_rev"=>"3767210759"}

doc.destroy # => truedb.get response["id"] # => RestClient::ResourceNotFound

32

Page 33: Relaxing With CouchDB

33

Page 34: Relaxing With CouchDB

function(doc) { if (doc.type == "book") { emit(null, doc); }}

simple view

db.view("books/all")

34

Page 35: Relaxing With CouchDB

function(doc) { emit(doc.type, doc);}

view with keys

db.view("books/all" )['rows'].size # => 10db.view("all/by_type" )['rows'].size # => 30db.view("all/by_type", :key => "book")['rows'].size # => 10

35

Page 36: Relaxing With CouchDB

// mapfunction(doc) { emit(doc.type, doc);}

map reduce// reducefunction(keys,values) { return(values.length);}

db.view("count/by_type") # => {"rows"=> {"value"=>3, "key"=>nil}]}db.view("count/by_type", :group => true) # =>

{"rows"=>[{"value"=>10, "key"=>"article"}, {"value"=>10, "key"=>"book"}, {"value"=>10, "key"=>"user"}]}

db.view("count/by_type", :key => "book") # =>{"rows"=>[{"value"=>10, "key"=>nil}]}

36

Page 37: Relaxing With CouchDB

versioning{ "title": "Slinkies!", "version": 4, "master_id": "3de0c...", "_id": "af322...", "chapters": [...]}

37

Page 38: Relaxing With CouchDB

versioning// mapfunction(doc) { emit( doc.master_id, doc );}

// reducefunction(keys, values) { var max = 0; for(i in values) { if( values[i].version > values[max].version ) { max = i; } } return(values[max]);}

38

Page 39: Relaxing With CouchDB

view collation{ "_id": "abc012", "_rev": "2387", "type": "post", "data": "..." }

{ "_id": "def345", "_rev": "2387", "type": "comment", "data": "..." }

{ "_id": "r2d2c3", "_rev": "2653", "type": "comment", "data": "..." }

39

Page 40: Relaxing With CouchDB

view collationfunction(doc) { if (doc.type == "post") { emit([doc._id, 0], doc); } else if (doc.type == "comment") { emit([doc.post, 1], doc); }}

40

Page 41: Relaxing With CouchDB

CouchRest::Modelbeing removed from couchrest

class Book < CouchRest::Model key_accessor :title, :text, :author cast :author, :as => "User" timestamps!end

# config/environment.rbCouchRest::Model.default_database = CouchRest.database!("appname-#{ENV['RAILS_ENV']}")

41

Page 42: Relaxing With CouchDB

others langalex-couch_potato active couch

42

Page 43: Relaxing With CouchDB

downsides

43

Page 44: Relaxing With CouchDB

moving target

44

Page 45: Relaxing With CouchDB

arbitrary queries are slow

45

Page 46: Relaxing With CouchDB

lack of supporting tools

46

Page 47: Relaxing With CouchDB

loss of intuition

47

Page 48: Relaxing With CouchDB

what you should do next

48

Page 49: Relaxing With CouchDB

thanks!

49