Skip to content

Instantly share code, notes, and snippets.

View thorwebdev's full-sized avatar
🔨
building

Thor 雷神 Schaeff thorwebdev

🔨
building
View GitHub Profile
@thorwebdev
thorwebdev / addFence_snippet.java
Created December 7, 2014 16:02
Now we need to create an endpoint called add. This endpoint expects a string for the group name, a boolean indicating whether to rebuild our index, and a JSON object representing our fence’s object model. From this we create a new fence and write it to Cloud Datastore.
@ApiMethod(name = "add", httpMethod = "post", path = "add")
public MyFence addFence(@Named("group") String group, @Named("index") boolean buildIndex, MyFence fence) {
//Get the last fences' id.
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key fenceKey = KeyFactory.createKey("Geofence", group);
long nextId;
if (fence.getId() != -1) {
nextId = fence.getId();
} else {
@thorwebdev
thorwebdev / MyIndex_build_snippet.java
Created December 7, 2014 16:01
We then build the index and store it to Memcache for fast read access.
//Build the index.
index.build();
//Write the index to Memcache.
MemcacheService syncCache = MemcacheServiceFactory.getMemcacheService();
//Last param is expiration date. Set to null to keep it in Memcache forever.
syncCache.put(group, index, null);
@thorwebdev
thorwebdev / MyIndex_add_polys_snippet.java
Last active August 29, 2015 14:10
To speed up our geofencing queries we’re going to index our fences in an STR tree. The JTS library does most of the heavy lifting here, so we only need to fetch all our fences from Datastore, create a polygon object for each one and add the polygon’s bounding box to the index.
//Get all fences of group from DataStore.
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Key fenceKey = KeyFactory.createKey("Geofence", group);
Query query = new Query("Fence", fenceKey).addSort("id", Query.SortDirection.DESCENDING);
List < Entity > fencesFromStore = datastore.prepare(query).asList(FetchOptions.Builder.withDefaults());
if (!fencesFromStore.isEmpty()) {
//Create STRTree-Index.
STRtree index = new STRtree();
@thorwebdev
thorwebdev / MyFence.java
Created December 7, 2014 15:59
For the sake of convenience we’re only storing the fences’ vertices and some metadata. To send and receive data through our endpoints we need an object model which we create in a little Java Bean called MyFence.java.
package com.google.appengine.geo.fencing;
/** The object model for the data we are sending through endpoints */
public class MyFence {
private long id = -1;
public String name;
public String entityGroup;
public String description;
public double[][] vertices;