צבירות זמן כתיבה

שאילתות ב-Cloud Firestore מאפשרות לך למצוא מסמכים באוספים גדולים. כדי לקבל תובנה לגבי המאפיינים של האוסף בכללותו, תוכל לצבור נתונים על גבי אוסף.

אתה יכול לצבור נתונים בזמן הקריאה או בזמן הכתיבה:

  • צבירות של זמן קריאה מחשבות תוצאה בזמן הבקשה. Cloud Firestore תומך בשאילתות הצבירה count() , sum() ו- average() בזמן הקריאה. קל יותר להוסיף שאילתות צבירה בזמן קריאה לאפליקציה שלך מאשר צבירה בזמן כתיבה. למידע נוסף על שאילתות צבירה, ראה סיכום נתונים עם שאילתות צבירה .

  • צבירות זמן כתיבה מחשבות תוצאה בכל פעם שהאפליקציה מבצעת פעולת כתיבה רלוונטית. צבירות של זמן כתיבה הן עבודה רבה יותר ליישום, אך ייתכן שתשתמש בהן במקום צבירות של זמן קריאה מאחת מהסיבות הבאות:

    • אתה רוצה להאזין לתוצאת הצבירה לקבלת עדכונים בזמן אמת. שאילתות הצבירה count() , sum() ו- average() אינן תומכות בעדכונים בזמן אמת.
    • אתה רוצה לאחסן את תוצאת הצבירה במטמון בצד הלקוח. שאילתות הצבירה count() , sum() ו- average() אינן תומכות בשמירת מטמון.
    • אתה צובר נתונים מעשרות אלפי מסמכים עבור כל אחד מהמשתמשים שלך ומתחשב בעלויות. במספר נמוך יותר של מסמכים, צבירה של זמן קריאה עולה פחות. עבור מספר רב של מסמכים בצבירה, צבירה של זמן כתיבה עשויה לעלות פחות.

אתה יכול ליישם צבירה של זמן כתיבה באמצעות עסקה בצד הלקוח או עם פונקציות ענן. הסעיפים הבאים מתארים כיצד ליישם צבירות של זמן כתיבה.

פתרון: צבירה של זמן כתיבה עם עסקה בצד הלקוח

שקול אפליקציית המלצות מקומיות שעוזרת למשתמשים למצוא מסעדות מעולות. השאילתה הבאה מאחזרת את כל הדירוגים של מסעדה נתונה:

אינטרנט

db.collection("restaurants")
  .doc("arinell-pizza")
  .collection("ratings")
  .get();

מָהִיר

הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
do {
  let snapshot = try await db.collection("restaurants")
    .document("arinell-pizza")
    .collection("ratings")
    .getDocuments()
  print(snapshot)
} catch {
  print(error)
}

Objective-C

הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
FIRQuery *query = [[[self.db collectionWithPath:@"restaurants"]
    documentWithPath:@"arinell-pizza"] collectionWithPath:@"ratings"];
[query getDocumentsWithCompletion:^(FIRQuerySnapshot * _Nullable snapshot,
                                    NSError * _Nullable error) {
  // ...
}];

Kotlin+KTX

db.collection("restaurants")
    .document("arinell-pizza")
    .collection("ratings")
    .get()

Java

db.collection("restaurants")
        .document("arinell-pizza")
        .collection("ratings")
        .get();

במקום להביא את כל הדירוגים ולאחר מכן לחשב מידע מצטבר, אנו יכולים לאחסן את המידע הזה במסמך המסעדה עצמו:

אינטרנט

var arinellDoc = {
  name: 'Arinell Pizza',
  avgRating: 4.65,
  numRatings: 683
};

מָהִיר

הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
struct Restaurant {

  let name: String
  let avgRating: Float
  let numRatings: Int

}

let arinell = Restaurant(name: "Arinell Pizza", avgRating: 4.65, numRatings: 683)

Objective-C

הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
@interface FIRRestaurant : NSObject

@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) float averageRating;
@property (nonatomic, readonly) NSInteger ratingCount;

