01: /**
02: * Sequoia: Database clustering technology.
03: * Copyright (C) 2002-2004 French National Institute For Research In Computer
04: * Science And Control (INRIA).
05: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
06: * Contact: sequoia@continuent.org
07: *
08: * Licensed under the Apache License, Version 2.0 (the "License");
09: * 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: */package org.continuent.sequoia.controller.loadbalancer.policies.createtable;
23:
24: import java.util.ArrayList;
25: import java.util.Random;
26:
27: /**
28: * Implements a random strategy for <code>CREATE TABLE</code> statements.
29: *
30: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
31: * @author <a href="mailto:jbvanzuylen@transwide.com">Jean-Bernard van Zuylen
32: * </a>
33: * @version 1.0
34: */
35: public class CreateTableRandom extends CreateTableRule {
36: private Random random = new Random();
37:
38: /**
39: * Creates a new <code>CreateTableRandom</code>.
40: */
41: public CreateTableRandom() {
42: super (CreateTablePolicy.RANDOM);
43: }
44:
45: /**
46: * Creates a new <code>CreateTableRandom</code>.
47: *
48: * @param backendList <code>ArrayList</code> of <code>DatabaseBackend</code>
49: */
50: public CreateTableRandom(ArrayList backendList) {
51: super (CreateTablePolicy.RANDOM, backendList);
52: }
53:
54: /**
55: * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getBackends(ArrayList)
56: */
57: public ArrayList getBackends(ArrayList backends)
58: throws CreateTableException {
59: if (nbOfNodes == 0)
60: return null;
61:
62: ArrayList clonedList = super .getBackends(backends);
63:
64: int clonedSize = clonedList.size();
65:
66: if (nbOfNodes == clonedSize)
67: return clonedList;
68: else if (nbOfNodes > clonedSize)
69: throw new CreateTableException("Asking for more backends ("
70: + nbOfNodes + ") than available (" + clonedSize
71: + ")");
72:
73: ArrayList result = new ArrayList(nbOfNodes);
74:
75: for (int i = 0; i < nbOfNodes; i++)
76: result.add(clonedList
77: .remove(random.nextInt(clonedSize - i)));
78:
79: return result;
80: }
81:
82: /**
83: * @see org.continuent.sequoia.controller.loadbalancer.policies.createtable.CreateTableRule#getInformation()
84: */
85: public String getInformation() {
86: String s;
87: if (tableName == null)
88: s = "Default rule create table on ";
89: else
90: s = "Rule for table " + tableName + " create table on ";
91:
92: return s + nbOfNodes + " node(s) randomly from " + backendList;
93: }
94: }
|