1 24 package edu.rice.rubis.client; 25 26 import java.io.File ; 27 import java.io.BufferedInputStream ; 28 import java.io.IOException ; 29 import java.lang.Thread ; 30 import java.lang.reflect.Array ; 31 import java.net.URL ; 32 import java.util.Random ; 33 34 41 42 public class InitDB 43 { 44 private URLGenerator urlGen = null; 45 private Random rand = new Random (); 46 private RUBiSProperties rubis = null; 47 private int[] itemsPerCategory; 48 49 50 54 public InitDB() 55 { 56 rubis = new RUBiSProperties(); 57 urlGen = rubis.checkPropertiesFileAndGetURLGenerator(); 58 if (urlGen == null) 59 Runtime.getRuntime().exit(1); 60 itemsPerCategory = rubis.getItemsPerCategory(); 61 } 62 63 64 74 public static void main(String [] args) 75 { 76 System.out.println("RUBiS database initialization - (C) Rice University/INRIA 2001\n"); 77 78 InitDB initDB = new InitDB(); 79 int argc = Array.getLength(args); 80 String params = ""; 81 82 if (argc == 0) 83 { 84 System.out.println("Command line : java -classpath .:./database edu.rice.rubis.client.InitDB parameters"); 85 System.out.println("Using Makefile: make initDB PARAM=\"parameters\""); 86 System.out.println("where parameter is one or any combination of the following arguments:"); 87 System.out.println(" all: generate the complete database"); 88 System.out.println(" users: generate only users"); 89 System.out.println(" items: generate only items"); 90 System.out.println(" bids: generate bids and items (it is not possible to create bids without creating the related items)"); 91 System.out.println(" comments: generate comments and items (it is not possible to create comments without creating the related items)"); 92 Runtime.getRuntime().exit(1); 93 } 94 95 for (int i = 0 ; i < argc ; i++) 96 params = params +" "+ args[i]; 97 98 if ((params.indexOf("users") != -1) || (params.indexOf("all") != -1)) 99 initDB.generateUsers(); 100 101 if ((params.indexOf("items") != -1) || (params.indexOf("bids") != -1) || 102 (params.indexOf("comments") != -1) || (params.indexOf("all") != -1)) 103 initDB.generateItems((params.indexOf("bids") != -1) || (params.indexOf("all") != -1), (params.indexOf("comments") != -1) || (params.indexOf("all") != -1)); 104 } 105 106 107 111 public void generateUsers() 112 { 113 String firstname; 114 String lastname; 115 String nickname; 116 String email; 117 String password; 118 String regionName; 119 String HTTPreply; 120 int i; 121 URL url; 122 123 int getNbOfUsers = rubis.getNbOfUsers(); 125 int getNbOfRegions = rubis.getNbOfRegions(); 126 127 System.out.print("Generating "+getNbOfUsers+" users "); 128 for (i = 0 ; i < getNbOfUsers ; i++) 129 { 130 firstname = "Great"+(i+1); 131 lastname = "User"+(i+1); 132 nickname = "user"+(i+1); 133 email = firstname+"."+lastname+"@rubis.com"; 134 password = "password"+(i+1); 135 regionName = (String )rubis.getRegions().elementAt(i % getNbOfRegions); 136 137 url = urlGen.registerUser(firstname, lastname, nickname, email, password, regionName); 139 HTTPreply = callHTTPServer(url); 140 if (HTTPreply.indexOf("ERROR") != -1) 141 { 142 System.err.println("Failed to add user "+firstname+"|"+lastname+"|"+nickname+"|"+email+"|"+password+"|"+regionName); 143 System.err.println(HTTPreply); 144 } 145 if (i % 100 == 0) 146 System.out.print("."); 147 } 148 System.out.println(" Done!"); 149 } 150 151 152 156 public void generateItems(boolean generateBids, boolean generateComments) 157 { 158 String name; 160 String description; 161 float initialPrice; 162 float reservePrice; 163 float buyNow; 164 int duration; 165 int quantity; 166 int categoryId; 167 int sellerId; 168 int oldItems = rubis.getNbOfOldItems(); 169 int activeItems = rubis.getTotalActiveItems(); 170 int totalItems = oldItems + activeItems; 171 String staticDescription = "This incredible item is exactly what you need !<br>It has a lot of very nice features including "+ 172 "a coffee option.<p>It comes with a free license for the free RUBiS software, that's really cool. But RUBiS even if it "+ 173 "is free, is <B>(C) Rice University/INRIA 2001</B>. It is really hard to write an interesting generic description for "+ 174 "automatically generated items, but who will really read this ?<p>You can also check some cool software available on "+ 175 "http://sci-serv.inrialpes.fr. There is a very cool DSM system called SciFS for SCI clusters, but you will need some "+ 176 "SCI adapters to be able to run it ! Else you can still try CART, the amazing 'Cluster Administration and Reservation "+ 177 "Tool'. All those software are open source, so don't hesitate ! If you have a SCI Cluster you can also try the Whoops! "+ 178 "clustered web server. Actually Whoops! stands for something ! Yes, it is a Web cache with tcp Handoff, On the fly "+ 179 "cOmpression, parallel Pull-based lru for Sci clusters !! Ok, that was a lot of fun but now it is starting to be quite late "+ 180 "and I'll have to go to bed very soon, so I think if you need more information, just go on <h1>http://sci-serv.inrialpes.fr</h1> "+ 181 "or you can even try http://www.cs.rice.edu and try to find where Emmanuel Cecchet or Julie Marguerite are and you will "+ 182 "maybe get fresh news about all that !!<p>"; 183 184 int staticDescriptionLength = staticDescription.length(); 186 String [] staticComment = { "This is a very bad comment. Stay away from this seller !!<p>", 187 "This is a comment below average. I don't recommend this user !!<p>", 188 "This is a neutral comment. It is neither a good or a bad seller !!<p>", 189 "This is a comment above average. You can trust this seller even if it is not the best deal !!<p>", 190 "This is an excellent comment. You can make really great deals with this seller !!<p>" }; 191 int[] staticCommentLength = { staticComment[0].length(), staticComment[1].length(), staticComment[2].length(), 192 staticComment[3].length(), staticComment[4].length()}; 193 int[] ratingValue = { -5, -3, 0, 3, 5 }; 194 int rating; 195 String comment; 196 197 int nbBids; 199 200 int i, j; 202 URL url; 203 String HTTPreply; 204 205 int getItemDescriptionLength = rubis.getItemDescriptionLength(); 207 float getPercentReservePrice = rubis.getPercentReservePrice(); 208 float getPercentBuyNow = rubis.getPercentBuyNow(); 209 float getPercentUniqueItems = rubis.getPercentUniqueItems(); 210 int getMaxItemQty = rubis.getMaxItemQty(); 211 int getCommentMaxLength = rubis.getCommentMaxLength(); 212 int getNbOfCategories = rubis.getNbOfCategories(); 213 int getNbOfUsers = rubis.getNbOfUsers(); 214 int getMaxBidsPerItem = rubis.getMaxBidsPerItem(); 215 216 System.out.println("Generating "+oldItems+" old items and "+activeItems+" active items."); 217 if (generateBids) 218 System.out.println("Generating up to "+getMaxBidsPerItem+" bids per item."); 219 if (generateComments) 220 System.out.println("Generating 1 comment per item"); 221 222 for (i = 0 ; i < totalItems ; i++) 223 { 224 name = "RUBiS automatically generated item #"+(i+1); 226 int descriptionLength = rand.nextInt(getItemDescriptionLength)+1; 227 description = ""; 228 while (staticDescriptionLength < descriptionLength) 229 { 230 description = description+staticDescription; 231 descriptionLength -= staticDescriptionLength; 232 } 233 description += staticDescription.substring(0, descriptionLength); 234 initialPrice = rand.nextInt(5000)+1; 235 duration = rand.nextInt(7)+1; 236 if (i < oldItems) 237 { duration = -duration; if (i < getPercentReservePrice*oldItems/100) 240 reservePrice = rand.nextInt(1000)+initialPrice; 241 else 242 reservePrice = 0; 243 if (i < getPercentBuyNow*oldItems/100) 244 buyNow = rand.nextInt(1000)+initialPrice+reservePrice; 245 else 246 buyNow = 0; 247 if (i < getPercentUniqueItems*oldItems/100) 248 quantity = 1; 249 else 250 quantity = rand.nextInt(getMaxItemQty)+1; 251 } 252 else 253 { 254 if (i < getPercentReservePrice*activeItems/100) 255 reservePrice = rand.nextInt(1000)+initialPrice; 256 else 257 reservePrice = 0; 258 if (i < getPercentBuyNow*activeItems/100) 259 buyNow = rand.nextInt(1000)+initialPrice+reservePrice; 260 else 261 buyNow = 0; 262 if (i < getPercentUniqueItems*activeItems/100) 263 quantity = 1; 264 else 265 quantity = rand.nextInt(getMaxItemQty)+1; 266 } 267 categoryId = i % getNbOfCategories; 268 while (itemsPerCategory[categoryId] == 0) 270 categoryId = (categoryId + 1) % getNbOfCategories; 271 if (i >= oldItems) 272 itemsPerCategory[categoryId]--; 273 sellerId = rand.nextInt(getNbOfUsers) + 1; 274 275 url = urlGen.registerItem(name, description, initialPrice, reservePrice, buyNow, duration, quantity, sellerId, categoryId+1); 277 HTTPreply = callHTTPServer(url); 278 if (HTTPreply.indexOf("ERROR") != -1) 279 { 280 System.err.println("Failed to add item "+name+" ("+url+")"); 281 System.err.println(HTTPreply); 282 } 283 284 if (generateBids) 285 { nbBids = rand.nextInt(getMaxBidsPerItem); 287 for (j = 0 ; j < nbBids ; j++) 288 { 289 int addBid = rand.nextInt(10)+1; 290 url = urlGen.storeBid(i+1, rand.nextInt(getNbOfUsers)+1, initialPrice, initialPrice+addBid, initialPrice+addBid*2, rand.nextInt(quantity)+1, quantity); 291 HTTPreply = callHTTPServer(url); 292 if (HTTPreply.indexOf("ERROR") != -1) 293 { 294 System.err.println("Failed to bid #"+j+" on item "+name+" ("+url+")"); 295 System.err.println(HTTPreply); 296 } 297 initialPrice += addBid; } 299 } 300 301 if (generateComments) 302 { rating = rand.nextInt(5); 304 int commentLength = rand.nextInt(getCommentMaxLength)+1; 305 comment = ""; 306 while (staticCommentLength[rating] < commentLength) 307 { 308 comment = comment+staticComment[rating]; 309 commentLength -= staticCommentLength[rating]; 310 } 311 comment += staticComment[rating].substring(0, commentLength); 312 313 url = urlGen.storeComment(i+1, sellerId, rand.nextInt(getNbOfUsers)+1, ratingValue[rating], comment); 315 HTTPreply = callHTTPServer(url); 316 if (HTTPreply.indexOf("ERROR") != -1) 317 { 318 System.err.println("Failed to add comment for item #"+(i+1)+" ("+url+")"); 319 System.err.println(HTTPreply); 320 } 321 } 322 323 if (i % 10 == 0) 324 System.out.print("."); 325 } 326 System.out.println(" Done!"); 327 } 328 329 330 331 337 private String callHTTPServer(URL url) 338 { 339 String HTMLReply = ""; 340 BufferedInputStream in = null; 341 int retry = 0; 342 343 while (retry < 5) 344 { 345 try 347 { 348 in = new BufferedInputStream (url.openStream(), 4096); 349 } 350 catch (IOException ioe) 351 { 352 System.err.println("Unable to open URL "+url+" ("+ioe.getMessage()+")"); 353 retry++; 354 try 355 { 356 Thread.currentThread().sleep(1000L); 357 } 358 catch (InterruptedException i) 359 { 360 System.err.println("Interrupted in callHTTPServer()"); 361 return null; 362 } 363 continue; 364 } 365 366 try 368 { 369 byte[] buffer = new byte[4096]; 370 int read; 371 372 while ((read = in.read(buffer, 0, buffer.length)) != -1) 373 { 374 if (read > 0) 375 HTMLReply = HTMLReply + new String (buffer, 0, read); 376 } 377 } 378 catch (IOException ioe) 379 { 380 System.err.println("Unable to read from URL "+url+" ("+ioe.getMessage()+")"); 381 return null; 382 } 383 384 break; 386 } 387 388 try 389 { 390 if (in != null) 391 in.close(); 392 } 393 catch (IOException ioe) 394 { 395 System.err.println("Unable to close URL "+url+" ("+ioe.getMessage()+")"); 396 } 397 return HTMLReply; 398 } 399 400 } 401 | Popular Tags |