Search in sources :

Example 21 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class BundleSerializer method decodeTimestamp.

private void decodeTimestamp(Value.Builder builder, Object timestamp) throws JSONException {
    Timestamp decoded = decodeTimestamp(timestamp);
    builder.setTimestampValue(com.google.protobuf.Timestamp.newBuilder().setSeconds(decoded.getSeconds()).setNanos(decoded.getNanoseconds()));
}
Also used : Timestamp(com.google.firebase.Timestamp)

Example 22 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class LocalSerializer method encodeDocument.

/**
 * Encodes a Document for local storage. This differs from the v1 RPC serializer for Documents in
 * that it preserves the updateTime, which is considered an output only value by the server.
 */
private com.google.firestore.v1.Document encodeDocument(Document document) {
    com.google.firestore.v1.Document.Builder builder = com.google.firestore.v1.Document.newBuilder();
    builder.setName(rpcSerializer.encodeKey(document.getKey()));
    builder.putAllFields(document.getData().getFieldsMap());
    Timestamp updateTime = document.getVersion().getTimestamp();
    builder.setUpdateTime(rpcSerializer.encodeTimestamp(updateTime));
    return builder.build();
}
Also used : Document(com.google.firebase.firestore.model.Document) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Timestamp(com.google.firebase.Timestamp)

Example 23 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class CustomClassMapper method serialize.

@SuppressWarnings("unchecked")
private static <T> Object serialize(T o, ErrorPath path) {
    if (path.getLength() > MAX_DEPTH) {
        throw serializeError(path, "Exceeded maximum depth of " + MAX_DEPTH + ", which likely indicates there's an object cycle");
    }
    if (o == null) {
        return null;
    } else if (o instanceof Number) {
        if (o instanceof Long || o instanceof Integer || o instanceof Double || o instanceof Float) {
            return o;
        } else {
            throw serializeError(path, String.format("Numbers of type %s are not supported, please use an int, long, float or double", o.getClass().getSimpleName()));
        }
    } else if (o instanceof String) {
        return o;
    } else if (o instanceof Boolean) {
        return o;
    } else if (o instanceof Character) {
        throw serializeError(path, "Characters are not supported, please use Strings");
    } else if (o instanceof Map) {
        Map<String, Object> result = new HashMap<>();
        for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) o).entrySet()) {
            Object key = entry.getKey();
            if (key instanceof String) {
                String keyString = (String) key;
                result.put(keyString, serialize(entry.getValue(), path.child(keyString)));
            } else {
                throw serializeError(path, "Maps with non-string keys are not supported");
            }
        }
        return result;
    } else if (o instanceof Collection) {
        if (o instanceof List) {
            List<Object> list = (List<Object>) o;
            List<Object> result = new ArrayList<>(list.size());
            for (int i = 0; i < list.size(); i++) {
                result.add(serialize(list.get(i), path.child("[" + i + "]")));
            }
            return result;
        } else {
            throw serializeError(path, "Serializing Collections is not supported, please use Lists instead");
        }
    } else if (o.getClass().isArray()) {
        throw serializeError(path, "Serializing Arrays is not supported, please use Lists instead");
    } else if (o instanceof Enum) {
        String enumName = ((Enum<?>) o).name();
        try {
            Field enumField = o.getClass().getField(enumName);
            return BeanMapper.propertyName(enumField);
        } catch (NoSuchFieldException ex) {
            return enumName;
        }
    } else if (o instanceof Date || o instanceof Timestamp || o instanceof GeoPoint || o instanceof Blob || o instanceof DocumentReference || o instanceof FieldValue) {
        return o;
    } else {
        Class<T> clazz = (Class<T>) o.getClass();
        BeanMapper<T> mapper = loadOrCreateBeanMapperForClass(clazz);
        return mapper.serialize(o, path);
    }
}
Also used : ArrayList(java.util.ArrayList) ServerTimestamp(com.google.firebase.firestore.ServerTimestamp) Timestamp(com.google.firebase.Timestamp) Field(java.lang.reflect.Field) GeoPoint(com.google.firebase.firestore.GeoPoint) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(com.google.firebase.firestore.FieldValue) DocumentReference(com.google.firebase.firestore.DocumentReference) Blob(com.google.firebase.firestore.Blob) GeoPoint(com.google.firebase.firestore.GeoPoint) Date(java.util.Date) Collection(java.util.Collection) AccessibleObject(java.lang.reflect.AccessibleObject) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 24 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method testHoldsBackTransforms.

