CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann Migration / Refactoring 16 / 61 I Yeah,...

119
CouchDB, PHP & PHPillow http://joind.in/1445 Kore Nordmann <[email protected]> @koredn May 15, 2010 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>

Transcript of CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann Migration / Refactoring 16 / 61 I Yeah,...

Page 1: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

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

Kore Nordmann<[email protected]>

@koredn

May 15, 2010

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

Kore Nordmann <[email protected]>

Page 2: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

About me 2 / 61

I Kore Nordmann, <[email protected]>

I Long time PHP developer

I Studies computer science in Dortmund, currently writingthesis

I Currently founding Qafoo ([email protected] / @qafoo)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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 3 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 4: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB is paradigmn shift 4 / 61

I Structure

I Consistency

I APII Applications

I I will show this by providing some views for a “blog”

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

Kore Nordmann <[email protected]>

Page 5: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB is paradigmn shift 4 / 61

I Structure

I Consistency

I APII Applications

I I will show this by providing some views for a “blog”

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

Kore Nordmann <[email protected]>

Page 6: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB is paradigmn shift 4 / 61

I Structure

I Consistency

I APII Applications

I I will show this by providing some views for a “blog”

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

Kore Nordmann <[email protected]>

Page 7: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB is paradigmn shift 4 / 61

I Structure

I Consistency

I APII Applications

I I will show this by providing some views for a “blog”

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

Kore Nordmann <[email protected]>

Page 8: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB is paradigmn shift 4 / 61

I Structure

I Consistency

I APII Applications

I I will show this by providing some views for a “blog”

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

Kore Nordmann <[email protected]>

Page 9: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB is paradigmn shift 4 / 61

I Structure

I Consistency

I APII Applications

I I will show this by providing some views for a “blog”

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

Kore Nordmann <[email protected]>

Page 10: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 5 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 11: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB 6 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB 6 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

CouchDB 6 / 61

I Apache top-level project

I Build in Erlang / on Erlang/OTP

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

Kore Nordmann <[email protected]>

Page 14: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

HTTP API 7 / 61

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 15: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

HTTP API 7 / 61

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 16: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

HTTP API 7 / 61

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 17: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 8 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 18: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 9 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 9 / 61

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 20: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 9 / 61

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 21: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 9 / 61

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 22: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 9 / 61

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 23: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 10 / 61

I We need to create a database first:

1 <?php2

3 $ f p = fopen (4 ’ h t t p : / / l o c a l h o s t :5984/ p h p d a y b l o g ’ , ’ r ’ , f a l s e ,5 s t r e a m c o n t e x t c r e a t e ( ar ray (6 ’ h t t p ’ => ar ray (7 ’ method ’ => ’PUT ’ ,8 ’ c o n t e n t ’ => n u l l ,9 ’ i g n o r e e r r o r s ’ => true ,

10 ’ h e a d e r ’ => ’ Content−t y p e :a p p l i c a t i o n / j s o n ’ ,

11 ) ) ) ) ;12

13 var dump ( j s o n d e c o d e ( s t r e a m g e t c o n t e n t s ( $ fp ) , t rue) ) ;

14 /∗ a r r a y ( 1 ) {15 [ ” ok ” ] => b o o l ( t r u e )16 } ∗/

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

Kore Nordmann <[email protected]>

Page 24: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 11 / 61

I Or do the same with curl:

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 d a y b l o g2

3 HTTP/ 1 . 1 412 P r e c o n d i t i o n F a i l e d4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )5 Date : Sun , 09 May 2010 1 0 : 1 1 : 0 9 GMT6 Content−Type : t e x t / p l a i n ; c h a r s e t=u t f−87 Content−Length : 958 Cache−C o n t r o l : must−r e v a l i d a t e9

10 {” e r r o r ” : ” f i l e e x i s t s ” ,” r e a s o n ” : ” The d a t a b a s e c o u l d notbe c r e a t e d , t h e f i l e a l r e a d y e x i s t s . ”}

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

Kore Nordmann <[email protected]>

Page 25: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 12 / 61

I Store a document

