-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Datastore indexes samples. #214
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<!-- | ||
Copyright 2016 Google Inc. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<project> | ||
<modelVersion>4.0.0</modelVersion> | ||
<packaging>war</packaging> | ||
<version>1.0-SNAPSHOT</version> | ||
<groupId>com.example.appengine</groupId> | ||
<artifactId>appengine-datastore-indexes-exploding</artifactId> | ||
<parent> | ||
<groupId>com.google.cloud</groupId> | ||
<artifactId>doc-samples</artifactId> | ||
<version>1.0.0</version> | ||
<relativePath>../../..</relativePath> | ||
</parent> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-1.0-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<type>jar</type> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<!-- Test Dependencies --> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.10</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-all</artifactId> | ||
<version>1.10.19</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-testing</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-api-stubs</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-tools-sdk</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.truth</groupId> | ||
<artifactId>truth</artifactId> | ||
<version>0.28</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<!-- for hot reload of the web application --> | ||
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory> | ||
<plugins> | ||
<!-- Parent POM defines ${appengine.sdk.version} (updates frequently). --> | ||
<plugin> | ||
<groupId>com.google.appengine</groupId> | ||
<artifactId>appengine-maven-plugin</artifactId> | ||
<version>${appengine.sdk.version}</version> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
62 changes: 62 additions & 0 deletions
62
...ngine/datastore/indexes-exploding/src/main/java/com/example/appengine/IndexesServlet.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.appengine; | ||
|
||
import com.google.appengine.api.datastore.DatastoreService; | ||
import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
import com.google.appengine.api.datastore.Entity; | ||
import com.google.appengine.api.datastore.FetchOptions; | ||
import com.google.appengine.api.datastore.Query; | ||
import com.google.appengine.api.datastore.Query.CompositeFilterOperator; | ||
import com.google.appengine.api.datastore.Query.FilterOperator; | ||
import com.google.appengine.api.datastore.Query.FilterPredicate; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.List; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* A servlet to demonstrate the use of Cloud Datastore indexes. | ||
*/ | ||
public class IndexesServlet extends HttpServlet { | ||
private final DatastoreService datastore; | ||
|
||
public IndexesServlet() { | ||
datastore = DatastoreServiceFactory.getDatastoreService(); | ||
} | ||
|
||
@Override | ||
public void doGet(HttpServletRequest req, HttpServletResponse resp) | ||
throws IOException, ServletException { | ||
Query q = | ||
new Query("Widget") | ||
.setFilter( | ||
CompositeFilterOperator.and( | ||
new FilterPredicate("x", FilterOperator.EQUAL, 1), | ||
new FilterPredicate("y", FilterOperator.EQUAL, "red"))) | ||
.addSort("date", Query.SortDirection.ASCENDING); | ||
List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()); | ||
|
||
PrintWriter out = resp.getWriter(); | ||
out.printf("Got %d widgets.\n", results.size()); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
appengine/datastore/indexes-exploding/src/main/webapp/WEB-INF/appengine-web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
Copyright 2016 Google Inc. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"> | ||
<application>YOUR-PROJECT-ID</application> | ||
<version>YOUR-VERSION-ID</version> | ||
<threadsafe>true</threadsafe> | ||
</appengine-web-app> |
28 changes: 28 additions & 0 deletions
28
appengine/datastore/indexes-exploding/src/main/webapp/WEB-INF/datastore-indexes.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- [START_EXCLUDE silent] --> | ||
<!-- | ||
Copyright 2016 Google Inc. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<!-- [END_EXCLUDE] --> | ||
<datastore-indexes autoGenerate="false"> | ||
<datastore-index kind="Widget"> | ||
<property name="x" direction="asc" /> | ||
<property name="date" direction="asc" /> | ||
</datastore-index> | ||
<datastore-index kind="Widget"> | ||
<property name="y" direction="asc" /> | ||
<property name="date" direction="asc" /> | ||
</datastore-index> | ||
</datastore-indexes> |
39 changes: 39 additions & 0 deletions
39
appengine/datastore/indexes-exploding/src/main/webapp/WEB-INF/web.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
Copyright 2016 Google Inc. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" | ||
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" | ||
version="2.5"> | ||
<servlet> | ||
<servlet-name>indexes-servlet</servlet-name> | ||
<servlet-class>com.example.appengine.IndexesServlet</servlet-class> | ||
</servlet> | ||
<servlet-mapping> | ||
<servlet-name>indexes-servlet</servlet-name> | ||
<url-pattern>/</url-pattern> | ||
</servlet-mapping> | ||
|
||
<security-constraint> | ||
<web-resource-collection> | ||
<web-resource-name>profile</web-resource-name> | ||
<url-pattern>/*</url-pattern> | ||
</web-resource-collection> | ||
<user-data-constraint> | ||
<transport-guarantee>CONFIDENTIAL</transport-guarantee> <!-- Require HTTPS. --> | ||
</user-data-constraint> | ||
</security-constraint> | ||
</web-app> |
104 changes: 104 additions & 0 deletions
104
...e/datastore/indexes-exploding/src/test/java/com/example/appengine/IndexesServletTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.example.appengine; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.appengine.api.datastore.DatastoreService; | ||
import com.google.appengine.api.datastore.DatastoreServiceFactory; | ||
import com.google.appengine.api.datastore.Entity; | ||
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig; | ||
import com.google.appengine.tools.development.testing.LocalServiceTestHelper; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Arrays; | ||
import java.util.Date; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
/** | ||
* Unit tests for {@link IndexesServlet}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class IndexesServletTest { | ||
|
||
private final LocalServiceTestHelper helper = | ||
new LocalServiceTestHelper( | ||
// Set no eventual consistency, that way queries return all results. | ||
// https://cloud.google.com/appengine/docs/java/tools/localunittesting#Java_Writing_High_Replication_Datastore_tests | ||
new LocalDatastoreServiceTestConfig() | ||
.setDefaultHighRepJobPolicyUnappliedJobPercentage(0)); | ||
|
||
@Mock private HttpServletRequest mockRequest; | ||
@Mock private HttpServletResponse mockResponse; | ||
private StringWriter responseWriter; | ||
private IndexesServlet servletUnderTest; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
MockitoAnnotations.initMocks(this); | ||
helper.setUp(); | ||
|
||
// Set up a fake HTTP response. | ||
responseWriter = new StringWriter(); | ||
when(mockResponse.getWriter()).thenReturn(new PrintWriter(responseWriter)); | ||
|
||
servletUnderTest = new IndexesServlet(); | ||
} | ||
|
||
@After | ||
public void tearDown() { | ||
helper.tearDown(); | ||
} | ||
|
||
@Test | ||
public void doGet_emptyDatastore_writesNoWidgets() throws Exception { | ||
servletUnderTest.doGet(mockRequest, mockResponse); | ||
|
||
assertThat(responseWriter.toString()) | ||
.named("IndexesServlet response") | ||
.isEqualTo("Got 0 widgets.\n"); | ||
} | ||
|
||
@Test | ||
public void doGet_repeatedPropertyEntities_writesWidgets() throws Exception { | ||
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); | ||
// [START exploding_index_example_3] | ||
Entity widget = new Entity("Widget"); | ||
widget.setProperty("x", Arrays.asList(1, 2, 3, 4)); | ||
widget.setProperty("y", Arrays.asList("red", "green", "blue")); | ||
widget.setProperty("date", new Date()); | ||
datastore.put(widget); | ||
// [END exploding_index_example_3] | ||
|
||
servletUnderTest.doGet(mockRequest, mockResponse); | ||
|
||
assertThat(responseWriter.toString()) | ||
.named("IndexesServlet response") | ||
.isEqualTo("Got 1 widgets.\n"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some whitespace for the future.