Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / docs / design / AZURE_FEDERATIONS.md
1 # Azure Federations\r
2 \r
3 Implementing Federations inside a new Doctrine Sharding Extension. Some extensions to the DBAL and ORM core have to be done to get this working.\r
4 \r
5 1. DBAL (Database Abstraction Layer)\r
6 \r
7 * Add support for Database Schema Operations\r
8     * CREATE FEDERATION\r
9     * CREATE TABLE ... FEDERATED ON\r
10     * Add support to create a multi-tenent schema from any given schema\r
11 * Add API to pick a shard based on distribution key and atomic value\r
12 * Add API to ask about federations, federation members and so on.\r
13 * Add Sharding Abstraction\r
14     * If a shard is picked via distribution key and atomic value fire queries against this only\r
15     * Or query the global database.\r
16 \r
17 2. ORM (Object-Relational Mapper)\r
18 \r
19 * Federation Key has to be part of the clustered index of the table\r
20     * Test with a pure Multi-Tenent App with Filtering = ON (TaskList)\r
21     * Test with sharded app (Weather)\r
22 \r
23 ## Implementation Details\r
24 \r
25 SQL Azure requires one and exactly one clustered index. It makes no difference if the primary key\r
26 or any other key is the clustered index. Sharding requires an external ID generation (no auto-increment)\r
27 such as GUIDs. GUIDs have negative properties with regard to clustered index performance, so that\r
28 typically you would add a "created" timestamp for example that holds the clustered index instead\r
29 of making the GUID a clustered index.\r
30 \r
31 ## Example API:\r
32 \r
33     @@@ php\r
34     <?php\r
35     use Doctrine\DBAL\DriverManager;\r
36 \r
37     $dbParams = array(\r
38         'dbname' => 'tcp:dbname.database.windows.net',\r
39         'sharding' => array(\r
40             'federationName'   => 'Orders_Federation',\r
41             'distributionKey'  => 'CustID',\r
42             'distributionType' => 'integer',\r
43             'filteringEnabled' => false,\r
44         ),\r
45         // ...\r
46     );\r
47 \r
48     $conn = DriverManager::getConnection($dbParams);\r
49     $shardManager = $conn->getShardManager();\r
50 \r
51     // Example 1: query against root database\r
52     $sql = "SELECT * FROM Products";\r
53     $rows = $conn->executeQuery($sql);\r
54 \r
55     // Example 2:  query against the selected shard with CustomerId = 100\r
56     $aCustomerID = 100;\r
57     $shardManager->selectShard($aCustomerID); // Using Default federationName and distributionKey\r
58     // Query: "USE FEDERATION Orders_Federation (CustID = $aCustomerID) WITH RESET, FILTERING OFF;"\r
59 \r
60     $sql = "SELECT * FROM Customers";\r
61     $rows = $conn->executeQuery($sql);\r
62 \r
63     // Example 3: Reset API to root database again\r
64     $shardManager->selectGlobal();\r
65 \r
66 ## ID Generation\r
67 \r
68 With sharding all the ids have to be generated for global uniqueness. There are three strategies for this.\r
69 \r
70 1. Use GUIDs as described here http://blogs.msdn.com/b/cbiyikoglu/archive/2011/06/20/id-generation-in-federations-identity-sequences-and-guids-uniqueidentifier.aspx\r
71 2. Having a central table that is accessed with a second connection to generate sequential ids\r
72 3. Using natural keys from the domain.\r
73 \r
74 The second approach has the benefit of having numerical primary keys, however also a central failure location. The third strategy can seldom be used, because the domains dont allow this. Identity columns cannot be used at all.\r
75 \r
76     @@@ php\r
77     <?php\r
78     use Doctrine\DBAL\DriverManager;\r
79     use Doctrine\DBAL\Id\TableHiLoIdGenerator;\r
80 \r
81     $dbParams = array(\r
82         'dbname' => 'dbname.database.windows.net',\r
83         // ...\r
84     );\r
85     $conn = DriverManager::getConnection($dbParams);\r
86 \r
87     $idGenerator = new TableHiLoIdGenerator($conn, 'id_table_name', $multiplicator = 1);\r
88     // only once, create this table\r
89     $idGenerator->createTable();\r
90 \r
91     $nextId = $idGenerator->generateId('for_table_name');\r
92     $nextOtherId = $idGenerator->generateId('for_other_table');\r
93 \r
94 The connection for the table generator has to be a different one than the one used for the main app to avoid transaction clashes.\r