SlideShare a Scribd company logo
CouchDB, PHP & PHPillow
     http://joind.in/1465


      Kore Nordmann
      <kore@php.net>
         @koredn


      February 26, 2010




                            http://kore-nordmann.de/portfolio.html
                                      Kore Nordmann <kore@php.net>
2 / 70
About me


     Kore Nordmann, <kore@php.net>
     Long time PHP developer
     Regular speaker, author, etc.
     Studies computer science in Dortmund, currently writing
     thesis
     Active open source developer:
           eZ Components (Graph, WebDav, Document), Arbit,
           PHPUnit, Torii, PHPillow, KaForkL, Image 3D, WCV, ...




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
3 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
4 / 70
CouchDB is paradigmn shift




      Structure
      Consistency
      API
      Applications




                             http://kore-nordmann.de/portfolio.html
                                       Kore Nordmann <kore@php.net>
4 / 70
CouchDB is paradigmn shift




      Structure
      Consistency
      API
      Applications




                             http://kore-nordmann.de/portfolio.html
                                       Kore Nordmann <kore@php.net>
4 / 70
CouchDB is paradigmn shift




      Structure
      Consistency
      API
      Applications




                             http://kore-nordmann.de/portfolio.html
                                       Kore Nordmann <kore@php.net>
4 / 70
CouchDB is paradigmn shift




      Structure
      Consistency
      API
      Applications




                             http://kore-nordmann.de/portfolio.html
                                       Kore Nordmann <kore@php.net>
4 / 70
CouchDB is paradigmn shift




      Structure
      Consistency
      API
      Applications




                             http://kore-nordmann.de/portfolio.html
                                       Kore Nordmann <kore@php.net>
5 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
6 / 70
CouchDB




     Apache top-level project
     Build in Erlang / on Erlang/OTP


                                       http://kore-nordmann.de/portfolio.html
                                                 Kore Nordmann <kore@php.net>
6 / 70
CouchDB




     Apache top-level project
     Build in Erlang / on Erlang/OTP


                                       http://kore-nordmann.de/portfolio.html
                                                 Kore Nordmann <kore@php.net>
6 / 70
CouchDB




     Apache top-level project
     Build in Erlang / on Erlang/OTP


                                       http://kore-nordmann.de/portfolio.html
                                                 Kore Nordmann <kore@php.net>
7 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
8 / 70
Structure




      Document based database
      No pre-defined structure (tables)
      Put in any JSON object you want
            Even deep structures (arrays of objects)
            You may attach any number of files to documents




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
8 / 70
Structure




      Document based database
      No pre-defined structure (tables)
      Put in any JSON object you want
            Even deep structures (arrays of objects)
            You may attach any number of files to documents




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
8 / 70
Structure




      Document based database
      No pre-defined structure (tables)
      Put in any JSON object you want
            Even deep structures (arrays of objects)
            You may attach any number of files to documents




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
8 / 70
Structure




      Document based database
      No pre-defined structure (tables)
      Put in any JSON object you want
            Even deep structures (arrays of objects)
            You may attach any number of files to documents




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
8 / 70
Structure




      Document based database
      No pre-defined structure (tables)
      Put in any JSON object you want
            Even deep structures (arrays of objects)
            You may attach any number of files to documents




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
9 / 70
Structure

          Example wiki document

1    { ” t i t l e ”:           ”PHPUK 2 0 1 0 ” ,
2      ” text ”:                ” Welcome t o t h e PHPUK . . . ” ,
3      ” creator ”:             ” u s e r −b a r ” ,
4      ” edited ”:              2935678239 ,
5      ” r e v i s i o n s ”: [
6          { ” t i t l e ”:        ”PHPUK 2 0 0 9 ” ,
7              ” text ”:           ” Welcome t o t h e PHPUK . . . ” ,
8              ” c r e a t o r ” : ” u s 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 <kore@php.net>
10 / 70
Attachements
           Example wiki document

1    { ” t i t l e ”:        ”PHPUK 2 0 1 0 ” ,
2      ” creator ”:          ” u s e r −b a r ” ,
3      ” text ”:             ”<h1>Welcome t o t h e PHPUK</h1>
4
5                                <img s r c =” p h p u k 2 0 1 0 / l o g o . png ” a l t =”
                                       PHPUK l o g o ”/>
6                                 ...” ,
7        ” attachments ”: {
8          ” l o g o . png ” : {
9              ” c o n t e n t t y p e ” : ” image / png ” ,
10             ” stub ”:                   true ,
11             ” length ”:                 42 ,
12         }
13       }
14   }



                                                                 http://kore-nordmann.de/portfolio.html
                                                                           Kore Nordmann <kore@php.net>
11 / 70
Migration / Refactoring



      Change document structure at any time
      No need for non-transaction-safe Data Definition Language
      (DDL)
      Fits rapid development approaches with common customer
      requested changes to the data structure
          You need to handle this in your application properly, of course:
               Incrementally update structure on modification
               Liberal validation on read




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
11 / 70
Migration / Refactoring



      Change document structure at any time
      No need for non-transaction-safe Data Definition Language
      (DDL)
      Fits rapid development approaches with common customer
      requested changes to the data structure
          You need to handle this in your application properly, of course:
               Incrementally update structure on modification
               Liberal validation on read




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
11 / 70
Migration / Refactoring



      Change document structure at any time
      No need for non-transaction-safe Data Definition Language
      (DDL)
      Fits rapid development approaches with common customer
      requested changes to the data structure
          You need to handle this in your application properly, of course:
               Incrementally update structure on modification
               Liberal validation on read




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
11 / 70
Migration / Refactoring



      Change document structure at any time
      No need for non-transaction-safe Data Definition Language
      (DDL)
      Fits rapid development approaches with common customer
      requested changes to the data structure
          You need to handle this in your application properly, of course:
               Incrementally update structure on modification
               Liberal validation on read




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
12 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
13 / 70
HTTP API



    RESTful HTTP access
    HTTP is available on “all” platforms natively
        No PHP extension required
        Just use PHPs HTTP stream wrapper, pecl/http or curl
    You can use all your known HTTP middleware
        Reverse proxies for scaling reads (Varnish, Squid)
        Simple custom proxy configuration for direct “AJAX” access




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
13 / 70
HTTP API



    RESTful HTTP access
    HTTP is available on “all” platforms natively
        No PHP extension required
        Just use PHPs HTTP stream wrapper, pecl/http or curl
    You can use all your known HTTP middleware
        Reverse proxies for scaling reads (Varnish, Squid)
        Simple custom proxy configuration for direct “AJAX” access




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
13 / 70
HTTP API



    RESTful HTTP access
    HTTP is available on “all” platforms natively
        No PHP extension required
        Just use PHPs HTTP stream wrapper, pecl/http or curl
    You can use all your known HTTP middleware
        Reverse proxies for scaling reads (Varnish, Squid)
        Simple custom proxy configuration for direct “AJAX” access




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
14 / 70
HTTP API




    GET / POST / PUT / DELETE
    <METHOD> http://<host>/<database>/<document>




                                    http://kore-nordmann.de/portfolio.html
                                              Kore Nordmann <kore@php.net>
15 / 70
Create a database


 1   $ c u r l − i −X PUT h t t p : / / l o c a l h o s t : 5 9 8 4 / p h p u k w i k i
 2
 3   HTTP/ 1 . 1 201 C r e a t e d
 4   S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )
 5   Location : http :// l o c a l h o s t :5984/ phpuk wiki
 6   Date : F r i , 13 Jan 2009 1 4 : 0 7 : 5 7 GMT
 7   Content −Type : t e x t / p l a i n ; c h a r s e t=u t f −8
 8   Content −L e n g t h : 12
 9   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 <kore@php.net>
16 / 70
Create a wiki document

1    $ c u r l − i −X PUT h t t p : / / l o c a l h o s t : 5 9 8 4 / p h p u k w i k i / S t a r t
           −−d a t a ’ { ” 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 d
4    S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )
5    Location : http :// l o c a l h o s t :5984/ phpuk wiki / Start
6    Etag : ”1−6 b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ”
7    Date : F r i , 13 Jan 2009 1 4 : 1 4 : 5 5 GMT
8    Content −Type : t e x t / p l a i n ; c h a r s e t=u t f −8
9    Content −L e n g t h : 68
10   Cache−C o n t r o l : must−r e v a l i d a t e
11
12   {” ok ” : t r u e , ” i d ” : ” S t a r t ” , ” r e v ”:”1 −6
          b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ”}




                                                                             http://kore-nordmann.de/portfolio.html
                                                                                       Kore Nordmann <kore@php.net>
17 / 70
Get a wiki document back


 1   $ c u r l − i −X GET h t t p : / / l o c a l h o s t : 5 9 8 4 / p h p u k w i k i / S t a r t
 2
 3   HTTP/ 1 . 1 200 OK
 4   S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B )
 5   Etag : ”1−6 b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ”
 6   Date : F r i , 13 Jan 2009 1 4 : 1 5 : 4 8 GMT
 7   Content −Type : t e x t / p l a i n ; c h a r s e t=u t f −8
 8   Content −L e n g t h : 97
 9   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 −6
          b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ” , ” name ” : ” S t a r t ” , ”
          t e x t ” : ” H e l l o World ! ” }




                                                                           http://kore-nordmann.de/portfolio.html
                                                                                     Kore Nordmann <kore@php.net>