- (instancetype)initWithName:(NSString *)name
               averageRating:(float)averageRating
                 ratingCount:(NSInteger)ratingCount;

@end

@implementation FIRRestaurant

- (instancetype)initWithName:(NSString *)name
               averageRating:(float)averageRating
                 ratingCount:(NSInteger)ratingCount {
  self = [super init];
  if (self != nil) {
    _name = name;
    _averageRating = averageRating;
    _ratingCount = ratingCount;
  }
  return self;
}

@end

Kotlin+KTX

data class Restaurant(
    // default values required for use with "toObject"
    internal var name: String = "",
    internal var avgRating: Double = 0.0,
    internal var numRatings: Int = 0,
)
val arinell = Restaurant("Arinell Pizza", 4.65, 683)

Java

public class Restaurant {
    String name;
    double avgRating;
    int numRatings;

    public Restaurant(String name, double avgRating, int numRatings) {
        this.name = name;
        this.avgRating = avgRating;
        this.numRatings = numRatings;
    }
}
Restaurant arinell = new Restaurant("Arinell Pizza", 4.65, 683);

על מנת לשמור על עקביות צבירות אלו, יש לעדכן אותן בכל פעם שמתווסף דירוג חדש לאוסף המשנה. אחת הדרכים להשיג עקביות היא לבצע את ההוספה והעדכון בעסקה אחת:

אינטרנט

function addRating(restaurantRef, rating) {
    // Create a reference for a new rating, for use inside the transaction
    var ratingRef = restaurantRef.collection('ratings').doc();

    // In a transaction, add the new rating and update the aggregate totals
    return db.runTransaction((transaction) => {
        return transaction.get(restaurantRef).then((res) => {
            if (!res.exists) {
                throw "Document does not exist!";
            }

            // Compute new number of ratings
            var newNumRatings = res.data().numRatings + 1;

            // Compute new average rating
            var oldRatingTotal = res.data().avgRating * res.data().numRatings;
            var newAvgRating = (oldRatingTotal + rating) / newNumRatings;

            // Commit to Firestore
            transaction.update(restaurantRef, {
                numRatings: newNumRatings,
                avgRating: newAvgRating
            });
            transaction.set(ratingRef, { rating: rating });
        });
    });
}

מָהִיר

הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
func addRatingTransaction(restaurantRef: DocumentReference, rating: Float) async {
  let ratingRef: DocumentReference = restaurantRef.collection("ratings").document()

  do {
    let _ = try await db.runTransaction({ (transaction, errorPointer) -> Any? in
      do {
        let restaurantDocument = try transaction.getDocument(restaurantRef).data()
        guard var restaurantData = restaurantDocument else { return nil }

        // Compute new number of ratings
        let numRatings = restaurantData["numRatings"] as! Int
        let newNumRatings = numRatings + 1

        // Compute new average rating
        let avgRating = restaurantData["avgRating"] as! Float
        let oldRatingTotal = avgRating * Float(numRatings)
        let newAvgRating = (oldRatingTotal + rating) / Float(newNumRatings)

        // Set new restaurant info
        restaurantData["numRatings"] = newNumRatings
        restaurantData["avgRating"] = newAvgRating

        // Commit to Firestore
        transaction.setData(restaurantData, forDocument: restaurantRef)
        transaction.setData(["rating": rating], forDocument: ratingRef)
      } catch {
        // Error getting restaurant data
        // ...
      }

      return nil
    })
  } catch {
    // ...
  }
}

Objective-C

