Slide 1

Slide 1 text

Ordered List Steve Smith Mongo Chicago October 20, 2010 MongoDB & Harmony Real World Usage of MongoDB

Slide 2

Slide 2 text

Hello! [email protected] @orderedlist

Slide 3

Slide 3 text

http://orderedlist.com

Slide 4

Slide 4 text

http://get.harmonyapp.com

Slide 5

Slide 5 text

Things to Cover 1. What is Harmony? 2. Multiple Item Types 3. Completely Custom Data 4. Images and Files 5. Activity Streams 6. Wrap Up

Slide 6

Slide 6 text

What is Harmony? Understanding Storage Needs

Slide 7

Slide 7 text

Building Websites Harmony is for

Slide 8

Slide 8 text

Be Flexible Harmony is designed to

Slide 9

Slide 9 text

Store Web Content Harmony is designed to

Slide 10

Slide 10 text

Demo Time

Slide 11

Slide 11 text

Things to Cover 1. What is Harmony? 2. Multiple Item Types 3. Completely Custom Data 4. Images and Files 5. Activity Streams 6. Wrap Up

Slide 12

Slide 12 text

Multiple Item Types Flexibility in Collections

Slide 13

Slide 13 text

Pages Harmony has

Slide 14

Slide 14 text

Blogs Harmony has

Slide 15

Slide 15 text

Blog Posts Harmony has

Slide 16

Slide 16 text

Blog Labels and Archives Harmony has

Slide 17

Slide 17 text

Many More Types... Harmony has

Slide 18

Slide 18 text

Have a URL All These Things

Slide 19

Slide 19 text

Single Collection Inheritance We Use

Slide 20

Slide 20 text

class Item include MongoMapper::Document key :_type, String, :index => true key :title, String, :required => true key :path, String, :required => true, :index => true key :template_name, String key :published_at, Time key :parent_id, ObjectId, :index => true key :parent_ids, Array, :index => true key :site_id, ObjectId, :required => true, :index => true key :nav_item, Boolean, :default => true key :asset_ids, Array, :index => true timestamps! userstamps! # more code here end

Slide 21

Slide 21 text

class Page < Item key :position, Integer, :default => 1 # more code here end

Slide 22

Slide 22 text

class Blog < Item key :position, Integer, :default => 1 key :labels_as, String, :default => 'tags' key :custom_permalink_structure, String # more code here end

Slide 23

Slide 23 text

class BlogPost < Item key :accepting_comments, Boolean, :default => true key :labels, Set, :index => true key :year_id, ObjectId key :month_id, ObjectId key :day_id, ObjectId # more code here end

Slide 24

Slide 24 text

Meaningful Keys Separation of Code into

Slide 25

Slide 25 text

Simple Querying One Collection Means

Slide 26

Slide 26 text

Things to Cover 1. What is Harmony? 2. Multiple Item Types 3. Completely Custom Data 4. Images and Files 5. Activity Streams 6. Wrap Up

Slide 27

Slide 27 text

Custom Data Unknown Possibilities

Slide 28

Slide 28 text

A Title and a Path Every Item in Harmony gets

Slide 29

Slide 29 text

Defined by the User All Other Data is

Slide 30

Slide 30 text

templates

Slide 31

Slide 31 text

templates Template 1 Template 2 Template 3

Slide 32

Slide 32 text

templates fields Template 1 Template 2 Template 3

Slide 33

Slide 33 text

templates fields Field 1 Field 2 Field 3 Template 1 Template 2 Template 3 Field 4 Field 5 Field 6 Field 7 Field 8 Field 9

Slide 34

Slide 34 text

Belongs Together This data is not related, it

Slide 35

Slide 35 text

templates Field 1 Field 2 Field 3 Template 1 Template 2 Template 3 Field 4 Field 5 Field 6 Field 7 Field 8 Field 9 Use Embedded Documents

Slide 36

Slide 36 text

items

Slide 37

Slide 37 text

items Item 1 Item 2 Item 3

Slide 38

Slide 38 text

items data Item 1 Item 2 Item 3

Slide 39

Slide 39 text

items data Data 1 Data 2 Data 3 Item 1 Item 2 Item 3 Data 4 Data 5 Data 6 Data 7 Data 8 Data 9

Slide 40

Slide 40 text

items Data 1 Data 2 Data 3 Item 1 Item 2 Item 3 Data 4 Data 5 Data 6 Data 7 Data 8 Data 9 templates Field 1 Field 2 Field 3 Template 1 Template 2 Template 3 Field 4 Field 5 Field 6 Field 7 Field 8 Field 9

Slide 41

Slide 41 text

class Template include MongoMapper::Document key :filename, String, :index => true key :theme_id, ObjectId, :index => true key :contents, String timestamps! userstamps! many :fields # more code here end

Slide 42

Slide 42 text

class Field include MongoMapper::EmbeddedDocument key :name, String, :required => true key :key, String, :required => true key :field_type_id, Integer, :required => true key :help_text, String key :settings, Hash key :required, Boolean embedded_in :template # more code here end

Slide 43

Slide 43 text

class Item include MongoMapper::Document key :_type, String, :index => true key :title, String, :required => true key :path, String, :required => true, :index => true key :template_name, String key :published_at, Time key :parent_id, ObjectId, :index => true key :parent_ids, Array, :index => true key :site_id, ObjectId, :required => true, :index => true key :nav_item, Boolean, :default => true key :asset_ids, Array, :index => true timestamps! userstamps! many :data # more code here end

Slide 44

Slide 44 text

class Datum include MongoMapper::EmbeddedDocument key :key, String, :required => true, :length => 2..100 key :file_upload, Boolean, :default => false key :value embedded_in :item # more code here end

Slide 45

Slide 45 text

Maximum Flexibility Embedding allows

Slide 46

Slide 46 text

Keeping Data Together Embedding allows

Slide 47

Slide 47 text

Things to Cover 1. What is Harmony? 2. Multiple Item Types 3. Completely Custom Data 4. Images and Files 5. Activity Streams 6. Wrap Up

Slide 48

Slide 48 text

Images and Files Binary Data Storage

Slide 49

Slide 49 text

Types of Files Harmony contains several

Slide 50

Slide 50 text

Text Files Harmony contains Stylesheets, JavaScripts, Includes, and Templates

Slide 51

Slide 51 text

Binary Files Harmony contains Images, File Uploads

Slide 52

Slide 52 text

Stored in Mongo All these are

Slide 53

Slide 53 text

class Stylesheet include MongoMapper::Document include PageCacheable key :filename, String, :index => true key :contents, String key :processor, String, :default => 'plain' key :theme_id, ObjectId, :index => true timestamps! userstamps! # more code here end

Slide 54

Slide 54 text

class StylesheetsController < ApplicationController caches_page :show def show render_not_found and return if params[:filename].blank? filename = File.basename(params[:filename].first, '.css') if stylesheet = Stylesheet.first(:filename => filename, :theme_id => params[:theme_id]) if stale?(:etag => stylesheet, :last_modified => stylesheet.updated_at.utc, :public => true) render :text => stylesheet.processed_contents, :content_type => 'text/css' end else render_not_found end end end

Slide 55

Slide 55 text

class Asset include MongoMapper::Document plugin Joint def self.versions @versions ||= { :feature => [:resize, {:width => 640}], :thumb => [:crop_resize, {:dimensions => [145, 75]}], :square => [:crop_resize, {:dimensions => [75, 75]}], :profile => [:crop_resize, {:dimensions => [100, 100]}], :profile_small => [:crop_resize, {:dimensions => [50, 50]}], } end key :title_tag, String key :description, String # long description timestamps! userstamps! attachment :file # more code here end

Slide 56

Slide 56 text

asset = Asset.new asset.file = params[:uploaded_file] asset.file.id asset.file.size asset.file.type asset.file.name

Slide 57

Slide 57 text

class FlyImages OriginalRegex = /^\/assets\/(.*)\/(.*)$/ # /assets/:id/name.ext VersionRegex = /^\/assets\/(.*)\/(.*)\/(.*)$/ # /assets/:id/:version/name.ext def initialize(app) @app = app end def call(env) case Rack::Request.new(env).path_info when VersionRegex id, version = $1, $2 serve_asset(id, version) when OriginalRegex id = $1 serve_asset(id) else @app.call(env) end end def serve_asset(id, version=nil) if asset = Asset.find(id) asset.page_cache(version) [200, {'Content-Type' => asset.content_type}, [File.read(asset.page_cache_path(version))]] else [404, {'Content-Type' => 'text/plain'}, ['File not found.']] end end end

Slide 58

Slide 58 text

Things to Cover 1. What is Harmony? 2. Multiple Item Types 3. Completely Custom Data 4. Images and Files 5. Activity Streams 6. Wrap Up

Slide 59

Slide 59 text

Activity Streams Quick Information

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

activities users items assets themes

Slide 62

Slide 62 text

activities users items assets themes

Slide 63

Slide 63 text

activities users items assets themes

Slide 64

Slide 64 text

activities users items assets themes

Slide 65

Slide 65 text

class Activity include MongoMapper::Document key :user, Hash key :source, Hash key :source_type, String key :action, String key :count, Integer, :default => 0 timestamps! end

Slide 66

Slide 66 text

class Activity include MongoMapper::Document key :user, Hash key :source, Hash key :source_type, String key :action, String key :count, Integer, :default => 0 timestamps! def source=(value) if value.is_a?(Hash) super else self.source_type = value.class.name super(value.to_mongo) end end def user=(value) if value.is_a?(User) super :id => value.id, :name => value.full_name else super end end end

Slide 67

Slide 67 text

class Activity # code on previous slide def self.article_created(article) create(:source => article, :user => article.creator, :action => 'create') end def self.article_updated(article) first_or_new(:action => 'update', :'source._id' => article.id, :created_at.gt => Time.zone.now.beginning_of_day.utc).tap do |a| a.count += 1 a.source = article a.user = article.updater a.save end end def self.article_published(article) create(:source => article, :user => article.updater, :action => 'publish') end def self.article_unpublished(article) create(:source => article, :user => article.updater, :action => 'unpublish') end end

Slide 68

Slide 68 text

Things to Cover 1. What is Harmony? 2. Multiple Item Types 3. Completely Custom Data 4. Images and Files 5. Activity Streams 6. Wrap Up

Slide 69

Slide 69 text

Wrapping Up Recapping the Main Points

Slide 70

Slide 70 text

Dynamic Keys in Collections Take advantage of

Slide 71

Slide 71 text

Simplify Relationships Use Embedding to

Slide 72

Slide 72 text

Simple & Useful Storing Binary Data is

Slide 73

Slide 73 text

Archiving and Query Reduction Embed Whole Objects for

Slide 74

Slide 74 text

Ordered List Thank you! [email protected] Steve Smith Mongo Chicago October 20, 2010 @orderedlist