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

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

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

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

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

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

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

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

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

אינטרנט

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

Swift

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

Objective-C

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

Kotlin

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
};

Swift

הערה: המוצר הזה לא זמין ב-watchOS וביעדים של קליפים של אפליקציות.
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 וביעדים של קליפים של אפליקציות.
@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

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 });
        });
    });
}

Swift

הערה: המוצר הזה לא זמין ב-watchOS וביעדים של קליפים של אפליקציות.
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 וביעדים של קליפים של אפליקציות.
- (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

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 זמין במאמר Transactions and Batched Writes.

מגבלות

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

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

פתרון: צבירה בזמן הכתיבה באמצעות Cloud Functions

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

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 Function לצורך צבירה מאפשר להימנע מחלק מהבעיות שקשורות לעסקאות בצד הלקוח, אבל יש לו מגבלות אחרות:

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