1 <?php23 $fp = fopen (4 ’ h t tp : // l o c a l h o s t :5984/ phpday b log / h e l l o w o r l d ’ , ’ r ’ , f a l s e ,5 s t r e am c o n t e x t c r e a t e ( a r r a y (6 ’ h t tp ’ => a r r a y (7 ’ method ’ => ’PUT ’ ,8 ’ con t en t ’ => j s o n en cod e ( a r r a y (9 ’ t ype ’ => ’ b l o g e n t r y ’ ,

10 ’ t i t l e ’ => ’ H e l l o wor ld ! ’ ,11 ’ t e x t ’ => ’ Some t e x t . . . ’ ,12 ’ au tho r ’ => ’ ko re ’ ,13 ) ) ,14 ’ i g n o r e e r r o r s ’ => t rue ,15 ’ heade r ’ => ’ Content−t ype : a p p l i c a t i o n / j s o n ’ ,16 ) ) ) ) ;1718 var dump ( j s o n d e cod e ( s t r e am g e t c o n t e n t s ( $ fp ) , t rue ) ) ;

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

Kore Nordmann <[email protected]>

Page 26: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 13 / 61

I Store a document

1 <?php2

3 /∗ a r r a y ( 6 ) {4 [ ” i d ” ] => s t r i n g ( 1 1 ) ” h e l l o w o r l d ”5 [ ” r e v ” ] => s t r i n g ( 3 4 ) ”1−0

a73fbe05e737f0519ab2792d71d4911 ”6 [ ” t y p e ” ] => s t r i n g ( 1 0 ) ” b l o g e n t r y ”7 [ ” t i t l e ” ] => s t r i n g ( 1 2 ) ” H e l l o w o r l d ! ”8 [ ” t e x t ” ] => s t r i n g ( 1 2 ) ”Some . . . t e x t ”9 [ ” a u t h o r ” ] => s t r i n g ( 4 ) ” k o r e ”

10 } ∗/

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

Kore Nordmann <[email protected]>

Page 27: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 14 / 61

I Custom deteministic IDs can ensure uniqueness of documentsI Just define the ID in the URL

I CouchDB can also generate IDs for youI Which then are just unique hashes

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

Kore Nordmann <[email protected]>

Page 28: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 14 / 61

I Custom deteministic IDs can ensure uniqueness of documentsI Just define the ID in the URL

I CouchDB can also generate IDs for youI Which then are just unique hashes

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

Kore Nordmann <[email protected]>

Page 29: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Structure 15 / 61

I Retrieve a document

1 <?php2

3 var dump ( j s o n d e c o d e ( f i l e g e t c o n t e n t s ( ’ h t t p : / /l o c a l h o s t :5984/ p h p d a y b l o g / h e l l o w o r l d ’ ) ) ) ;

4

5 /∗ a r r a y ( 5 ) {6 [ ” i d ” ] => s t r i n g ( 1 1 ) ” h e l l o w o r l d ”7 [ ” r e v ” ] => s t r i n g ( 3 4 ) ”1−0

f563b09aa9a73b56a906919f013d492 ”8 [ ” t y p e ” ] => s t r i n g ( 1 0 ) ” b l o g e n t r y ”9 [ ” t e x t ” ] => s t r i n g ( 1 2 ) ” H e l l o w o r l d ! ”

10 [ ” a u t h o r ” ] => s t r i n g ( 4 ) ” k o r e ”11 } ∗/

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

Kore Nordmann <[email protected]>

Page 30: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Migration / Refactoring 16 / 61

I Yeah, it is that easy.

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 31: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Migration / Refactoring 16 / 61

I Yeah, it is that easy.

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 32: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Migration / Refactoring 16 / 61

I Yeah, it is that easy.

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 33: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Migration / Refactoring 16 / 61

I Yeah, it is that easy.

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 34: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Migration / Refactoring 16 / 61

I Yeah, it is that easy.

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 35: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 17 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 36: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Data access 18 / 61

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 37: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Data access 18 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Data access 18 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Data access 18 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Data access 18 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Data access 18 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View 19 / 61

I Index all blog 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 43: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View 20 / 61

I Index all blog documents by their title