@Test
public void testHoldsBackTransforms() {
    Query query = query("foo");
    allocateQuery(query);
    assertTargetId(2);
    writeMutation(setMutation("foo/bar", map("sum", 0, "array_union", new ArrayList<>())));
    assertChanged(doc("foo/bar", 0, map("sum", 0, "array_union", new ArrayList<>())).setHasLocalMutations());
    acknowledgeMutation(1);
    assertChanged(doc("foo/bar", 1, map("sum", 0, "array_union", new ArrayList<>())).setHasCommittedMutations());
    applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 1, map("sum", 0, "array_union", new ArrayList<>())), asList(2), emptyList()));
    assertChanged(doc("foo/bar", 1, map("sum", 0, "array_union", new ArrayList<>())));
    writeMutations(asList(patchMutation("foo/bar", map("sum", FieldValue.increment(1))), patchMutation("foo/bar", map("array_union", FieldValue.arrayUnion("foo")))));
    assertChanged(doc("foo/bar", 1, map("sum", 1, "array_union", Collections.singletonList("foo"))).setHasLocalMutations());
    // The sum transform is not idempotent and the backend's updated value is ignored. The
    // ArrayUnion transform is recomputed and includes the backend value.
    applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 2, map("sum", 1337, "array_union", Collections.singletonList("bar"))), asList(2), emptyList()));
    assertChanged(doc("foo/bar", 2, map("sum", 1, "array_union", asList("foo"))).setHasLocalMutations());
    acknowledgeMutationWithTransformResults(3, 1338, asList("bar", "foo"));
    assertChanged(doc("foo/bar", 3, map("sum", 1338, "array_union", asList("bar", "foo"))).setReadTime(new SnapshotVersion(new Timestamp(0, 3000))).setHasCommittedMutations());
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) ArrayList(java.util.ArrayList) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 25 with Timestamp

use of com.google.firebase.Timestamp in project firebase-android-sdk by firebase.

the class CursorTest method timestampsAreTruncatedToMicroseconds.

@Test
public void timestampsAreTruncatedToMicroseconds() {
    Timestamp nanos = new Timestamp(0, 123456789);
    Timestamp micros = new Timestamp(0, 123456000);
    Timestamp millis = new Timestamp(0, 123000000);
    CollectionReference testCollection = testCollectionWithDocs(map("a", map("timestamp", nanos)));
    QuerySnapshot snapshot = waitFor(testCollection.whereEqualTo("timestamp", nanos).get());
    assertThat(querySnapshotToValues(snapshot)).hasSize(1);
    // Because Timestamp should have been truncated to microseconds, the microsecond timestamp
    // should be considered equal to the nanosecond one.
    snapshot = waitFor(testCollection.whereEqualTo("timestamp", micros).get());
    assertThat(querySnapshotToValues(snapshot)).hasSize(1);
    // The truncation is just to the microseconds, however, so the millisecond timestamp should be
    // treated as different and thus the query should return no results.
    snapshot = waitFor(testCollection.whereEqualTo("timestamp", millis).get());
    assertThat(querySnapshotToValues(snapshot)).isEmpty();
}
Also used : Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Aggregations

Timestamp (com.google.firebase.Timestamp)36 Test (org.junit.Test)18 MutableDocument (com.google.firebase.firestore.model.MutableDocument)7 Date (java.util.Date)6 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)5 ArrayList (java.util.ArrayList)5 GeoPoint (com.google.firebase.firestore.GeoPoint)4 ObjectValue (com.google.firebase.firestore.model.ObjectValue)4 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)4 Mutation (com.google.firebase.firestore.model.mutation.Mutation)3 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)3 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)3 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)3 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)3 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)3 Value (com.google.firestore.v1.Value)3 HashMap (java.util.HashMap)3 FieldValue (com.google.firebase.firestore.FieldValue)2 Query (com.google.firebase.firestore.core.Query)2