Search in sources :

Example 16 with Timestamp

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

the class TestUtil method version.

public static SnapshotVersion version(long versionMicros) {
    long seconds = versionMicros / 1000000;
    int nanos = (int) (versionMicros % 1000000L) * 1000;
    return new SnapshotVersion(new Timestamp(seconds, nanos));
}
Also used : SnapshotVersion(com.google.firebase.firestore.model.SnapshotVersion) Timestamp(com.google.firebase.Timestamp)

Example 17 with Timestamp

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

the class SQLiteRemoteDocumentCache method getAll.

/**
 * Returns the next {@code count} documents from the provided collections, ordered by read time.
 */
private Map<DocumentKey, MutableDocument> getAll(List<ResourcePath> collections, IndexOffset offset, int count) {
    Timestamp readTime = offset.getReadTime().getTimestamp();
    DocumentKey documentKey = offset.getDocumentKey();
    StringBuilder sql = repeatSequence("SELECT contents, read_time_seconds, read_time_nanos, path " + "FROM remote_documents " + "WHERE path >= ? AND path < ? AND path_length = ? " + "AND (read_time_seconds > ? OR ( " + "read_time_seconds = ? AND read_time_nanos > ?) OR ( " + "read_time_seconds = ? AND read_time_nanos = ? and path > ?)) ", collections.size(), " UNION ");
    sql.append("ORDER BY read_time_seconds, read_time_nanos, path LIMIT ?");
    Object[] bindVars = new Object[BINDS_PER_STATEMENT * collections.size() + 1];
    int i = 0;
    for (ResourcePath collection : collections) {
        String prefixPath = EncodedPath.encode(collection);
        bindVars[i++] = prefixPath;
        bindVars[i++] = EncodedPath.prefixSuccessor(prefixPath);
        bindVars[i++] = collection.length() + 1;
        bindVars[i++] = readTime.getSeconds();
        bindVars[i++] = readTime.getSeconds();
        bindVars[i++] = readTime.getNanoseconds();
        bindVars[i++] = readTime.getSeconds();
        bindVars[i++] = readTime.getNanoseconds();
        bindVars[i++] = EncodedPath.encode(documentKey.getPath());
    }
    bindVars[i] = count;
    BackgroundQueue backgroundQueue = new BackgroundQueue();
    Map<DocumentKey, MutableDocument> results = new HashMap<>();
    db.query(sql.toString()).binding(bindVars).forEach(row -> processRowInBackground(backgroundQueue, results, row));
    backgroundQueue.drain();
    return results;
}
Also used : BackgroundQueue(com.google.firebase.firestore.util.BackgroundQueue) ResourcePath(com.google.firebase.firestore.model.ResourcePath) HashMap(java.util.HashMap) DocumentKey(com.google.firebase.firestore.model.DocumentKey) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Timestamp(com.google.firebase.Timestamp)

Example 18 with Timestamp

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

the class SQLiteRemoteDocumentCache method add.

@Override
public void add(MutableDocument document, SnapshotVersion readTime) {
    hardAssert(!readTime.equals(SnapshotVersion.NONE), "Cannot add document to the RemoteDocumentCache with a read time of zero");
    DocumentKey documentKey = document.getKey();
    Timestamp timestamp = readTime.getTimestamp();
    MessageLite message = serializer.encodeMaybeDocument(document);
    db.execute("INSERT OR REPLACE INTO remote_documents " + "(path, path_length, read_time_seconds, read_time_nanos, contents) " + "VALUES (?, ?, ?, ?, ?)", EncodedPath.encode(documentKey.getPath()), documentKey.getPath().length(), timestamp.getSeconds(), timestamp.getNanoseconds(), message.toByteArray());
    indexManager.addToCollectionParentIndex(document.getKey().getCollectionPath());
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) Timestamp(com.google.firebase.Timestamp) MessageLite(com.google.protobuf.MessageLite)

Example 19 with Timestamp

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

the class LruGarbageCollectorTestCase method testRemoveOrphanedDocuments.