18 / 70
Security




      Simple database based access restrictions
      Using HTTP plain auth
      More fine grained access control will be in next release
           Define functions which decide if a request from a user will be
           accepted.




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
18 / 70
Security




      Simple database based access restrictions
      Using HTTP plain auth
      More fine grained access control will be in next release
           Define functions which decide if a request from a user will be
           accepted.




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
18 / 70
Security




      Simple database based access restrictions
      Using HTTP plain auth
      More fine grained access control will be in next release
           Define functions which decide if a request from a user will be
           accepted.




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
19 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
20 / 70
Data access



      How to query such a mess?
          Views are small scripts, run for all documents in a database
          Views are built iteratively, results stored in BTrees
               Once built, they are fast
          Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...
          A view may emit any number of key-value pairs for each
          document
          Key and value may be any JSON structure




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
20 / 70
Data access



      How to query such a mess?
          Views are small scripts, run for all documents in a database
          Views are built iteratively, results stored in BTrees
               Once built, they are fast
          Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...
          A view may emit any number of key-value pairs for each
          document
          Key and value may be any JSON structure




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
20 / 70
Data access



      How to query such a mess?
          Views are small scripts, run for all documents in a database
          Views are built iteratively, results stored in BTrees
               Once built, they are fast
          Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...
          A view may emit any number of key-value pairs for each
          document
          Key and value may be any JSON structure




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
20 / 70
Data access



      How to query such a mess?
          Views are small scripts, run for all documents in a database
          Views are built iteratively, results stored in BTrees
               Once built, they are fast
          Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...
          A view may emit any number of key-value pairs for each
          document
          Key and value may be any JSON structure




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
20 / 70
Data access



      How to query such a mess?
          Views are small scripts, run for all documents in a database
          Views are built iteratively, results stored in BTrees
               Once built, they are fast
          Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...
          A view may emit any number of key-value pairs for each
          document
          Key and value may be any JSON structure




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
20 / 70
Data access



      How to query such a mess?
          Views are small scripts, run for all documents in a database
          Views are built iteratively, results stored in BTrees
               Once built, they are fast
          Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ...
          A view may emit any number of key-value pairs for each
          document
          Key and value may be any JSON structure




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
21 / 70
Wiki



          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                   e m i t ( doc . t i t l e , doc . i d ) ;
6          }
7   }




                                                                http://kore-nordmann.de/portfolio.html
                                                                          Kore Nordmann <kore@php.net>
22 / 70
Wiki



          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                   e m i t ( doc . t i t l e , doc . i d ) ;
6          }
7   }




                                                                http://kore-nordmann.de/portfolio.html
                                                                          Kore Nordmann <kore@php.net>
23 / 70
Wiki

        Index all documents by their title

1   ” BuildModuleDesign ”          =>   ” w i k i −b u i l d m o d u l e d e s i g n ”
2   ” CodingGuidelines ”           =>   ” w i k i −c o d i n g g u i d e l i n e s ”
3   ” DiscussionProtocols ”        =>   ” 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   ” Protocol 08 02 07 ”          =>   ” 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   ...

        Custom deteministic IDs can ensure uniqueness of documents
            Just set the id property on insert.
        CouchDB can also generate IDs for you




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
23 / 70
Wiki

        Index all documents by their title

1   ” BuildModuleDesign ”          =>   ” w i k i −b u i l d m o d u l e d e s i g n ”
2   ” CodingGuidelines ”           =>   ” w i k i −c o d i n g g u i d e l i n e s ”
3   ” DiscussionProtocols ”        =>   ” 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   ” Protocol 08 02 07 ”          =>   ” 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   ...

        Custom deteministic IDs can ensure uniqueness of documents
            Just set the id property on insert.
        CouchDB can also generate IDs for you




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
23 / 70
Wiki

        Index all documents by their title

1   ” BuildModuleDesign ”          =>   ” w i k i −b u i l d m o d u l e d e s i g n ”
2   ” CodingGuidelines ”           =>   ” w i k i −c o d i n g g u i d e l i n e s ”
3   ” DiscussionProtocols ”        =>   ” 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   ” Protocol 08 02 07 ”          =>   ” 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   ...

        Custom deteministic IDs can ensure uniqueness of documents
            Just set the id property on insert.
        CouchDB can also generate IDs for you




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
24 / 70
Map-reduce-views



      “MapReduce is a software framework introduced by Google to
      support distributed computing on large data sets on clusters
      of computers.” [Wik09]
      Used by CouchDB to implement views
      Just a framework / pattern: You can implement “any”
      algorithm using map-reduce.




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
24 / 70
Map-reduce-views



      “MapReduce is a software framework introduced by Google to
      support distributed computing on large data sets on clusters
      of computers.” [Wik09]
      Used by CouchDB to implement views
      Just a framework / pattern: You can implement “any”
      algorithm using map-reduce.




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
24 / 70
Map-reduce-views



      “MapReduce is a software framework introduced by Google to
      support distributed computing on large data sets on clusters
      of computers.” [Wik09]
      Used by CouchDB to implement views
      Just a framework / pattern: You can implement “any”
      algorithm using map-reduce.




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
25 / 70
Map-Reduce

    Documents




                http://kore-nordmann.de/portfolio.html
                          Kore Nordmann <kore@php.net>
26 / 70
Map-Reduce

    Documents      Map

                $key:$value

                $key:$value

                $key:$value

                $key:$value




                              http://kore-nordmann.de/portfolio.html
                                        Kore Nordmann <kore@php.net>
27 / 70
Map-Reduce

    Documents      Map                Reduce

                $key:$value

                $key:$value
                                     $key:$value
                $key:$value

                $key:$value




                              http://kore-nordmann.de/portfolio.html
                                        Kore Nordmann <kore@php.net>
28 / 70
Map-Reduce

   Documents     Map         Reduce          Re-Reduce
                                 Cluster 1
               $key:$value
                             $key:$value
               $key:$value

                                                $key:$value
                                 Cluster 2
               $key:$value
                             $key:$value
               $key:$value




                                     http://kore-nordmann.de/portfolio.html
                                               Kore Nordmann <kore@php.net>
29 / 70
Map-Reduce




     Map and reduce functions are custom
     Reduce is optional, plain view serves as a document index
     Reduce may be applied to subsets of the documents
     Reduce may be grouped




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
29 / 70
Map-Reduce




     Map and reduce functions are custom
     Reduce is optional, plain view serves as a document index
     Reduce may be applied to subsets of the documents
     Reduce may be grouped




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
29 / 70
Map-Reduce




     Map and reduce functions are custom
     Reduce is optional, plain view serves as a document index
     Reduce may be applied to subsets of the documents
     Reduce may be grouped




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
29 / 70
Map-Reduce




     Map and reduce functions are custom
     Reduce is optional, plain view serves as a document index
     Reduce may be applied to subsets of the documents
     Reduce may be grouped




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
30 / 70
Map-Reduce example



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

1   f u n c t i o n ( k e y s , v a l u e s , combine )
2   {
3           r e t u r n sum ( v a l u e s ) ;
4   }




                                                          http://kore-nordmann.de/portfolio.html
                                                                    Kore Nordmann <kore@php.net>
31 / 70
Map-Reduce example




           The reduce result

1   null                       => 42




                                       http://kore-nordmann.de/portfolio.html
                                                 Kore Nordmann <kore@php.net>
32 / 70
More...




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




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
33 / 70
Map-Reduce example
           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 t e = new Date ( ) ;
 6                   d a t e . s e t T i m e ( doc . e d i t e d ∗ 1000 ) ;
 7                   emit ( [
 8                                   date . getUTCFullYear ( ) ,
 9                                   d a t e . getUTCMonth ( ) + 1 ,
10                                   d a t e . getUTCDate ( ) ,
11                                   d a t e . getUTCHours ( ) ,
12                                   d a t e . getUTCMinutes ( ) ,
13                                   d a t e . getUTCSeconds ( ) ,
14                           ], 1 );
15                   // You c o u l d a l s o e m i t t h e w h o l e doc a s v a l u e
16          }
17   }

                                                              http://kore-nordmann.de/portfolio.html
                                                                        Kore Nordmann <kore@php.net>
34 / 70
Map-Reduce example

         The mapping result

 1   [2008 ,   10 ,   11 ,   9,     11 ,   12]   =>   1
 2   [2008 ,   10 ,   11 ,   9,     11 ,   12]   =>   1
 3   [2008 ,   10 ,   11 ,   9,     11 ,   12]   =>   1
 4   [2008 ,   10 ,   11 ,   9,     13 ,   8]    =>   1
 5   [2008 ,   10 ,   11 ,   9,     13 ,   44]   =>   1
 6   [2008 ,   10 ,   11 ,   9,     14 ,   2]    =>   1
 7   [2008 ,   10 ,   12 ,   17 ,   46 ,   15]   =>   1
 8   [2008 ,   10 ,   12 ,   17 ,   57 ,   52]   =>   1
 9   [2008 ,   10 ,   12 ,   18 ,   0,     45]   =>   1
10   [2008 ,   10 ,   14 ,   8,     36 ,   29]   =>   1
11   [2008 ,   10 ,   14 ,   19 ,   33 ,   21]   =>   1
12   [2008 ,   10 ,   14 ,   19 ,   33 ,   35]   =>   1




                                                          http://kore-nordmann.de/portfolio.html
                                                                    Kore Nordmann <kore@php.net>
35 / 70
Map-Reduce example




          The reduce function

1   f u n c t i o n ( k e y s , v a l u e s , combine )
2   {
3           r e t u r n sum ( v a l u e s ) ;
4   }




                                                          http://kore-nordmann.de/portfolio.html
                                                                    Kore Nordmann <kore@php.net>
36 / 70
Map-Reduce example




           The reduce result

1   null                       => 12




                                       http://kore-nordmann.de/portfolio.html
                                                 Kore Nordmann <kore@php.net>
