Bigger: Drupal + Mongo
Bigger: Drupal + Mongo
Bigger: Drupal + Mongo
Drupal + Mongo:
is Better?
by Forest Mars
Drupal (FTW!)
Drupal: Aspect-oriented (modular) social-publishing framework, written in php (pdo) that allows easy creation and integration of multiple data-rich social networking sites and robust web applications & services. Used on large high-performance websites. Roadmap anticipates future web (rdf, ggg) & emerging technologies (MongoDB!)
ACID / BASE
Acid Atomic
Consistent
Isolated
Scales well
Eventually consistant
Durable
(Why Mongo?)
Tableless Queriless Schemaless
Blazingly fast
Faster Development times Nicer learning curves Code is trimmer Future-proof
Performance / Scaling
Whitehouse.gov / direct engagement
15K/day contact requests 2M records in db
Install Mongo
public array authenticate (string $username, string $password ) public array command ( array $data ) __construct ( Mongo $conn , string $name ) public MongoCollection createCollection ( string $name [, bool $capped = FALSE [, int $size = 0 [, int $max = 0 ]]] ) public array createDBRef ( string $collection , mixed $a ) public array drop ( void ) public array dropCollection ( mixed $coll ) public array execute ( mixed $code [, array $args = array() ] ) public bool forceError ( void ) public MongoCollection __get ( string $name ) public array getDBRef ( array $ref ) public MongoGridFS getGridFS ([ string $prefix = "fs" ] ) public int getProfilingLevel ( void ) public array lastError ( void )
list the nodes of a user ordered by comment count uid is stored in the node table and the comment count is in node_comment_statistics > thus query cannot be indexed (Comparison of dissimilar columns may prevent use of indexes if values cannot be compared directly without conversion.)
* mongodb_block: Store block information in mongodb. Very close to the core block API. * * * mongodb_cache: Store cache items in mongodb. mongodb_session: Store sessions in mongodb. mongodb_watchdog: Store watchdog messages in mongodb
* mongodb_queue: DrupalQueueInterface implementation using mongodb. * mongodb_field_storage: Store the fields in mongodb.
Mongo Watchdog
mongodb_watchdog
mongodb_cache
mongodb_sessions
mongodb_sessions
function mongodb_session_user_update($edit, $account) { if (!module_exists('mongodb_field_storage')) { $roles = _mongodb_session_get_roles($account); $save = (array) $account + array( '_id' => (int) $account->uid, '@bundle' => 'user', '@fields' => array(), 'roles' => $roles, ); foreach (array('uid', 'created', 'access', 'login', 'status', 'picture') as $key) { $save[$key] = (int) $save[$key]; } mongodb_collection('fields_current', 'user')>save($save); } return $roles; }
mongodb_sessions
* * * * * * * The user-level session storage handlers: - _drupal_session_open() - _drupal_session_close() - _drupal_session_read() - _drupal_session_write() - _drupal_session_destroy() - _drupal_session_garbage_collection()
mongodb_block
function hook_block_view_alter(&$data, $block) { // Remove the contextual links on all blocks that provide them. if (is_array($data['content']) && isset($data['content']['#contextual_links' ])) {
unset($data['content']['#contextual_links' ]); } // Add a theme wrapper function defined by the current module to all blocks // provided by the "somemodule" module. if (is_array($data['content']) && $block->module == 'somemodule') { $data['content']['#theme_wrappers'][] = 'mymodule_special_block';
Block rebuild
Notice: Undefined variable: block_html_id in include() (line 4 of /var/www/Drupal/drupal-7.0alpha4/themes/garland/block.tpl.php). Notice: Undefined variable: block_html_id in include() (line 4 of /var/www/Drupal/drupal-7.0alpha4/themes/garland/block.tpl.php). Notice: Undefined variable: block_html_id in include() (line 4 of /var/www/Drupal/drupal-7.0alpha4/themes/garland/block.tpl.php). Notice: Undefined variable: block_html_id in include() (line 4 of /var/www/Drupal/drupal-7.0alpha4/themes/garland/block.tpl.php). Notice: Undefined variable: block_html_id in include() (line 4 of /var/www/Drupal/drupal-7.0alpha4/themes/garland/block.tpl.php).
mongodb_field_storage
don't: variable_set('field_storage_default', 'mongodb_field_storage'); instead: $conf['field_storage_default'] = 'mongodb_field_storage'; in settings.php ESP. for session/caching backends
Drupal 7
Everything In MongoDB*
*(some restrictions may apply)
(* in 14 l.o.c.)
// Get the database (it is created automatically) $db = $mongo->testDatabase; // Get the collection for nodes (it is created automatically) $collection = $db->nodes; // Get a listing of all of the node IDs $r = db_query('SELECT nid FROM {node}'); // Loop through all of the nodes... while($row = db_fetch_object($r)) { print "Writing node $row->nid\n"; // Load each node and convert it to an array. $node = (array)node_load($row->nid);
// Write our search filter (same as shell example above) $filter = array( 'title' => new MongoRegex('/about/i'), ); // Run the query, getting only 5 results. $res = $mongo->quiddity->nodes>find($filter)->limit(5); // Loop through and print the title of each article. foreach ($res as $row) { print $row['title'] . PHP_EOL; } ?>
What's Next?
Multiple DB servers Data Persistance
Query logging - Devel support Query builder Views integration DBTNG Full DB Abstraction MongoDB API
Query Logging
awesomesauce
page callback => 'drupal_json' $items['node/%node/json'] = array('page callback' => 'drupal_json', 'page arguments' => array(1), 'type' => MENU_CALLBACK);
Joining across Entities ex. return birthday from profile belonging to author of current node
Thanks!
Comments & questions to: ForestMars @gmail.com ForestMars @googlewave.com Facebook, LinkedIn, etc.