128
CouchDB, PHP & PHPillow http://joind.in/1465 Kore Nordmann <[email protected]> @koredn February 26, 2010 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>

CouchDB, PHPillow & PHP

Embed Size (px)

DESCRIPTION

Kore Nordmann's slides from his talk at PHP UK Conference 2010 on PHPillow (a wrapper library he has written), CouchDB & PHP.

Citation preview

Page 1: CouchDB, PHPillow & PHP

CouchDB, PHP & PHPillowhttp://joind.in/1465

Kore Nordmann<[email protected]>

@koredn

February 26, 2010

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

Kore Nordmann <[email protected]>

Page 2: CouchDB, PHPillow & PHP

About me 2 / 70

I Kore Nordmann, <[email protected]>

I Long time PHP developer

I Regular speaker, author, etc.

I Studies computer science in Dortmund, currently writingthesis

I 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: CouchDB, PHPillow & PHP

Outline 3 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 4: CouchDB, PHPillow & PHP

CouchDB is paradigmn shift 4 / 70

I Structure

I Consistency

I API

I Applications

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

Kore Nordmann <[email protected]>

Page 5: CouchDB, PHPillow & PHP

CouchDB is paradigmn shift 4 / 70

I Structure

I Consistency

I API

I Applications

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

Kore Nordmann <[email protected]>

Page 6: CouchDB, PHPillow & PHP

CouchDB is paradigmn shift 4 / 70

I Structure

I Consistency

I API

I Applications

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

Kore Nordmann <[email protected]>

Page 7: CouchDB, PHPillow & PHP

CouchDB is paradigmn shift 4 / 70

I Structure

I Consistency

I API

I Applications

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

Kore Nordmann <[email protected]>

Page 8: CouchDB, PHPillow & PHP

CouchDB is paradigmn shift 4 / 70

I Structure

I Consistency

I API

I Applications

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

Kore Nordmann <[email protected]>

Page 9: CouchDB, PHPillow & PHP

Outline 5 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 10: CouchDB, PHPillow & PHP

CouchDB 6 / 70

I Apache top-level project

I Build in Erlang / on Erlang/OTP

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

Kore Nordmann <[email protected]>

Page 11: CouchDB, PHPillow & PHP

CouchDB 6 / 70

I Apache top-level project

I Build in Erlang / on Erlang/OTP

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

Kore Nordmann <[email protected]>

Page 12: CouchDB, PHPillow & PHP

CouchDB 6 / 70

I Apache top-level project

I Build in Erlang / on Erlang/OTP

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

Kore Nordmann <[email protected]>

Page 13: CouchDB, PHPillow & PHP

Outline 7 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 14: CouchDB, PHPillow & PHP

Structure 8 / 70

I Document based database

I No pre-defined structure (tables)I Put in any JSON object you want

I Even deep structures (arrays of objects)I You may attach any number of files to documents

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

Kore Nordmann <[email protected]>

Page 15: CouchDB, PHPillow & PHP

Structure 8 / 70

I Document based database

I No pre-defined structure (tables)I Put in any JSON object you want

I Even deep structures (arrays of objects)I You may attach any number of files to documents

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

Kore Nordmann <[email protected]>

Page 16: CouchDB, PHPillow & PHP

Structure 8 / 70

I Document based database

I No pre-defined structure (tables)I Put in any JSON object you want

I Even deep structures (arrays of objects)I You may attach any number of files to documents

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

Kore Nordmann <[email protected]>

Page 17: CouchDB, PHPillow & PHP

Structure 8 / 70

I Document based database

I No pre-defined structure (tables)I Put in any JSON object you want

I Even deep structures (arrays of objects)I You may attach any number of files to documents

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

Kore Nordmann <[email protected]>

Page 18: CouchDB, PHPillow & PHP

Structure 8 / 70

I Document based database

I No pre-defined structure (tables)I Put in any JSON object you want

I Even deep structures (arrays of objects)I You may attach any number of files to documents

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

Kore Nordmann <[email protected]>

Page 19: CouchDB, PHPillow & PHP

Structure 9 / 70

I Example wiki document

1 { ” t i t l e ” : ”PHPUK 2010” ,2 ” t e x t ” : ”Welcome to t he PHPUK . . . ” ,3 ” c r e a t o r ” : ” u se r−bar ” ,4 ” e d i t e d ” : 2935678239 ,5 ” r e v i s i o n s ” : [6 { ” t i t l e ” : ”PHPUK 2009” ,7 ” t e x t ” : ”Welcome to th e PHPUK . . . ” ,8 ” c r e a t o r ” : ” us e r−f o o ” ,9 ” e d i t e d ” : 2935678183 ,

10 ]11 } ,12 . . .13 ]14 }

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

Kore Nordmann <[email protected]>

Page 20: CouchDB, PHPillow & PHP

Attachements 10 / 70

I Example wiki document

1 { ” t i t l e ” : ”PHPUK 2010” ,2 ” c r e a t o r ” : ” u se r−bar ” ,3 ” t e x t ” : ”<h1>Welcome to t h e PHPUK</h1>4

5 <img s r c =\”phpuk 2010 / l o g o . png\” a l t =\”PHPUK l o g o \”/>

6 . . . ” ,7 ” a t t a c h m e n t s ” : {8 ” l o g o . png ” : {9 ” c o n t e n t t y p e ” : ” image \/ png ” ,

10 ” s t u b ” : t r u e ,11 ” l e n g t h ” : 42 ,12 }13 }14 }

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

Kore Nordmann <[email protected]>

Page 21: CouchDB, PHPillow & PHP

Migration / Refactoring 11 / 70

I Change document structure at any time

I No need for non-transaction-safe Data Definition Language(DDL)

I Fits rapid development approaches with common customerrequested changes to the data structure

I You need to handle this in your application properly, of course:I Incrementally update structure on modificationI Liberal validation on read

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