1 f u n c t i o n ( doc )2 {3 i f ( doc . t y p e == ” b l o g p o s 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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View 21 / 61

I Upload the view

1 <?php2

3 $ f p = fopen (4 ’ h t t p : / / l o c a l h o s t :5984/ p h p d a y b l o g / d e s i g n / b l o g ’ , ’

r ’ , f a l s e ,5 s t r e a m c o n t e x t c r e a t e ( ar ray (6 ’ h t t p ’ => ar ray (7 ’ method ’ => ’PUT ’ ,8 ’ c o n t e n t ’ => j s o n e n c o d e ( ar ray (9 ’ l a n g u a g e ’ => ’ j a v a s c r i p t ’ ,

10 ’ v i e w s ’ => ar ray (11 ’ l i s t ’ => ar ray (12 ’map ’ => f i l e g e t c o n t e n t s ( ’

005 map . j s ’ ) ,13 ) , ) , ) ) ,14 ’ i g n o r e e r r o r s ’ => true ,15 ’ h e a d e r ’ => ’ Content−t y p e :

a p p l i c a t i o n / j s o n ’ ,16 ) ) ) ) ;

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

Kore Nordmann <[email protected]>

Page 45: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View 22 / 61

I Fetching data

1 <?php2

3 var dump ( j s o n d e c o d e ( f i l e g e t c o n t e n t s (4 ’ h t t p : / / l o c a l h o s t :5984/ p h p d a y b l o g / d e s i g n / b l o g /

v i e w / l i s t ’5 ) , t rue ) ) ;6

7 /∗ a r r a y ( 3 ) {8 [ ” t o t a l r o w s ” ] => i n t ( 1 )9 [ ” o f f s e t ” ] => i n t ( 0 )

10 [ ” rows ” ] => a r r a y ( 1 ) {11 [ 0 ] => a r r a y ( 3 ) {12 [ ” i d ” ] => s t r i n g ( 1 1 ) ” h e l l o w o r l d ”13 [ ” key ” ] => s t r i n g ( 1 2 ) ” H e l l o w o r l d ! ”14 [ ” v a l u e ” ] => s t r i n g ( 1 1 ) ” h e l l o w o r l d ”15 }16 }17 } ∗/

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

Kore Nordmann <[email protected]>

Page 46: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Views 23 / 61

I Comparable to SELECT * FROM table WHERE column ="value"

I Allows range requestsI Allows limit / offsetI Allows key accross multiple fieldsI Even allow multiple indexes per document (tags)I Allow complex keys [category, title]

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

Kore Nordmann <[email protected]>

Page 47: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Views 23 / 61

I Comparable to SELECT * FROM table WHERE column ="value"

I Allows range requestsI Allows limit / offsetI Allows key accross multiple fieldsI Even allow multiple indexes per document (tags)I Allow complex keys [category, title]

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

Kore Nordmann <[email protected]>

Page 48: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Views 23 / 61

I Comparable to SELECT * FROM table WHERE column ="value"

I Allows range requestsI Allows limit / offsetI Allows key accross multiple fieldsI Even allow multiple indexes per document (tags)I Allow complex keys [category, title]

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

Kore Nordmann <[email protected]>

Page 49: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Views 23 / 61

I Comparable to SELECT * FROM table WHERE column ="value"

I Allows range requestsI Allows limit / offsetI Allows key accross multiple fieldsI Even allow multiple indexes per document (tags)I Allow complex keys [category, title]

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

Kore Nordmann <[email protected]>

Page 50: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Views 23 / 61

I Comparable to SELECT * FROM table WHERE column ="value"

I Allows range requestsI Allows limit / offsetI Allows key accross multiple fieldsI Even allow multiple indexes per document (tags)I Allow complex keys [category, title]

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

Kore Nordmann <[email protected]>

Page 51: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Views 23 / 61

I Comparable to SELECT * FROM table WHERE column ="value"

I Allows range requestsI Allows limit / offsetI Allows key accross multiple fieldsI Even allow multiple indexes per document (tags)I Allow complex keys [category, title]

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

Kore Nordmann <[email protected]>

Page 52: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-reduce-views 24 / 61

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 53: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-reduce-views 24 / 61

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 54: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-reduce-views 24 / 61

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 55: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 25 / 61

Documents

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

Kore Nordmann <[email protected]>

Page 56: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 26 / 61

Documents Map

$key:$value

$key:$value

$key:$value

$key:$value

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

Kore Nordmann <[email protected]>

Page 57: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 27 / 61

ReduceDocuments Map

$key:$value

$key:$value

$key:$value

$key:$value

$key:$value

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

Kore Nordmann <[email protected]>

Page 58: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 28 / 61

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 59: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 29 / 61

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 60: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 29 / 61

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 61: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 29 / 61

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 62: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce 29 / 61

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 63: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 30 / 61

I The map function

1 f u n c t i o n ( doc )2 {3 i f ( doc . t y p e == ” b l o g p o s t ” )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 64: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 31 / 61

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 65: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 32 / 61

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 66: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 33 / 61

I The reduce result

1 nu l l => 42

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

Kore Nordmann <[email protected]>

Page 67: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 34 / 61

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 68: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 35 / 61

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 69: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Map-Reduce example 36 / 61

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 70: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 37 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 71: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Local conflicts 38 / 61

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 72: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Local conflicts 38 / 61

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 73: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Local conflicts 38 / 61

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 74: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Local conflicts 38 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Local conflicts 38 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Inter document links 39 / 61

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 ” : ” b l o g p o s t ” ,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 77: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Inter document links 39 / 61

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 ” : ” b l o g p o s t ” ,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 78: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Inter document links 39 / 61

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 ” : ” b l o g p o s t ” ,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 79: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Inter document links 39 / 61

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 ” : ” b l o g p o s t ” ,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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Inter document links 39 / 61

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 ” : ” b l o g p o s t ” ,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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Inter document links 39 / 61

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 ” : ” b l o g p o s t ” ,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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

JOIN query 40 / 61

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

8 i f ( doc . t y p e == ” blog comment ” )9 {

10 emit ( [ doc . b l o g p o s t , doc . i d ] , doc . i d ) ;11 }12 }

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

Kore Nordmann <[email protected]>

Page 83: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Issue tracker 41 / 61

I JOIN query result

1 [ ” b l o g p o s t −1” , 0 ] => ” b l o g p o s t −1”2 [ ” b l o g p o s t −1” , ” eaaa503adbec07 ” ] => ” eaaa503adbec07 ”3 [ ” b l o g p o s t −1” , ” e77ebd5ea0383c ” ] => ” e77ebd5ea0383c ”4 [ ” b l o g p o s t −1” , ” 0502 afb84a80a4 ” ] => ” 0502 afb84a80a4 ”5 [ ” b l o g p o s t −10” , 0 ] => ” b l o g p o s t −10”6 [ ” b l o g p o s t −10” , ” 3872 cd8a4d530f ” ] => ” 3872 cd8a4d530f ”7 [ ” b l o g p o s t −10” , ” a f 0 d d 3 c 4 d 1 8 4 c f ” ] => ” a f 0 d d 3 c 4 d 1 8 4 c f ”

I Can again be filtered...

I Using “?include docs=true” also provides you alldocuments

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

Kore Nordmann <[email protected]>

Page 84: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Scaling: The CAP theorem 42 / 61

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 85: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Eventual consistency 43 / 61

Europe

Asia Amerika

Client /Web-Application

I Important for seperated nodes (sharding)

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

Kore Nordmann <[email protected]>

Page 86: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 44 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 87: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 45 / 61

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 88: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 45 / 61

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 89: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 45 / 61

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 90: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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 91: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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 92: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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 93: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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 94: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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 95: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

PHPillow 46 / 61

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, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View example 47 / 61

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 function, if the view is newI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 98: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View example 47 / 61

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 function, if the view is newI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 99: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View example 47 / 61

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 function, if the view is newI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 100: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

View example 47 / 61

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 function, if the view is newI View results will be returned as an array

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

Kore Nordmann <[email protected]>

Page 101: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 48 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 102: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Attachments 49 / 61

I CouchDB allows you to attach files to documentsI Files are replicated

I Even incrementally since 0.11

I You can serve full Web-Applications from a CouchDBI See CouchApp

I Deploy using PUSH-replication

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

Kore Nordmann <[email protected]>

Page 103: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Attachments 49 / 61

I CouchDB allows you to attach files to documentsI Files are replicated

I Even incrementally since 0.11

I You can serve full Web-Applications from a CouchDBI See CouchApp

I Deploy using PUSH-replication

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

Kore Nordmann <[email protected]>

Page 104: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Attachments 49 / 61

I CouchDB allows you to attach files to documentsI Files are replicated

I Even incrementally since 0.11

I You can serve full Web-Applications from a CouchDBI See CouchApp

I Deploy using PUSH-replication

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

Kore Nordmann <[email protected]>

Page 105: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Eventual consistency 50 / 61

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 106: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Eventual consistency 50 / 61

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 107: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Eventual consistency 50 / 61

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 108: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Ubuntu One 51 / 61

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 109: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Ubuntu One 51 / 61

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 110: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Various applications 52 / 61

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 111: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Summary 53 / 61

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 112: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Outline 54 / 61

Introduction

General

Structure

Views

Consistency

PHPillow

Applications

QA

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

Kore Nordmann <[email protected]>

Page 113: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Resources 55 / 61

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 114: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

The end 56 / 61

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/1445

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

Kore Nordmann <[email protected]>

Page 115: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Full-Text-Search 57 / 61

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 116: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Wiki 58 / 61

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 117: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Full-Text-Search 59 / 61

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 118: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Wiki 60 / 61

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 119: CouchDB, PHP & PHPillow - - Kore Nordmann · Kore Nordmann  Migration / Refactoring 16 / 61 I Yeah, it is that easy. I Change document structure at any time I

Bibliography I 61 / 61

[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]>