phpillow & couchdb - content - kore nordmann · pdf filephpillow & couchdb kore...

69
PHPillow & CouchDB Kore Nordmann <[email protected]> September 11, 2009 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>

Upload: nguyendiep

Post on 04-Mar-2018

257 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

PHPillow & CouchDB

Kore Nordmann <[email protected]>

September 11, 2009

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 2: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

About me 2 / 46

I Kore Nordmann, <[email protected]>, <[email protected]>

I Long time PHP developer

I Regular speaker, author, etc.

I Studies computer science in DortmundI Active open source developer:

I eZ Components (Graph, WebDav, Document), Arbit,PHPUnit, Torii, PHPillow, KaForkL, Image 3D, WCV, ...

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 3: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Outline 3 / 46

Introduction

CouchDB

PHPillow

Views

Real world examples

QA

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 4: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Databases 4 / 46

I Who uses an RDBMS (relational database managementsystem)?

I Who uses a hash based “database”? (MemcacheDB, ...)I Who uses a “document” based database?

I Amazon SimpleDBI StrokeDB (Ruby)I FeatherDB (Java port of CouchDB)I CouchDB

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 5: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Databases 4 / 46

I Who uses an RDBMS (relational database managementsystem)?

I Who uses a hash based “database”? (MemcacheDB, ...)I Who uses a “document” based database?

I Amazon SimpleDBI StrokeDB (Ruby)I FeatherDB (Java port of CouchDB)I CouchDB

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 6: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Databases 4 / 46

I Who uses an RDBMS (relational database managementsystem)?

I Who uses a hash based “database”? (MemcacheDB, ...)I Who uses a “document” based database?

I Amazon SimpleDBI StrokeDB (Ruby)I FeatherDB (Java port of CouchDB)I CouchDB

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 7: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Outline 5 / 46

Introduction

CouchDB

PHPillow

Views

Real world examples

QA

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 8: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

PHPillow 6 / 46

I A PHP based object oriented client for CouchDB

I The Pillow to make using the couch more comfortable

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 9: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

PHPillow 6 / 46

I A PHP based object oriented client for CouchDB

I The Pillow to make using the couch more comfortable

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 10: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB vs. RDBMS 7 / 46

I RDBMS: Static data with dynamic views

I CouchDB: Dynamic data with “static” views

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 11: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 12: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 13: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 14: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 15: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 16: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 17: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 18: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 19: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

CouchDB 8 / 46

I Document based databaseI Arbitrary JSON objects, attachmentsI Store anything you want, even deep structuresI Schema-lessI No inter-document consistency

I Apache Project

I Communicates with clients using HTTP

I Trivial replication [Apa09]

I Local consistency, weak clustered consitency (ACID, MVCC)

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 20: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

The CAP theorem 9 / 46

I The CAP theorem, read more in “CouchDB: The DefinitiveGuide” [JCA09]

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 21: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Architecture 10 / 46

I Written in/for Erlang/OTP

I Scales nearly linearly with amount of processorsI Highly fault tolerant (9 nines @Ericsson)

I “Live” updates of application code

I VM Developed by Ericsson for telephony systems (OTP)I Focus on stability and scalability

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 22: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Architecture 10 / 46

I Written in/for Erlang/OTP

I Scales nearly linearly with amount of processorsI Highly fault tolerant (9 nines @Ericsson)

I “Live” updates of application code

I VM Developed by Ericsson for telephony systems (OTP)I Focus on stability and scalability

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 23: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Architecture 10 / 46

I Written in/for Erlang/OTP

I Scales nearly linearly with amount of processorsI Highly fault tolerant (9 nines @Ericsson)

I “Live” updates of application code

I VM Developed by Ericsson for telephony systems (OTP)I Focus on stability and scalability

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 24: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Architecture 10 / 46

I Written in/for Erlang/OTP

I Scales nearly linearly with amount of processorsI Highly fault tolerant (9 nines @Ericsson)

I “Live” updates of application code

I VM Developed by Ericsson for telephony systems (OTP)I Focus on stability and scalability

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 25: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Communicates with HTTP (1/3) 11 / 46

1 $ c u r l − I −X PUT ht tp :// l o c a l h o s t :5984/ unconf2 HTTP/1 .1 201 Created3 Se r v e r : CouchDB/0 . 1 0 . 0 a773833 ( E r l ang OTP/R12B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85