Kore Nordmann <[email protected]>

Page 22: CouchDB, PHPillow & PHP

Migration / Refactoring 11 / 70

I Change document structure at any time

I No need for non-transaction-safe Data Definition Language(DDL)

I Fits rapid development approaches with common customerrequested changes to the data structure

I You need to handle this in your application properly, of course:I Incrementally update structure on modificationI Liberal validation on read

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

Kore Nordmann <[email protected]>

Page 23: CouchDB, PHPillow & PHP

Migration / Refactoring 11 / 70

I Change document structure at any time

I No need for non-transaction-safe Data Definition Language(DDL)

I Fits rapid development approaches with common customerrequested changes to the data structure

I You need to handle this in your application properly, of course:I Incrementally update structure on modificationI Liberal validation on read

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

Kore Nordmann <[email protected]>

Page 24: CouchDB, PHPillow & PHP

Migration / Refactoring 11 / 70

I Change document structure at any time

I No need for non-transaction-safe Data Definition Language(DDL)

I Fits rapid development approaches with common customerrequested changes to the data structure

I You need to handle this in your application properly, of course:I Incrementally update structure on modificationI Liberal validation on read

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

Kore Nordmann <[email protected]>

Page 25: CouchDB, PHPillow & PHP

Outline 12 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 26: CouchDB, PHPillow & PHP

HTTP API 13 / 70

I RESTful HTTP accessI HTTP is available on “all” platforms natively

I No PHP extension requiredI Just use PHPs HTTP stream wrapper, pecl/http or curl

I You can use all your known HTTP middlewareI Reverse proxies for scaling reads (Varnish, Squid)I Simple custom proxy configuration for direct “AJAX” access

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

Kore Nordmann <[email protected]>

Page 27: CouchDB, PHPillow & PHP

HTTP API 13 / 70

I RESTful HTTP accessI HTTP is available on “all” platforms natively

I No PHP extension requiredI Just use PHPs HTTP stream wrapper, pecl/http or curl

I You can use all your known HTTP middlewareI Reverse proxies for scaling reads (Varnish, Squid)I Simple custom proxy configuration for direct “AJAX” access

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

Kore Nordmann <[email protected]>

Page 28: CouchDB, PHPillow & PHP

HTTP API 13 / 70

I RESTful HTTP accessI HTTP is available on “all” platforms natively

I No PHP extension requiredI Just use PHPs HTTP stream wrapper, pecl/http or curl

I You can use all your known HTTP middlewareI Reverse proxies for scaling reads (Varnish, Squid)I Simple custom proxy configuration for direct “AJAX” access

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

Kore Nordmann <[email protected]>

Page 29: CouchDB, PHPillow & PHP

HTTP API 14 / 70

I GET / POST / PUT / DELETE

I <METHOD> http://<host>/<database>/<document>

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

Kore Nordmann <[email protected]>

Page 30: CouchDB, PHPillow & PHP

Create a database 15 / 70

1 $ c u r l − i −X PUT h t t p : / / l o c a l h o s t :5984/ p h p u k w i k i2

3 HTTP/ 1 . 1 201 C r e a t e d4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )5 L o c a t i o n : h t t p : / / l o c a l h o s t :5984/ p h p u k w i k i6 Date : F r i , 13 Jan 2009 1 4 : 0 7 : 5 7 GMT7 Content−Type : t e x t / p l a i n ; c h a r s e t=u t f−88 Content−Length : 129 Cache−C o n t r o l : must−r e v a l i d a t e

10

11 {” ok ” : t r u e }

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

Kore Nordmann <[email protected]>

Page 31: CouchDB, PHPillow & PHP

Create a wiki document 16 / 70

1 $ c u r l − i −X PUT h t t p : / / l o c a l h o s t :5984/ p h p u k w i k i / S t a r t−−data ’{” name ” : ” S t a r t ” , ” t e x t ” : ” H e l l o World ! ”} ’

2

3 HTTP/ 1 . 1 201 C r e a t e d4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )5 L o c a t i o n : h t t p : / / l o c a l h o s t :5984/ p h p u k w i k i / S t a r t6 Etag : ”1−6 bfd4885b6c62bb5169a19d5a81927e3 ”7 Date : F r i , 13 Jan 2009 1 4 : 1 4 : 5 5 GMT8 Content−Type : t e x t / p l a i n ; c h a r s e t=u t f−89 Content−Length : 68

10 Cache−C o n t r o l : must−r e v a l i d a t e11

12 {” ok ” : t r u e , ” i d ” : ” S t a r t ” ,” r e v ”:”1−6bfd4885b6c62bb5169a19d5a81927e3 ”}

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

Kore Nordmann <[email protected]>

Page 32: CouchDB, PHPillow & PHP

Get a wiki document back 17 / 70

1 $ c u r l − i −X GET h t t p : / / l o c a l h o s t :5984/ p h p u k w i k i / S t a r t2

3 HTTP/ 1 . 1 200 OK4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )5 Etag : ”1−6 bfd4885b6c62bb5169a19d5a81927e3 ”6 Date : F r i , 13 Jan 2009 1 4 : 1 5 : 4 8 GMT7 Content−Type : t e x t / p l a i n ; c h a r s e t=u t f−88 Content−Length : 979 Cache−C o n t r o l : must−r e v a l i d a t e

10

11 {” i d ” : ” S t a r t ” ,” r e v ”:”1−6bfd4885b6c62bb5169a19d5a81927e3 ” ,” name ” : ” S t a r t ” ,”t e x t ” : ” H e l l o World ! ”}

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

Kore Nordmann <[email protected]>

Page 33: CouchDB, PHPillow & PHP

Security 18 / 70

I Simple database based access restrictions

I Using HTTP plain authI More fine grained access control will be in next release

I Define functions which decide if a request from a user will beaccepted.

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

