PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 6ms RepoModel.GetById 34ms app.codeStats 0ms

/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
16package com.google.ie.web.controller;
17
18import com.google.appengine.api.datastore.DatastoreService;
19import com.google.appengine.api.datastore.DatastoreServiceFactory;
20import com.google.appengine.api.datastore.Entity;
21import com.google.appengine.api.datastore.FetchOptions;
22import com.google.appengine.api.datastore.PreparedQuery;
23import com.google.appengine.api.datastore.Query;
24
25import org.apache.log4j.Logger;
26import org.springframework.stereotype.Controller;
27import org.springframework.web.bind.annotation.RequestMapping;
28
29/**
30 * Controller to clean up the database
31 * 
32 * @author Sachneet
33 * 
34 */
35@Controller
36public class DataStoreCleanUpController {
37    private static Logger log = Logger.getLogger(DataStoreCleanUpController.class);
38    private static final int THREE_HUNDRED = 300;
39    private static final int ZERO = 0;
40
41    /**
42     * Clean database data.
43     * 
44     * @return String
45     */
46    @RequestMapping("/cleanDatastore")
47    public String clean() {
48        DatastoreService datastore =
49                        DatastoreServiceFactory.getDatastoreService();
50         String[] arrayOfEntities = { "Audit", "Tag", "Idea", "AdminRequest",
51         "CommentVote", "Developer",
52         "EntityIndex", "IdeaComment", "IdeaVote", "Project",
53         "ProjectComment",
54         "ShardedCounter", "User", "IdeaCategory", "BadWord", "_ah_SESSION",
55         "content",
56         "meta", };
57//        String[] arrayOfEntities = { "_ah_SESSION", "content" };
58        /*
59         * Fetch 300 records.Such a big number is used because other then the
60         * last three entities all others won't have much data (in development)
61         */
62        FetchOptions options = FetchOptions.Builder.withLimit(THREE_HUNDRED);
63        Query query;
64        PreparedQuery results = null;
65        for (String kind : arrayOfEntities) {
66            /* prepare the query for the entity kind */
67            query = new Query(kind);
68            /* Makes this query fetch and return only keys, not full entities. */
69            query.setKeysOnly();
70            /* Get the PreparedQuery object */
71            results = datastore.prepare(query);
72            /*
73             * No of entities existing in the datastore of the corresponding
74             * type
75             */
76            int noOfEntities = results.countEntities();
77            log.info(noOfEntities + "    no. of entities exist of kind   " + query.getKind());
78            /* If entities exist delete */
79            if (noOfEntities > ZERO) {
80                try {
81
82                    for (Entity session : results.asIterable(options)) {
83
84                        datastore.delete(session.getKey());
85                    }
86                    log.info("                A max of " + options.getLimit()
87                                    + " of them deleted if existent");
88                } catch (Throwable e) {
89                    log.error(e.getMessage());
90                }
91            }
92
93        }
94        return "queue/queue";
95    }
96
97}
98