Skip to content

Commit 4a3b5f0

Browse files
committed
Add App Engine Datastore stats sample.
Stats queries aren't supported on the local server for unit testing, so no tests. :-(
1 parent 8d758bd commit 4a3b5f0

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.Cursor;
20+
import com.google.appengine.api.datastore.DatastoreService;
21+
import com.google.appengine.api.datastore.DatastoreServiceFactory;
22+
import com.google.appengine.api.datastore.Entity;
23+
import com.google.appengine.api.datastore.FetchOptions;
24+
import com.google.appengine.api.datastore.PreparedQuery;
25+
import com.google.appengine.api.datastore.Query;
26+
import com.google.appengine.api.datastore.Query.SortDirection;
27+
import com.google.appengine.api.datastore.QueryResultList;
28+
29+
import java.io.IOException;
30+
import java.io.PrintWriter;
31+
32+
import javax.servlet.ServletException;
33+
import javax.servlet.http.HttpServlet;
34+
import javax.servlet.http.HttpServletRequest;
35+
import javax.servlet.http.HttpServletResponse;
36+
37+
public class StatsServlet extends HttpServlet {
38+
@Override
39+
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
40+
throws ServletException, IOException {
41+
// [START stat_example]
42+
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
43+
Entity globalStat = datastore.prepare(new Query("__Stat_Total__")).asSingleEntity();
44+
Long totalBytes = (Long) globalStat.getProperty("bytes");
45+
Long totalEntities = (Long) globalStat.getProperty("count");
46+
// [END stat_example]
47+
48+
resp.setContentType("text/plain");
49+
resp.setCharacterEncoding("UTF-8");
50+
PrintWriter w = resp.getWriter();
51+
w.printf("%d bytes\n%d entities\n", totalBytes, totalEntities);
52+
}
53+
}
54+
// [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)