Kore Nordmann <[email protected]>

Page 34: CouchDB, PHPillow & PHP

Security 18 / 70

I Simple database based access restrictions

I Using HTTP plain authI More fine grained access control will be in next release

I Define functions which decide if a request from a user will beaccepted.

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

Kore Nordmann <[email protected]>

Page 35: CouchDB, PHPillow & PHP

Security 18 / 70

I Simple database based access restrictions

I Using HTTP plain authI More fine grained access control will be in next release

I Define functions which decide if a request from a user will beaccepted.

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

Kore Nordmann <[email protected]>

Page 36: CouchDB, PHPillow & PHP

Outline 19 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 37: CouchDB, PHPillow & PHP

Data access 20 / 70

I How to query such a mess?I Views are small scripts, run for all documents in a databaseI Views are built iteratively, results stored in BTrees

I Once built, they are fast

I Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...I A view may emit any number of key-value pairs for each

documentI Key and value may be any JSON structure

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

Kore Nordmann <[email protected]>

Page 38: CouchDB, PHPillow & PHP

Data access 20 / 70

I How to query such a mess?I Views are small scripts, run for all documents in a databaseI Views are built iteratively, results stored in BTrees

I Once built, they are fast

I Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...I A view may emit any number of key-value pairs for each

documentI Key and value may be any JSON structure

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

Kore Nordmann <[email protected]>

Page 39: CouchDB, PHPillow & PHP

Data access 20 / 70

I How to query such a mess?I Views are small scripts, run for all documents in a databaseI Views are built iteratively, results stored in BTrees

I Once built, they are fast

I Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...I A view may emit any number of key-value pairs for each

documentI Key and value may be any JSON structure

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

Kore Nordmann <[email protected]>

Page 40: CouchDB, PHPillow & PHP

Data access 20 / 70

I How to query such a mess?I Views are small scripts, run for all documents in a databaseI Views are built iteratively, results stored in BTrees

I Once built, they are fast

I Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...I A view may emit any number of key-value pairs for each

documentI Key and value may be any JSON structure

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

Kore Nordmann <[email protected]>

Page 41: CouchDB, PHPillow & PHP

Data access 20 / 70

I How to query such a mess?I Views are small scripts, run for all documents in a databaseI Views are built iteratively, results stored in BTrees

I Once built, they are fast

I Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...I A view may emit any number of key-value pairs for each

documentI Key and value may be any JSON structure

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

Kore Nordmann <[email protected]>

Page 42: CouchDB, PHPillow & PHP

Data access 20 / 70

I How to query such a mess?I Views are small scripts, run for all documents in a databaseI Views are built iteratively, results stored in BTrees

I Once built, they are fast

I Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...I A view may emit any number of key-value pairs for each

documentI Key and value may be any JSON structure

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

Kore Nordmann <[email protected]>

Page 43: CouchDB, PHPillow & PHP

Wiki 21 / 70

I Index all wiki documents by their title

1 f u n c t i o n ( doc )2 {3 i f ( doc . t i t l e && doc . t e x t )4 {5 emit ( doc . t i t l e , doc . i d ) ;6 }7 }

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

Kore Nordmann <[email protected]>

Page 44: CouchDB, PHPillow & PHP

Wiki 22 / 70

I Index all wiki documents by their title

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

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

Kore Nordmann <[email protected]>

Page 45: CouchDB, PHPillow & PHP

Wiki 23 / 70

I Index all documents by their title

1 ” Bu i ldModu leDes ign ” => ” w i k i−b u i l d m o d u l e d e s i g n ”2 ” C o d i n g G u i d e l i n e s ” => ” w i k i−c o d i n g g u i d e l i n e s ”3 ” D i s c u s s i o n P r o t o c o l s ” => ” w i k i−d i s c u s s i o n p r o t o c o l s ”4 ” ModuleDesign ” => ” w i k i−m o d u l e d e s i g n ”5 ” P r o t o c o l 0 8 0 2 0 7 ” => ” w i k i−p r o t o c o l 0 8 0 2 0 7 ”6 ” VCSModuleDesign ” => ” w i k i−v c s m o d u l e d e s i g n ”7 . . .

I Custom deteministic IDs can ensure uniqueness of documentsI Just set the id property on insert.

I CouchDB can also generate IDs for you

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

Kore Nordmann <[email protected]>

Page 46: CouchDB, PHPillow & PHP

Wiki 23 / 70

I Index all documents by their title

1 ” Bu i ldModu leDes ign ” => ” w i k i−b u i l d m o d u l e d e s i g n ”2 ” C o d i n g G u i d e l i n e s ” => ” w i k i−c o d i n g g u i d e l i n e s ”3 ” D i s c u s s i o n P r o t o c o l s ” => ” w i k i−d i s c u s s i o n p r o t o c o l s ”4 ” ModuleDesign ” => ” w i k i−m o d u l e d e s i g n ”5 ” P r o t o c o l 0 8 0 2 0 7 ” => ” w i k i−p r o t o c o l 0 8 0 2 0 7 ”6 ” VCSModuleDesign ” => ” w i k i−v c s m o d u l e d e s i g n ”7 . . .

I Custom deteministic IDs can ensure uniqueness of documentsI Just set the id property on insert.

I CouchDB can also generate IDs for you

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

Kore Nordmann <[email protected]>

Page 47: CouchDB, PHPillow & PHP

Wiki 23 / 70

I Index all documents by their title

1 ” Bu i ldModu leDes ign ” => ” w i k i−b u i l d m o d u l e d e s i g n ”2 ” C o d i n g G u i d e l i n e s ” => ” w i k i−c o d i n g g u i d e l i n e s ”3 ” D i s c u s s i o n P r o t o c o l s ” => ” w i k i−d i s c u s s i o n p r o t o c o l s ”4 ” ModuleDesign ” => ” w i k i−m o d u l e d e s i g n ”5 ” P r o t o c o l 0 8 0 2 0 7 ” => ” w i k i−p r o t o c o l 0 8 0 2 0 7 ”6 ” VCSModuleDesign ” => ” w i k i−v c s m o d u l e d e s i g n ”7 . . .