הערה: מוצר זה אינו זמין ביעדי watchOS ו-App Clip.
- (void)addRatingTransactionWithRestaurantReference:(FIRDocumentReference *)restaurant
                                             rating:(float)rating {
  FIRDocumentReference *ratingReference =
      [[restaurant collectionWithPath:@"ratings"] documentWithAutoID];

  [self.db runTransactionWithBlock:^id (FIRTransaction *transaction,
                                        NSError **errorPointer) {
    FIRDocumentSnapshot *restaurantSnapshot =
        [transaction getDocument:restaurant error:errorPointer];

    if (restaurantSnapshot == nil) {
      return nil;
    }

    NSMutableDictionary *restaurantData = [restaurantSnapshot.data mutableCopy];
    if (restaurantData == nil) {
      return nil;
    }

    // Compute new number of ratings
    NSInteger ratingCount = [restaurantData[@"numRatings"] integerValue];
    NSInteger newRatingCount = ratingCount + 1;

    // Compute new average rating
    float averageRating = [restaurantData[@"avgRating"] floatValue];
    float newAverageRating = (averageRating * ratingCount + rating) / newRatingCount;

    // Set new restaurant info

    restaurantData[@"numRatings"] = @(newRatingCount);
    restaurantData[@"avgRating"] = @(newAverageRating);

    // Commit to Firestore
    [transaction setData:restaurantData forDocument:restaurant];
    [transaction setData:@{@"rating": @(rating)} forDocument:ratingReference];
    return nil;
  } completion:^(id  _Nullable result, NSError * _Nullable error) {
    // ...
  }];
}

Kotlin+KTX

private fun addRating(restaurantRef: DocumentReference, rating: Float): Task<Void> {
    // Create reference for new rating, for use inside the transaction
    val ratingRef = restaurantRef.collection("ratings").document()

    // In a transaction, add the new rating and update the aggregate totals
    return db.runTransaction { transaction ->
        val restaurant = transaction.get(restaurantRef).toObject<Restaurant>()!!

        // Compute new number of ratings
        val newNumRatings = restaurant.numRatings + 1

        // Compute new average rating
        val oldRatingTotal = restaurant.avgRating * restaurant.numRatings
        val newAvgRating = (oldRatingTotal + rating) / newNumRatings

        // Set new restaurant info
        restaurant.numRatings = newNumRatings
        restaurant.avgRating = newAvgRating

        // Update restaurant
        transaction.set(restaurantRef, restaurant)

        // Update rating
        val data = hashMapOf<String, Any>(
            "rating" to rating,
        )
        transaction.set(ratingRef, data, SetOptions.merge())

        null
    }
}

Java

private Task<Void> addRating(final DocumentReference restaurantRef, final float rating) {
    // Create reference for new rating, for use inside the transaction
    final DocumentReference ratingRef = restaurantRef.collection("ratings").document();

    // In a transaction, add the new rating and update the aggregate totals
    return db.runTransaction(new Transaction.Function<Void>() {
        @Override
        public Void apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {
            Restaurant restaurant = transaction.get(restaurantRef).toObject(Restaurant.class);

            // Compute new number of ratings
            int newNumRatings = restaurant.numRatings + 1;

            // Compute new average rating
            double oldRatingTotal = restaurant.avgRating * restaurant.numRatings;
            double newAvgRating = (oldRatingTotal + rating) / newNumRatings;

            // Set new restaurant info
            restaurant.numRatings = newNumRatings;
            restaurant.avgRating = newAvgRating;

            // Update restaurant
            transaction.set(restaurantRef, restaurant);

            // Update rating
            Map<String, Object> data = new HashMap<>();
            data.put("rating", rating);
            transaction.set(ratingRef, data, SetOptions.merge());

            return null;
        }
    });
}

השימוש בעסקה שומר על הנתונים המצטברים שלך עקביים עם האוסף הבסיסי. כדי לקרוא עוד על עסקאות ב-Cloud Firestore, ראה עסקאות וכתיבה באצווה .

מגבלות