6 {”ok ” : t r u e }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 26: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Communicates with HTTP (2/3) 12 / 46

1 $ c u r l − I −X GET ht tp :// l o c a l h o s t :5984/ unconf2 HTTP/1 .1 200 OK3 Se r v e r : CouchDB/0 . 1 0 . 0 a773833 ( E r l ang OTP/R12B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85

6 {7 ”db name ” : ” unconf ” ,8 ” doc count ” : 0 ,9 ” do c d e l c o un t ” : 0 ,

10 ” upda t e s eq ” : 0 ,11 ” pu rg e s eq ” : 0 ,12 ” compact runn ing ” : f a l s e ,13 ” d i s k s i z e ” : 4096 ,14 ” i n s t a n c e s t a r t t i m e ” : ”1243673303593969” ,15 ” d i s k f o r m a t v e r s i o n ” : 216 }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 27: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Communicates with HTTP (3/3) 13 / 46

1 $ c u r l − I −X GET ht tp :// l o c a l h o s t :5984/ unknown2 HTTP/1 .1 404 Object Not Found3 Se r v e r : CouchDB/0 . 1 0 . 0 a773833 ( E r l ang OTP/R12B)4 Content−Type : t e x t / p l a i n ; c h a r s e t=ut f−85

6 {” e r r o r ” : ” no t f ound ” ,” r ea son ” :” Mi s s i ng ”}

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 28: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Outline 14 / 46

Introduction

CouchDB

PHPillow

Views

Real world examples

QA

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 29: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

PHPillow 15 / 46

I Object-oriented client for CouchDB

I PHP >= 5.2 since last release (5.3 only before)

I >96% test coverageI Still in alpha state

I Since CouchDB is still “alpha”

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 30: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

PHPillow 15 / 46

I Object-oriented client for CouchDB

I PHP >= 5.2 since last release (5.3 only before)

I >96% test coverageI Still in alpha state

I Since CouchDB is still “alpha”

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 31: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

PHPillow 15 / 46

I Object-oriented client for CouchDB

I PHP >= 5.2 since last release (5.3 only before)

I >96% test coverageI Still in alpha state

I Since CouchDB is still “alpha”

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 32: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Document examples 16 / 46

I Document creation example

1 // Crea te a document2 $doc = new phpi l lowUserDocument ( ) ;3 $doc−> l o g i n = ’ ko re ’ ;4 $doc−>name = ’ Kore Nordmann ’ ;5 $doc−>data = ar ray (6 ’ ma i l ’ => ”kore@php . net ” ,7 // . . .8 ) ;9 $ i d = $doc−>save ( ) ;

10

11 // Fetch a document by ID12 $doc = new phpi l lowUserDocument ( $ i d ) ;

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 33: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Outline 17 / 46

Introduction

CouchDB

PHPillow

Views

Real world examples

QA

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 34: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-reduce-views 18 / 46

I “MapReduce is a software framework introduced by Google tosupport distributed computing on large data sets on clustersof computers.” [Wik09]

I Used by CouchDB to implement views

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 35: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 19 / 46

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 36: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 20 / 46

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 37: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 21 / 46

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 38: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 22 / 46

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 39: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 40: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 41: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 42: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 43: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 44: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 45: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce 23 / 46

I Map and reduce functions are customI Defined in any language

I ECMAScript (Spidermonkey), PHP, Ruby, Python, Erlang, . . .

I Multiple map-reduce-functions (per document)

I Keys and values may be any JSON data structure

I Reduce is optional, mapping serves as a document index

I Reduce may be applied to subsets of the documents

I Reduce may be grouped

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 46: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 24 / 46

I A single document