I Custom deteministic IDs can ensure uniqueness of documentsI Just set the id property on insert.

I CouchDB can also generate IDs for you

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

Kore Nordmann <[email protected]>

Page 48: CouchDB, PHPillow & PHP

Map-reduce-views 24 / 70

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

I Just a framework / pattern: You can implement “any”algorithm using map-reduce.

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

Kore Nordmann <[email protected]>

Page 49: CouchDB, PHPillow & PHP

Map-reduce-views 24 / 70

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

I Just a framework / pattern: You can implement “any”algorithm using map-reduce.

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

Kore Nordmann <[email protected]>

Page 50: CouchDB, PHPillow & PHP

Map-reduce-views 24 / 70

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

I Just a framework / pattern: You can implement “any”algorithm using map-reduce.

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

Kore Nordmann <[email protected]>

Page 51: CouchDB, PHPillow & PHP

Map-Reduce 25 / 70

Documents

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

Kore Nordmann <[email protected]>

Page 52: CouchDB, PHPillow & PHP

Map-Reduce 26 / 70

Documents Map

$key:$value

$key:$value

$key:$value

$key:$value

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

Kore Nordmann <[email protected]>

Page 53: CouchDB, PHPillow & PHP

Map-Reduce 27 / 70

ReduceDocuments Map

$key:$value

$key:$value

$key:$value

$key:$value

$key:$value

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

Kore Nordmann <[email protected]>

Page 54: CouchDB, PHPillow & PHP

Map-Reduce 28 / 70

Documents Re-ReduceReduceMap

$key:$value

$key:$value

$key:$value

$key:$value

$key:$value

$key:$value

$key:$value

Cluster 1

Cluster 2

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

Kore Nordmann <[email protected]>

Page 55: CouchDB, PHPillow & PHP

Map-Reduce 29 / 70

I Map and reduce functions are custom

I Reduce is optional, plain view 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 56: CouchDB, PHPillow & PHP

Map-Reduce 29 / 70

I Map and reduce functions are custom

I Reduce is optional, plain view 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 57: CouchDB, PHPillow & PHP

Map-Reduce 29 / 70

I Map and reduce functions are custom

I Reduce is optional, plain view 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 58: CouchDB, PHPillow & PHP

Map-Reduce 29 / 70

I Map and reduce functions are custom

I Reduce is optional, plain view 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 59: CouchDB, PHPillow & PHP

Map-Reduce example 30 / 70

I The simplest reduce function is just count()I Often used for statistics

1 f u n c t i o n ( keys , v a l u e s , combine )2 {3 re tu rn sum ( v a l u e s ) ;4 }

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

Kore Nordmann <[email protected]>

Page 60: CouchDB, PHPillow & PHP

Map-Reduce example 31 / 70

I The reduce result

1 nu l l => 42

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

Kore Nordmann <[email protected]>

Page 61: CouchDB, PHPillow & PHP

More... 32 / 70

I What should be covered in more depth?I Map-reduce views in CouchDBI Scalability / consistency in CouchDBI The PHPillow API

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

Kore Nordmann <[email protected]>

Page 62: CouchDB, PHPillow & PHP

Map-Reduce example 33 / 70

I The map function

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

10 d a te . getUTCDate ( ) ,11 d a te . getUTCHours ( ) ,12 d a te . getUTCMinutes ( ) ,13 d a te . getUTCSeconds ( ) ,14 ] , 1 ) ;15 // You c o u l d a l s o emit t he whole doc as v a l u e16 }17 }

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

Kore Nordmann <[email protected]>

Page 63: CouchDB, PHPillow & PHP

Map-Reduce example 34 / 70

I The mapping result

1 [ 2 0 0 8 , 10 , 11 , 9 , 11 , 1 2 ] => 12 [ 2 0 0 8 , 10 , 11 , 9 , 11 , 1 2 ] => 13 [ 2 0 0 8 , 10 , 11 , 9 , 11 , 1 2 ] => 14 [ 2 0 0 8 , 10 , 11 , 9 , 13 , 8 ] => 15 [ 2 0 0 8 , 10 , 11 , 9 , 13 , 4 4 ] => 16 [ 2 0 0 8 , 10 , 11 , 9 , 14 , 2 ] => 17 [ 2 0 0 8 , 10 , 12 , 17 , 46 , 1 5 ] => 18 [ 2 0 0 8 , 10 , 12 , 17 , 57 , 5 2 ] => 19 [ 2 0 0 8 , 10 , 12 , 18 , 0 , 4 5 ] => 1

10 [ 2 0 0 8 , 10 , 14 , 8 , 36 , 2 9 ] => 111 [ 2 0 0 8 , 10 , 14 , 19 , 33 , 2 1 ] => 112 [ 2 0 0 8 , 10 , 14 , 19 , 33 , 3 5 ] => 1

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

Kore Nordmann <[email protected]>

Page 64: CouchDB, PHPillow & PHP

Map-Reduce example 35 / 70

I The reduce function

1 f u n c t i o n ( keys , v a l u e s , combine )2 {3 re tu rn sum ( v a l u e s ) ;4 }

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

Kore Nordmann <[email protected]>

Page 65: CouchDB, PHPillow & PHP

Map-Reduce example 36 / 70

I The reduce result

1 nu l l => 12

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

Kore Nordmann <[email protected]>

Page 66: CouchDB, PHPillow & PHP

Map-Reduce example 37 / 70

I The grouped reduce result

