Skip to content

Commit b0850b9

Browse files
committed
Add App Engine Datastore stats sample.
Stats queries aren't supported on the local server for unit testing, so no tests. :-( Sample from https://cloud.google.com/appengine/docs/java/datastore/stats
1 parent 8d758bd commit b0850b9

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.appengine;
18+
19+
import com.google.appengine.api.datastore.DatastoreService;
20+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
21+
import com.google.appengine.api.datastore.Entity;
22+
import com.google.appengine.api.datastore.Query;
23+
24+
import java.io.IOException;
25+
import java.io.PrintWriter;
26+
27+
import javax.servlet.ServletException;
28+
import javax.servlet.http.HttpServlet;
29+
import javax.servlet.http.HttpServletRequest;
30+
import javax.servlet.http.HttpServletResponse;
31+
32+
public class StatsServlet extends HttpServlet {
33+
@Override
34+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
35+
throws ServletException, IOException {
36+
// [START stat_example]
37+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
38+
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
39+
Long totalBytes = (Long) globalStat.getProperty("bytes");
40+
Long totalEntities = (Long) globalStat.getProperty("count");
41+
// [END stat_example]
42+
43+
resp.setContentType("text/plain");
44+
resp.setCharacterEncoding("UTF-8");
45+
PrintWriter w = resp.getWriter();
46+
w.printf("%d bytes\n%d entities\n", totalBytes, totalEntities);
47+
}
48+
}
49+
// [END cursors]

appengine/datastore/src/main/webapp/WEB-INF/web.xml

+21
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@
5050
<servlet-name>projection</servlet-name>
5151
<url-pattern>/projection</url-pattern>
5252
</servlet-mapping>
53+
<servlet>
54+
<servlet-name>stats</servlet-name>
55+
<servlet-class>com.example.appengine.StatsServlet</servlet-class>
56+
</servlet>
57+
<servlet-mapping>
58+
<servlet-name>stats</servlet-name>
59+
<url-pattern>/stats</url-pattern>
60+
</servlet-mapping>
5361

5462
<!-- Special handler to populate Datastore with default values. -->
5563
<servlet>
@@ -73,4 +81,17 @@
7381
<role-name>*</role-name> <!-- Require users to login. -->
7482
</auth-constraint>
7583
</security-constraint>
84+
85+
<security-constraint>
86+
<web-resource-collection>
87+
<web-resource-name>profile</web-resource-name>
88+
<url-pattern>/stats</url-pattern>
89+
</web-resource-collection>
90+
<user-data-constraint>
91+
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
92+
</user-data-constraint>
93+
<auth-constraint>
94+
<role-name>admin</role-name>
95+
</auth-constraint>
96+
</security-constraint>
7697
</web-app>

0 commit comments

Comments
 (0)