Graph databases in PHP @ PHPCon Poland 10-22-2011

163
1 David Funaro Alessandro Nadalin GraphDB in PHP domenica 23 ottobre 11

description

Presentation given at the national PHP conference in Poland, in Kielce, October 2011, dealing with the introduction of graph databases in PHP, taking a practical look at OrientDB.

Transcript of Graph databases in PHP @ PHPCon Poland 10-22-2011

Page 1: Graph databases in PHP @ PHPCon Poland 10-22-2011

1

David FunaroAlessandro Nadalin

GraphDB in PHP

domenica 23 ottobre 11

Page 2: Graph databases in PHP @ PHPCon Poland 10-22-2011

Agenda

2

•Theory•When to use a graph?•Why graphDB?•The graphDB community•OrientDB•OrientDB in PHP•Demo

domenica 23 ottobre 11

Page 3: Graph databases in PHP @ PHPCon Poland 10-22-2011

Essential (Theory)

3

domenica 23 ottobre 11

Page 4: Graph databases in PHP @ PHPCon Poland 10-22-2011

Essential (Theory)

3

Gra

phG =

domenica 23 ottobre 11

Page 5: Graph databases in PHP @ PHPCon Poland 10-22-2011

Essential (Theory)

3

Ver

tex

(V,G

raph

G =

domenica 23 ottobre 11

Page 6: Graph databases in PHP @ PHPCon Poland 10-22-2011

Essential (Theory)

A

3

Ver

tex

(V,G

raph

G =

domenica 23 ottobre 11

Page 7: Graph databases in PHP @ PHPCon Poland 10-22-2011

Essential (Theory)

A

3

Ver

tex

(V,G

raph

G =

Edg

e

E)

domenica 23 ottobre 11

Page 8: Graph databases in PHP @ PHPCon Poland 10-22-2011

Essential (Theory)

A

3

Ver

tex

(V,G

raph

G =

Edg

e

E)

domenica 23 ottobre 11

Page 9: Graph databases in PHP @ PHPCon Poland 10-22-2011

Binary Relation

4

BA

Hates

Itchy Scratchy

domenica 23 ottobre 11

Page 10: Graph databases in PHP @ PHPCon Poland 10-22-2011

Binary Relation

4

B

Vertex Vertex

Edge

A

domenica 23 ottobre 11

Page 11: Graph databases in PHP @ PHPCon Poland 10-22-2011

Graph

5

B

D

E

G

FA

domenica 23 ottobre 11

Page 12: Graph databases in PHP @ PHPCon Poland 10-22-2011

Undirected Graph

B

D

E

F

A

Example: Friendship 6

domenica 23 ottobre 11

Page 13: Graph databases in PHP @ PHPCon Poland 10-22-2011

Directed Edge

7

B

Vertex Vertex

A

domenica 23 ottobre 11

Page 14: Graph databases in PHP @ PHPCon Poland 10-22-2011

Directed Edge

7

B

Vertex Vertex

Edge

A

domenica 23 ottobre 11

Page 15: Graph databases in PHP @ PHPCon Poland 10-22-2011

Directed Graph

8Example: Followee

D

FA

BA

domenica 23 ottobre 11

Page 16: Graph databases in PHP @ PHPCon Poland 10-22-2011

Path

9

B

D

E

G

FA

domenica 23 ottobre 11

Page 17: Graph databases in PHP @ PHPCon Poland 10-22-2011

Path

10

B D EG FA

domenica 23 ottobre 11

Page 18: Graph databases in PHP @ PHPCon Poland 10-22-2011

Graph -> GraphDB

11

GraphDB is a database that use the Graph as its primary data structure

domenica 23 ottobre 11

Page 19: Graph databases in PHP @ PHPCon Poland 10-22-2011

... when to use a graph ?

domenica 23 ottobre 11

Page 20: Graph databases in PHP @ PHPCon Poland 10-22-2011

Web in ’99

13

domenica 23 ottobre 11

Page 21: Graph databases in PHP @ PHPCon Poland 10-22-2011

Web in 2005

14

domenica 23 ottobre 11

Page 22: Graph databases in PHP @ PHPCon Poland 10-22-2011

The social web

15

domenica 23 ottobre 11

Page 23: Graph databases in PHP @ PHPCon Poland 10-22-2011

Your data is a graph

16

domenica 23 ottobre 11

Page 24: Graph databases in PHP @ PHPCon Poland 10-22-2011

a tree is a graph

17

domenica 23 ottobre 11

Page 25: Graph databases in PHP @ PHPCon Poland 10-22-2011

parent_id is a graph

18

domenica 23 ottobre 11

Page 26: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

19

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows

domenica 23 ottobre 11

Page 27: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

20

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x domenica 23 ottobre 11

Page 28: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

21

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

domenica 23 ottobre 11

Page 29: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

22

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

✓ ✓

domenica 23 ottobre 11

Page 30: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

23

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

✓ ✓ x

domenica 23 ottobre 11

Page 31: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

24

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

x x x ✓

domenica 23 ottobre 11

Page 32: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

25

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

x x x ✓ ✓

domenica 23 ottobre 11

Page 33: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

26

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

x x x ✓ ✓ ✓

domenica 23 ottobre 11

Page 34: Graph databases in PHP @ PHPCon Poland 10-22-2011

Recommendations

27

John

Rome

Milan

Cinema A

Cinema B

Cinema C

Se7en

Mr Bean

Thriller

Fun

lives in

location

location

location

type

type

likes

shows

shows

shows x

x

x x x ✓ ✓ ✓ ✓

domenica 23 ottobre 11

Page 35: Graph databases in PHP @ PHPCon Poland 10-22-2011

Solve decision problems

domenica 23 ottobre 11

Page 36: Graph databases in PHP @ PHPCon Poland 10-22-2011

Maximum flow

domenica 23 ottobre 11

Page 37: Graph databases in PHP @ PHPCon Poland 10-22-2011

domenica 23 ottobre 11

Page 38: Graph databases in PHP @ PHPCon Poland 10-22-2011

maximum flowGiven a dataset, calculate how to best organize it

domenica 23 ottobre 11

Page 39: Graph databases in PHP @ PHPCon Poland 10-22-2011

travelling salesman problem

domenica 23 ottobre 11

Page 40: Graph databases in PHP @ PHPCon Poland 10-22-2011

The pizza guy needs to deliver on A, B,C.

domenica 23 ottobre 11

Page 41: Graph databases in PHP @ PHPCon Poland 10-22-2011

Decision base on distance, traffic, time and so on.

domenica 23 ottobre 11

Page 42: Graph databases in PHP @ PHPCon Poland 10-22-2011

Shortest pathdomenica 23 ottobre 11

Page 43: Graph databases in PHP @ PHPCon Poland 10-22-2011

Identify "special" nodes of the graph

domenica 23 ottobre 11

Page 44: Graph databases in PHP @ PHPCon Poland 10-22-2011

Given your dataset, organize some clusters

Are there some nodes which cannot belong to a cluster?

They probably have some properties different from the average

domenica 23 ottobre 11

Page 45: Graph databases in PHP @ PHPCon Poland 10-22-2011

Given your dataset, organize some clusters

Are there some nodes which cannot belong to a cluster?

They probably have some properties different from the average

ACHTUNG!TERRORISTEN!

domenica 23 ottobre 11

Page 46: Graph databases in PHP @ PHPCon Poland 10-22-2011

but ... why graphDB?

38

domenica 23 ottobre 11

Page 49: Graph databases in PHP @ PHPCon Poland 10-22-2011

where is the difference ?

40

domenica 23 ottobre 11

Page 50: Graph databases in PHP @ PHPCon Poland 10-22-2011

A graph database is any storage system that provides index-free adjacency.

GraphDB

http://www.slideshare.net/slidarko/problemsolving-using-graph-traversals-searching-scoring-ranking-and-recommendation

domenica 23 ottobre 11

Page 51: Graph databases in PHP @ PHPCon Poland 10-22-2011

Step by step example

42

Given a list of people, find their homepages

domenica 23 ottobre 11

Page 52: Graph databases in PHP @ PHPCon Poland 10-22-2011

Tree-based DB WAY

43

1

domenica 23 ottobre 11

Page 53: Graph databases in PHP @ PHPCon Poland 10-22-2011

Tree-based DB WAY

43

1

David Funaro

put in the Search Engine2

domenica 23 ottobre 11

Page 54: Graph databases in PHP @ PHPCon Poland 10-22-2011

Tree-based DB WAY

43

1

find

http://davidfunaro.com

3

David Funaro

put in the Search Engine2

domenica 23 ottobre 11

Page 55: Graph databases in PHP @ PHPCon Poland 10-22-2011

Tree-based DB WAY

43

1

find

http://davidfunaro.com

3

David Funaro

put in the Search Engine2

The cost to find a single friend HP grows as the friends HP tables grows

domenica 23 ottobre 11

Page 56: Graph databases in PHP @ PHPCon Poland 10-22-2011

GraphDB WAY

44

it’s like that the GraphDB has an additional information(the ancor <a>)

domenica 23 ottobre 11

Page 57: Graph databases in PHP @ PHPCon Poland 10-22-2011

GraphDB WAY

44

get the embedded information(index)

www.odino.org

1

it’s like that the GraphDB has an additional information(the ancor <a>)

domenica 23 ottobre 11

Page 58: Graph databases in PHP @ PHPCon Poland 10-22-2011

GraphDB WAY

45

<a href=”http://odino.org”>Alessandro Nadalin

</a>

The Anchor work as a local index to reach the document = index-free

adjacency

domenica 23 ottobre 11

Page 59: Graph databases in PHP @ PHPCon Poland 10-22-2011

Local cost

46

The local cost is O(k) = Constant

domenica 23 ottobre 11

Page 60: Graph databases in PHP @ PHPCon Poland 10-22-2011

Local cost

47

The local cost is O(k) = Constant

domenica 23 ottobre 11

Page 61: Graph databases in PHP @ PHPCon Poland 10-22-2011

Local cost

48

domenica 23 ottobre 11

Page 62: Graph databases in PHP @ PHPCon Poland 10-22-2011

Local cost

48

Thus, as the graph grows in size, the cost of a local step remain the same

domenica 23 ottobre 11

Page 63: Graph databases in PHP @ PHPCon Poland 10-22-2011

any database can implicity represent a graph

BUTonly a graph database make the graph

structure explicit

49

domenica 23 ottobre 11

Page 64: Graph databases in PHP @ PHPCon Poland 10-22-2011

Benchmark

50

• 1 Million Vertex

• 4 Million Edge

• Scale Free Tolopogy

• Postgres VS Neo4J

• Both Hash and BTree

Deph RDBMS Graph

1

2

3

4

5

100ms 30ms

1000ms 500ms

10000ms 3000ms

100000ms

50000ms

N/A 100000ms

http://markorodriguez.com/2011/02/18/mysql-vs-neo4j-on-a-large-scale-graph-traversal/

domenica 23 ottobre 11

Page 65: Graph databases in PHP @ PHPCon Poland 10-22-2011

Databases

community that is building and feeding the GraphDB ecosystem

ThinkerPopStack

GraphDB community

domenica 23 ottobre 11

Page 66: Graph databases in PHP @ PHPCon Poland 10-22-2011

Blueprints is a collection of interfaces, implementations, ouplementations, and test suites for the property graph data

model. Blueprints is analogous to the JDBC, but for graph databases.

https://github.com/tinkerpop/blueprints/wiki/

data model and their implementation

domenica 23 ottobre 11

Page 67: Graph databases in PHP @ PHPCon Poland 10-22-2011

provide a collection of "pipes" that are connected togheter to from processing

pipelines

a data flow Framework using Process Graph

domenica 23 ottobre 11

Page 68: Graph databases in PHP @ PHPCon Poland 10-22-2011

a graph-based programming language.

a Turing-Complete graph-base programming language that compiles Gremlin syntax down to Pipes

domenica 23 ottobre 11

Page 69: Graph databases in PHP @ PHPCon Poland 10-22-2011

a REST-full graph shell.

Allow blueprints graph to be exposed through a RESTful API (HTTP)

domenica 23 ottobre 11

Page 70: Graph databases in PHP @ PHPCon Poland 10-22-2011

What's hot

domenica 23 ottobre 11

Page 71: Graph databases in PHP @ PHPCon Poland 10-22-2011

OrientDB

domenica 23 ottobre 11

Page 72: Graph databases in PHP @ PHPCon Poland 10-22-2011

Glossary

58

<10:05>RID

Cluster Position

domenica 23 ottobre 11

Page 73: Graph databases in PHP @ PHPCon Poland 10-22-2011

Glossary

58

<10:05>RID

Cluster Position

CLASS

domenica 23 ottobre 11

Page 74: Graph databases in PHP @ PHPCon Poland 10-22-2011

Main features

domenica 23 ottobre 11

Page 75: Graph databases in PHP @ PHPCon Poland 10-22-2011

Inheritance

domenica 23 ottobre 11

Page 76: Graph databases in PHP @ PHPCon Poland 10-22-2011

class Bike

class Vehicle

class Car

domenica 23 ottobre 11

Page 77: Graph databases in PHP @ PHPCon Poland 10-22-2011

class Bike

class Vehicle

class Car

SELECT FROM Vehicle WHERE owner = 1:1

domenica 23 ottobre 11

Page 78: Graph databases in PHP @ PHPCon Poland 10-22-2011

class Bike

class Vehicle

class Car

can return records of class Bike or Car

domenica 23 ottobre 11

Page 79: Graph databases in PHP @ PHPCon Poland 10-22-2011

Traversal

domenica 23 ottobre 11

Page 80: Graph databases in PHP @ PHPCon Poland 10-22-2011

domenica 23 ottobre 11

Page 81: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM fellas WHERE any() traverse(0,-1) ( @rid = [Michelle @rid] )66

domenica 23 ottobre 11

Page 82: Graph databases in PHP @ PHPCon Poland 10-22-2011

67SELECT FROM fellas WHERE any() traverse(0,-1) ( @rid = [Michelle @rid] )

domenica 23 ottobre 11

Page 83: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM fellas WHERE any() traverse(0,2) ( @rid = [Michelle @rid] )SELECT FROM fellas WHERE any() traverse(0,2) ( @rid = [Michelle @rid] )

domenica 23 ottobre 11

Page 84: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM fellas WHERE any() traverse(0,2) ( @rid = [Michelle @rid] )

domenica 23 ottobre 11

Page 85: Graph databases in PHP @ PHPCon Poland 10-22-2011

SQL synthax

domenica 23 ottobre 11

Page 86: Graph databases in PHP @ PHPCon Poland 10-22-2011

beyond SQL

domenica 23 ottobre 11

Page 87: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM authors WHERE book.title = ...

domenica 23 ottobre 11

Page 88: Graph databases in PHP @ PHPCon Poland 10-22-2011

ACIDdomenica 23 ottobre 11

Page 89: Graph databases in PHP @ PHPCon Poland 10-22-2011

speaks JSON

domenica 23 ottobre 11

Page 90: Graph databases in PHP @ PHPCon Poland 10-22-2011

{ "schema": { "name": "Address" }, "result": [{ "@type": "d", "@rid": "#13:0", "@version": 6, "@class": "Address", "type": "Residence", "street": "Piazza Navona, 1", "city": "#14:0", "nick": "Luca2" }, { ... ...

domenica 23 ottobre 11

Page 91: Graph databases in PHP @ PHPCon Poland 10-22-2011

Double Protocol

domenica 23 ottobre 11

Page 92: Graph databases in PHP @ PHPCon Poland 10-22-2011

HTTP

domenica 23 ottobre 11

Page 93: Graph databases in PHP @ PHPCon Poland 10-22-2011

HTTP

Universal

domenica 23 ottobre 11

Page 94: Graph databases in PHP @ PHPCon Poland 10-22-2011

HTTP

Easy to interact with

domenica 23 ottobre 11

Page 95: Graph databases in PHP @ PHPCon Poland 10-22-2011

binary

domenica 23 ottobre 11

Page 96: Graph databases in PHP @ PHPCon Poland 10-22-2011

Blazing fast

binary

domenica 23 ottobre 11

Page 97: Graph databases in PHP @ PHPCon Poland 10-22-2011

on-record SELECTs

domenica 23 ottobre 11

Page 98: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM cats

domenica 23 ottobre 11

Page 99: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM cats

domenica 23 ottobre 11

Page 100: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM 11:0

domenica 23 ottobre 11

Page 101: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM 11:0

domenica 23 ottobre 11

Page 102: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM [11:0,11:1]

domenica 23 ottobre 11

Page 103: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM [11:0,11:1]

domenica 23 ottobre 11

Page 104: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM [11:0,12:0]

domenica 23 ottobre 11

Page 105: Graph databases in PHP @ PHPCon Poland 10-22-2011

SELECT FROM [11:0,12:0]

domenica 23 ottobre 11

Page 106: Graph databases in PHP @ PHPCon Poland 10-22-2011

stress-free setupdomenica 23 ottobre 11

Page 107: Graph databases in PHP @ PHPCon Poland 10-22-2011

2 Mb

domenica 23 ottobre 11

Page 108: Graph databases in PHP @ PHPCon Poland 10-22-2011

./orient/bin/server.sh

93

domenica 23 ottobre 11

Page 109: Graph databases in PHP @ PHPCon Poland 10-22-2011

in-memory DB

domenica 23 ottobre 11

Page 110: Graph databases in PHP @ PHPCon Poland 10-22-2011

or disk-persisted

domenica 23 ottobre 11

Page 111: Graph databases in PHP @ PHPCon Poland 10-22-2011

Supports standards Supports standards

96

domenica 23 ottobre 11

Page 112: Graph databases in PHP @ PHPCon Poland 10-22-2011

OrientDB

•Inheritance

•Traversal

•Sql syntax like

•ACID

•Speak JSON

•Double protocol

•on-record Select

•ThinkerPop Compliant

domenica 23 ottobre 11

Page 113: Graph databases in PHP @ PHPCon Poland 10-22-2011

Oh, it's Java.

98

domenica 23 ottobre 11

Page 114: Graph databases in PHP @ PHPCon Poland 10-22-2011

PHP ?

domenica 23 ottobre 11

Page 115: Graph databases in PHP @ PHPCon Poland 10-22-2011

somebody started writing thebinary-protocol binding

https://github.com/AntonTerekhov/OrientDB-PHP( beta0.4.1, 28 April 2010 )

domenica 23 ottobre 11

Page 116: Graph databases in PHP @ PHPCon Poland 10-22-2011

$db = new OrientDB($host, $port);

$record = $db->recordLoad('1:1', '*:-1');

// $record instance of OrientDBRecord

domenica 23 ottobre 11

Page 117: Graph databases in PHP @ PHPCon Poland 10-22-2011

and others

domenica 23 ottobre 11

Page 118: Graph databases in PHP @ PHPCon Poland 10-22-2011

domenica 23 ottobre 11

Page 119: Graph databases in PHP @ PHPCon Poland 10-22-2011

Orient Library

104

... are writing a complete library

https://github.com/congow/Orient

domenica 23 ottobre 11

Page 120: Graph databases in PHP @ PHPCon Poland 10-22-2011

Orient = PHP Library to work with OrientDB

105

domenica 23 ottobre 11

Page 121: Graph databases in PHP @ PHPCon Poland 10-22-2011

Data Mapper

Query BuilderHTTP Binding

domenica 23 ottobre 11

Page 122: Graph databases in PHP @ PHPCon Poland 10-22-2011

HTTP Binding

domenica 23 ottobre 11

Page 123: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient;use Congow\Orient\Foundation\Binding;

$driver   = new Orient\Http\Client\Curl();$orient   = new Binding($driver, '127.0.0.1', '2480', 'admin', 'admin', 'demo');

$response = $orient->query("SELECT FROM Address");

$output   = json_decode($response->getBody());

foreach ($output->result as $address){  var_dump($address->street);}

domenica 23 ottobre 11

Page 124: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient;use Congow\Orient\Foundation\Binding;

$driver   = new Orient\Http\Client\Curl();$orient   = new Binding($driver, '127.0.0.1', '2480', 'admin', 'admin', 'demo');

$response = $orient->query("SELECT FROM Address");

$output   = json_decode($response->getBody());

foreach ($output->result as $address){  var_dump($address->street);}

domenica 23 ottobre 11

Page 125: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient;use Congow\Orient\Foundation\Binding;

$driver   = new Orient\Http\Client\Curl();$orient   = new Binding($driver, '127.0.0.1', '2480', 'admin', 'admin', 'demo');

$response = $orient->query("SELECT FROM Address");

$output   = json_decode($response->getBody());

foreach ($output->result as $address){  var_dump($address->street);}

domenica 23 ottobre 11

Page 126: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient;use Congow\Orient\Foundation\Binding;

$driver   = new Orient\Http\Client\Curl();$orient   = new Binding($driver, '127.0.0.1', '2480', 'admin', 'admin', 'demo');

$response = $orient->query("SELECT FROM Address");

$output   = json_decode($response->getBody());

foreach ($output->result as $address){  var_dump($address->street);}

domenica 23 ottobre 11

Page 127: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient;use Congow\Orient\Foundation\Binding;

$driver   = new Orient\Http\Client\Curl();$orient   = new Binding($driver, '127.0.0.1', '2480', 'admin', 'admin', 'demo');

$response = $orient->query("SELECT FROM Address");

$output   = json_decode($response->getBody());

foreach ($output->result as $address){  var_dump($address->street);}

{ "schema": { "name": "Address" }, "result": [{ "@type": "d", "@rid": "#13:0", "@version": 6, "@class": "Address", "type": "Residence", "street": "Piazza Navona, 1", "city": "#14:0", "nick": "Luca2" }, { ... ...

domenica 23 ottobre 11

Page 128: Graph databases in PHP @ PHPCon Poland 10-22-2011

apart from ->query($SQL)

domenica 23 ottobre 11

Page 129: Graph databases in PHP @ PHPCon Poland 10-22-2011

->get|delete|postClass($class)

domenica 23 ottobre 11

Page 130: Graph databases in PHP @ PHPCon Poland 10-22-2011

->post|delete|put|getDocument($rid)

domenica 23 ottobre 11

Page 131: Graph databases in PHP @ PHPCon Poland 10-22-2011

...and much more!

(connect, disconnect, ...)

domenica 23 ottobre 11

Page 132: Graph databases in PHP @ PHPCon Poland 10-22-2011

Query Builder

domenica 23 ottobre 11

Page 133: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient\Query;

$query = new Query();$query->from(array('users'))->where('username = ?', "admin");

echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"

domenica 23 ottobre 11

Page 134: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient\Query;

$query = new Query();$query->from(array('users'))->where('username = ?', "admin");

echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"

domenica 23 ottobre 11

Page 135: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient\Query;

$query = new Query();$query->from(array('users'))->where('username = ?', "admin");

echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"

domenica 23 ottobre 11

Page 136: Graph databases in PHP @ PHPCon Poland 10-22-2011

use Congow\Orient\Query;

$query = new Query();$query->from(array('users'))->where('username = ?', "admin");

echo $query->getRaw(); // SELECT FROM users WHERE username = "admin"

domenica 23 ottobre 11

Page 137: Graph databases in PHP @ PHPCon Poland 10-22-2011

               $query->select(array('name', 'username', 'email'), false)                ->from(array('12:0', '12:1'), false)                ->where('any() traverse ( any() like "%danger%" )')                ->orWhere("1 = ?", 1)                ->andWhere("links = ?", 1)                ->limit(20)                ->orderBy('username')                ->orderBy('name', true, true)                ->range("12:0", "12:1");

              SELECT name, username, email               FROM [12:0, 12:1]               WHERE any() traverse ( any() like "%danger%" )              OR 1 = "1" AND links = "1"               ORDER BY name, username               LIMIT 20               RANGE 12:0 12:1

domenica 23 ottobre 11

Page 138: Graph databases in PHP @ PHPCon Poland 10-22-2011

Data Mapper

domenica 23 ottobre 11

Page 139: Graph databases in PHP @ PHPCon Poland 10-22-2011

A Doctrine2 strange ODM

domenica 23 ottobre 11

Page 140: Graph databases in PHP @ PHPCon Poland 10-22-2011

namespace Poland\PHPCon\Entity;

use Congow\Orient\ODM\Mapper\Annotations as ODM;

/*** @ODM\Document(class="Person")*/class Speaker{    /**     * @ODM\Property( type="string")     */    protected $name;

    public function setName($name)    {        $this->name = $name;    }

domenica 23 ottobre 11

Page 141: Graph databases in PHP @ PHPCon Poland 10-22-2011

namespace Poland\PHPCon\Entity;

use Congow\Orient\ODM\Mapper\Annotations as ODM;

/*** @ODM\Document(class="Person")*/class Speaker{    /**     * @ODM\Property(type="string")     */    protected $name;

    public function setName($name)    {        $this->name = $name;    }

domenica 23 ottobre 11

Page 142: Graph databases in PHP @ PHPCon Poland 10-22-2011

namespace Poland\PHPCon\Entity;

use Congow\Orient\ODM\Mapper\Annotations as ODM;

/*** @ODM\Document(class="Person")*/class Speaker{    /**     * @ODM\Property(type="string")     */    protected $name;

    public function setName($name)    {        $this->name = $name;    }

domenica 23 ottobre 11

Page 143: Graph databases in PHP @ PHPCon Poland 10-22-2011

namespace Poland\PHPCon\Entity;

use Congow\Orient\ODM\Mapper\Annotations as ODM;

/*** @ODM\Document(class="Person")*/class Speaker{    /**     * @ODM\Property(type="string")     */    protected $name;

    public function setName($name)    {        $this->name = $name;    }

domenica 23 ottobre 11

Page 144: Graph databases in PHP @ PHPCon Poland 10-22-2011

Domain Driven Design

domenica 23 ottobre 11

Page 145: Graph databases in PHP @ PHPCon Poland 10-22-2011

{ "schema": { "name": "Speaker" }, "result": [{ "@type": "d", "@rid": "#1:0", "@version": 6, "@class": "Speaker", "name": "David Coallier" }, { ... ...

domenica 23 ottobre 11

Page 146: Graph databases in PHP @ PHPCon Poland 10-22-2011

{ "schema": { "name": "Speaker" }, "result": [{ "@type": "d", "@rid": "#1:0", "@version": 6, "@class": "Speaker", "name": "David Coallier" }, { ... ...

$david = $mapper->hydrate(json_decode($speaker));

domenica 23 ottobre 11

Page 147: Graph databases in PHP @ PHPCon Poland 10-22-2011

{ "schema": { "name": "Speaker" }, "result": [{ "@type": "d", "@rid": "#1:0", "@version": 6, "@class": "Speaker", "name": "David Coallier" }, { ... ...

$david instanceOf Poland\PHPCon\Entity\Speaker

domenica 23 ottobre 11

Page 148: Graph databases in PHP @ PHPCon Poland 10-22-2011

Repository Pattern

$repo = $manager->getRepository('Speaker')

domenica 23 ottobre 11

Page 149: Graph databases in PHP @ PHPCon Poland 10-22-2011

$speakers = $repo->findAll();

domenica 23 ottobre 11

Page 150: Graph databases in PHP @ PHPCon Poland 10-22-2011

$speaker = $repo->find($rid);

domenica 23 ottobre 11

Page 151: Graph databases in PHP @ PHPCon Poland 10-22-2011

$criteria = array('Name' => 'Lorna');

$lornas = $repo->findBy($criteria);

domenica 23 ottobre 11

Page 152: Graph databases in PHP @ PHPCon Poland 10-22-2011

$criteria = array( 'Name' => 'Lorna', 'last_name' => 'Jane');

$lornaJ = $repo->findOneBy($criteria);

domenica 23 ottobre 11

Page 153: Graph databases in PHP @ PHPCon Poland 10-22-2011

Know your boundaries

138

domenica 23 ottobre 11

Page 154: Graph databases in PHP @ PHPCon Poland 10-22-2011

https://github.com/doctrine/common/tree/master/lib/Doctrine/Common/Persistence

139

domenica 23 ottobre 11

Page 155: Graph databases in PHP @ PHPCon Poland 10-22-2011

Theory sucks.

140

domenica 23 ottobre 11

Page 156: Graph databases in PHP @ PHPCon Poland 10-22-2011

Demo

domenica 23 ottobre 11

Page 157: Graph databases in PHP @ PHPCon Poland 10-22-2011

Demo

142

id type page url

1 external NULL http://www.google.com

2 page 1 NULL

Menu items in RDBMS

domenica 23 ottobre 11

Page 158: Graph databases in PHP @ PHPCon Poland 10-22-2011

Demo

143

rid title url

8:2 google google.com

Menu items in OrientDB

rid title page

9:1 home 1{ Link

PageLink ExternalLink

domenica 23 ottobre 11

Page 159: Graph databases in PHP @ PHPCon Poland 10-22-2011

144

That’s all, folks!

domenica 23 ottobre 11

Page 160: Graph databases in PHP @ PHPCon Poland 10-22-2011

144

David Funaro@ingdavidinohttp://davidfunaro.com

That’s all, folks!

domenica 23 ottobre 11

Page 161: Graph databases in PHP @ PHPCon Poland 10-22-2011

144

David Funaro@ingdavidinohttp://davidfunaro.com

Alessandro Nadalin@_odino_

http://odino.org

That’s all, folks!

domenica 23 ottobre 11

Page 162: Graph databases in PHP @ PHPCon Poland 10-22-2011

144

David Funaro@ingdavidinohttp://davidfunaro.com

Alessandro Nadalin@_odino_

http://odino.org

That’s all, folks!

domenica 23 ottobre 11

Page 163: Graph databases in PHP @ PHPCon Poland 10-22-2011

Credits

http://www.flickr.com/photos/sayamindu/5677281218/sizes/l/in/photostream/http://farm1.static.flickr.com/182/471383865_79d04aec36_o.pnghttp://farm1.static.flickr.com/134/318947873_12028f1b66_b.jpg

http://www.flickr.com/photos/atomdocs/3275758118/sizes/o/in/photostream/http://www.flickr.com/photos/pattipics/5229478393/sizes/o/in/photostream/

http://www.flickr.com/photos/kongharald/366597251/sizes/o/in/photostream/http://www.everaldo.com/

http://www.flickr.com/photos/tusnelda/6140792529/sizes/l/in/photostream/http://www.flickr.com/photos/mondi/5368644355/sizes/l/in/photostream/

http://www.flickr.com/photos/jayneandd/4191106566/sizes/l/in/photostream/http://www.flickr.com/photos/jooon/2093253534/sizes/l/in/photostream/

http://www.flickr.com/photos/bluedharma/89186151/sizes/o/in/photostream/http://www.flickr.com/photos/exfordy/2747089295/sizes/l/in/photostream/

http://www.flickr.com/photos/nostri-imago/3137422976/sizes/o/in/photostream/http://www.flickr.com/photos/fionasjournal/379587818/sizes/z/in/photostream/

http://www.flickr.com/photos/nperlapro/1297392267/http://www.flickr.com/photos/fastphive/28428808/sizes/m/in/photostream/

http://www.flickr.com/photos/rnugraha/2003147365/sizes/o/in/photostream/http://www.flickr.com/photos/zigazou76/4412946911/sizes/l/in/photostream/http://www.flickr.com/photos/greatnet/4667555436/sizes/l/in/photostream/

http://www.flickr.com/photos/mnsc/2768391365/sizes/l/in/photostream/http://www.flickr.com/photos/christmaswithak/4675962453/sizes/l/in/photostream/

http://www.amazon.com/Trainspotting-Irvine-Welsh/dp/0393314804http://www.flickr.com/photos/franconadalin59/5778176872/sizes/l/in/photostream/

http://farm6.static.flickr.com/5176/5474445627_875d621689_b.jpghttp://farm3.static.flickr.com/2243/2189435082_a16d3c89ae_b.jpghttp://farm3.static.flickr.com/2647/3816311930_ac52cff491_o.jpg

http://i130.photobucket.com/albums/p266/feike1977/PES6-4-3-3defencesettings.jpghttp://images.usatoday.com/life/_photos/2006/11/30/numb3rs-topper.jpg

http://www.flickr.com/photos/jakecaptive/3205277810/sizes/l/in/photostream/

domenica 23 ottobre 11