הפתרון המוצג לעיל מדגים צבירת נתונים באמצעות ספריית הלקוחות של Cloud Firestore, אך עליך להיות מודע למגבלות הבאות:

  • אבטחה - עסקאות בצד הלקוח דורשות מתן הרשאה ללקוחות לעדכן את הנתונים המצטברים במסד הנתונים שלך. למרות שאתה יכול להפחית את הסיכונים של גישה זו על ידי כתיבת כללי אבטחה מתקדמים, ייתכן שזה לא מתאים בכל המצבים.
  • תמיכה לא מקוונת - עסקאות בצד הלקוח ייכשלו כאשר המכשיר של המשתמש במצב לא מקוון, מה שאומר שעליך לטפל במקרה זה באפליקציה שלך ולנסות שוב בזמן המתאים.
  • ביצועים - אם העסקה שלך מכילה מספר פעולות קריאה, כתיבה ועדכון, היא עשויה לדרוש מספר בקשות ל-Cloud Firestore. במכשיר נייד זה עלול לקחת זמן רב.
  • קצבי כתיבה - ייתכן שהפתרון הזה לא יעבוד עבור צבירות המתעדכנות לעתים קרובות מכיוון שניתן לעדכן מסמכי Cloud Firestore רק פעם בשנייה לכל היותר. בנוסף, אם עסקה קוראת מסמך ששונה מחוץ לעסקה, היא מנסה שוב מספר סופי של פעמים ואז נכשלת. בדוק את המונים המבוזרים כדי למצוא פתרון רלוונטי עבור צבירות שזקוקות לעדכונים תכופים יותר.

פתרון: צבירה של זמן כתיבה עם פונקציות ענן

אם עסקאות בצד הלקוח אינן מתאימות לאפליקציה שלך, תוכל להשתמש בפונקציית ענן כדי לעדכן את המידע המצטבר בכל פעם שמתווסף דירוג חדש למסעדה:

Node.js

exports.aggregateRatings = functions.firestore
    .document('restaurants/{restId}/ratings/{ratingId}')
    .onWrite(async (change, context) => {
      // Get value of the newly added rating
      const ratingVal = change.after.data().rating;

      // Get a reference to the restaurant
      const restRef = db.collection('restaurants').doc(context.params.restId);

      // Update aggregations in a transaction
      await db.runTransaction(async (transaction) => {
        const restDoc = await transaction.get(restRef);

        // Compute new number of ratings
        const newNumRatings = restDoc.data().numRatings + 1;

        // Compute new average rating
        const oldRatingTotal = restDoc.data().avgRating * restDoc.data().numRatings;
        const newAvgRating = (oldRatingTotal + ratingVal) / newNumRatings;

        // Update restaurant info
        transaction.update(restRef, {
          avgRating: newAvgRating,
          numRatings: newNumRatings
        });
      });
    });

פתרון זה מוריד את העבודה מהלקוח לפונקציה מתארחת, מה שאומר שהאפליקציה לנייד שלך יכולה להוסיף דירוגים מבלי לחכות להשלמת העסקה. קוד המבוצע בפונקציית ענן אינו מחויב לכללי אבטחה, מה שאומר שאינך צריך עוד לתת ללקוחות גישת כתיבה לנתונים המצטברים.

מגבלות

שימוש בפונקציית ענן לצבירה מונע חלק מהבעיות בעסקאות בצד הלקוח, אך מגיע עם סט שונה של מגבלות:

  • עלות - כל דירוג שנוסף יגרום להפעלת Cloud Function, מה שעשוי להגדיל את העלויות שלך. למידע נוסף, עיין בדף התמחור של פונקציות ענן .
  • חביון - על ידי הורדת עבודת הצבירה לפונקציית ענן, האפליקציה שלך לא תראה נתונים מעודכנים עד שפונקציית הענן תסיים לפעול והלקוח קיבל הודעה על הנתונים החדשים. בהתאם למהירות של פונקציית הענן שלך, זה עשוי להימשך זמן רב יותר מביצוע העסקה באופן מקומי.
  • קצבי כתיבה - ייתכן שהפתרון הזה לא יעבוד עבור צבירות המתעדכנות לעתים קרובות מכיוון שניתן לעדכן מסמכי Cloud Firestore רק פעם בשנייה לכל היותר. בנוסף, אם עסקה קוראת מסמך ששונה מחוץ לעסקה, היא מנסה שוב מספר סופי של פעמים ואז נכשלת. בדוק את המונים המבוזרים כדי למצוא פתרון רלוונטי עבור צבירות שזקוקות לעדכונים תכופים יותר.