Relaxing With CouchDB

2 029 vues

Publié le

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

Publié dans : Technologie
0 commentaire
3 j’aime
Statistiques
Remarques
  • Soyez le premier à commenter

Aucun téléchargement
Vues
Nombre de vues
2 029
Sur SlideShare
0
Issues des intégrations
0
Intégrations
93
Actions
Partages
0
Téléchargements
56
Commentaires
0
J’aime
3
Intégrations 0
Aucune incorporation

Aucune remarque pour cette diapositive

Relaxing With CouchDB

  1. 1. relaxing with couchdb will leinweber will@bitfission.com acts_as_conference 2009 1
  2. 2. 2
  3. 3. this talk: why & how 3
  4. 4. what is couchdb? 4
  5. 5. document database no schema 5
  6. 6. erlang concurrent scalable 6
  7. 7. built for the web restful json 7
  8. 8. json { quot;titlequot;: quot;A Brief History of Slinkiesquot;, quot;chaptersquot;: [ { quot;titlequot;: quot;Sorta like a springquot;, quot;textquot;: quot;Round and metal...quot; }, { quot;titlequot;: quot;Stairsquot;, quot;textquot;: quot;They can go down, but not upquot; }], quot;_idquot;: quot;4b859...quot;, quot;_revquot;: quot;3280991488quot; } 8
  9. 9. multi version concurrency control no locking locking mvcc write reads 9
  10. 10. add only never in a bad state 10
  11. 11. incremental replication eventual consistency winning documents 11
  12. 12. couchdb only apps javascript + html 12
  13. 13. multiple databases for a single app 13
  14. 14. integrate with full text search 14
  15. 15. file attachment 15
  16. 16. views (not queries) 16
  17. 17. stored as design documents 17
  18. 18. view server javascript (spidermonkey) ruby and python too 18
  19. 19. map reduce (optional) 19
  20. 20. goes through each document very slow 20
  21. 21. map step emits keys value pairs 21
  22. 22. persisted index keeps track of changes 22
  23. 23. keep your views fresh 23
  24. 24. http benefits 24
  25. 25. cacheable load balancers 25
  26. 26. easy interface understand and implement 26
  27. 27. getting started 27
  28. 28. install couch from svn head get erlang, spidermonkey, icu 28
  29. 29. gem install jchris-couchrest lightweight api wrapper 29
  30. 30. db = CouchRest.database!(quot;http://localhost:5984/booksquot;) response = db.save(:title => quot;recipesquot;) # => {quot;revquot;=>quot;2351730874quot;, quot;idquot;=>quot;07cb62...quot;, quot;okquot;=>true} doc = db.get response[quot;idquot;] # => {quot;titlequot;=>quot;recipesquot;, quot;_idquot;=>quot;07cb62...quot;, quot;_revquot;=>quot;2351730874quot;} 30
  31. 31. $ curl http://localhost:5984/books/07cb6232593b61dd022d1c05b1c7deac {quot;_idquot;:quot;07cb6232593b61dd022d1c05b1c7deacquot;,quot;_revquot;:quot;2351730874quot;, quot;titlequot;:quot;recipesquot;} 31
  32. 32. doc[quot;titlequot;] = quot;cook bookquot; doc.save # => true db.get response[quot;idquot;] # => {quot;titlequot;=>quot;cook bookquot;, quot;_idquot;=>quot;07cb623...quot;, quot;_revquot;=>quot;3767210759quot;} doc.destroy # => true db.get response[quot;idquot;] # => RestClient::ResourceNotFound 32
  33. 33. 33
  34. 34. simple view function(doc) { if (doc.type == quot;bookquot;) { emit(null, doc); } db.view(quot;books/allquot;) } 34
  35. 35. view with keys function(doc) { emit(doc.type, doc); } db.view(quot;books/allquot; )['rows'].size # => 10 db.view(quot;all/by_typequot; )['rows'].size # => 30 db.view(quot;all/by_typequot;, :key => quot;bookquot;)['rows'].size # => 10 35
  36. 36. map reduce // map // reduce function(doc) { function(keys,values) { emit(doc.type, doc); return(values.length); } } db.view(quot;count/by_typequot;) # => {quot;rowsquot;=> {quot;valuequot;=>3, quot;keyquot;=>nil}]} db.view(quot;count/by_typequot;, :group => true) # => {quot;rowsquot;=>[{quot;valuequot;=>10, quot;keyquot;=>quot;articlequot;}, {quot;valuequot;=>10, quot;keyquot;=>quot;bookquot;}, {quot;valuequot;=>10, quot;keyquot;=>quot;userquot;}]} db.view(quot;count/by_typequot;, :key => quot;bookquot;) # => {quot;rowsquot;=>[{quot;valuequot;=>10, quot;keyquot;=>nil}]} 36
  37. 37. versioning { quot;titlequot;: quot;Slinkies!quot;, quot;versionquot;: 4, quot;master_idquot;: quot;3de0c...quot;, quot;_idquot;: quot;af322...quot;, quot;chaptersquot;: [...] } 37
  38. 38. versioning // map // reduce function(doc) { function(keys, values) { emit( doc.master_id, var max = 0; doc ); } for(i in values) { if( values[i].version > values[max].version ) { max = i; } } return(values[max]); } 38
  39. 39. view collation { quot;_idquot;: quot;def345quot;, { quot;_idquot;: quot;abc012quot;, quot;_revquot;: quot;2387quot;, quot;_revquot;: quot;2387quot;, quot;typequot;: quot;commentquot;, quot;typequot;: quot;postquot;, quot;dataquot;: quot;...quot; } quot;dataquot;: quot;...quot; } { quot;_idquot;: quot;r2d2c3quot;, quot;_revquot;: quot;2653quot;, quot;typequot;: quot;commentquot;, quot;dataquot;: quot;...quot; } 39
  40. 40. view collation function(doc) { if (doc.type == quot;postquot;) { emit([doc._id, 0], doc); } else if (doc.type == quot;commentquot;) { emit([doc.post, 1], doc); } } 40
  41. 41. CouchRest::Model being removed from couchrest class Book < CouchRest::Model key_accessor :title, :text, :author cast :author, :as => quot;Userquot; timestamps! end # config/environment.rb CouchRest::Model.default_database = CouchRest.database!(quot;appname-#{ENV['RAILS_ENV']}quot;) 41
  42. 42. others langalex-couch_potato active couch 42
  43. 43. downsides 43
  44. 44. moving target 44
  45. 45. arbitrary queries are slow 45
  46. 46. lack of supporting tools 46
  47. 47. loss of intuition 47
  48. 48. what you should do next 48
  49. 49. thanks! 49

×