37 / 70
Map-Reduce example


         The grouped reduce result

 1   [2008 ,   10 ,   11 ,   9,     11 ,   12]   =>   3
 2   [2008 ,   10 ,   11 ,   9,     13 ,   8]    =>   1
 3   [2008 ,   10 ,   11 ,   9,     13 ,   44]   =>   1
 4   [2008 ,   10 ,   11 ,   9,     14 ,   2]    =>   1
 5   [2008 ,   10 ,   12 ,   17 ,   46 ,   15]   =>   1
 6   [2008 ,   10 ,   12 ,   17 ,   57 ,   52]   =>   1
 7   [2008 ,   10 ,   12 ,   18 ,   0,     45]   =>   1
 8   [2008 ,   10 ,   14 ,   8,     36 ,   29]   =>   1
 9   [2008 ,   10 ,   14 ,   19 ,   33 ,   21]   =>   1
10   [2008 ,   10 ,   14 ,   19 ,   33 ,   35]   =>   1




                                                          http://kore-nordmann.de/portfolio.html
                                                                    Kore Nordmann <kore@php.net>
38 / 70
Map-Reduce example



        The filtered grouped reduce result
        startkey=[2008,10,11] and endkey=[2008,10,12]

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




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
39 / 70
Map-Reduce example




        The grouped reduce result, with group level
        group-level=3

1   [2008 , 10 , 11]               => 6
2   [2008 , 10 , 12]               => 3
3   [2008 , 10 , 14]               => 3




                                               http://kore-nordmann.de/portfolio.html
                                                         Kore Nordmann <kore@php.net>
40 / 70
Full-Text-Search
            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 i m p l e word i n d e x i n g , d o e s n o t 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           // s t o p w o r d s , 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                 value = {};
10                v a l u e [ doc . i d ] = 1 ;
11               e m i t ( words [ i ] . t o L o w e r C a s e ( ) , v a l u e ) ;
12          }
13       }
14   }

                                                                        http://kore-nordmann.de/portfolio.html
                                                                                  Kore Nordmann <kore@php.net>
41 / 70
Wiki
          Index all documents by all their words

 1   ...
 2   ”a”         =>   { wiki   −8: 1}
 3   ”a”         =>   { wiki   −8: 1}
 4   ”a”         =>   { wiki   −8: 1}
 5   ”a”         =>   { wiki   −8: 1}
 6   ”a”         =>   { wiki   −81: 1}
 7   ”a”         =>   { wiki   −83: 1}
 8   ”a”         =>   { wiki   −83: 1}
 9   ” able ”    =>   { wiki   −39: 1}
10   ” able ”    =>   { wiki   −56: 1}
11   ” able ”    =>   { wiki   −73: 1}
12   ” able ”    =>   { wiki   −80: 1}
13   ” about ”   =>   { wiki   −24: 1}
14   ” about ”   =>   { wiki   −43: 1}
15   ” about ”   =>   { wiki   −85: 1}
16   ...


                                                   http://kore-nordmann.de/portfolio.html
                                                             Kore Nordmann <kore@php.net>
42 / 70
Full-Text-Search

           Reduce by word count

1    f u n c t i o n ( keys , v a l u e s ) {
2        var count = {};
3        for ( var i in values ) {
4            for ( var id in values [ i ] ) {
5                 i f ( count [ i d ] ) {
6                    count [ i d ] = v a l u e s [ i ] [ i d ] + count [ i d ] ;
7                } else {
8                    count [ i d ] = v a l u e s [ i ] [ i d ] ;
9                }
10          }
11       }
12       return count ;
13   }




                                                                  http://kore-nordmann.de/portfolio.html
                                                                            Kore Nordmann <kore@php.net>
43 / 70
Wiki
           Index all documents by all their words

 1   ...
 2   ”a”        => {
 3                     wiki   −68:   6,
 4                     wiki   −66:   6,
 5                     wiki   −22:   4,
 6                     wiki   −63:   3,
 7                     wiki   −60:   2,
 8                     wiki   −35:   2,
 9                     wiki   −34:   1,
10                     wiki   −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   ” a b o u t ” => { 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 <kore@php.net>
44 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
45 / 70
Local conflicts




      Multi-Version Concurrency Control
      All documents in the database are versioned
          Don’t use it for application level document versioning
      Updates and deletes need to specify the revision ID
      Changing outdated documents result in conflicts




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
45 / 70
Local conflicts




      Multi-Version Concurrency Control
      All documents in the database are versioned
          Don’t use it for application level document versioning
      Updates and deletes need to specify the revision ID
      Changing outdated documents result in conflicts




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
45 / 70
Local conflicts




      Multi-Version Concurrency Control
      All documents in the database are versioned
          Don’t use it for application level document versioning
      Updates and deletes need to specify the revision ID
      Changing outdated documents result in conflicts




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
45 / 70
Local conflicts




      Multi-Version Concurrency Control
      All documents in the database are versioned
          Don’t use it for application level document versioning
      Updates and deletes need to specify the revision ID
      Changing outdated documents result in conflicts




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
45 / 70
Local conflicts




      Multi-Version Concurrency Control
      All documents in the database are versioned
          Don’t use it for application level document versioning
      Updates and deletes need to specify the revision ID
      Changing outdated documents result in conflicts




                                                 http://kore-nordmann.de/portfolio.html
                                                           Kore Nordmann <kore@php.net>
46 / 70
Inter document links

        There is no ensured inter document consistency in CouchDB
        Different possibilities of relating documents:
              List IDs of related documents in document (n:m)
              ... both directions are feasible
              Embed the whole related document (1:n)
        Solution depends on update-ratio
    1   { ” type ”: ” wiki ” ,
    2     ” t i t l e ”: ” Hello world ” ,
    3     ” text ”:         ”...” ,
    4     ” comments ” : [
    5        { ”comment ” : ” . . . ” } ,
    6     ],
    7     ” c r e a t o r ” : ” u s e r −f o o ” ,
    8   }




                                                     http://kore-nordmann.de/portfolio.html
                                                               Kore Nordmann <kore@php.net>
46 / 70
Inter document links

        There is no ensured inter document consistency in CouchDB
        Different possibilities of relating documents:
              List IDs of related documents in document (n:m)
              ... both directions are feasible
              Embed the whole related document (1:n)
        Solution depends on update-ratio
    1   { ” type ”: ” wiki ” ,
    2     ” t i t l e ”: ” Hello world ” ,
    3     ” text ”:         ”...” ,
    4     ” comments ” : [
    5        { ”comment ” : ” . . . ” } ,
    6     ],
    7     ” c r e a t o r ” : ” u s e r −f o o ” ,
    8   }




                                                     http://kore-nordmann.de/portfolio.html
                                                               Kore Nordmann <kore@php.net>
46 / 70
Inter document links

        There is no ensured inter document consistency in CouchDB
        Different possibilities of relating documents:
              List IDs of related documents in document (n:m)
              ... both directions are feasible
              Embed the whole related document (1:n)
        Solution depends on update-ratio
    1   { ” type ”: ” wiki ” ,
    2     ” t i t l e ”: ” Hello world ” ,
    3     ” text ”:         ”...” ,
    4     ” comments ” : [
    5        { ”comment ” : ” . . . ” } ,
    6     ],
    7     ” c r e a t o r ” : ” u s e r −f o o ” ,
    8   }




                                                     http://kore-nordmann.de/portfolio.html
                                                               Kore Nordmann <kore@php.net>
46 / 70
Inter document links

        There is no ensured inter document consistency in CouchDB
        Different possibilities of relating documents:
              List IDs of related documents in document (n:m)
              ... both directions are feasible
              Embed the whole related document (1:n)
        Solution depends on update-ratio
    1   { ” type ”: ” wiki ” ,
    2     ” t i t l e ”: ” Hello world ” ,
    3     ” text ”:         ”...” ,
    4     ” comments ” : [
    5        { ”comment ” : ” . . . ” } ,
    6     ],
    7     ” c r e a t o r ” : ” u s e r −f o o ” ,
    8   }




                                                     http://kore-nordmann.de/portfolio.html
                                                               Kore Nordmann <kore@php.net>
46 / 70
Inter document links

        There is no ensured inter document consistency in CouchDB
        Different possibilities of relating documents:
              List IDs of related documents in document (n:m)
              ... both directions are feasible
              Embed the whole related document (1:n)
        Solution depends on update-ratio
    1   { ” type ”: ” wiki ” ,
    2     ” t i t l e ”: ” Hello world ” ,
    3     ” text ”:         ”...” ,
    4     ” comments ” : [
    5        { ”comment ” : ” . . . ” } ,
    6     ],
    7     ” c r e a t o r ” : ” u s e r −f o o ” ,
    8   }




                                                     http://kore-nordmann.de/portfolio.html
                                                               Kore Nordmann <kore@php.net>
46 / 70
Inter document links

        There is no ensured inter document consistency in CouchDB
        Different possibilities of relating documents:
              List IDs of related documents in document (n:m)
              ... both directions are feasible
              Embed the whole related document (1:n)
        Solution depends on update-ratio
    1   { ” type ”: ” wiki ” ,
    2     ” t i t l e ”: ” Hello world ” ,
    3     ” text ”:         ”...” ,
    4     ” comments ” : [
    5        { ”comment ” : ” . . . ” } ,
    6     ],
    7     ” c r e a t o r ” : ” u s e r −f o o ” ,
    8   }




                                                     http://kore-nordmann.de/portfolio.html
                                                               Kore Nordmann <kore@php.net>
47 / 70
Scaling: The CAP theorem
      The CAP theorem, read more in “CouchDB: The Definitive
      Guide” [JCA09]



                                                             CouchDB
                                              Partition
                    Availability
                                             tolerance         eventual
                                                               consistency



       PAXON                                                 RDBMS
        consensus                                              enforced
                               Consistency
        protocols                                              consistency




      CouchDB employs “Eventual Consistency” [Vog09]

                                                          http://kore-nordmann.de/portfolio.html
                                                                    Kore Nordmann <kore@php.net>
48 / 70
Eventual consistency



                                                  Europe

              Client /
           Web-Application


                                           Asia              Amerika



      Delayed, triggered synchronization (push, pull)
          Deterministic (manual) conflict resolution on replication on all
          nodes
      Scales well for seldom concurrent writes
          Structure your documents accordingly

                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
48 / 70
Eventual consistency



                                                  Europe

              Client /
           Web-Application


                                           Asia              Amerika



      Delayed, triggered synchronization (push, pull)
          Deterministic (manual) conflict resolution on replication on all
          nodes
      Scales well for seldom concurrent writes
          Structure your documents accordingly

                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
48 / 70
Eventual consistency



                                                  Europe

              Client /
           Web-Application


                                           Asia              Amerika



      Delayed, triggered synchronization (push, pull)
          Deterministic (manual) conflict resolution on replication on all
          nodes
      Scales well for seldom concurrent writes
          Structure your documents accordingly

                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
48 / 70
Eventual consistency



                                                  Europe




                                           Asia              Amerika



      Delayed, triggered synchronization (push, pull)
          Deterministic (manual) conflict resolution on replication on all
          nodes
      Scales well for seldom concurrent writes
          Structure your documents accordingly

                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
48 / 70
Eventual consistency



                                                  Europe

              Client /
           Web-Application


                                           Asia              Amerika



      Delayed, triggered synchronization (push, pull)
          Deterministic (manual) conflict resolution on replication on all
          nodes
      Scales well for seldom concurrent writes
          Structure your documents accordingly

                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
49 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
50 / 70
PHPillow



     Object-oriented client for CouchDB
     PHP >= 5.2 since last release (5.3 only before)
     >96% test coverage
     Still in alpha state
           Since CouchDB just got “beta” recently, and no new release
           was required.




                                                http://kore-nordmann.de/portfolio.html
                                                          Kore Nordmann <kore@php.net>
50 / 70
PHPillow



     Object-oriented client for CouchDB
     PHP >= 5.2 since last release (5.3 only before)
     >96% test coverage
     Still in alpha state
           Since CouchDB just got “beta” recently, and no new release
           was required.




                                                http://kore-nordmann.de/portfolio.html
                                                          Kore Nordmann <kore@php.net>
50 / 70
PHPillow



     Object-oriented client for CouchDB
     PHP >= 5.2 since last release (5.3 only before)
     >96% test coverage
     Still in alpha state
           Since CouchDB just got “beta” recently, and no new release
           was required.




                                                http://kore-nordmann.de/portfolio.html
                                                          Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
51 / 70
PHPillow


     Lightweight layer
     Features
           Simple document validation constraints
           Automatic synchronization of views
           Automatic versioning of documents
           couchdb-python compatible tool for dump and import
     Different connection handlers
           PHP HTTP stream wrapper
           Custom HTTP protocol implementation
               Which is faster, most likely because of Connection:
               Keep-Alive




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
52 / 70
A document class



          A custom document class

1   <?php
2   c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t {
3           protected $versioned = true ;
4
5         // . . .
6   }




                                                             http://kore-nordmann.de/portfolio.html
                                                                       Kore Nordmann <kore@php.net>
53 / 70
A document class


           A custom document class

 1   <?php
 2   c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t {
 3           protected $versioned = true ;
 4
 5         protected $ r e q u i r e d P r o p e r t i e s = array (
 6             ’ login ’ ,
 7         );
 8
 9         // . . .
10   }




                                                              http://kore-nordmann.de/portfolio.html
                                                                        Kore Nordmann <kore@php.net>
54 / 70
A document class
            A custom document class
 1   <?php
 2   c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t {
 3           // . . .
 4           public function                    construct () {
 5                $ t h i s −>p r o p e r t i e s = a r r a y (
 6                         ’ login ’            => 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 −x 7 e
                                   ]+$ ) ’ ) ,
 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
                                   phpillowDocumentArrayValidator ( ’
                                   myUserDocument ’ ) ,
 9                         ...
10                );
11                parent : : construct () ;
12           }
13           // . . .
14   }

                                                                         http://kore-nordmann.de/portfolio.html
                                                                                   Kore Nordmann <kore@php.net>
55 / 70
A document class


          A custom document class

1   <?php
2   c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t {
3           // . . .
4           protected function generateId () {
5                // R e t u r n n u l l f o r auto−g e n e r a t e d I D s
6                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 <kore@php.net>
56 / 70
A document class



          A custom document class

1   <?php
2   c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t {
3           // . . .
4           p r o t e c t e d f u n c t i o n getType ( ) {
5                   return ’ user ’ ;
6           }
7   }




                                                             http://kore-nordmann.de/portfolio.html
                                                                       Kore Nordmann <kore@php.net>
57 / 70
Document examples
           Document creation example
 1   // C r e a t e a document
 2   $doc = new myUserDocument ( ) ;
 3   $doc−>l o g i n = ’ k o r e ’ ;
 4   $doc−>name = ’ Kore Nordmann ’ ;
 5   $doc−>d a t a = a r r a y (
 6        ’ m a i l ’ => ” kore@php . n e t ” ,
 7       // . . .
 8   );
 9
10   try {
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 s
14   }
15
16   // F e t c h a document by ID
17   $doc = new myUserDocument ( $ i d ) ;

                                                                      http://kore-nordmann.de/portfolio.html
                                                                                Kore Nordmann <kore@php.net>
58 / 70
View examples
          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            public function        construct () {
3                parent : : construct () ;
4
5                $ t h i s −>v i e w F u n c t i o n s   = array (
6                         ’ a l l ’ => a r r a y (
7                                 ’ map ’         =>     ’ map/ u s e r a l l . j s ’ ,
8                        ),
9                         ’ u s e r ’ => a r r a y (
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 <kore@php.net>
59 / 70
View examples



         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                 return ’ users ’ ;
6         }
7   }




                                                              http://kore-nordmann.de/portfolio.html
                                                                        Kore Nordmann <kore@php.net>
60 / 70
View examples


         Query a view

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

         PHPillow validates and converts allowed view parameters
         What happens:
               View function will be uploaded to the database
               CouchDB will index all documents in the database using the
               view function
               View results will be returned as an array




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
60 / 70
View examples


         Query a view

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

         PHPillow validates and converts allowed view parameters
         What happens:
               View function will be uploaded to the database
               CouchDB will index all documents in the database using the
               view function
               View results will be returned as an array




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
60 / 70
View examples


         Query a view

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

         PHPillow validates and converts allowed view parameters
         What happens:
               View function will be uploaded to the database
               CouchDB will index all documents in the database using the
               view function
               View results will be returned as an array




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
60 / 70
View examples


         Query a view

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

         PHPillow validates and converts allowed view parameters
         What happens:
               View function will be uploaded to the database
               CouchDB will index all documents in the database using the
               view function
               View results will be returned as an array




                                                       http://kore-nordmann.de/portfolio.html
                                                                 Kore Nordmann <kore@php.net>
61 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
62 / 70
Attachments




     CouchDB allows you to attach files to documents
     Files are replicated
     You can serve full Web-Applications from a CouchDB
         See CouchApp
     Deploy using PUSH-replication




                                          http://kore-nordmann.de/portfolio.html
                                                    Kore Nordmann <kore@php.net>
62 / 70
Attachments




     CouchDB allows you to attach files to documents
     Files are replicated
     You can serve full Web-Applications from a CouchDB
         See CouchApp
     Deploy using PUSH-replication




                                          http://kore-nordmann.de/portfolio.html
                                                    Kore Nordmann <kore@php.net>
62 / 70
Attachments




     CouchDB allows you to attach files to documents
     Files are replicated
     You can serve full Web-Applications from a CouchDB
         See CouchApp
     Deploy using PUSH-replication




                                          http://kore-nordmann.de/portfolio.html
                                                    Kore Nordmann <kore@php.net>
63 / 70
Eventual consistency




      Mirror database into userspace
      Offline usage and synchronization of Browser applications
      Mozilla develops a JavaScript implementation of the
      CouchDB API [Moz09]




                                             http://kore-nordmann.de/portfolio.html
                                                       Kore Nordmann <kore@php.net>
63 / 70
Eventual consistency




      Mirror database into userspace
      Offline usage and synchronization of Browser applications
      Mozilla develops a JavaScript implementation of the
      CouchDB API [Moz09]




                                             http://kore-nordmann.de/portfolio.html
                                                       Kore Nordmann <kore@php.net>
63 / 70
Eventual consistency




      Mirror database into userspace
      Offline usage and synchronization of Browser applications
      Mozilla develops a JavaScript implementation of the
      CouchDB API [Moz09]




                                             http://kore-nordmann.de/portfolio.html
                                                       Kore Nordmann <kore@php.net>
64 / 70
Ubuntu One




     Ubuntu One uses CouchDB
     Synchronize contacts & date between nodes, or to a server
     Yes, all Ubuntu Karmic users already have a CouchDB running




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
64 / 70
Ubuntu One




     Ubuntu One uses CouchDB
     Synchronize contacts & date between nodes, or to a server
     Yes, all Ubuntu Karmic users already have a CouchDB running




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
65 / 70
Various applications




      Arbit uses CouchDB for issue tracking, wiki, FAQ and more
      Other applications: http:
      //wiki.apache.org/couchdb/CouchDB_in_the_wild




                                            http://kore-nordmann.de/portfolio.html
                                                      Kore Nordmann <kore@php.net>
66 / 70
Summary



     CouchDB is fast (enough)
     Document oriented approach allows new application
     development approaches
     CouchDB scales really well, horizontaly and verticaly
     CouchDB fits web applications really well
          RDBMS are still better for single-cluster scalable applications
          with strong integrity requirements.




                                                  http://kore-nordmann.de/portfolio.html
                                                            Kore Nordmann <kore@php.net>
67 / 70
Outline
   Introduction

   General

   Structure

   API

   Views

   Consistency

   PHPillow

   Applications

   QA


                  http://kore-nordmann.de/portfolio.html
                            Kore Nordmann <kore@php.net>
68 / 70
Resources




      Apache CouchDB: http://couchdb.org/
      Free CouchDB book: http://books.couchdb.org/relax/
      PHPillow: http://arbitracker.org/phpillow.html




                                       http://kore-nordmann.de/portfolio.html
                                                 Kore Nordmann <kore@php.net>
69 / 70
The end



     Open questions?
     Further remarks?
     Contact
          Mail: <kore@php.net>
          Web: http://kore-nordmann.de/ (Slides will be available
          here soonish)
          Twitter: http://twitter.com/koredn
          Comment: http://joind.in/1465




                                             http://kore-nordmann.de/portfolio.html
                                                       Kore Nordmann <kore@php.net>
70 / 70
Bibliography I



   [JCA09] Noah Slater J. Chris Anderson, Jan Lehnardt, Couchdb: The
           definitive 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 <kore@php.net>

More Related Content

What's hot (20)

KEY
Zend_Tool: Practical use and Extending
ZendCon
 
PDF
Gemification for Ruby 2.5/3.0
Hiroshi SHIBATA
 
PDF
Implementing a JSR-283 Content Repository in PHP
Karsten Dambekalns
 
PPTX
Zend Products and PHP for IBMi
Shlomo Vanunu
 
PDF
Introduction to column oriented databases in PHP
Zend by Rogue Wave Software
 
PDF
Optimize Performance of I/O-intensive Java applications Using Zero Copy
IndicThreads
 
PDF
international PHP2011_Kore Nordmann_Tobias Schlitt_Modular Application Archit...
smueller_sandsmedia
 
PDF
Performance tuning with zend framework
Alan Seiden
 
PDF
Zero-Copy Event-Driven Servers with Netty
Daniel Bimschas
 
PDF
Wt unit 1 ppts web development process
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PDF
Ftp servlet
Alexander Decker
 
PDF
Object Pascal Clean Code Guidelines Proposal (at EKON 22)
Arnaud Bouchez
 
PDF
Strategic Modernization with PHP on IBM i
Alan Seiden
 
PPT
My DIaries
Mehak Raza
 
PDF
(Oleg zhurakousky)spring integration-scala-intro
Skills Matter Talks
 
PDF
groovy DSLs from beginner to expert
Paul King
 
PDF
PHP Toolkit from Zend and IBM: Open Source on IBM i
Alan Seiden
 
PPTX
spdy
Kensaku Komatsu
 
PDF
System to generate speech to text in real time
Saptarshi Chatterjee
 
PPTX
Getting started with PHP on IBM i
Zend by Rogue Wave Software
 
Zend_Tool: Practical use and Extending
ZendCon
 
Gemification for Ruby 2.5/3.0
Hiroshi SHIBATA
 
Implementing a JSR-283 Content Repository in PHP
Karsten Dambekalns
 
Zend Products and PHP for IBMi
Shlomo Vanunu
 
Introduction to column oriented databases in PHP
Zend by Rogue Wave Software
 
Optimize Performance of I/O-intensive Java applications Using Zero Copy
IndicThreads
 
international PHP2011_Kore Nordmann_Tobias Schlitt_Modular Application Archit...
smueller_sandsmedia
 
Performance tuning with zend framework
Alan Seiden
 
Zero-Copy Event-Driven Servers with Netty
Daniel Bimschas
 
Wt unit 1 ppts web development process
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Ftp servlet
Alexander Decker
 
Object Pascal Clean Code Guidelines Proposal (at EKON 22)
Arnaud Bouchez
 
Strategic Modernization with PHP on IBM i
Alan Seiden
 
My DIaries
Mehak Raza
 
(Oleg zhurakousky)spring integration-scala-intro
Skills Matter Talks
 
groovy DSLs from beginner to expert
Paul King
 
PHP Toolkit from Zend and IBM: Open Source on IBM i
Alan Seiden
 
System to generate speech to text in real time
Saptarshi Chatterjee
 
Getting started with PHP on IBM i
Zend by Rogue Wave Software
 

Similar to CouchDB, PHPillow & PHP (20)

PDF
Doctrine and NoSQL
Benjamin Eberlei
 
KEY
Future of PHP
Richard McIntyre
 
PPS
Flickr Php
royans
 
PDF
Doctrine for NoSQL
Benjamin Eberlei
 
PDF
Multi model-databases
Michael Hackstein
 
PDF
Multi model-databases
ArangoDB Database
 
PPTX
Why Organizations are Looking at Alternative Database Technologies – Introduc...
DATAVERSITY
 
KEY
Mongo NYC PHP Development
Fitz Agard
 
PDF
CouchDB
codebits
 
KEY
NoSQL: Why, When, and How
BigBlueHat
 
PPTX
Node.js for PHP developers
Andrew Eddie
 
PDF
Non-Framework MVC sites with PHP
César Rodas
 
PDF
Flickr Architecture Presentation
web25
 
PDF
Couch Db
Ross Lawley
 
PDF
NoSQL - Motivation and Overview
Jonathan Weiss
 
KEY
OSCON 2011 CouchApps
Bradley Holt
 
PDF
Post-relational databases: What's wrong with web development? v3
Dobrica Pavlinušić
 
PDF
CouchDB Open Source Bridge
Chris Anderson
 
PDF
CouchDB in The Room
Makoto Ohnami
 
PDF
flickr's architecture & php
coolpics
 
Doctrine and NoSQL
Benjamin Eberlei
 
Future of PHP
Richard McIntyre
 
Flickr Php
royans
 
Doctrine for NoSQL
Benjamin Eberlei
 
Multi model-databases
Michael Hackstein
 
Multi model-databases
ArangoDB Database
 
Why Organizations are Looking at Alternative Database Technologies – Introduc...
DATAVERSITY
 
Mongo NYC PHP Development
Fitz Agard
 
CouchDB
codebits
 
NoSQL: Why, When, and How
BigBlueHat
 
Node.js for PHP developers
Andrew Eddie
 
Non-Framework MVC sites with PHP
César Rodas
 
Flickr Architecture Presentation
web25
 
Couch Db
Ross Lawley
 
NoSQL - Motivation and Overview
Jonathan Weiss
 
OSCON 2011 CouchApps
Bradley Holt
 
Post-relational databases: What's wrong with web development? v3
Dobrica Pavlinušić
 
CouchDB Open Source Bridge
Chris Anderson
 
CouchDB in The Room
Makoto Ohnami
 
flickr's architecture & php
coolpics
 
Ad

Recently uploaded (20)

PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
Staying Human in a Machine- Accelerated World
Catalin Jora
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PDF
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Staying Human in a Machine- Accelerated World
Catalin Jora
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
Kit-Works Team Study_20250627_한달만에만든사내서비스키링(양다윗).pdf
Wonjun Hwang
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
“Voice Interfaces on a Budget: Building Real-time Speech Recognition on Low-c...
Edge AI and Vision Alliance
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
Digital Circuits, important subject in CS
contactparinay1
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
Ad

CouchDB, PHPillow & PHP

  • 1. 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]>
  • 2. 2 / 70 About me Kore Nordmann, <[email protected]> Long time PHP developer Regular speaker, author, etc. Studies computer science in Dortmund, currently writing thesis Active open source developer: eZ Components (Graph, WebDav, Document), Arbit, PHPUnit, Torii, PHPillow, KaForkL, Image 3D, WCV, ... http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 3. 3 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 4. 4 / 70 CouchDB is paradigmn shift Structure Consistency API Applications http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 5. 4 / 70 CouchDB is paradigmn shift Structure Consistency API Applications http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 6. 4 / 70 CouchDB is paradigmn shift Structure Consistency API Applications http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 7. 4 / 70 CouchDB is paradigmn shift Structure Consistency API Applications http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 8. 4 / 70 CouchDB is paradigmn shift Structure Consistency API Applications http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 9. 5 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 10. 6 / 70 CouchDB Apache top-level project Build in Erlang / on Erlang/OTP http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 11. 6 / 70 CouchDB Apache top-level project Build in Erlang / on Erlang/OTP http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 12. 6 / 70 CouchDB Apache top-level project Build in Erlang / on Erlang/OTP http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 13. 7 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 14. 8 / 70 Structure Document based database No pre-defined structure (tables) Put in any JSON object you want Even deep structures (arrays of objects) You may attach any number of files to documents http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 15. 8 / 70 Structure Document based database No pre-defined structure (tables) Put in any JSON object you want Even deep structures (arrays of objects) You may attach any number of files to documents http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 16. 8 / 70 Structure Document based database No pre-defined structure (tables) Put in any JSON object you want Even deep structures (arrays of objects) You may attach any number of files to documents http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 17. 8 / 70 Structure Document based database No pre-defined structure (tables) Put in any JSON object you want Even deep structures (arrays of objects) You may attach any number of files to documents http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 18. 8 / 70 Structure Document based database No pre-defined structure (tables) Put in any JSON object you want Even deep structures (arrays of objects) You may attach any number of files to documents http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 19. 9 / 70 Structure Example wiki document 1 { ” t i t l e ”: ”PHPUK 2 0 1 0 ” , 2 ” text ”: ” Welcome t o t h e PHPUK . . . ” , 3 ” creator ”: ” u s e r −b a r ” , 4 ” edited ”: 2935678239 , 5 ” r e v i s i o n s ”: [ 6 { ” t i t l e ”: ”PHPUK 2 0 0 9 ” , 7 ” text ”: ” Welcome t o t h e PHPUK . . . ” , 8 ” c r e a t o r ” : ” u s 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]>
  • 20. 10 / 70 Attachements Example wiki document 1 { ” t i t l e ”: ”PHPUK 2 0 1 0 ” , 2 ” creator ”: ” u s e r −b a r ” , 3 ” text ”: ”<h1>Welcome t o t h e PHPUK</h1> 4 5 <img s r c =” p h p u k 2 0 1 0 / l o g o . png ” a l t =” PHPUK l o g o ”/> 6 ...” , 7 ” attachments ”: { 8 ” l o g o . png ” : { 9 ” c o n t e n t t y p e ” : ” image / png ” , 10 ” stub ”: true , 11 ” length ”: 42 , 12 } 13 } 14 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 21. 11 / 70 Migration / Refactoring Change document structure at any time No need for non-transaction-safe Data Definition Language (DDL) Fits rapid development approaches with common customer requested changes to the data structure You need to handle this in your application properly, of course: Incrementally update structure on modification Liberal validation on read http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 22. 11 / 70 Migration / Refactoring Change document structure at any time No need for non-transaction-safe Data Definition Language (DDL) Fits rapid development approaches with common customer requested changes to the data structure You need to handle this in your application properly, of course: Incrementally update structure on modification Liberal validation on read http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 23. 11 / 70 Migration / Refactoring Change document structure at any time No need for non-transaction-safe Data Definition Language (DDL) Fits rapid development approaches with common customer requested changes to the data structure You need to handle this in your application properly, of course: Incrementally update structure on modification Liberal validation on read http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 24. 11 / 70 Migration / Refactoring Change document structure at any time No need for non-transaction-safe Data Definition Language (DDL) Fits rapid development approaches with common customer requested changes to the data structure You need to handle this in your application properly, of course: Incrementally update structure on modification Liberal validation on read http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 25. 12 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 26. 13 / 70 HTTP API RESTful HTTP access HTTP is available on “all” platforms natively No PHP extension required Just use PHPs HTTP stream wrapper, pecl/http or curl You can use all your known HTTP middleware Reverse proxies for scaling reads (Varnish, Squid) Simple custom proxy configuration for direct “AJAX” access http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 27. 13 / 70 HTTP API RESTful HTTP access HTTP is available on “all” platforms natively No PHP extension required Just use PHPs HTTP stream wrapper, pecl/http or curl You can use all your known HTTP middleware Reverse proxies for scaling reads (Varnish, Squid) Simple custom proxy configuration for direct “AJAX” access http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 28. 13 / 70 HTTP API RESTful HTTP access HTTP is available on “all” platforms natively No PHP extension required Just use PHPs HTTP stream wrapper, pecl/http or curl You can use all your known HTTP middleware Reverse proxies for scaling reads (Varnish, Squid) Simple custom proxy configuration for direct “AJAX” access http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 29. 14 / 70 HTTP API GET / POST / PUT / DELETE <METHOD> http://<host>/<database>/<document> http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 30. 15 / 70 Create a database 1 $ c u r l − i −X PUT h t t p : / / l o c a l h o s t : 5 9 8 4 / p h p u k w i k i 2 3 HTTP/ 1 . 1 201 C r e a t e d 4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B ) 5 Location : http :// l o c a l h o s t :5984/ phpuk wiki 6 Date : F r i , 13 Jan 2009 1 4 : 0 7 : 5 7 GMT 7 Content −Type : t e x t / p l a i n ; c h a r s e t=u t f −8 8 Content −L e n g t h : 12 9 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]>
  • 31. 16 / 70 Create a wiki document 1 $ c u r l − i −X PUT h t t p : / / l o c a l h o s t : 5 9 8 4 / p h p u k w i k i / S t a r t −−d a t a ’ { ” 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 d 4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B ) 5 Location : http :// l o c a l h o s t :5984/ phpuk wiki / Start 6 Etag : ”1−6 b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ” 7 Date : F r i , 13 Jan 2009 1 4 : 1 4 : 5 5 GMT 8 Content −Type : t e x t / p l a i n ; c h a r s e t=u t f −8 9 Content −L e n g t h : 68 10 Cache−C o n t r o l : must−r e v a l i d a t e 11 12 {” ok ” : t r u e , ” i d ” : ” S t a r t ” , ” r e v ”:”1 −6 b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ”} http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 32. 17 / 70 Get a wiki document back 1 $ c u r l − i −X GET h t t p : / / l o c a l h o s t : 5 9 8 4 / p h p u k w i k i / S t a r t 2 3 HTTP/ 1 . 1 200 OK 4 S e r v e r : CouchDB / 0 . 1 0 . 0 ( E r l a n g OTP/R13B ) 5 Etag : ”1−6 b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ” 6 Date : F r i , 13 Jan 2009 1 4 : 1 5 : 4 8 GMT 7 Content −Type : t e x t / p l a i n ; c h a r s e t=u t f −8 8 Content −L e n g t h : 97 9 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 −6 b f d 4 8 8 5 b 6 c 6 2 b b 5 1 6 9 a 1 9 d 5 a 8 1 9 2 7 e 3 ” , ” 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]>
  • 33. 18 / 70 Security Simple database based access restrictions Using HTTP plain auth More fine grained access control will be in next release Define functions which decide if a request from a user will be accepted. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 34. 18 / 70 Security Simple database based access restrictions Using HTTP plain auth More fine grained access control will be in next release Define functions which decide if a request from a user will be accepted. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 35. 18 / 70 Security Simple database based access restrictions Using HTTP plain auth More fine grained access control will be in next release Define functions which decide if a request from a user will be accepted. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 36. 19 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 37. 20 / 70 Data access How to query such a mess? Views are small scripts, run for all documents in a database Views are built iteratively, results stored in BTrees Once built, they are fast Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ... A view may emit any number of key-value pairs for each document Key and value may be any JSON structure http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 38. 20 / 70 Data access How to query such a mess? Views are small scripts, run for all documents in a database Views are built iteratively, results stored in BTrees Once built, they are fast Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ... A view may emit any number of key-value pairs for each document Key and value may be any JSON structure http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 39. 20 / 70 Data access How to query such a mess? Views are small scripts, run for all documents in a database Views are built iteratively, results stored in BTrees Once built, they are fast Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ... A view may emit any number of key-value pairs for each document Key and value may be any JSON structure http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 40. 20 / 70 Data access How to query such a mess? Views are small scripts, run for all documents in a database Views are built iteratively, results stored in BTrees Once built, they are fast Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ... A view may emit any number of key-value pairs for each document Key and value may be any JSON structure http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 41. 20 / 70 Data access How to query such a mess? Views are small scripts, run for all documents in a database Views are built iteratively, results stored in BTrees Once built, they are fast Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ... A view may emit any number of key-value pairs for each document Key and value may be any JSON structure http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 42. 20 / 70 Data access How to query such a mess? Views are small scripts, run for all documents in a database Views are built iteratively, results stored in BTrees Once built, they are fast Mostly JavaScript, but also PHP, Ruby, Perl, Erlang, ... A view may emit any number of key-value pairs for each document Key and value may be any JSON structure http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 43. 21 / 70 Wiki 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 e m i t ( doc . t i t l e , doc . i d ) ; 6 } 7 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 44. 22 / 70 Wiki 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 e m i t ( doc . t i t l e , doc . i d ) ; 6 } 7 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 45. 23 / 70 Wiki Index all documents by their title 1 ” BuildModuleDesign ” => ” w i k i −b u i l d m o d u l e d e s i g n ” 2 ” CodingGuidelines ” => ” w i k i −c o d i n g g u i d e l i n e s ” 3 ” DiscussionProtocols ” => ” 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 ” Protocol 08 02 07 ” => ” 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 ... Custom deteministic IDs can ensure uniqueness of documents Just set the id property on insert. CouchDB can also generate IDs for you http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 46. 23 / 70 Wiki Index all documents by their title 1 ” BuildModuleDesign ” => ” w i k i −b u i l d m o d u l e d e s i g n ” 2 ” CodingGuidelines ” => ” w i k i −c o d i n g g u i d e l i n e s ” 3 ” DiscussionProtocols ” => ” 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 ” Protocol 08 02 07 ” => ” 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 ... Custom deteministic IDs can ensure uniqueness of documents Just set the id property on insert. CouchDB can also generate IDs for you http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 47. 23 / 70 Wiki Index all documents by their title 1 ” BuildModuleDesign ” => ” w i k i −b u i l d m o d u l e d e s i g n ” 2 ” CodingGuidelines ” => ” w i k i −c o d i n g g u i d e l i n e s ” 3 ” DiscussionProtocols ” => ” 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 ” Protocol 08 02 07 ” => ” 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 ... Custom deteministic IDs can ensure uniqueness of documents Just set the id property on insert. CouchDB can also generate IDs for you http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 48. 24 / 70 Map-reduce-views “MapReduce is a software framework introduced by Google to support distributed computing on large data sets on clusters of computers.” [Wik09] Used by CouchDB to implement views Just a framework / pattern: You can implement “any” algorithm using map-reduce. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 49. 24 / 70 Map-reduce-views “MapReduce is a software framework introduced by Google to support distributed computing on large data sets on clusters of computers.” [Wik09] Used by CouchDB to implement views Just a framework / pattern: You can implement “any” algorithm using map-reduce. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 50. 24 / 70 Map-reduce-views “MapReduce is a software framework introduced by Google to support distributed computing on large data sets on clusters of computers.” [Wik09] Used by CouchDB to implement views Just a framework / pattern: You can implement “any” algorithm using map-reduce. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 51. 25 / 70 Map-Reduce Documents http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 52. 26 / 70 Map-Reduce Documents Map $key:$value $key:$value $key:$value $key:$value http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 53. 27 / 70 Map-Reduce Documents Map Reduce $key:$value $key:$value $key:$value $key:$value $key:$value http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 54. 28 / 70 Map-Reduce Documents Map Reduce Re-Reduce Cluster 1 $key:$value $key:$value $key:$value $key:$value Cluster 2 $key:$value $key:$value $key:$value http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 55. 29 / 70 Map-Reduce Map and reduce functions are custom Reduce is optional, plain view serves as a document index Reduce may be applied to subsets of the documents Reduce may be grouped http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 56. 29 / 70 Map-Reduce Map and reduce functions are custom Reduce is optional, plain view serves as a document index Reduce may be applied to subsets of the documents Reduce may be grouped http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 57. 29 / 70 Map-Reduce Map and reduce functions are custom Reduce is optional, plain view serves as a document index Reduce may be applied to subsets of the documents Reduce may be grouped http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 58. 29 / 70 Map-Reduce Map and reduce functions are custom Reduce is optional, plain view serves as a document index Reduce may be applied to subsets of the documents Reduce may be grouped http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 59. 30 / 70 Map-Reduce example The simplest reduce function is just count() Often used for statistics 1 f u n c t i o n ( k e y s , v a l u e s , combine ) 2 { 3 r e t u r n sum ( v a l u e s ) ; 4 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 60. 31 / 70 Map-Reduce example The reduce result 1 null => 42 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 61. 32 / 70 More... What should be covered in more depth? Map-reduce views in CouchDB Scalability / consistency in CouchDB The PHPillow API http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 62. 33 / 70 Map-Reduce example 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 t e = new Date ( ) ; 6 d a t e . s e t T i m e ( doc . e d i t e d ∗ 1000 ) ; 7 emit ( [ 8 date . getUTCFullYear ( ) , 9 d a t e . getUTCMonth ( ) + 1 , 10 d a t e . getUTCDate ( ) , 11 d a t e . getUTCHours ( ) , 12 d a t e . getUTCMinutes ( ) , 13 d a t e . getUTCSeconds ( ) , 14 ], 1 ); 15 // You c o u l d a l s o e m i t t h e w h o l e doc a s v a l u e 16 } 17 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 63. 34 / 70 Map-Reduce example The mapping result 1 [2008 , 10 , 11 , 9, 11 , 12] => 1 2 [2008 , 10 , 11 , 9, 11 , 12] => 1 3 [2008 , 10 , 11 , 9, 11 , 12] => 1 4 [2008 , 10 , 11 , 9, 13 , 8] => 1 5 [2008 , 10 , 11 , 9, 13 , 44] => 1 6 [2008 , 10 , 11 , 9, 14 , 2] => 1 7 [2008 , 10 , 12 , 17 , 46 , 15] => 1 8 [2008 , 10 , 12 , 17 , 57 , 52] => 1 9 [2008 , 10 , 12 , 18 , 0, 45] => 1 10 [2008 , 10 , 14 , 8, 36 , 29] => 1 11 [2008 , 10 , 14 , 19 , 33 , 21] => 1 12 [2008 , 10 , 14 , 19 , 33 , 35] => 1 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 64. 35 / 70 Map-Reduce example The reduce function 1 f u n c t i o n ( k e y s , v a l u e s , combine ) 2 { 3 r e t u r n sum ( v a l u e s ) ; 4 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 65. 36 / 70 Map-Reduce example The reduce result 1 null => 12 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 66. 37 / 70 Map-Reduce example The grouped reduce result 1 [2008 , 10 , 11 , 9, 11 , 12] => 3 2 [2008 , 10 , 11 , 9, 13 , 8] => 1 3 [2008 , 10 , 11 , 9, 13 , 44] => 1 4 [2008 , 10 , 11 , 9, 14 , 2] => 1 5 [2008 , 10 , 12 , 17 , 46 , 15] => 1 6 [2008 , 10 , 12 , 17 , 57 , 52] => 1 7 [2008 , 10 , 12 , 18 , 0, 45] => 1 8 [2008 , 10 , 14 , 8, 36 , 29] => 1 9 [2008 , 10 , 14 , 19 , 33 , 21] => 1 10 [2008 , 10 , 14 , 19 , 33 , 35] => 1 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 67. 38 / 70 Map-Reduce example The filtered grouped reduce result startkey=[2008,10,11] and endkey=[2008,10,12] 1 [2008 , 10 , 11 , 9, 11 , 12] => 3 2 [2008 , 10 , 11 , 9, 13 , 8] => 1 3 [2008 , 10 , 11 , 9, 13 , 44] => 1 4 [2008 , 10 , 11 , 9, 14 , 2] => 1 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 68. 39 / 70 Map-Reduce example The grouped reduce result, with group level group-level=3 1 [2008 , 10 , 11] => 6 2 [2008 , 10 , 12] => 3 3 [2008 , 10 , 14] => 3 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 69. 40 / 70 Full-Text-Search 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 i m p l e word i n d e x i n g , d o e s n o t 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 // s t o p w o r d s , 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 value = {}; 10 v a l u e [ doc . i d ] = 1 ; 11 e m i t ( words [ i ] . t o L o w e r C a s e ( ) , v a l u e ) ; 12 } 13 } 14 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 70. 41 / 70 Wiki Index all documents by all their words 1 ... 2 ”a” => { wiki −8: 1} 3 ”a” => { wiki −8: 1} 4 ”a” => { wiki −8: 1} 5 ”a” => { wiki −8: 1} 6 ”a” => { wiki −81: 1} 7 ”a” => { wiki −83: 1} 8 ”a” => { wiki −83: 1} 9 ” able ” => { wiki −39: 1} 10 ” able ” => { wiki −56: 1} 11 ” able ” => { wiki −73: 1} 12 ” able ” => { wiki −80: 1} 13 ” about ” => { wiki −24: 1} 14 ” about ” => { wiki −43: 1} 15 ” about ” => { wiki −85: 1} 16 ... http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 71. 42 / 70 Full-Text-Search Reduce by word count 1 f u n c t i o n ( keys , v a l u e s ) { 2 var count = {}; 3 for ( var i in values ) { 4 for ( var id in values [ i ] ) { 5 i f ( count [ i d ] ) { 6 count [ i d ] = v a l u e s [ i ] [ i d ] + count [ i d ] ; 7 } else { 8 count [ i d ] = v a l u e s [ i ] [ i d ] ; 9 } 10 } 11 } 12 return count ; 13 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 72. 43 / 70 Wiki Index all documents by all their words 1 ... 2 ”a” => { 3 wiki −68: 6, 4 wiki −66: 6, 5 wiki −22: 4, 6 wiki −63: 3, 7 wiki −60: 2, 8 wiki −35: 2, 9 wiki −34: 1, 10 wiki −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 ” a b o u t ” => { 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]>
  • 73. 44 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 74. 45 / 70 Local conflicts Multi-Version Concurrency Control All documents in the database are versioned Don’t use it for application level document versioning Updates and deletes need to specify the revision ID Changing outdated documents result in conflicts http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 75. 45 / 70 Local conflicts Multi-Version Concurrency Control All documents in the database are versioned Don’t use it for application level document versioning Updates and deletes need to specify the revision ID Changing outdated documents result in conflicts http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 76. 45 / 70 Local conflicts Multi-Version Concurrency Control All documents in the database are versioned Don’t use it for application level document versioning Updates and deletes need to specify the revision ID Changing outdated documents result in conflicts http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 77. 45 / 70 Local conflicts Multi-Version Concurrency Control All documents in the database are versioned Don’t use it for application level document versioning Updates and deletes need to specify the revision ID Changing outdated documents result in conflicts http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 78. 45 / 70 Local conflicts Multi-Version Concurrency Control All documents in the database are versioned Don’t use it for application level document versioning Updates and deletes need to specify the revision ID Changing outdated documents result in conflicts http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 79. 46 / 70 Inter document links There is no ensured inter document consistency in CouchDB Different possibilities of relating documents: List IDs of related documents in document (n:m) ... both directions are feasible Embed the whole related document (1:n) Solution depends on update-ratio 1 { ” type ”: ” wiki ” , 2 ” t i t l e ”: ” Hello world ” , 3 ” text ”: ”...” , 4 ” comments ” : [ 5 { ”comment ” : ” . . . ” } , 6 ], 7 ” c r e a t o r ” : ” u s e r −f o o ” , 8 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 80. 46 / 70 Inter document links There is no ensured inter document consistency in CouchDB Different possibilities of relating documents: List IDs of related documents in document (n:m) ... both directions are feasible Embed the whole related document (1:n) Solution depends on update-ratio 1 { ” type ”: ” wiki ” , 2 ” t i t l e ”: ” Hello world ” , 3 ” text ”: ”...” , 4 ” comments ” : [ 5 { ”comment ” : ” . . . ” } , 6 ], 7 ” c r e a t o r ” : ” u s e r −f o o ” , 8 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 81. 46 / 70 Inter document links There is no ensured inter document consistency in CouchDB Different possibilities of relating documents: List IDs of related documents in document (n:m) ... both directions are feasible Embed the whole related document (1:n) Solution depends on update-ratio 1 { ” type ”: ” wiki ” , 2 ” t i t l e ”: ” Hello world ” , 3 ” text ”: ”...” , 4 ” comments ” : [ 5 { ”comment ” : ” . . . ” } , 6 ], 7 ” c r e a t o r ” : ” u s e r −f o o ” , 8 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 82. 46 / 70 Inter document links There is no ensured inter document consistency in CouchDB Different possibilities of relating documents: List IDs of related documents in document (n:m) ... both directions are feasible Embed the whole related document (1:n) Solution depends on update-ratio 1 { ” type ”: ” wiki ” , 2 ” t i t l e ”: ” Hello world ” , 3 ” text ”: ”...” , 4 ” comments ” : [ 5 { ”comment ” : ” . . . ” } , 6 ], 7 ” c r e a t o r ” : ” u s e r −f o o ” , 8 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 83. 46 / 70 Inter document links There is no ensured inter document consistency in CouchDB Different possibilities of relating documents: List IDs of related documents in document (n:m) ... both directions are feasible Embed the whole related document (1:n) Solution depends on update-ratio 1 { ” type ”: ” wiki ” , 2 ” t i t l e ”: ” Hello world ” , 3 ” text ”: ”...” , 4 ” comments ” : [ 5 { ”comment ” : ” . . . ” } , 6 ], 7 ” c r e a t o r ” : ” u s e r −f o o ” , 8 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 84. 46 / 70 Inter document links There is no ensured inter document consistency in CouchDB Different possibilities of relating documents: List IDs of related documents in document (n:m) ... both directions are feasible Embed the whole related document (1:n) Solution depends on update-ratio 1 { ” type ”: ” wiki ” , 2 ” t i t l e ”: ” Hello world ” , 3 ” text ”: ”...” , 4 ” comments ” : [ 5 { ”comment ” : ” . . . ” } , 6 ], 7 ” c r e a t o r ” : ” u s e r −f o o ” , 8 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 85. 47 / 70 Scaling: The CAP theorem The CAP theorem, read more in “CouchDB: The Definitive Guide” [JCA09] CouchDB Partition Availability tolerance eventual consistency PAXON RDBMS consensus enforced Consistency protocols consistency CouchDB employs “Eventual Consistency” [Vog09] http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 86. 48 / 70 Eventual consistency Europe Client / Web-Application Asia Amerika Delayed, triggered synchronization (push, pull) Deterministic (manual) conflict resolution on replication on all nodes Scales well for seldom concurrent writes Structure your documents accordingly http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 87. 48 / 70 Eventual consistency Europe Client / Web-Application Asia Amerika Delayed, triggered synchronization (push, pull) Deterministic (manual) conflict resolution on replication on all nodes Scales well for seldom concurrent writes Structure your documents accordingly http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 88. 48 / 70 Eventual consistency Europe Client / Web-Application Asia Amerika Delayed, triggered synchronization (push, pull) Deterministic (manual) conflict resolution on replication on all nodes Scales well for seldom concurrent writes Structure your documents accordingly http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 89. 48 / 70 Eventual consistency Europe Asia Amerika Delayed, triggered synchronization (push, pull) Deterministic (manual) conflict resolution on replication on all nodes Scales well for seldom concurrent writes Structure your documents accordingly http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 90. 48 / 70 Eventual consistency Europe Client / Web-Application Asia Amerika Delayed, triggered synchronization (push, pull) Deterministic (manual) conflict resolution on replication on all nodes Scales well for seldom concurrent writes Structure your documents accordingly http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 91. 49 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 92. 50 / 70 PHPillow Object-oriented client for CouchDB PHP >= 5.2 since last release (5.3 only before) >96% test coverage Still in alpha state Since CouchDB just got “beta” recently, and no new release was required. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 93. 50 / 70 PHPillow Object-oriented client for CouchDB PHP >= 5.2 since last release (5.3 only before) >96% test coverage Still in alpha state Since CouchDB just got “beta” recently, and no new release was required. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 94. 50 / 70 PHPillow Object-oriented client for CouchDB PHP >= 5.2 since last release (5.3 only before) >96% test coverage Still in alpha state Since CouchDB just got “beta” recently, and no new release was required. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 95. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 96. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 97. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 98. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 99. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 100. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 101. 51 / 70 PHPillow Lightweight layer Features Simple document validation constraints Automatic synchronization of views Automatic versioning of documents couchdb-python compatible tool for dump and import Different connection handlers PHP HTTP stream wrapper Custom HTTP protocol implementation Which is faster, most likely because of Connection: Keep-Alive http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 102. 52 / 70 A document class A custom document class 1 <?php 2 c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t { 3 protected $versioned = true ; 4 5 // . . . 6 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 103. 53 / 70 A document class A custom document class 1 <?php 2 c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t { 3 protected $versioned = true ; 4 5 protected $ r e q u i r e d P r o p e r t i e s = array ( 6 ’ login ’ , 7 ); 8 9 // . . . 10 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 104. 54 / 70 A document class A custom document class 1 <?php 2 c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t { 3 // . . . 4 public function construct () { 5 $ t h i s −>p r o p e r t i e s = a r r a y ( 6 ’ login ’ => 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 −x 7 e ]+$ ) ’ ) , 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 phpillowDocumentArrayValidator ( ’ myUserDocument ’ ) , 9 ... 10 ); 11 parent : : construct () ; 12 } 13 // . . . 14 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 105. 55 / 70 A document class A custom document class 1 <?php 2 c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t { 3 // . . . 4 protected function generateId () { 5 // R e t u r n n u l l f o r auto−g e n e r a t e d I D s 6 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]>
  • 106. 56 / 70 A document class A custom document class 1 <?php 2 c l a s s myUserDocument e x t e n d s p h p i l l o w D o c u m e n t { 3 // . . . 4 p r o t e c t e d f u n c t i o n getType ( ) { 5 return ’ user ’ ; 6 } 7 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 107. 57 / 70 Document examples Document creation example 1 // C r e a t e a document 2 $doc = new myUserDocument ( ) ; 3 $doc−>l o g i n = ’ k o r e ’ ; 4 $doc−>name = ’ Kore Nordmann ’ ; 5 $doc−>d a t a = a r r a y ( 6 ’ m a i l ’ => ” kore@php . n e t ” , 7 // . . . 8 ); 9 10 try { 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 s 14 } 15 16 // F e t c h a document by ID 17 $doc = new myUserDocument ( $ i d ) ; http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 108. 58 / 70 View examples 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 public function construct () { 3 parent : : construct () ; 4 5 $ t h i s −>v i e w F u n c t i o n s = array ( 6 ’ a l l ’ => a r r a y ( 7 ’ map ’ => ’ map/ u s e r a l l . j s ’ , 8 ), 9 ’ u s e r ’ => a r r a y ( 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]>
  • 109. 59 / 70 View examples 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 return ’ users ’ ; 6 } 7 } http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 110. 60 / 70 View examples Query a view 1 $ u s e r s = myUserView : : a l l ( a r r a y ( 2 ’ k e y ’ => ’ Kore Nordmann ’ , 3 ) ); PHPillow validates and converts allowed view parameters What happens: View function will be uploaded to the database CouchDB will index all documents in the database using the view function View results will be returned as an array http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 111. 60 / 70 View examples Query a view 1 $ u s e r s = myUserView : : a l l ( a r r a y ( 2 ’ k e y ’ => ’ Kore Nordmann ’ , 3 ) ); PHPillow validates and converts allowed view parameters What happens: View function will be uploaded to the database CouchDB will index all documents in the database using the view function View results will be returned as an array http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 112. 60 / 70 View examples Query a view 1 $ u s e r s = myUserView : : a l l ( a r r a y ( 2 ’ k e y ’ => ’ Kore Nordmann ’ , 3 ) ); PHPillow validates and converts allowed view parameters What happens: View function will be uploaded to the database CouchDB will index all documents in the database using the view function View results will be returned as an array http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 113. 60 / 70 View examples Query a view 1 $ u s e r s = myUserView : : a l l ( a r r a y ( 2 ’ k e y ’ => ’ Kore Nordmann ’ , 3 ) ); PHPillow validates and converts allowed view parameters What happens: View function will be uploaded to the database CouchDB will index all documents in the database using the view function View results will be returned as an array http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 114. 61 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 115. 62 / 70 Attachments CouchDB allows you to attach files to documents Files are replicated You can serve full Web-Applications from a CouchDB See CouchApp Deploy using PUSH-replication http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 116. 62 / 70 Attachments CouchDB allows you to attach files to documents Files are replicated You can serve full Web-Applications from a CouchDB See CouchApp Deploy using PUSH-replication http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 117. 62 / 70 Attachments CouchDB allows you to attach files to documents Files are replicated You can serve full Web-Applications from a CouchDB See CouchApp Deploy using PUSH-replication http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 118. 63 / 70 Eventual consistency Mirror database into userspace Offline usage and synchronization of Browser applications Mozilla develops a JavaScript implementation of the CouchDB API [Moz09] http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 119. 63 / 70 Eventual consistency Mirror database into userspace Offline usage and synchronization of Browser applications Mozilla develops a JavaScript implementation of the CouchDB API [Moz09] http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 120. 63 / 70 Eventual consistency Mirror database into userspace Offline usage and synchronization of Browser applications Mozilla develops a JavaScript implementation of the CouchDB API [Moz09] http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 121. 64 / 70 Ubuntu One Ubuntu One uses CouchDB Synchronize contacts & date between nodes, or to a server Yes, all Ubuntu Karmic users already have a CouchDB running http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 122. 64 / 70 Ubuntu One Ubuntu One uses CouchDB Synchronize contacts & date between nodes, or to a server Yes, all Ubuntu Karmic users already have a CouchDB running http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 123. 65 / 70 Various applications Arbit uses CouchDB for issue tracking, wiki, FAQ and more Other applications: http: //wiki.apache.org/couchdb/CouchDB_in_the_wild http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 124. 66 / 70 Summary CouchDB is fast (enough) Document oriented approach allows new application development approaches CouchDB scales really well, horizontaly and verticaly CouchDB fits web applications really well RDBMS are still better for single-cluster scalable applications with strong integrity requirements. http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 125. 67 / 70 Outline Introduction General Structure API Views Consistency PHPillow Applications QA http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 126. 68 / 70 Resources Apache CouchDB: http://couchdb.org/ Free CouchDB book: http://books.couchdb.org/relax/ PHPillow: http://arbitracker.org/phpillow.html http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 127. 69 / 70 The end Open questions? Further remarks? Contact Mail: <[email protected]> Web: http://kore-nordmann.de/ (Slides will be available here soonish) Twitter: http://twitter.com/koredn Comment: http://joind.in/1465 http://kore-nordmann.de/portfolio.html Kore Nordmann <[email protected]>
  • 128. 70 / 70 Bibliography I [JCA09] Noah Slater J. Chris Anderson, Jan Lehnardt, Couchdb: The definitive 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]>