21. “[The NoSQL movement] departs from the relational
model altogether, it should therefore have been called
more appropriately ‘NoREL’, or something to that
effect.”
- Carlo Strozzi
47. PHP LIBRARIES
• PHPillow (LGPL)
• PHP Object Freezer (BSD)
• PHP On Couch (GPL 2 / 3)
• PHP CouchDB Extension (PHP license)
• SAG for CouchDB (apache)
• Doctrine 2 CouchDB ODM
All are quite nice, doctrine has some rough edges, I use PHP On Couch with custom patch for
Zend autoloader easyness
48. <?PHP
// setup connection for couchdb
$client = new Couchdb_Client('http://ponies.couchone.com:5984','rainbows');
// fetch a document
$doc = $client->getDoc('awesome_pony');
// updating document
$doc->newproperty = array("type","awesome");
try
{
$client->storeDoc($doc);
}
catch (Exception $e)
{
echo "Document storage failed : " . $e->getMessage();
}
PHP On Couch with small ZF autoloader fix
51. DEFINITION
“Replication is the process of sharing information so
as to ensure consistency between redundant
resources, such as software or hardware components,
to improve reliability, fault-tolerance, or accessibility.”
Source: wikipedia
64. Couchdb doesn’t support partitioning (sharding) itself, couchdb -> http based -> lots of
possibilities
65. loadbalancer
...n
CouchDB
relax
CouchDB
relax
The basics are all the same: easy => couchdb instances 1..n => loadbalancer
66. CHALLENGES
• Large amounts of data
• Large views (with big/long map/reduce queries)
• LOTS of traffic
• Location based partitions
• For fun and profit
72. MAPPER => REDUCER
Partition IP Bytes
18271
191726
0 212.122.174.13
198
43
91272
1 74.119.8.111
8371
If data is big enough, you could even need a re-re-re-reducer
73. AFTER REDUCE
IP Bytes
212.122.174.13 210238
74.119.8.111 99643
76. LOUNGE
• dumb_proxy => proxy for simple PUT/GET’s
• smart_proxy => proxy for map/reduce over shards
• replicator => updates all copies, redudantly
it can make sure that there are N copies of a document at every moment
84. PROXIED
CouchDB
relax
proxied via middleware, or via mod_proxy or similiar
85. DIRECT
CouchDB
relax
or direct, because http based, content is directly available in javascript
86. NOSQL && SQL HYBRID
• onSave, onCommit hooks available in every major framework
• onSave -> make a JSON representation of your object, and
PUT it to couchdb (#protip: only ‘public’ data)
• sql db is leading, you don’t care about versioning in couchdb
• youcan use your data directly from couchdb within your
frontend javascript
87. MODEL
<?php
class Pony extends Application_models
{
public function toArray()
{
$data = $this->_getData();
unset($data['created_on']);
unset($data['created_by']);
unset($data['access_level']);
unset($data['private_data']);
$data['tags'] = $this->getTags();
$data['categories'] = $this->getCategories();
$data['rainbows'] = 'double';
return $data;
}
}
88. AFTER_SAVE
<?php
class article_module extends admin_module
{
public function after_save()
{
parent::after_save();
$data = $this->toJson();
$res = CouchDB::put($data);
$this->_id = $res->_id;
$this->_rev = $res->_rev;
}
}
89. PROXY
RewriteEngine On
RewriteRule /data/(.*) http://127.0.0.1:5984/db/$1 [P,L]
Proxy the calls (work around sandbox/other domain error), or use jsonp
122. $ curl -X GET "http://ponies.couchone.com/rainbows/_changes?since=20"
123. curl -X GET "http://ponies.couchone.com/rainbows/_changes?feed=longpoll&since=2"
Longpolling: good for little updates, connections stays open until change, then gets closed
and you need to reconnect, lots of reconnects for lots of updates
124. curl -X GET "http://ponies.couchone.com/rainbows/_changes?feed=continuous&since=2"
Connections stays open, and you get updates on the fly!
134. function(doc) {
return {
"code": 302,
"body": "See other",
"headers": {
"Location": doc.target
}
};
}
You can also define http headers, we used this for translating public id’s into private storage
id’s. In this way, couchdb took care of all the headers and http stuff, and we could use a
regular nginx proxy module
141. GEOCOUCH
• Supports bbox
• fork
• outputs via lists, georss possible
• directly useable by google maps
• can read GIS data
• combined with _changes makes interesting usecase
- bbox => all items withing a certain bounding box, polygon is in the works
- currently a fork of couchdb, in the works as external module
- output can be setup seperately
- google maps can use georss
- GIS: Geographic Information System (used worldwide?)
144. GEORSS && GOOGLE MAPS
if (GBrowserIsCompatible())
{
map = new GMap2(document.getElementById('map'));
var geoXML = new GGeoXml('http://ponies.couchone.com/rainbows/url-to-georss-view');
map.addOverlay(geoXML);
}
145. curl -X GET "http://ponies.couchone.com/rainbows/_design/alarmeringen/_spatial/points?
bbox=51.711369,4.218407,52.136520,4.745740";