Skip to content

Commit

Permalink
Fix incomplete chunked data issue #200
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Jan 6, 2025
1 parent da43ba9 commit 65e99ed
Show file tree
Hide file tree
Showing 14 changed files with 210 additions and 153 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mobizt/FirebaseClient/.github%2Fworkflows%2Fcompile_library.yml?logo=github&label=compile) [![Github Stars](https://img.shields.io/github/stars/mobizt/FirebaseClient?logo=github)](https://github.com/mobizt/FirebaseClient/stargazers) ![Github Issues](https://img.shields.io/github/issues/mobizt/FirebaseClient?logo=github)

![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.13-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)
![GitHub Release](https://img.shields.io/github/v/release/mobizt/FirebaseClient) ![Arduino](https://img.shields.io/badge/Arduino-v1.4.14-57C207?logo=arduino) ![PlatformIO](https://badges.registry.platformio.org/packages/mobizt/library/FirebaseClient.svg) ![GitHub Release Date](https://img.shields.io/github/release-date/mobizt/FirebaseClient)

[![GitHub Sponsors](https://img.shields.io/github/sponsors/mobizt?logo=github)](https://github.com/sponsors/mobizt)

Revision `2024-12-31T17:50:08Z`
Revision `2025-01-06T05:00:54Z`

## Table of Contents

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ AsyncClient aClient(ssl_client, getNetwork(network));

Firestore::Documents Docs;

bool taskCompleted = false;
int data_count = 0;

int cnt = 0;
unsigned long dataMillis = 0;

void setup()
{
Expand Down Expand Up @@ -128,18 +128,25 @@ void loop()

Docs.loop();

if (app.ready() && !taskCompleted)
if (app.ready() && (millis() - dataMillis > 20000 || dataMillis == 0))
{
taskCompleted = true;
dataMillis = millis();

// Note: If new document created under non-existent ancestor documents, that document will not appear in queries and snapshot
// We will create the documents in this parent path "test_doc_creation/doc_1/col_1/data_?"
// (collection > document > collection > documents that contains fields).

// Note: If new document created under non-existent ancestor documents as in this example
// which the document "test_doc_creation/doc_1" does not exist, that document (doc_1) will not appear in queries and snapshot
// https://cloud.google.com/firestore/docs/using-console#non-existent_ancestor_documents.

// We will create the document in the parent path "a0/b?
// a0 is the collection id, b? is the document id in collection a0.
// In the console, you can create the ancestor document "test_doc_creation/doc_1" before running this example
// to avoid non-existent ancestor documents case.


String documentPath = "test_doc_creation/doc_1/col_1/data_";

String documentPath = "a0/b";
documentPath += cnt;
documentPath += data_count;
data_count++;

// If the document path contains space e.g. "a b c/d e f"
// It should encode the space as %20 then the path will be "a%20b%20c/d%20e%20f"
Expand All @@ -154,7 +161,7 @@ void loop()
Values::BooleanValue bolV(true);

// integer
Values::IntegerValue intV(random(500, 1000));
Values::IntegerValue intV(data_count);

// null
Values::NullValue nullV;
Expand Down Expand Up @@ -197,7 +204,7 @@ void loop()

// The value of Values::xxxValue, Values::Value and Document can be printed on Serial.

Serial.println("Create document... ");
Serial.println("Creating a document... ");

Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, asyncCB, "createDocumentTask");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,6 @@ AsyncClient aClient(ssl_client, getNetwork(network));

Firestore::Documents Docs;

int counter = 0;

unsigned long dataMillis = 0;

bool taskCompleted = false;

void setup()
Expand Down Expand Up @@ -132,27 +128,22 @@ void loop()

Docs.loop();

if (app.ready() && (millis() - dataMillis > 60000 || dataMillis == 0))
if (app.ready() && !taskCompleted)
{
dataMillis = millis();

if (!taskCompleted)
{
taskCompleted = true;
taskCompleted = true;

// aa is the collection id, bb is the document id in collection aa.
String documentPath = "aa/bb";
// collection id > document id.
String documentPath = "test_doc_deletion/my_doc";

Serial.println("Create a document... ");
Serial.println("Creating a document... ");

Document<Values::Value> doc("myDouble", Values::Value(Values::DoubleValue(123.456)));
Document<Values::Value> doc("myDouble", Values::Value(Values::DoubleValue(123.456)));

Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, asyncCB, "createDocumentTask");
Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, asyncCB, "createDocumentTask");

Serial.println("Delete a document... ");
Serial.println("Deleting a document... ");

Docs.deleteDoc(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, Precondition() /* Precondition (currentocument) */, asyncCB, "deleteDocTask");
}
Docs.deleteDoc(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, Precondition() /* Precondition (currentocument) */, asyncCB, "deleteDocTask");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,41 +128,55 @@ void loop()

Docs.loop();

if (app.ready() && (millis() - dataMillis > 60000 || dataMillis == 0))
if (app.ready() && (millis() - dataMillis > 20000 || dataMillis == 0))
{
dataMillis = millis();

Serial.println("Query a Firestore database... ");
Serial.println("Querying a Firestore database... ");

// You can run the CreateDocument.ino example to create the data before querying.

StructuredQuery query;

Projection projection(FieldReference("myDouble"));
// This change in v1.0.7 from add to fields to correspond to the Firestore documentation.
projection.fields(FieldReference("myInteger"));
projection.fields(FieldReference("myBool"));
projection.fields(FieldReference("myInt"));
projection.fields(FieldReference("myTimestamp"));
projection.fields(FieldReference("myBytes"));
projection.fields(FieldReference("myString"));
projection.fields(FieldReference("myArr"));
projection.fields(FieldReference("myMap"));
projection.fields(FieldReference("myGeo"));
query.select(projection);

// Or shorter form
// query.select(Projection(FieldReference("myDouble")).add(FieldReference("myInteger")).add(FieldReference("myTimestamp")));
// query.select(Projection(FieldReference("myDouble")).add(FieldReference("myBool")).add(FieldReference("myInt")));

query.from(CollectionSelector("a0", true));
query.from(CollectionSelector("col_1", false));

Order order;
order.field(FieldReference("myInteger"));
order.direction(FilterSort::DESCENDING);
query.orderBy(order);
// This ordering requires index
// Order order;
// order.field(FieldReference("myInt"));
// order.direction(FilterSort::DESCENDING);
// query.orderBy(order);
// Or shorter form
// query.orderBy(Order(FieldReference("myInteger"), FilterSort::DESCENDING));
// query.orderBy(Order(FieldReference("myInt"), FilterSort::DESCENDING));

FieldFilter fieldFilter;
Values::StringValue stringValue("hello");
Values::Value val(stringValue);
fieldFilter.field(FieldReference("myString")).op(FieldFilterOperator::EQUAL).value(val);
Filter filter(fieldFilter);
query.where(filter);

query.limit(3);
query.limit(4);

QueryOptions queryOptions;
queryOptions.structuredQuery(query);

query.clear();
projection.clear();
order.clear();

String documentPath = "/"; // Query from all collections under root
String documentPath = "test_doc_creation/doc_1";

// You can set the content of queryOptions object directly with queryOptions.setContent("your content")

Expand Down Expand Up @@ -201,5 +215,7 @@ void printResult(AsyncResult &aResult)
if (aResult.available())
{
Firebase.printf("task: %s, payload: %s\n", aResult.uid().c_str(), aResult.c_str());
// The information printed from Firebase.printf may be truncated because of limited buffer memory to reduce the stack usage,
// use Serial.println(aResult.c_str()) to print entire content.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ Firestore::Documents Docs;

AsyncResult aResult_no_callback;

bool taskCompleted = false;
int data_count = 0;

int cnt = 0;
unsigned long dataMillis = 0;

void setup()
{
Expand Down Expand Up @@ -127,29 +127,39 @@ void loop()

Docs.loop();

if (app.ready() && !taskCompleted)
if (app.ready() && (millis() - dataMillis > 20000 || dataMillis == 0))
{
taskCompleted = true;
dataMillis = millis();

// Note: If new document created under non-existent ancestor documents, that document will not appear in queries and snapshot
// We will create the documents in this parent path "test_doc_creation/doc_1/col_1/data_?"
// (collection > document > collection > documents that contains fields).

// Note: If new document created under non-existent ancestor documents as in this example
// which the document "test_doc_creation/doc_1" does not exist, that document (doc_1) will not appear in queries and snapshot
// https://cloud.google.com/firestore/docs/using-console#non-existent_ancestor_documents.

// We will create the document in the parent path "a0/b?
// a0 is the collection id, b? is the document id in collection a0.
// In the console, you can create the ancestor document "test_doc_creation/doc_1" before running this example
// to avoid non-existent ancestor documents case.

String documentPath = "test_doc_creation/doc_1/col_1/data_";

String documentPath = "a0/b" + String(cnt);
documentPath += data_count;
data_count++;

// If the document path contains space e.g. "a b c/d e f"
// It should encode the space as %20 then the path will be "a%20b%20c/d%20e%20f"

// double
Values::DoubleValue dblV(random(1, 500) / 100.0);
// double (obsoleted)
// Values::DoubleValue dblV(1234.567891);

// double value with precision.
Values::DoubleValue dblV(number_t(1234.567891, 6));

// boolean
Values::BooleanValue bolV(true);

// integer
Values::IntegerValue intV(random(500, 1000));
Values::IntegerValue intV(data_count);

// null
Values::NullValue nullV;
Expand Down Expand Up @@ -178,8 +188,11 @@ void loop()
Values::MapValue mapV("name", Values::StringValue("wrench"));
mapV.add("mass", Values::StringValue("1.3kg")).add("count", Values::IntegerValue(3));

// lat long
Values::GeoPointValue geoV(1.486284, 23.678198);
// lat long (Obsoleated)
// Values::GeoPointValue geoV(1.486284, 23.678198);

// lat long with precision
Values::GeoPointValue geoV(number_t(1.486284, 6), number_t(23.678198, 6));

Document<Values::Value> doc("myDouble", Values::Value(dblV));
doc.add("myBool", Values::Value(bolV)).add("myInt", Values::Value(intV)).add("myNull", Values::Value(nullV));
Expand All @@ -189,7 +202,7 @@ void loop()

// The value of Values::xxxValue, Values::Value and Document can be printed on Serial.

Serial.println("Create document... ");
Serial.println("Creating a document... ");

Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, aResult_no_callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ Firestore::Documents Docs;

AsyncResult aResult_no_callback;

int counter = 0;

unsigned long dataMillis = 0;

bool taskCompleted = false;

void setup()
Expand Down Expand Up @@ -131,27 +127,22 @@ void loop()

Docs.loop();

if (app.ready() && (millis() - dataMillis > 60000 || dataMillis == 0))
if (app.ready() && !taskCompleted)
{
dataMillis = millis();

if (!taskCompleted)
{
taskCompleted = true;
taskCompleted = true;

// aa is the collection id, bb is the document id in collection aa.
String documentPath = "aa/bb";
// collection id > document id.
String documentPath = "test_doc_deletion/my_doc";

Serial.println("Create a document... ");
Serial.println("Creating a document... ");

Document<Values::Value> doc("myDouble", Values::Value(Values::DoubleValue(123.456)));
Document<Values::Value> doc("myDouble", Values::Value(Values::DoubleValue(123.456)));

Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, aResult_no_callback);
Docs.createDocument(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, DocumentMask(), doc, aResult_no_callback);

Serial.println("Delete a document... ");
Serial.println("Deleting a document... ");

Docs.deleteDoc(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, Precondition() /* Precondition (currentocument) */, aResult_no_callback);
}
Docs.deleteDoc(aClient, Firestore::Parent(FIREBASE_PROJECT_ID), documentPath, Precondition() /* Precondition (currentocument) */, aResult_no_callback);
}

printResult(aResult_no_callback);
Expand Down
Loading

0 comments on commit 65e99ed

Please sign in to comment.