/src/main/java/com/google/ie/web/controller/DataStoreCleanUpController.java

http://thoughtsite.googlecode.com/ · Java · 98 lines · 50 code · 10 blank · 38 comment · 3 complexity · 008420c02abb48c675dbef1b40401443 MD5 · raw file

  1. /* Copyright 2010 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS.
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License
  14. */
  15. package com.google.ie.web.controller;
  16. import com.google.appengine.api.datastore.DatastoreService;
  17. import com.google.appengine.api.datastore.DatastoreServiceFactory;
  18. import com.google.appengine.api.datastore.Entity;
  19. import com.google.appengine.api.datastore.FetchOptions;
  20. import com.google.appengine.api.datastore.PreparedQuery;
  21. import com.google.appengine.api.datastore.Query;
  22. import org.apache.log4j.Logger;
  23. import org.springframework.stereotype.Controller;
  24. import org.springframework.web.bind.annotation.RequestMapping;
  25. /**
  26. * Controller to clean up the database
  27. *
  28. * @author Sachneet
  29. *
  30. */
  31. @Controller
  32. public class DataStoreCleanUpController {
  33. private static Logger log = Logger.getLogger(DataStoreCleanUpController.class);
  34. private static final int THREE_HUNDRED = 300;
  35. private static final int ZERO = 0;
  36. /**
  37. * Clean database data.
  38. *
  39. * @return String
  40. */
  41. @RequestMapping("/cleanDatastore")
  42. public String clean() {
  43. DatastoreService datastore =
  44. DatastoreServiceFactory.getDatastoreService();
  45. String[] arrayOfEntities = { "Audit", "Tag", "Idea", "AdminRequest",
  46. "CommentVote", "Developer",
  47. "EntityIndex", "IdeaComment", "IdeaVote", "Project",
  48. "ProjectComment",
  49. "ShardedCounter", "User", "IdeaCategory", "BadWord", "_ah_SESSION",
  50. "content",
  51. "meta", };
  52. // String[] arrayOfEntities = { "_ah_SESSION", "content" };
  53. /*
  54. * Fetch 300 records.Such a big number is used because other then the
  55. * last three entities all others won't have much data (in development)
  56. */
  57. FetchOptions options = FetchOptions.Builder.withLimit(THREE_HUNDRED);
  58. Query query;
  59. PreparedQuery results = null;
  60. for (String kind : arrayOfEntities) {
  61. /* prepare the query for the entity kind */
  62. query = new Query(kind);
  63. /* Makes this query fetch and return only keys, not full entities. */
  64. query.setKeysOnly();
  65. /* Get the PreparedQuery object */
  66. results = datastore.prepare(query);
  67. /*
  68. * No of entities existing in the datastore of the corresponding
  69. * type
  70. */
  71. int noOfEntities = results.countEntities();
  72. log.info(noOfEntities + " no. of entities exist of kind " + query.getKind());
  73. /* If entities exist delete */
  74. if (noOfEntities > ZERO) {
  75. try {
  76. for (Entity session : results.asIterable(options)) {
  77. datastore.delete(session.getKey());
  78. }
  79. log.info(" A max of " + options.getLimit()
  80. + " of them deleted if existent");
  81. } catch (Throwable e) {
  82. log.error(e.getMessage());
  83. }
  84. }
  85. }
  86. return "queue/queue";
  87. }
  88. }