@Test
public void testRemoveOrphanedDocuments() {
    // Track documents we expect to be retained so we can verify post-GC.
    // This will contain documents associated with targets that survive GC, as well
    // as any documents with pending mutations.
    Set<DocumentKey> expectedRetained = new HashSet<>();
    // we add two mutations later, for now track them in an array.
    List<Mutation> mutations = new ArrayList<>();
    persistence.runTransaction("add a target and add two documents to it", () -> {
        // Add two documents to first target, queue a mutation on the second document
        TargetData targetData = addNextQueryInTransaction();
        MutableDocument doc1 = cacheADocumentInTransaction();
        addDocumentToTarget(doc1.getKey(), targetData.getTargetId());
        expectedRetained.add(doc1.getKey());
        MutableDocument doc2 = cacheADocumentInTransaction();
        addDocumentToTarget(doc2.getKey(), targetData.getTargetId());
        expectedRetained.add(doc2.getKey());
        mutations.add(mutation(doc2.getKey()));
    });
    // Add a second query and register a third document on it
    persistence.runTransaction("second query", () -> {
        TargetData targetData = addNextQueryInTransaction();
        MutableDocument doc3 = cacheADocumentInTransaction();
        addDocumentToTarget(doc3.getKey(), targetData.getTargetId());
        expectedRetained.add(doc3.getKey());
    });
    // cache another document and prepare a mutation on it.
    persistence.runTransaction("queue a mutation", () -> {
        MutableDocument doc4 = cacheADocumentInTransaction();
        mutations.add(mutation(doc4.getKey()));
        expectedRetained.add(doc4.getKey());
    });
    // Insert the mutations. These operations don't have a sequence number, they just
    // serve to keep the mutated documents from being GC'd while the mutations are outstanding.
    persistence.runTransaction("actually register the mutations", () -> {
        Timestamp writeTime = Timestamp.now();
        mutationQueue.addMutationBatch(writeTime, Collections.emptyList(), mutations);
    });
    // Mark 5 documents eligible for GC. This simulates documents that were mutated then ack'd.
    // Since they were ack'd, they are no longer in a mutation queue, and there is nothing keeping
    // them alive.
    Set<DocumentKey> toBeRemoved = new HashSet<>();
    persistence.runTransaction("add orphaned docs (previously mutated, then ack'd)", () -> {
        for (int i = 0; i < 5; i++) {
            MutableDocument doc = cacheADocumentInTransaction();
            toBeRemoved.add(doc.getKey());
            markDocumentEligibleForGcInTransaction(doc.getKey());
        }
    });
    // We expect only the orphaned documents, those not in a mutation or a target, to be removed.
    // use a large sequence number to remove as much as possible
    int removed = garbageCollector.removeOrphanedDocuments(1000);
    assertEquals(toBeRemoved.size(), removed);
    persistence.runTransaction("verify", () -> {
        for (DocumentKey key : toBeRemoved) {
            assertFalse(documentCache.get(key).isValidDocument());
            assertFalse(targetCache.containsKey(key));
        }
        for (DocumentKey key : expectedRetained) {
            assertTrue(documentCache.get(key).isValidDocument());
        }
    });
}
Also used : DocumentKey(com.google.firebase.firestore.model.DocumentKey) ArrayList(java.util.ArrayList) MutableDocument(com.google.firebase.firestore.model.MutableDocument) Mutation(com.google.firebase.firestore.model.mutation.Mutation) SetMutation(com.google.firebase.firestore.model.mutation.SetMutation) Timestamp(com.google.firebase.Timestamp) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with Timestamp

use of com.google.firebase.Timestamp in project Zweet-Fit-App by rachit0904.

the class otp method verifyNewUser.

private boolean verifyNewUser() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("DD-MM-YYYY hh:mm:ss");
    Date now = new Date();
    String[] cur = dateFormat.format(now).split(" ");
    String curDate = cur[0];
    String curDD = curDate.split("-")[0];
    String curMM = curDate.split("-")[1];
    String curYY = curDate.split("-")[2];
    String curTime = cur[1];
    String[] curT = curTime.split(":");
    String curHrs = curT[0];
    String curMin = curT[1];
    Timestamp ts = new Timestamp(new Date(mAuth.getCurrentUser().getMetadata().getCreationTimestamp()));
    Date date = ts.toDate();
    String[] creation = dateFormat.format(date).split(" ");
    String creationDate = creation[0];
    String creationDD = creationDate.split("-")[0];
    String creationMM = creationDate.split("-")[1];
    String creationYY = creationDate.split("-")[2];
    String creationTime = creation[1];
    String creationHrs = creationTime.split(":")[0];
    String creationMin = creationTime.split(":")[1];
    if (Integer.parseInt(creationYY) == Integer.parseInt(curYY) && Integer.parseInt(creationMM) == Integer.parseInt(curMM) && Integer.parseInt(creationDD) == Integer.parseInt(curDD)) {
        if (Integer.parseInt(curHrs) == Integer.parseInt(creationHrs) && (Integer.parseInt(curMin) - Integer.parseInt(creationMin)) < 2) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
Also used : SimpleDateFormat(java.text.SimpleDateFormat) Timestamp(com.google.firebase.Timestamp) Date(java.util.Date)

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