Slide 1

Slide 1 text

Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole 16.4.2014 / cologne.rb / @mixxt Dirk Breuer / @railsbros_dirk

Slide 2

Slide 2 text

Data Source Pattern Architectural patterns which drive the way in which the domain logic talks to the database. – Martin Fowler

Slide 3

Slide 3 text

• Active Record • Data Mapper
 • Table Data Gateway • Row Data Gateway • …

Slide 4

Slide 4 text

NoSQL Not ACID compliant Big Data Distributed Web Scale ROFL Scale CAP Documents Flexible High Availability

Slide 5

Slide 5 text

NoSQL Databases • „Not only SQL“ • Not one definition • Sometimes just a marketing buzz-fuzz

Slide 6

Slide 6 text

Classifications • Document-based • Key-Value stores • Graph-based • Column stores • Time series Aggregate-based }

Slide 7

Slide 7 text

Multi-Model Combine more than one approach within one database system

Slide 8

Slide 8 text

Guacamole Source: https://en.wikipedia.org/wiki/File:Guacamole.jpg A dip consisting at least of ripe avocados and salt

Slide 9

Slide 9 text

…and…

Slide 10

Slide 10 text

gem 'guacamole' An ODM for ArangoDB to be used with Rails and other Ruby frameworks* Object Document Mapper *which is not yet finished

Slide 11

Slide 11 text

• Open-Source NoSQL database • Multi-Model • Document-based • Graph-based

Slide 12

Slide 12 text

Why should I use
 ArangoDB in favor of
 ? With ArangoDB you can perform joins
 over your documents similar
 to joins in relational databases.

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Before you start

Slide 15

Slide 15 text

Patterns of Enterprise Application Architecture

Slide 16

Slide 16 text

Ingredients

Slide 17

Slide 17 text

1 Design Goal 1 Data Source Pattern 3 Supporting Libraries 1 Database driver 1 solid Test-Suite 1 Documentation

Slide 18

Slide 18 text

Design Goal • Support building web applications with Ruby on Rails or other Rack-based frameworks • Focus on easy integration in Views and the general workflow • Reflect the nature of NoSQL in general and ArangoDB in particular

Slide 19

Slide 19 text

Data Source Pattern

Slide 20

Slide 20 text

Data Mapper It’s a Nein! – Doch! – Ohhhh!

Slide 21

Slide 21 text

+ Testability is increased + Separation of Concern + Easier to support database
 features like embedded objects - The average Rails dev is
 used to Active Record - Starting with Data Mapper
 requires more effort

Slide 22

Slide 22 text

Supporting Libraries

Slide 23

Slide 23 text

spec.add_dependency 'ashikawa-core' spec.add_dependency 'virtus' spec.add_dependency 'activemodel' spec.add_dependency 'hamster' spec.add_dependency 'activesupport'

Slide 24

Slide 24 text

Implementation

Slide 25

Slide 25 text

Just some bits and pieces…

Slide 26

Slide 26 text

The Model class Post include Guacamole::Model ! attribute :title, String attribute :body, String attribute :comments, Array[Comment] attribute :user, User end

Slide 27

Slide 27 text

The Collection (a.k.a. The Mapper) class PostsCollection include Guacamole::Collection ! map do embeds :comments references :user end end

Slide 28

Slide 28 text

ActiveRecord equivalent class Post < ActiveRecord belongs_to :user has_many :comments end

Slide 29

Slide 29 text

Retrieving Data # Get a user by email UsersCollection.by_example(email: "[email protected]")

Slide 30

Slide 30 text

Identity Map

Slide 31

Slide 31 text

„Ensures that each object gets loaded only once by keeping every loaded object in a map. Looks up objects using the map when referring to them.“ – Martin Fowler

Slide 32

Slide 32 text

• Not a Cache! • Every session needs its own, fresh instance • Should be suited for concurrency (you know, just in case)

Slide 33

Slide 33 text

module Guacamole class IdentityMap class << self def store(object) @_map = _map.put(key_for(object), object) object end ! def retrieve(klass, key) _map.get key_for(klass, key) end ! def _map @_map ||= Hamster.hash end end end end

Slide 34

Slide 34 text

Lazy Loading

Slide 35

Slide 35 text

Post#user # Proxy UsersCollection.by_key(post_document[:user_key])

Slide 36

Slide 36 text

Active Model compliance

Slide 37

Slide 37 text

• Really useful, not only for Rails • A lot of Gems build on top of Active Model • A lot of useful features (i.e. Validations) • Not related to the Active Record pattern

Slide 38

Slide 38 text

Caveats

Slide 39

Slide 39 text

rom-rb/devtools

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Virtus and Circular Dependencies

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

class Author extend ActiveSupport::Autoload include Guacamole::Model ! autoload :Book, 'models/book' ! attribute :name, String attribute :books, Array[Book] end class Book extend ActiveSupport::Autoload include Guacamole::Model ! autoload :Author, 'models/author' ! attribute :title, String attribute :author, Author end This is not very Ruby

Slide 45

Slide 45 text

Next Steps

Slide 46

Slide 46 text

Support for Arango Query Language (AQL)

Slide 47

Slide 47 text

Support for Transactions

Slide 48

Slide 48 text

Support the Graph API

Slide 49

Slide 49 text

https://github.com/triAGENS/guacamole Get a Nacho and dip in

Slide 50

Slide 50 text

http://tfcl.de/ http://geekstammtisch.de/ https://github.com/railsbros-dirk Thanks