implement a data source pattern for a multi-model nosql ...€¦ · implement a data source pattern...

50
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

Upload: others

Post on 25-Aug-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Implement a Data Source Pattern for a Multi-Model NoSQL database

or

How to make Guacamole

16.4.2014 / cologne.rb / @mixxtDirk Breuer / @railsbros_dirk

Page 2: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Data Source Pattern

Architectural patterns which drive the way in which the domain logic

talks to the database.

– Martin Fowler

Page 3: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

• Active Record

• Data Mapper

• Table Data Gateway

• Row Data Gateway

• …

Page 4: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

NoSQL

Not ACID compliant

Big DataDistributed

Web Scale

ROFL Scale

CAP

Documents

Flexible

High Availability

Page 5: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

NoSQL Databases

• „Not only SQL“

• Not one definition

• Sometimes just a marketing buzz-fuzz

Page 6: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Classifications

• Document-based

• Key-Value stores

• Graph-based

• Column stores

• Time series

Aggregate-based}

Page 7: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Multi-Model

Combine more than one approach within one database system

Page 8: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Guacamole

Source: https://en.wikipedia.org/wiki/File:Guacamole.jpg

A dip consisting at least of ripe avocados and salt

Page 9: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

…and…

Page 10: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

gem 'guacamole'An ODM for ArangoDB to be used with Rails and other Ruby frameworks*

Object Document Mapper

*which is not yet finished 😜

Page 11: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

• Open-Source NoSQL database

• Multi-Model

• Document-based

• Graph-based

Page 12: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Why should I useArangoDB in favor of

<any other document store>?

With ArangoDB you can perform joinsover your documents similar

to joins in relational databases.

Page 13: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk
Page 14: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Before you start

Page 15: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Patterns of Enterprise Application Architecture

Page 16: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Ingredients

Page 17: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

1 Design Goal

1 Data Source Pattern

3 📦 Supporting Libraries

1 Database driver

1 solid Test-Suite

1 📦 Documentation

Page 18: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

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

Page 19: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Data Source Pattern

Page 20: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Data MapperIt’s a

Nein! – Doch! – Ohhh

h!

Page 21: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

+ Testability is increased

+ Separation of Concern

+ Easier to support databasefeatures like embedded objects

- The average Rails dev isused to Active Record

- Starting with Data Mapper requires more effort

Page 22: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Supporting Libraries

Page 23: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

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

Page 24: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Implementation

Page 25: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Just some bits and pieces…

Page 26: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

The Model

class Post include Guacamole::Model !

attribute :title, String attribute :body, String attribute :comments, Array[Comment] attribute :user, User end

Page 27: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

The Collection (a.k.a. The Mapper)

class PostsCollection include Guacamole::Collection !

map do embeds :comments references :user end end

Page 28: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

ActiveRecord equivalent

class Post < ActiveRecord belongs_to :user has_many :comments end

Page 29: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Retrieving Data

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

Page 30: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Identity Map

Page 31: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

„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

Page 32: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

• Not a Cache!

• Every session needs its own, fresh instance

• Should be suited for concurrency (you know, just in case)

Page 33: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

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

Page 34: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Lazy Loading

Page 35: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Post#user

#<User>

Proxy

UsersCollection.by_key(post_document[:user_key])

Page 36: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Active Model compliance

Page 37: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

• 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

Page 38: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Caveats

Page 39: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

rom-rb/devtools

Page 40: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk
Page 41: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk
Page 42: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Virtus and Circular Dependencies

Page 43: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk
Page 44: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

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

Page 45: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Next Steps

Page 46: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Support for Arango Query Language (AQL)

Page 47: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Support for Transactions

Page 48: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

Support the Graph API

Page 49: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

https://github.com/triAGENS/guacamole

Get a Nacho and dip in

Page 50: Implement a Data Source Pattern for a Multi-Model NoSQL ...€¦ · Implement a Data Source Pattern for a Multi-Model NoSQL database or How to make Guacamole Dirk Breuer / @railsbros_dirk

http://tfcl.de/

http://geekstammtisch.de/

https://github.com/railsbros-dirk

Thanks