1 { i d : ” t r a c k e r i s s u e −24” ,2 r e v : ”1−1015848757” ,3 r e v i s i o n s : [ . . . ] ,4 i s s u e : ”24” ,5 autho r : ” use r−ko re ” ,6 e d i t e d : 1249305176 , // !7 t i t l e : ” Implement VCS module” ,8 t e x t : ” . . . ” ,9 i s s u eType : ” enhancement ” ,

10 s t a t e : ” c l o s e d ” ,11 p r i o r i t y : ”medium” ,12 r e s o l u t i o n : ” implemented ” ,13 s ch edu l ed : ”0.2− a lpha ” ,14 a s s i g n e d : [ ” use r−kore ” ] ,15 t ype : ” t r a c k e r i s s u e ” // !16 }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 47: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 25 / 46

I The map function

1 f u n c t i o n ( doc )2 {3 i f ( doc . type == ” t r a c k e r i s s u e ” )4 {5 date = new Date ( ) ;6 date . setTime ( doc . e d i t e d ∗ 1000 ) ;7 emit ( [8 date . getUTCFul lYear ( ) ,9 date . getUTCMonth ( ) + 1 ,

10 date . getUTCDate ( ) ,11 date . getUTCHours ( ) ,12 date . getUTCMinutes ( ) ,13 date . getUTCSeconds ( ) ,14 ] , 1 ) ;15 // You cou ld a l s o emit the whole doc as v a l u e16 }17 }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 48: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 26 / 46

I The mapping result

1 [ 2008 , 10 , 11 , 9 , 11 , 12 ] => 12 [ 2008 , 10 , 11 , 9 , 11 , 12 ] => 13 [ 2008 , 10 , 11 , 9 , 11 , 12 ] => 14 [ 2008 , 10 , 11 , 9 , 13 , 8 ] => 15 [ 2008 , 10 , 11 , 9 , 13 , 44 ] => 16 [ 2008 , 10 , 11 , 9 , 14 , 2 ] => 17 [ 2008 , 10 , 12 , 17 , 46 , 15 ] => 18 [ 2008 , 10 , 12 , 17 , 57 , 52 ] => 19 [ 2008 , 10 , 12 , 18 , 0 , 45 ] => 1

10 [ 2008 , 10 , 14 , 8 , 36 , 29 ] => 111 [ 2008 , 10 , 14 , 19 , 33 , 21 ] => 112 [ 2008 , 10 , 14 , 19 , 33 , 35 ] => 1

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 49: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 27 / 46

I The reduce function

1 f u n c t i o n ( keys , v a l u e s , combine )2 {3 i f ( combine )4 {5 re tu rn sum( v a l u e s ) ;6 }7 e l s e8 {9 re tu rn v a l u e s . l e n g t h ;

10 }11 }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 50: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 28 / 46

I The reduce result

1 nu l l => 12

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 51: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 29 / 46

I The grouped reduce result

1 [ 2008 , 10 , 11 , 9 , 11 , 12 ] => 32 [ 2008 , 10 , 11 , 9 , 13 , 8 ] => 13 [ 2008 , 10 , 11 , 9 , 13 , 44 ] => 14 [ 2008 , 10 , 11 , 9 , 14 , 2 ] => 15 [ 2008 , 10 , 12 , 17 , 46 , 15 ] => 16 [ 2008 , 10 , 12 , 17 , 57 , 52 ] => 17 [ 2008 , 10 , 12 , 18 , 0 , 45 ] => 18 [ 2008 , 10 , 14 , 8 , 36 , 29 ] => 19 [ 2008 , 10 , 14 , 19 , 33 , 21 ] => 1

10 [ 2008 , 10 , 14 , 19 , 33 , 35 ] => 1

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 52: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 30 / 46

I The filtered grouped reduce result

I startkey=[2008,10,11] endkey=[2008,10,12]

1 [ 2008 , 10 , 11 , 9 , 11 , 12 ] => 32 [ 2008 , 10 , 11 , 9 , 13 , 8 ] => 13 [ 2008 , 10 , 11 , 9 , 13 , 44 ] => 14 [ 2008 , 10 , 11 , 9 , 14 , 2 ] => 1

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 53: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Map-Reduce example 31 / 46

I The grouped reduce result, with group level

I group-level=3

1 [ 2008 , 10 , 11 ] => 62 [ 2008 , 10 , 12 ] => 33 [ 2008 , 10 , 14 ] => 3

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 54: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Outline 32 / 46

Introduction

CouchDB

PHPillow

Views

Real world examples

QA

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 55: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Applications 33 / 46

I Seems useful for everything, which follows the ”document”paradigm.

I WikisI Issue trackerI Content management

I Not useful for data with hard consistency constraints

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 56: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Applications 33 / 46

I Seems useful for everything, which follows the ”document”paradigm.

I WikisI Issue trackerI Content management

I Not useful for data with hard consistency constraints

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 57: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Wiki 34 / 46

I Index all documents by their title

1 f u n c t i o n ( doc )2 {3 i f ( doc . type == ” w i k i p a g e ” )4 {5 emit ( [ doc . namespace , doc . t i t l e ] , doc . i d ) ;6 }7 }

I No reduce function

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 58: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Wiki 35 / 46

I Index all documents by their title

1 [ ” d e v e l opmen t w i k i ” , ” Bu i ldModu leDes ign ” ] => ”w ik i page−d e v e l o pmen t w i k i b u i l dmodu l e d e s i g n ”

2 [ ” d e v e l opmen t w i k i ” , ” Cod i n gGu i d e l i n e s ” ] => ”w ik i page−d e v e l o pme n t w i k i c o d i n g g u i d e l i n e s ”

3 [ ” d e v e l opmen t w i k i ” , ” D i s c u s s i o nP r o t o c o l s ” ] => ”w ik i page−d e v e l o pm e n t w i k i d i s c u s s i o n p r o t o c o l s ”

4 [ ” d e v e l opmen t w i k i ” , ”ModuleDesign ” ] => ”w ik i page−dev e l opmen t w i k i modu l e d e s i gn ”

5 [ ” d e v e l opmen t w i k i ” , ” P r o t o c o l 0 8 02 07 ” ] => ”w ik i page−d e v e l o pmen t w i k i p r o t o c o l 0 8 0 2 0 7 ”

6 [ ” d e v e l opmen t w i k i ” , ”VCSModuleDesign” ] => ”w ik i page−de v e l o pmen t w i k i v c smodu l e d e s i g n ”

7 . . .

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 59: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Wiki 36 / 46

I Find last edits in one namespace

1 f u n c t i o n ( doc )2 {3 i f ( doc . type == ” w i k i p a g e ” &&4 doc . e d i t e d )5 {6 emit ( [ doc . namespace , doc . e d i t e d ] , doc . i d ) ;7 }8 }

I No reduce function

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 60: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Wiki 37 / 46

I Find last edits in one namespace

1 [ ” d e v e l opmen t w i k i ” , 1223731672] => ” w ik i page−d e v e l o pme n t w i k i c o d i n g g u i d e l i n e s ”

2 [ ” d e v e l opmen t w i k i ” , 1223731854] => ” w ik i page−d e v e l o pmen t w i k i p r o t o c o l 0 8 0 2 1 4 ”

3 [ ” d e v e l opmen t w i k i ” , 1223731918] => ” w ik i page−d e v e l o pmen t w i k i p r o t o c o l 0 8 0 2 0 7 ”

4 [ ” d e v e l opmen t w i k i ” , 1223817097] => ” w ik i page−deve lopment w ik i r oadmap ”

5 [ ” d e v e l opmen t w i k i ” , 1223832559] => ” w ik i page−d e v e l o pm e n t w i k i d i s c u s s i o n p r o t o c o l s ”

6 [ ” d e v e l opmen t w i k i ” , 1223833275] => ” w ik i page−dev e l opmen t w i k i modu l e d e s i gn ”

7 . . .

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 61: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Full-Text-Search 38 / 46

I Index all documents by all their words

1 f u n c t i o n ( doc ) {2 i f ( doc . type == ” t r a c k e r i s s u e ” ) {3 // S imple word i ndex i ng , does not r e s p e c t o v e r a l l

o c cu r en c e s o f words ,4 // stopwords , d i f f e r e n t word s e p e r a t i o n c h a r a c t e r s ,

o r word v a r i a t i o n s .5 va r t e x t = doc . t i t l e . r e p l a c e ( / [\ s : . , ! ? − ]+/ g , ” ”

) +6 doc . t e x t . r e p l a c e ( / [\ s : . , ! ? − ]+/ g , ” ” ) ;7 va r words = t e x t . s p l i t ( ” ” ) ;8 f o r ( va r i = 0 ; i < words . l e n g t h ; ++i ) {9 v a l u e = {} ;

10 v a l u e [ doc . i d ] = 1 ;11 emit ( words [ i ] . toLowerCase ( ) , v a l u e ) ;12 }13 }14 }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 62: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Wiki 39 / 46

I Index all documents by all their words

1 . . .2 ”a” => { t r a c k e r i s s u e −8: 1}3 ”a” => { t r a c k e r i s s u e −8: 1}4 ”a” => { t r a c k e r i s s u e −8: 1}5 ”a” => { t r a c k e r i s s u e −8: 1}6 ”a” => { t r a c k e r i s s u e −81: 1}7 ”a” => { t r a c k e r i s s u e −83: 1}8 ”a” => { t r a c k e r i s s u e −83: 1}9 ” ab l e ” => { t r a c k e r i s s u e −39: 1}

10 ” ab l e ” => { t r a c k e r i s s u e −56: 1}11 ” ab l e ” => { t r a c k e r i s s u e −73: 1}12 ” ab l e ” => { t r a c k e r i s s u e −80: 1}13 ” about ” => { t r a c k e r i s s u e −24: 1}14 ” about ” => { t r a c k e r i s s u e −43: 1}15 ” about ” => { t r a c k e r i s s u e −85: 1}16 . . .

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 63: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Full-Text-Search 40 / 46

I Reduce by word count

1 f u n c t i o n ( keys , v a l u e s ) {2 va r count = {} ;3 f o r ( va r i i n v a l u e s ) {4 f o r ( va r i d i n v a l u e s [ i ] ) {5 i f ( count [ i d ] ) {6 count [ i d ] = v a l u e s [ i ] [ i d ] + count [ i d ] ;7 } e l s e {8 count [ i d ] = v a l u e s [ i ] [ i d ] ;9 }

10 }11 }12 re tu rn count ;13 }

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 64: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Wiki 41 / 46

I Index all documents by all their words

1 . . .2 ”a” => {3 t r a c k e r i s s u e −68: 6 ,4 t r a c k e r i s s u e −66: 6 ,5 t r a c k e r i s s u e −22: 4 ,6 t r a c k e r i s s u e −63: 3 ,7 t r a c k e r i s s u e −60: 2 ,8 t r a c k e r i s s u e −35: 2 ,9 t r a c k e r i s s u e −34: 1 ,

10 t r a c k e r i s s u e −31: 1 ,11 . . .12 }13 ” ab l e ” => { t r a c k e r i s s u e −86: 1 , t r a c k e r i s s u e −80:

1 , t r a c k e r i s s u e −73: 1 , t r a c k e r i s s u e −56: 1 ,t r a c k e r i s s u e −39: 1}

14 ” about ” => { t r a c k e r i s s u e −85: 1 , t r a c k e r i s s u e −43:1 , t r a c k e r i s s u e −24: 1}

15 . . .

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 65: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Summary 42 / 46

I Document based database

I Views using any language, following the map-reduce-pattern.

I Fast, reliable, scalable

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 66: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Outline 43 / 46

Introduction

CouchDB

PHPillow

Views

Real world examples

QA

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 67: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

URLs 44 / 46

I Apache CouchDB: http://couchdb.org/

I Free CouchDB book: http://books.couchdb.org/relax/

I PHPillow:http://kore-nordmann.de/projects/phpillow/

I CouchDB use case: http://kore-nordmann.de/blog/phpillow_php_couchdb_wrapper.html

I Implementing a user permission system using CouchDB

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 68: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

The end 45 / 46

I Open questions?

I Further remarks?I Contact

I Mail: <[email protected]>I Web: http://kore-nordmann.de/ (Slides will be available

here soonish)I Twitter: http://twitter.com/korednI Thank me: http://wishlist.kore-nordmann.de/

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>

Page 69: PHPillow & CouchDB - content - Kore Nordmann · PDF filePHPillow & CouchDB Kore Nordmann  September 11, 2009   Kore Nordmann

Bibliography I 46 / 46

[Apa09] Apache, Replication,http://wiki.apache.org/couchdb/Replication, July 2009.

[JCA09] Noah Slater J. Chris Anderson, Jan Lehnardt, Couchdb: Thedefinitive guide, O’Reilly Media, Inc., 2009.

[Wik09] Wikipedia, Mapreduce — wikipedia, the free encyclopedia, 2009,[Online; accessed 27-August-2009].

http://kore-nordmann.de/portfolio.html

Kore Nordmann <[email protected]>