KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > loadbalancer > policies > createtable > CreateTableRoundRobin


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): Jean-Bernard van Zuylen
22  */

23
24 package org.continuent.sequoia.controller.loadbalancer.policies.createtable;
25
26 import java.util.ArrayList JavaDoc;
27
28 /**
29  * Implements a round-robin strategy for <code>CREATE TABLE</code>
30  * statements.
31  *
32  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
33  * @author <a HREF="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
34  * </a>
35  * @version 1.0
36  */

37 public class CreateTableRoundRobin extends CreateTableRule
38 {
39   private int index = 0;
40
41   /**
42    * Creates a new <code>CreateTableRoundRobin</code>.
43    */

44   public CreateTableRoundRobin()
45   {
46     super(CreateTablePolicy.ROUND_ROBIN);
47   }
48
49   /**
50    * Creates a new <code>CreateTableRoundRobin</code>.
51    *
52    * @param backendList <code>ArrayList</code> of <code>DatabaseBackend</code>
53    */

54   public CreateTableRoundRobin(ArrayList JavaDoc backendList)
55   {
56     super(CreateTablePolicy.ROUND_ROBIN, backendList);
57   }
58
59   /**
60    * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getBackends(ArrayList)
61    */

62   public ArrayList JavaDoc getBackends(ArrayList JavaDoc backends) throws CreateTableException
63   {
64     if (nbOfNodes == 0)
65       return null;
66     
67     ArrayList JavaDoc clonedList = super.getBackends(backends);
68     
69     int clonedSize = clonedList.size();
70
71     if (nbOfNodes == clonedSize)
72       return clonedList;
73     else if (nbOfNodes > clonedSize)
74       throw new CreateTableException(
75         "Asking for more backends ("
76           + nbOfNodes
77           + ") than available ("
78           + clonedSize
79           + ")");
80
81     ArrayList JavaDoc result = new ArrayList JavaDoc(nbOfNodes);
82
83     synchronized (this)
84     { // index must be modified in mutual exclusion
85
for (int i = 0; i < nbOfNodes; i++)
86       {
87         index = (index + 1) % clonedSize;
88         if (index - i < 0)
89           result.add(clonedList.remove(0));
90         else
91           result.add(clonedList.remove(index - i));
92       }
93     }
94
95     return result;
96   }
97
98   /**
99    * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getInformation()
100    */

101   public String JavaDoc getInformation()
102   {
103     String JavaDoc s;
104     if (tableName == null)
105       s = "Default rule create table on ";
106     else
107       s = "Rule for table " + tableName + " create table on ";
108
109     return s + nbOfNodes + " node(s) in round-robin from " + backendList;
110   }
111 }
112
Popular Tags