Created
October 5, 2011 09:53
-
-
Save vvakame/1264065 to your computer and use it in GitHub Desktop.
appengine 1.5.5 prerelease , XG(Cross Group) tx sample.
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
package net.vvakame.xg; | |
import java.io.IOException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
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.EntityNotFoundException; | |
import com.google.appengine.api.datastore.Key; | |
import com.google.appengine.api.datastore.KeyFactory; | |
import com.google.appengine.api.datastore.Transaction; | |
import com.google.appengine.api.datastore.TransactionOptions; | |
@SuppressWarnings("serial") | |
public class XGtxTestingServlet extends HttpServlet { | |
DatastoreService db; | |
public void doGet(HttpServletRequest req, HttpServletResponse resp) | |
throws IOException { | |
resp.setContentType("text/plain"); | |
// over 6 => too many entity groups... | |
int count = 5; | |
{ | |
String countStr = req.getParameter("count"); | |
if (countStr != null) { | |
count = Integer.parseInt(countStr); | |
} | |
} | |
int egCount = 1000; | |
{ | |
String countStr = req.getParameter("egCount"); | |
if (countStr != null) { | |
egCount = Integer.parseInt(countStr); | |
} | |
} | |
resp.getWriter().println("count=" + count + ", egCount=" + egCount); | |
db = DatastoreServiceFactory.getDatastoreService(); | |
String kind = "hoge"; | |
for (int i = 1; i <= count; i++) { | |
createEntityGroups(kind + i, i, egCount); | |
} | |
TransactionOptions opt = TransactionOptions.Builder.withXG(true); | |
Transaction tx = db.beginTransaction(opt); | |
for (int i = 1; i <= count; i++) { | |
Entity entity = get(tx, KeyFactory.createKey(kind + i, i)); | |
incProperty(entity, "i"); | |
db.put(tx, entity); | |
} | |
tx.commit(); | |
} | |
void createEntityGroups(String kind, long id, int count) { | |
Key parentKey = KeyFactory.createKey(kind, id); | |
{ | |
Entity entity = new Entity(parentKey); | |
entity.setProperty("keyStr", parentKey.toString()); | |
db.put(entity); | |
} | |
for (int i = 1; i <= count; i++) { | |
Key key = KeyFactory.createKey(parentKey, kind, i); | |
Entity entity = new Entity(key); | |
entity.setProperty("keyStr", key.toString()); | |
db.put(entity); | |
} | |
} | |
Entity get(Transaction tx, Key key) { | |
try { | |
return db.get(tx, key); | |
} catch (EntityNotFoundException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
void incProperty(Entity entity, String propertyName) { | |
Long value = (Long) entity.getProperty(propertyName); | |
if (value == null) { | |
value = 1L; | |
} else { | |
value++; | |
} | |
entity.setProperty(propertyName, value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment