KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.Random JavaDoc;
28
29 /**
30  * Implements a random strategy for <code>CREATE TABLE</code> 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 CreateTableRandom extends CreateTableRule
38 {
39   private Random JavaDoc random = new Random JavaDoc();
40
41   /**
42    * Creates a new <code>CreateTableRandom</code>.
43    */

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

54   public CreateTableRandom(ArrayList JavaDoc backendList)
55   {
56     super(CreateTablePolicy.RANDOM, 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     for (int i = 0; i < nbOfNodes; i++)
84       result.add(clonedList.remove(random.nextInt(clonedSize - i)));
85
86     return result;
87   }
88
89   /**
90    * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getInformation()
91    */

92   public String JavaDoc getInformation()
93   {
94     String JavaDoc s;
95     if (tableName == null)
96       s = "Default rule create table on ";
97     else
98       s = "Rule for table " + tableName + " create table on ";
99
100     return s + nbOfNodes + " node(s) randomly from " + backendList;
101   }
102 }
103
Popular Tags