Revision: 8532
Author: amitman...@google.com
Date: Thu Aug 12 15:07:28 2010
Log: Fixed the create bug that was being caused by server ids and futureIds
by using the futureId field in RecordKey and RecordImpl
Patch by: amitmanjhi
Review by: rjrjr(desk review)
http://code.google.com/p/google-web-toolkit/source/detail?r=8532
Modified:
/trunk/user/src/com/google/gwt/requestfactory/client/impl/AbstractRequest.java
/trunk/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RecordKey.java
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RecordSchema.java
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java
/trunk/user/src/com/google/gwt/requestfactory/client/impl/ValueStoreJsonImpl.java
/trunk/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java
=======================================
---
/trunk/user/src/com/google/gwt/requestfactory/client/impl/AbstractRequest.java
Wed Aug 11 06:10:03 2010
+++
/trunk/user/src/com/google/gwt/requestfactory/client/impl/AbstractRequest.java
Thu Aug 12 15:07:28 2010
@@ -64,7 +64,7 @@
@SuppressWarnings("unchecked")
public <P extends Record> P edit(P record) {
P returnRecordImpl = (P) ((RecordImpl) record).getSchema().create(
- ((RecordImpl) record).asJso());
+ ((RecordImpl) record).asJso(), ((RecordImpl) record).isFuture());
((RecordImpl) returnRecordImpl).setDeltaValueStore(deltaValueStore);
return returnRecordImpl;
}
=======================================
---
/trunk/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java
Tue Aug 10 18:34:06 2010
+++
/trunk/user/src/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImpl.java
Thu Aug 12 15:07:28 2010
@@ -165,15 +165,14 @@
if (violations == NULL_VIOLATIONS) {
Long datastoreId = futureToDatastoreId.get(futureKey.id);
assert datastoreId != null;
- requestFactory.futureIdGenerator.delete(futureKey.id);
- final RecordKey key = new RecordKey(datastoreId,
futureKey.schema);
+ final RecordKey key = new RecordKey(datastoreId,
futureKey.schema, RequestFactoryJsonImpl.NOT_FUTURE);
RecordJsoImpl value = entry.getValue();
value.set(Record.id, datastoreId);
RecordJsoImpl masterRecord = master.records.get(key);
assert masterRecord == null;
master.records.put(key, value);
masterRecord = value;
- toRemove.add(key);
+ toRemove.add(new RecordKey(datastoreId, futureKey.schema,
RequestFactoryJsonImpl.IS_FUTURE));
master.eventBus.fireEvent(masterRecord.getSchema().createChangeEvent(
masterRecord, WriteOperation.CREATE));
syncResults.add(new SyncResultImpl(masterRecord, null,
futureKey.id));
@@ -349,8 +348,7 @@
}
}
- public String toJson() {
- used = true;
+ String toJson() {
if (operations.size() > 1) {
throw new UnsupportedOperationException(
"Currently, only one entity can be saved/persisted at a time");
@@ -367,6 +365,11 @@
* allowed to span multiple entity groups.
*/
}
+ return toJsonWithoutChecks();
+ }
+
+ String toJsonWithoutChecks() {
+ used = true;
StringBuffer jsonData = new StringBuffer("{");
for (WriteOperation writeOperation : WriteOperation.values()) {
String jsonDataForOperation = getJsonForOperation(writeOperation);
=======================================
---
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RecordKey.java
Wed Aug 11 09:22:23 2010
+++
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RecordKey.java
Thu Aug 12 15:07:28 2010
@@ -27,21 +27,23 @@
class RecordKey {
final RecordSchema<?> schema;
final Long id;
+ final boolean isFuture;
RecordKey(RecordImpl record) {
- this(record.getId(), record.getSchema());
+ this(record.getId(), record.getSchema(), record.isFuture());
}
- RecordKey(RecordJsoImpl record) {
- this(record.getId(), record.getSchema());
+ RecordKey(RecordJsoImpl record, boolean isFuture) {
+ this(record.getId(), record.getSchema(), isFuture);
}
- protected RecordKey(Long id, RecordSchema<?> schema) {
+ protected RecordKey(Long id, RecordSchema<?> schema, boolean isFuture) {
assert id != null;
assert schema != null;
this.id = id;
this.schema = schema;
+ this.isFuture = isFuture;
}
@Override
@@ -68,7 +70,7 @@
@Override
public int hashCode() {
final int prime = 31;
- int result = 1;
+ int result = (isFuture ? 0 : 1);
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((schema == null) ? 0 : schema.hashCode());
return result;
@@ -77,6 +79,6 @@
@Override
public String toString() {
return "[RecordKey schema: " + schema.getClass().getName() + " id: " +
id
- + "]";
+ + " isFuture: " + (isFuture ? "true" : "false") + "]";
}
}
=======================================
---
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RecordSchema.java
Thu Aug 12 09:59:46 2010
+++
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RecordSchema.java
Thu Aug 12 15:07:28 2010
@@ -52,7 +52,7 @@
}
public final R create(RecordJsoImpl jso) {
- return create(jso, false);
+ return create(jso, RequestFactoryJsonImpl.NOT_FUTURE);
}
public abstract R create(RecordJsoImpl jso, boolean isFuture);
=======================================
---
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java
Thu Aug 12 09:59:46 2010
+++
/trunk/user/src/com/google/gwt/requestfactory/client/impl/RequestFactoryJsonImpl.java
Thu Aug 12 15:07:28 2010
@@ -31,9 +31,7 @@
import com.google.gwt.valuestore.shared.Record;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.Map;
-import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -47,25 +45,8 @@
*/
public abstract class RequestFactoryJsonImpl implements RequestFactory {
- // TODO(amitmanjhi) Dump this and the one in DeltaValueStore in favor of
- // RecordImpl#isFuture
- static class FutureIdGenerator {
- Set<Long> idsInTransit = new HashSet<Long>();
- Long maxId = 1L;
-
- void delete(Long id) {
- idsInTransit.remove(id);
- }
-
- Long getFutureId() {
- Long futureId = maxId++;
- if (maxId == Long.MAX_VALUE) {
- maxId = 1L;
- }
- assert !idsInTransit.contains(futureId);
- return futureId;
- }
- }
+ static final boolean IS_FUTURE = true;
+ static final boolean NOT_FUTURE = false;
private static Logger logger =
Logger.getLogger(RequestFactory.class.getName());
@@ -79,7 +60,7 @@
private static final Integer INITIAL_VERSION = 1;
- final FutureIdGenerator futureIdGenerator = new FutureIdGenerator();
+ private long currentFutureId = 0;
final Map<RecordKey, RecordJsoImpl> creates = new HashMap<RecordKey,
RecordJsoImpl>();
@@ -103,7 +84,7 @@
builder.setHeader("Content-Type",
RequestFactory.JSON_CONTENT_TYPE_UTF8);
builder.setHeader("pageurl", Location.getHref());
builder.setRequestData(ClientRequestHelper.getRequestString(requestObject.getRequestData().getRequestMap(
- ((AbstractRequest) requestObject).deltaValueStore.toJson())));
+ ((AbstractRequest<?,?>) requestObject).deltaValueStore.toJson())));
builder.setCallback(new RequestCallback() {
public void onError(Request request, Throwable exception) {
@@ -157,12 +138,12 @@
private Record createFuture(
RecordSchema<? extends Record> schema) {
- Long futureId = futureIdGenerator.getFutureId();
+ Long futureId = ++currentFutureId;
RecordJsoImpl newRecord = RecordJsoImpl.create(futureId,
INITIAL_VERSION,
schema);
- RecordKey recordKey = new RecordKey(newRecord);
+ RecordKey recordKey = new RecordKey(newRecord, IS_FUTURE);
creates.put(recordKey, newRecord);
- return schema.create(newRecord);
+ return schema.create(newRecord, IS_FUTURE);
}
private void postRequestEvent(State received, Response response) {
=======================================
---
/trunk/user/src/com/google/gwt/requestfactory/client/impl/ValueStoreJsonImpl.java
Tue Aug 10 18:34:06 2010
+++
/trunk/user/src/com/google/gwt/requestfactory/client/impl/ValueStoreJsonImpl.java
Thu Aug 12 15:07:28 2010
@@ -58,7 +58,7 @@
*/
private void setRecordInList(RecordJsoImpl newRecord, int i,
JsArray<RecordJsoImpl> array) {
- RecordKey recordKey = new RecordKey(newRecord);
+ RecordKey recordKey = new RecordKey(newRecord,
RequestFactoryJsonImpl.NOT_FUTURE);
RecordJsoImpl oldRecord = records.get(recordKey);
if (oldRecord == null) {
=======================================
---
/trunk/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java
Thu Aug 12 09:59:46 2010
+++
/trunk/user/test/com/google/gwt/requestfactory/client/impl/DeltaValueStoreJsonImplTest.java
Thu Aug 12 15:07:28 2010
@@ -57,6 +57,7 @@
RecordJsoImpl jso = null;
+ @Override
public String getModuleName() {
return "com.google.gwt.requestfactory.RequestFactoryTest";
}
@@ -70,12 +71,13 @@
return create(token, typeMap);
}
+ @Override
public void init(HandlerManager eventBus) {
// ignore.
}
public LoggingRequest loggingRequest() {
- return null; //ignore
+ return null; // ignore
}
};
@@ -128,7 +130,9 @@
assertTrue(deltaValueStore.isChanged());
JSONObject changeRecord =
testAndGetChangeRecord(deltaValueStore.toJson(),
WriteOperation.CREATE);
- changeRecord.get(SimpleFooRecord.userName.getName());
+ assertEquals(
+ "harry",
+
changeRecord.get(SimpleFooRecord.userName.getName()).isString().stringValue());
}
public void testDelete() {
@@ -151,8 +155,10 @@
assertTrue(deltaValueStore.isChanged());
JSONObject changeRecord =
testAndGetChangeRecord(deltaValueStore.toJson(),
WriteOperation.UPDATE);
- changeRecord.get(SimpleFooRecord.userName.getName());
- }
+ assertEquals(
+ "harry",
+
changeRecord.get(SimpleFooRecord.userName.getName()).isString().stringValue());
+ }
public void testOperationAfterJson() {
DeltaValueStoreJsonImpl deltaValueStore = new DeltaValueStoreJsonImpl(
@@ -174,6 +180,41 @@
deltaValueStore.set(SimpleFooRecord.userName, new MyRecordImpl(jso),
"harry");
}
+
+ public void testSeparateIds() {
+ RecordImpl createRecord = (RecordImpl)
requestFactory.create(SimpleFooRecord.class);
+ assertTrue(createRecord.isFuture());
+ Long futureId = createRecord.getId();
+
+ RecordImpl mockRecord = new RecordImpl(RecordJsoImpl.create(futureId,
1,
+ SimpleFooRecordImpl.SCHEMA), RequestFactoryJsonImpl.NOT_FUTURE);
+ valueStore.setRecord(mockRecord.asJso()); // marked as non-future..
+ DeltaValueStoreJsonImpl deltaValueStore = new DeltaValueStoreJsonImpl(
+ valueStore, requestFactory);
+
+ deltaValueStore.set(SimpleFooRecord.userName, createRecord, "harry");
+ deltaValueStore.set(SimpleFooRecord.userName, mockRecord, "bovik");
+ assertTrue(deltaValueStore.isChanged());
+ String jsonString = deltaValueStore.toJsonWithoutChecks();
+ JSONObject jsonObject = (JSONObject) JSONParser.parse(jsonString);
+ assertFalse(jsonObject.containsKey(WriteOperation.DELETE.name()));
+ assertTrue(jsonObject.containsKey(WriteOperation.CREATE.name()));
+ assertTrue(jsonObject.containsKey(WriteOperation.UPDATE.name()));
+
+ JSONArray createOperationArray = jsonObject.get(
+ WriteOperation.CREATE.name()).isArray();
+ assertEquals(1, createOperationArray.size());
+ assertEquals("harry", createOperationArray.get(0).isObject().get(
+ SimpleFooRecord.class.getName()).isObject().get(
+ SimpleFooRecord.userName.getName()).isString().stringValue());
+
+ JSONArray updateOperationArray = jsonObject.get(
+ WriteOperation.UPDATE.name()).isArray();
+ assertEquals(1, updateOperationArray.size());
+ assertEquals("bovik", updateOperationArray.get(0).isObject().get(
+ SimpleFooRecord.class.getName()).isObject().get(
+ SimpleFooRecord.userName.getName()).isString().stringValue());
+ }
public void testUpdate() {
DeltaValueStoreJsonImpl deltaValueStore = new DeltaValueStoreJsonImpl(
@@ -183,7 +224,9 @@
assertTrue(deltaValueStore.isChanged());
JSONObject changeRecord =
testAndGetChangeRecord(deltaValueStore.toJson(),
WriteOperation.UPDATE);
- changeRecord.get(SimpleFooRecord.userName.getName());
+ assertEquals(
+ "harry",
+
changeRecord.get(SimpleFooRecord.userName.getName()).isString().stringValue());
}
public void testUpdateDelete() {
--
http://groups.google.com/group/Google-Web-Toolkit-Contributors