Mongo and Harmony

download Mongo and Harmony

If you can't read please download the document

  • date post

    12-May-2015
  • Category

    Documents

  • view

    4.759
  • download

    0

Embed Size (px)

Transcript of Mongo and Harmony

  • 1.MongoDB & Harmony Real World Usage of MongoDB MongoSF, San FranciscoSteve Smith April 30, 2010Ordered List

2. Hello! steve@orderedlist.com @orderedlist 3. http://orderedlist.com 4. http://get.harmonyapp.com 5. 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 6. What is Harmony? Understanding Storage Needs 7. Harmony is forBuilding Websites 8. Harmony is design toBe Flexible 9. Harmony is designed toStore Web Content 10. Demo Time 11. 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 12. Multiple Item Types Flexibility in Collections 13. Harmony hasPages 14. Harmony hasBlogs 15. Harmony hasBlog Posts 16. Harmony hasBlog Labels and Archives 17. Harmony hasMany More Types... 18. All These ThingsHave a URL 19. We UseSingle Collection Inheritance 20. class Item include MongoMapper::Documentkey :_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 hereend 21. class Page < Itemkey :position, Integer, :default => 1# more code hereend 22. class Blog < Itemkey :position, Integer, :default => 1 key :labels_as,String, :default => 'tags' key :custom_permalink_structure, String# more code hereend 23. class BlogPost < Itemkey :accepting_comments, Boolean, :default => true key :labels, Set, :index => true key :year_id,ObjectId key :month_id, ObjectId key :day_id, ObjectId# more code hereend 24. Separation of Code intoMeaningful Keys 25. One Collection MeansSimple Querying 26. 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 27. Custom Data Unknown Possibilities 28. Every Item in Harmony getsA Title and a Path 29. All Other Data isDened by the User 30. templates 31. templates Template 1 Template 2 Template 3 32. templateselds Template 1 Template 2 Template 3 33. templateselds Template 1 Field 1Field 2Field 3Template 2 Field 4Field 5Field 6Template 3 Field 7Field 8Field 9 34. This data is not related, itBelongs Together 35. templates Template 1 Field 1Field 2Field 3 Template 2Use Embedded Field 4Documents Field 5Field 6 Template 3 Field 7Field 8Field 9 36. items 37. items Item 1 Item 2 Item 3 38. itemsdata Item 1 Item 2 Item 3 39. itemsdata Item 1 Data 1Data 2Data 3Item 2 Data 4Data 5Data 6Item 3 Data 7Data 8Data 9 40. itemstemplates Item 1 Template 1 Data 1 Field 1Data 2 Field 2Data 3 Field 3 Item 2 Template 2 Data 4 Field 4Data 5 Field 5Data 6 Field 6 Item 3 Template 3 Data 7 Field 7Data 8 Field 8Data 9 Field 9 41. class Template include MongoMapper::Documentkey :filename, String, :index => true key :theme_id, ObjectId, :index => true key :contents, String timestamps! userstamps!many :fields# more code hereend 42. class Field include MongoMapper::EmbeddedDocumentkey :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,Booleanembedded_in :template# more code hereend 43. class Item include MongoMapper::Documentkey :_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 hereend 44. class Datum include MongoMapper::EmbeddedDocumentkey :key, String, :required => true, :length => 2..100 key :file_upload, Boolean, :default => false key :valueembedded_in :item# more code hereend 45. Embedding allowsMaximum Flexibility 46. Embedding allowsKeeping Data Together 47. 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 48. Images and Files Binary Data Storage 49. Harmony contains severalTypes of Files 50. Harmony containsText Files Stylesheets, JavaScripts, Includes, and Templates 51. Harmony containsBinary Files Images, File Uploads 52. All these areStored in Mongo 53. class Stylesheet include MongoMapper::Document include PageCacheablekey :filename, String, :index => true key :contents, String key :processor,String, :default => 'plain' key :theme_id, ObjectId, :index => true timestamps! userstamps!# more code hereend 54. class StylesheetsController < ApplicationController caches_page :showdef 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 55. class Asset include MongoMapper::Document plugin Jointdef 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]}],}endkey :title_tag, String key :description, String # long description timestamps! userstamps!attachment :file# more code hereend 56. asset = Asset.new asset.file = params[:uploaded_file]asset.file.id asset.file.size asset.file.type asset.file.name 57. class RackAssets OriginalRegex = /^/assets/(.*)/(.*)$/# /assets/:id/name.ext VersionRegex = /^/assets/(.*)/(.*)/(.*)$/ # /assets/:id/:version/name.ext def initialize(app)@app = append def call(env)case Rack::Request.new(env).path_infowhen VersionRegexid, version = $1, $2serve_asset(id, version)when OriginalRegexid = $1serve_asset(id)else@app.call(env)endenddef serve_asset(id, version=nil) if asset = Asset.find(id) asset.page_cache(version) [200, {'Content-Type' => asset.file.type}, [File.read(asset.page_cache_path(version))]] else [404, {'Content-Type' => 'text/plain'}, ['File not found.']] end end end 58. 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 59. Activity Streams Quick Information 60. activities usersitemsassets themes 61. activities usersitemsassets themes 62. activities usersitemsassets themes 63. activities usersitemsassets themes 64. class Activity include MongoMapper::Documentkey :user,Hash key :source,Hash key :source_type, String key :action,String key :count, Integer, :default => 0timestamps!end 65. class Activity include MongoMapper::Documentkey :user,Hash key :source,Hash key :source_type, String key :action,String key :count, Integer, :default => 0timestamps!def source=(value) if value.is_a?(Hash) super else self.source_type = value.class.name super(value.to_mongo) end enddef user=(value) if value.is_a?(User) super :id => value.id, :name => value.full_name else super end endend 66. class Activity# code on previous slidedef self.article_created(article) create(:source => article, :user => article.creator, :action => 'create') enddef 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 enddef self.article_published(article) create(:source => article, :user => article.updater, :action => 'publish') enddef self.article_unpublished(article) create(:source => article, :user => article.updater, :action => 'unpublish') endend 67. 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 68. Wrapping Up Recapping the Main Points 69. Take advantage ofDynamic Keys in Collections 70. Use Embedding toSimplify Relationships 71. Storing Binary Data isSimple & Useful 72. Embed Whole Objects forArchiving and Query Reduction 73. Thank you! steve@orderedlist.com @orderedlistMongoSF, San Francisco Steve Smith April 30, 2010 Ordered List