1 [ 2 0 0 8 , 10 , 11 , 9 , 11 , 1 2 ] => 32 [ 2 0 0 8 , 10 , 11 , 9 , 13 , 8 ] => 13 [ 2 0 0 8 , 10 , 11 , 9 , 13 , 4 4 ] => 14 [ 2 0 0 8 , 10 , 11 , 9 , 14 , 2 ] => 15 [ 2 0 0 8 , 10 , 12 , 17 , 46 , 1 5 ] => 16 [ 2 0 0 8 , 10 , 12 , 17 , 57 , 5 2 ] => 17 [ 2 0 0 8 , 10 , 12 , 18 , 0 , 4 5 ] => 18 [ 2 0 0 8 , 10 , 14 , 8 , 36 , 2 9 ] => 19 [ 2 0 0 8 , 10 , 14 , 19 , 33 , 2 1 ] => 1

10 [ 2 0 0 8 , 10 , 14 , 19 , 33 , 3 5 ] => 1

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

Kore Nordmann <[email protected]>

Page 67: CouchDB, PHPillow & PHP

Map-Reduce example 38 / 70

I The filtered grouped reduce result

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

1 [ 2 0 0 8 , 10 , 11 , 9 , 11 , 1 2 ] => 32 [ 2 0 0 8 , 10 , 11 , 9 , 13 , 8 ] => 13 [ 2 0 0 8 , 10 , 11 , 9 , 13 , 4 4 ] => 14 [ 2 0 0 8 , 10 , 11 , 9 , 14 , 2 ] => 1

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

Kore Nordmann <[email protected]>

Page 68: CouchDB, PHPillow & PHP

Map-Reduce example 39 / 70

I The grouped reduce result, with group level

I group-level=3

1 [ 2 0 0 8 , 10 , 1 1 ] => 62 [ 2 0 0 8 , 10 , 1 2 ] => 33 [ 2 0 0 8 , 10 , 1 4 ] => 3

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

Kore Nordmann <[email protected]>

Page 69: CouchDB, PHPillow & PHP

Full-Text-Search 40 / 70

I Index all documents by all their words

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

