00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.controller.loadbalancer.policies.createtable;
00026
00027 import java.util.ArrayList;
00028 import java.util.Random;
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 public class CreateTableRandom extends CreateTableRule
00039 {
00040 private Random random = new Random();
00041
00042
00043
00044
00045 public CreateTableRandom()
00046 {
00047 super(CreateTablePolicy.RANDOM);
00048 }
00049
00050
00051
00052
00053
00054
00055 public CreateTableRandom(ArrayList backendList)
00056 {
00057 super(CreateTablePolicy.RANDOM, backendList);
00058 }
00059
00060
00061
00062
00063 public ArrayList getBackends(ArrayList backends) throws CreateTableException
00064 {
00065 if (nbOfNodes == 0)
00066 return null;
00067
00068 ArrayList clonedList = super.getBackends(backends);
00069
00070 int clonedSize = clonedList.size();
00071
00072 if (nbOfNodes == clonedSize)
00073 return clonedList;
00074 else if (nbOfNodes > clonedSize)
00075 throw new CreateTableException(
00076 "Asking for more backends ("
00077 + nbOfNodes
00078 + ") than available ("
00079 + clonedSize
00080 + ")");
00081
00082 ArrayList result = new ArrayList(nbOfNodes);
00083
00084 for (int i = 0; i < nbOfNodes; i++)
00085 result.add(clonedList.remove(random.nextInt(clonedSize - i)));
00086
00087 return result;
00088 }
00089
00090
00091
00092
00093 public String getInformation()
00094 {
00095 String s;
00096 if (tableName == null)
00097 s = "Default rule create table on ";
00098 else
00099 s = "Rule for table " + tableName + " create table on ";
00100
00101 return s + nbOfNodes + " node(s) randomly from " + backendList;
00102 }
00103 }