Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CouchDB: a Database for the Web

Renato Neves
September 22, 2012

CouchDB: a Database for the Web

Nesta apresentação irei falar sobre conceitos do banco de dados CouchDB (http://couchdb.apache.org/), como design documents, views, show e list functions. Também irei falar sobre replicação e change notifications, que são poderosas features deste banco de dados NoSQL.

Renato Neves

September 22, 2012
Tweet

Other Decks in Programming

Transcript

  1. Summary • Introduction • MVCC • The Core API •

    Design Documents • Views, Show Functions, List Functions • Change Notifications • Be Careful... • References Sunday, September 30, 12
  2. Introduction • Built on the Erlang Platform • NoSQL Database

    • Schemaless • MapReduce • B-tree storage engine - O(log N) • Eventual Consistency by incremental replication Sunday, September 30, 12
  3. MVCC • Multi-Version Concurrency Control • No Locking • Documents

    are versioned • Each read operation is guaranteed a consistent view of the database Sunday, September 30, 12
  4. The Core API • Server • Databases • Documents •

    Replication Sunday, September 30, 12
  5. Documents API curl -X PUT http://127.0.0.1:5984/albums/ 6e1295ed6c29495e54cc05947f18c8af -d '{"title":"There is

    Nothing Left to Lose","artist":"Foo Fighters"}’ {"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af ","rev":"1-2902191555"} Creating a Document Sunday, September 30, 12
  6. Documents API curl -X PUT http://127.0.0.1:5984/albums/ 6e1295ed6c29495e54cc05947f18c8af -d '{"_rev":"1-2902191555","title":"There is

    Nothing Left to Lose", "artist":"Foo Fighters","year":"1997"} {"ok":true,"id":"6e1295ed6c29495e54cc05947f18c8af ","rev":"2-2739352689"} Updating a Document Sunday, September 30, 12
  7. Replication curl -X PUT http://127.0.0.1:5984/albums-replica curl -vX POST http://127.0.0.1:5984/_replicate -d

    '{"source":"albums","target":"albums-replica"}' Sunday, September 30, 12
  8. Design Documents • Special type of CouchDB document that contains

    application code • Views • Shows • Lists Sunday, September 30, 12
  9. Views function(doc) { if(doc.date && doc.title) { emit(doc.date, doc.title); }

    } Map Function GET /database/_design/ designdocname/_view/viewname { "total_rows": 3, "offset": 0, "rows": [ { "key": "2009/01/15 15:52:20", "id": "hello-world", "value": "Hello World" }, { "key": "2009/02/17 21:13:39", "id": "bought-a-cat", "value": "Bought a Cat" }, { "key": "2009/01/30 18:04:11", "id": "biking", "value": "Biking" } ] } Response API Sunday, September 30, 12
  10. Views function (key, values, rereduce) { return sum(values); } Reduce

    Function • Reduce: results emitted by map function • Rereduce: results returned by the reduce function itself function(null, [3, 1], true) { return sum([3, 1]); } Rereduce Function Sunday, September 30, 12
  11. Show Functions { “_id” : “_design/mydesign”, “shows” : { “myshow”

    : “function(doc, req) { return '<h1>' + doc.title + '</h1>'; }” } } Show Function GET /mydb/_design/mydesign/_show/ myshow/72d43a93eb74b5f2 API • Query parameters • Etags • Functions and templates (CouchApp) Sunday, September 30, 12
  12. List Functions { "_id" : "_design/foo", "_rev" : "1-67at7bg", "lists"

    : { "bar" : "function(head, req) { var row; while (row = getRow()) { ... send() ... } }", "zoom" : "function() { return 'zoom!' }", } } List Function GET /mydb/_design/mydesign/_list/ mylist/myview API Sunday, September 30, 12
  13. Be careful... • It’s not a relational database!!! • Linked

    documents when include_docs • Views are updated on read access • Changing a view rebuild all views of the design document • “Stale” views • Pre-warm views • Database compaction and cleanup Sunday, September 30, 12
  14. References • CouchDB (http://couchdb.apache.org/) • CouchApp (http://couchapp.org) • CouchDB The

    Definitive Guide (http:// guide.couchdb.org/) • Ruby: couchrest (https://github.com/ couchrest/couchrest), couchrest_model (https://github.com/couchrest/ couchrest_model) Sunday, September 30, 12