o c c u r e n 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 v a 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 v a r words = t e x t . s p l i t ( ” ” ) ;8 f o r ( v a 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 70: CouchDB, PHPillow & PHP

Wiki 41 / 70

I Index all documents by all their words

1 . . .2 ”a” => {w i k i −8: 1}3 ”a” => {w i k i −8: 1}4 ”a” => {w i k i −8: 1}5 ”a” => {w i k i −8: 1}6 ”a” => {w i k i −81: 1}7 ”a” => {w i k i −83: 1}8 ”a” => {w i k i −83: 1}9 ” a b l e ” => {w i k i −39: 1}

10 ” a b l e ” => {w i k i −56: 1}11 ” a b l e ” => {w i k i −73: 1}12 ” a b l e ” => {w i k i −80: 1}13 ” about ” => {w i k i −24: 1}14 ” about ” => {w i k i −43: 1}15 ” about ” => {w i k i −85: 1}16 . . .

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

Kore Nordmann <[email protected]>

Page 71: CouchDB, PHPillow & PHP

Full-Text-Search 42 / 70

I Reduce by word count

1 f u n c t i o n ( keys , v a l u e s ) {2 v a r count = {} ;3 f o r ( v a r i i n v a l u e s ) {4 f o r ( v a 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 72: CouchDB, PHPillow & PHP

Wiki 43 / 70

I Index all documents by all their words

1 . . .2 ”a” => {3 w i k i −68: 6 ,4 w i k i −66: 6 ,5 w i k i −22: 4 ,6 w i k i −63: 3 ,7 w i k i −60: 2 ,8 w i k i −35: 2 ,9 w i k i −34: 1 ,

10 w i k i −31: 1 ,11 . . .12 }13 ” a b l e ” => {w i k i −86: 1 , w i k i −80: 1 , w i k i −73: 1 , w i k i

−56: 1 , w i k i −39: 1}14 ” about ” => {w i k i −85: 1 , w i k i −43: 1 , w i k i −24: 1}15 . . .

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

Kore Nordmann <[email protected]>

Page 73: CouchDB, PHPillow & PHP

Outline 44 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 74: CouchDB, PHPillow & PHP

Local conflicts 45 / 70

I Multi-Version Concurrency ControlI All documents in the database are versioned

I Don’t use it for application level document versioning

I Updates and deletes need to specify the revision ID

I Changing outdated documents result in conflicts

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

Kore Nordmann <[email protected]>

Page 75: CouchDB, PHPillow & PHP

Local conflicts 45 / 70

I Multi-Version Concurrency ControlI All documents in the database are versioned

I Don’t use it for application level document versioning

I Updates and deletes need to specify the revision ID

I Changing outdated documents result in conflicts

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

Kore Nordmann <[email protected]>

Page 76: CouchDB, PHPillow & PHP

Local conflicts 45 / 70

I Multi-Version Concurrency ControlI All documents in the database are versioned

I Don’t use it for application level document versioning

I Updates and deletes need to specify the revision ID

I Changing outdated documents result in conflicts

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

Kore Nordmann <[email protected]>

Page 77: CouchDB, PHPillow & PHP

Local conflicts 45 / 70

I Multi-Version Concurrency ControlI All documents in the database are versioned

I Don’t use it for application level document versioning

I Updates and deletes need to specify the revision ID

I Changing outdated documents result in conflicts

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

Kore Nordmann <[email protected]>

Page 78: CouchDB, PHPillow & PHP

Local conflicts 45 / 70

I Multi-Version Concurrency ControlI All documents in the database are versioned

I Don’t use it for application level document versioning

I Updates and deletes need to specify the revision ID

I Changing outdated documents result in conflicts

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

Kore Nordmann <[email protected]>

Page 79: CouchDB, PHPillow & PHP

Inter document links 46 / 70

I There is no ensured inter document consistency in CouchDBI Different possibilities of relating documents:

I List IDs of related documents in document (n:m)I ... both directions are feasibleI Embed the whole related document (1:n)

I Solution depends on update-ratio

1 { ” t y p e ” : ” w i k i ” ,2 ” t i t l e ” : ” H e l l o w o r l d ” ,3 ” t e x t ” : ” . . . ” ,4 ”comments ” : [5 { ”comment ” : ” . . . ” } ,6 ] ,7 ” c r e a t o r ” : ” us e r−f o o ” ,8 }

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

Kore Nordmann <[email protected]>

Page 80: CouchDB, PHPillow & PHP

Inter document links 46 / 70

I There is no ensured inter document consistency in CouchDBI Different possibilities of relating documents:

I List IDs of related documents in document (n:m)I ... both directions are feasibleI Embed the whole related document (1:n)

I Solution depends on update-ratio

1 { ” t y p e ” : ” w i k i ” ,2 ” t i t l e ” : ” H e l l o w o r l d ” ,3 ” t e x t ” : ” . . . ” ,4 ”comments ” : [5 { ”comment ” : ” . . . ” } ,6 ] ,7 ” c r e a t o r ” : ” us e r−f o o ” ,8 }

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

Kore Nordmann <[email protected]>

Page 81: CouchDB, PHPillow & PHP

Inter document links 46 / 70

I There is no ensured inter document consistency in CouchDBI Different possibilities of relating documents:

I List IDs of related documents in document (n:m)I ... both directions are feasibleI Embed the whole related document (1:n)

I Solution depends on update-ratio

1 { ” t y p e ” : ” w i k i ” ,2 ” t i t l e ” : ” H e l l o w o r l d ” ,3 ” t e x t ” : ” . . . ” ,4 ”comments ” : [5 { ”comment ” : ” . . . ” } ,6 ] ,7 ” c r e a t o r ” : ” us e r−f o o ” ,8 }

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

Kore Nordmann <[email protected]>

Page 82: CouchDB, PHPillow & PHP

Inter document links 46 / 70

I There is no ensured inter document consistency in CouchDBI Different possibilities of relating documents:

I List IDs of related documents in document (n:m)I ... both directions are feasibleI Embed the whole related document (1:n)

I Solution depends on update-ratio

1 { ” t y p e ” : ” w i k i ” ,2 ” t i t l e ” : ” H e l l o w o r l d ” ,3 ” t e x t ” : ” . . . ” ,4 ”comments ” : [5 { ”comment ” : ” . . . ” } ,6 ] ,7 ” c r e a t o r ” : ” us e r−f o o ” ,8 }

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

Kore Nordmann <[email protected]>

Page 83: CouchDB, PHPillow & PHP

Inter document links 46 / 70

I There is no ensured inter document consistency in CouchDBI Different possibilities of relating documents:

I List IDs of related documents in document (n:m)I ... both directions are feasibleI Embed the whole related document (1:n)

I Solution depends on update-ratio

1 { ” t y p e ” : ” w i k i ” ,2 ” t i t l e ” : ” H e l l o w o r l d ” ,3 ” t e x t ” : ” . . . ” ,4 ”comments ” : [5 { ”comment ” : ” . . . ” } ,6 ] ,7 ” c r e a t o r ” : ” us e r−f o o ” ,8 }

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

Kore Nordmann <[email protected]>

Page 84: CouchDB, PHPillow & PHP

Inter document links 46 / 70

I There is no ensured inter document consistency in CouchDBI Different possibilities of relating documents:

I List IDs of related documents in document (n:m)I ... both directions are feasibleI Embed the whole related document (1:n)

I Solution depends on update-ratio

1 { ” t y p e ” : ” w i k i ” ,2 ” t i t l e ” : ” H e l l o w o r l d ” ,3 ” t e x t ” : ” . . . ” ,4 ”comments ” : [5 { ”comment ” : ” . . . ” } ,6 ] ,7 ” c r e a t o r ” : ” us e r−f o o ” ,8 }

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

Kore Nordmann <[email protected]>

Page 85: CouchDB, PHPillow & PHP

Scaling: The CAP theorem 47 / 70

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

Availability Partitiontolerance

Consistency

CouchDB

RDBMSPAXON

eventualconsistency

enforcedconsistency

consensusprotocols

I CouchDB employs “Eventual Consistency” [Vog09]

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

Kore Nordmann <[email protected]>

Page 86: CouchDB, PHPillow & PHP

Eventual consistency 48 / 70

Europe

Asia Amerika

Client /Web-Application

I Delayed, triggered synchronization (push, pull)I Deterministic (manual) conflict resolution on replication on all

nodes

I Scales well for seldom concurrent writesI Structure your documents accordingly

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

Kore Nordmann <[email protected]>

Page 87: CouchDB, PHPillow & PHP

Eventual consistency 48 / 70

Europe

Asia Amerika

Client /Web-Application

I Delayed, triggered synchronization (push, pull)I Deterministic (manual) conflict resolution on replication on all

nodes

I Scales well for seldom concurrent writesI Structure your documents accordingly

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

Kore Nordmann <[email protected]>

Page 88: CouchDB, PHPillow & PHP

Eventual consistency 48 / 70

Europe

Asia Amerika

Client /Web-Application

I Delayed, triggered synchronization (push, pull)I Deterministic (manual) conflict resolution on replication on all

nodes

I Scales well for seldom concurrent writesI Structure your documents accordingly

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

Kore Nordmann <[email protected]>

Page 89: CouchDB, PHPillow & PHP

Eventual consistency 48 / 70

Europe

Asia Amerika

I Delayed, triggered synchronization (push, pull)I Deterministic (manual) conflict resolution on replication on all

nodes

I Scales well for seldom concurrent writesI Structure your documents accordingly

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

Kore Nordmann <[email protected]>

Page 90: CouchDB, PHPillow & PHP

Eventual consistency 48 / 70

Europe

Asia Amerika

Client /Web-Application

I Delayed, triggered synchronization (push, pull)I Deterministic (manual) conflict resolution on replication on all

nodes

I Scales well for seldom concurrent writesI Structure your documents accordingly

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

Kore Nordmann <[email protected]>

Page 91: CouchDB, PHPillow & PHP

Outline 49 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 92: CouchDB, PHPillow & PHP

PHPillow 50 / 70

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 just got “beta” recently, and no new releasewas required.

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

Kore Nordmann <[email protected]>

Page 93: CouchDB, PHPillow & PHP

PHPillow 50 / 70

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 just got “beta” recently, and no new releasewas required.

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

Kore Nordmann <[email protected]>

Page 94: CouchDB, PHPillow & PHP

PHPillow 50 / 70

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 just got “beta” recently, and no new releasewas required.

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

Kore Nordmann <[email protected]>

Page 95: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 96: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 97: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 98: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 99: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 100: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 101: CouchDB, PHPillow & PHP

PHPillow 51 / 70

I Lightweight layerI Features

I Simple document validation constraintsI Automatic synchronization of viewsI Automatic versioning of documentsI couchdb-python compatible tool for dump and import

I Different connection handlersI PHP HTTP stream wrapperI Custom HTTP protocol implementation

I Which is faster, most likely because of Connection:

Keep-Alive

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

Kore Nordmann <[email protected]>

Page 102: CouchDB, PHPillow & PHP

A document class 52 / 70

I A custom document class

1 <?php2 c l a s s myUserDocument e x t e n d s phpi l lowDocument {3 p r o t e c t e d $ v e r s i o n e d = t rue ;4

5 // . . .6 }

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

Kore Nordmann <[email protected]>

Page 103: CouchDB, PHPillow & PHP

A document class 53 / 70

I A custom document class

1 <?php2 c l a s s myUserDocument e x t e n d s phpi l lowDocument {3 p r o t e c t e d $ v e r s i o n e d = t rue ;4

5 p r o t e c t e d $ r e q u i r e d P r o p e r t i e s = ar ray (6 ’ l o g i n ’ ,7 ) ;8

9 // . . .10 }

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

Kore Nordmann <[email protected]>

Page 104: CouchDB, PHPillow & PHP

A document class 54 / 70

I A custom document class

1 <?php2 c l a s s myUserDocument e x t e n d s phpi l lowDocument {3 // . . .4 p u b l i c f u n c t i o n c o n s t r u c t ( ) {5 $ t h i s−>p r o p e r t i e s = ar ray (6 ’ l o g i n ’ => new new

p h p i l l o w R e g e x p V a l i d a t o r ( ’ ( ˆ [\ x21−\x7e]+$ ) ’ ) ,

7 ’ name ’ => new p h p i l l o w S t r i n g V a l i d a t o r ( ) ,8 ’ f r i e n d s ’ => new

p h p i l l o w D o c u m e n t A r r a y V a l i d a t o r ( ’myUserDocument ’ ) ,

9 . . .10 ) ;11 p a r e n t : : c o n s t r u c t ( ) ;12 }13 // . . .14 }

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

Kore Nordmann <[email protected]>

Page 105: CouchDB, PHPillow & PHP

A document class 55 / 70

I A custom document class

1 <?php2 c l a s s myUserDocument e x t e n d s phpi l lowDocument {3 // . . .4 p r o t e c t e d f u n c t i o n g e n e r a t e I d ( ) {5 // Return n u l l f o r auto−g e n e r a t e d IDs6 r e t u r n $ t h i s−>s t r i n g T o I d ( $ t h i s−>s t o r a g e−> l o g i n

) ;7 }8 // . . .9 }

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

Kore Nordmann <[email protected]>

Page 106: CouchDB, PHPillow & PHP

A document class 56 / 70

I A custom document class

1 <?php2 c l a s s myUserDocument e x t e n d s phpi l lowDocument {3 // . . .4 p r o t e c t e d f u n c t i o n getType ( ) {5 r e t u r n ’ u s e r ’ ;6 }7 }

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

Kore Nordmann <[email protected]>

Page 107: CouchDB, PHPillow & PHP

Document examples 57 / 70

I Document creation example

1 // C r e a t e a document2 $doc = new myUserDocument ( ) ;3 $doc−> l o g i n = ’ k o r e ’ ;4 $doc−>name = ’ Kore Nordmann ’ ;5 $doc−>data = ar ray (6 ’ m a i l ’ => ” kore@php . ne t ” ,7 // . . .8 ) ;9

10 t r y {11 $ i d = $doc−>s a v e ( ) ;12 } ( p h p i l l o w R e s p o n s e C o n f l i c t E r r o r E x c e p t i o n $e ) {13 // Document a l r e a d y e x i s t s14 }15

16 // Fetch a document by ID17 $doc = new myUserDocument ( $ i d ) ;

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

Kore Nordmann <[email protected]>

Page 108: CouchDB, PHPillow & PHP

View examples 58 / 70

I View class

1 c l a s s myUserView e x t e n d s p h p i l l o w F i l e V i e w {2 p u b l i c f u n c t i o n c o n s t r u c t ( ) {3 p a r e n t : : c o n s t r u c t ( ) ;4

5 $ t h i s−>v i e w F u n c t i o n s = ar ray (6 ’ a l l ’ => ar ray (7 ’map ’ => ’map/ u s e r a l l . j s ’ ,8 ) ,9 ’ u s e r ’ => ar ray (

10 ’map ’ => ’map/ u s e r u s e r . j s ’ ,11 ’ r e d u c e ’ => ’ r e d u c e /sum . j s ’ ,12 ) ,13 ) ;14 }15

16 // . . .17 }

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

Kore Nordmann <[email protected]>

Page 109: CouchDB, PHPillow & PHP

View examples 59 / 70

I View class

1 c l a s s myUserView e x t e n d s p h p i l l o w F i l e V i e w {2 // . . .3

4 p r o t e c t e d f u n c t i o n getViewName ( ) {5 r e t u r n ’ u s e r s ’ ;6 }7 }

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

Kore Nordmann <[email protected]>

Page 110: CouchDB, PHPillow & PHP

View examples 60 / 70

I Query a view

1 $ u s e r s = myUserView : : a l l ( ar ray (2 ’ key ’ => ’ Kore Nordmann ’ ,3 ) ) ;

I PHPillow validates and converts allowed view parametersI What happens:

I View function will be uploaded to the databaseI CouchDB will index all documents in the database using the

view functionI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 111: CouchDB, PHPillow & PHP

View examples 60 / 70

I Query a view

1 $ u s e r s = myUserView : : a l l ( ar ray (2 ’ key ’ => ’ Kore Nordmann ’ ,3 ) ) ;

I PHPillow validates and converts allowed view parametersI What happens:

I View function will be uploaded to the databaseI CouchDB will index all documents in the database using the

view functionI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 112: CouchDB, PHPillow & PHP

View examples 60 / 70

I Query a view

1 $ u s e r s = myUserView : : a l l ( ar ray (2 ’ key ’ => ’ Kore Nordmann ’ ,3 ) ) ;

I PHPillow validates and converts allowed view parametersI What happens:

I View function will be uploaded to the databaseI CouchDB will index all documents in the database using the

view functionI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 113: CouchDB, PHPillow & PHP

View examples 60 / 70

I Query a view

1 $ u s e r s = myUserView : : a l l ( ar ray (2 ’ key ’ => ’ Kore Nordmann ’ ,3 ) ) ;

I PHPillow validates and converts allowed view parametersI What happens:

I View function will be uploaded to the databaseI CouchDB will index all documents in the database using the

view functionI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 114: CouchDB, PHPillow & PHP

Outline 61 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 115: CouchDB, PHPillow & PHP

Attachments 62 / 70

I CouchDB allows you to attach files to documents

I Files are replicatedI You can serve full Web-Applications from a CouchDB

I See CouchApp

I Deploy using PUSH-replication

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

Kore Nordmann <[email protected]>

Page 116: CouchDB, PHPillow & PHP

Attachments 62 / 70

I CouchDB allows you to attach files to documents

I Files are replicatedI You can serve full Web-Applications from a CouchDB

I See CouchApp

I Deploy using PUSH-replication

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

Kore Nordmann <[email protected]>

Page 117: CouchDB, PHPillow & PHP

Attachments 62 / 70

I CouchDB allows you to attach files to documents

I Files are replicatedI You can serve full Web-Applications from a CouchDB

I See CouchApp

I Deploy using PUSH-replication

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

Kore Nordmann <[email protected]>

Page 118: CouchDB, PHPillow & PHP

Eventual consistency 63 / 70

I Mirror database into userspace

I Offline usage and synchronization of Browser applications

I Mozilla develops a JavaScript implementation of theCouchDB API [Moz09]

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

Kore Nordmann <[email protected]>

Page 119: CouchDB, PHPillow & PHP

Eventual consistency 63 / 70

I Mirror database into userspace

I Offline usage and synchronization of Browser applications

I Mozilla develops a JavaScript implementation of theCouchDB API [Moz09]

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

Kore Nordmann <[email protected]>

Page 120: CouchDB, PHPillow & PHP

Eventual consistency 63 / 70

I Mirror database into userspace

I Offline usage and synchronization of Browser applications

I Mozilla develops a JavaScript implementation of theCouchDB API [Moz09]

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

Kore Nordmann <[email protected]>

Page 121: CouchDB, PHPillow & PHP

Ubuntu One 64 / 70

I Ubuntu One uses CouchDB

I Synchronize contacts & date between nodes, or to a server

I Yes, all Ubuntu Karmic users already have a CouchDB running

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

Kore Nordmann <[email protected]>

Page 122: CouchDB, PHPillow & PHP

Ubuntu One 64 / 70

I Ubuntu One uses CouchDB

I Synchronize contacts & date between nodes, or to a server

I Yes, all Ubuntu Karmic users already have a CouchDB running

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

Kore Nordmann <[email protected]>

Page 123: CouchDB, PHPillow & PHP

Various applications 65 / 70

I Arbit uses CouchDB for issue tracking, wiki, FAQ and more

I Other applications: http://wiki.apache.org/couchdb/CouchDB_in_the_wild

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

Kore Nordmann <[email protected]>

Page 124: CouchDB, PHPillow & PHP

Summary 66 / 70

I CouchDB is fast (enough)

I Document oriented approach allows new applicationdevelopment approaches

I CouchDB scales really well, horizontaly and verticalyI CouchDB fits web applications really well

I RDBMS are still better for single-cluster scalable applicationswith strong integrity requirements.

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

Kore Nordmann <[email protected]>

Page 125: CouchDB, PHPillow & PHP

Outline 67 / 70

Introduction

General

Structure

API

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 126: CouchDB, PHPillow & PHP

Resources 68 / 70

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

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

I PHPillow: http://arbitracker.org/phpillow.html

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

Kore Nordmann <[email protected]>

Page 127: CouchDB, PHPillow & PHP

The end 69 / 70

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 Comment: http://joind.in/1465

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

Kore Nordmann <[email protected]>

Page 128: CouchDB, PHPillow & PHP

Bibliography I 70 / 70

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

[Moz09] Mozilla, Browsercouch documentation, November 2009.

[Vog09] Werner Vogels, Eventually consistent - revisited,http://www.allthingsdistributed.com/2008/12/eventually_

consistent.html, December 2009.

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

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

Kore Nordmann <[email protected]>