def delete(self, singleOrbitRef: DocumentReference): """ Description This function deletes an orbit from the database Note that this function should not be called directly from any logics, since you have to delete all references to it from RideRequest, Event, etc. """ return singleOrbitRef.delete()
def remove_event_schedule_with_transaction( transaction: Transaction, userRef: DocumentReference = None, orbitId: str = None): eventScheduleRef: DocumentReference = userRef.collection( "eventSchedules").document(orbitId) transaction.delete(eventScheduleRef)
def update_in_transaction(transaction: firestore.Transaction, reference: firestore.DocumentReference, document_data: Dict, node_id: str): snapshot = reference.get(transaction=transaction) if not snapshot.exists: transaction.create(reference=reference, document_data=document_data) self.log.info('creating ' + reference.id) return True else: update_time = snapshot.update_time snapshot_data = snapshot.to_dict() snapshot_node_id = snapshot_data[NODE_ID_KEY] time_elapsed = int(time.time()) - update_time.seconds self.log.debug('lease created ' + str(time_elapsed) + ' seconds ago') lease_expired = time_elapsed > LEASE_DURATION_SECONDS lease_owned = snapshot_node_id == node_id if lease_owned or lease_expired: if lease_expired: self.log.info('lease expired for ' + snapshot_node_id) if lease_owned: self.log.info('renewing lease') transaction.update(reference=reference, field_updates=document_data, option=firestore.Client.write_option( last_update_time=update_time)) return True else: self.log.info('lease held by ' + snapshot_node_id) return False
def get_with_transaction(transaction: Transaction, locationRef: DocumentReference) -> Type[Location]: """ Description Note that this cannot take place if transaction already received write operations. "If a transaction is used and it already has write operations added, this method cannot be used (i.e. read-after-write is not allowed)." :type self: :param self: :type transaction:Transaction: :param transaction:Transaction: :type locationRef:DocumentReference: :param locationRef:DocumentReference: :raises: :rtype: """ try: snapshot: DocumentSnapshot = locationRef.get( transaction=transaction) snapshotDict: dict = snapshot.to_dict() location = Location.from_dict(snapshotDict) location.set_firestore_ref(locationRef) return location except google.cloud.exceptions.NotFound: raise Exception('No such document! ' + str(locationRef.id))
def get_with_transaction(self, transaction: Transaction, eventScheduleRef: DocumentReference) \ -> Type[AirportEventSchedule]: """ Description Note that this cannot take place if transaction already received write operations. "If a transaction is used and it already has write operations added, this method cannot be used (i.e. read-after-write is not allowed)." :type self: :param self: :type transaction:Transaction: :param transaction:Transaction: :type eventScheduleRef:DocumentReference: :param eventScheduleRef:DocumentReference: :raises: :rtype: """ try: snapshot: DocumentSnapshot = eventScheduleRef.get( transaction=transaction) snapshotDict: dict = snapshot.to_dict() eventSchedule = AirportEventSchedule.from_dict(snapshotDict) return eventSchedule except google.cloud.exceptions.NotFound: raise Exception('No such document! ' + str(eventScheduleRef.id))
def get(self, eventRef: DocumentReference): if isinstance(eventRef, str): eventRef = str_to_ref(eventRef) snapshot: DocumentSnapshot = eventRef.get() snapshotDict: dict = snapshot.to_dict() event = Event.from_dict(snapshotDict) event.set_firestore_ref(eventRef) return event
def retrieve_nodeID(transaction: firestore.Transaction, reference: firestore.DocumentReference, document_data: Dict, node_id: str): snapshot = reference.get(transaction=transaction) snapshot_data = snapshot.to_dict() snapshot_node_id = snapshot_data[NODE_ID_KEY] return snapshot_node_id == self.node_id
def get(self, locationRef: DocumentReference): if isinstance(locationRef, str): locationRef = str_to_ref(locationRef) snapshot: DocumentSnapshot = locationRef.get() snapshotDict: dict = snapshot.to_dict() location = Location.from_dict(snapshotDict) location.set_firestore_ref(locationRef) return location return locationResult
def delete(self, eventRef: DocumentReference): """ Description This function deletes a ride request from the database :type self: :param self: :type eventRef:DocumentReference: :param eventRef:DocumentReference: :raises: :rtype: """ return eventRef.delete()
def get(self, eventRef: DocumentReference): """ Gets an event from database :param eventRef: firestore document reference of the event :return: event object """ if isinstance(eventRef, str): eventRef = str_to_ref(eventRef) snapshot: DocumentSnapshot = eventRef.get() snapshotDict: dict = snapshot.to_dict() event = Event.from_dict(snapshotDict) event.set_firestore_ref(eventRef) return event
def increment_execution_counter(transaction: Transaction, event_reference: DocumentReference, finished_processing: bool): """Increments execution counter in a task :param transaction: transaction :param event_reference: event document reference :param finished_processing: flags whether the repeated task processing has been finished """ event = event_reference.get().to_dict() transaction.update( event_reference, { 'execution_counter': event.get('execution_counter', 0) + 1, 'processed': finished_processing, })
def get_with_transaction(transaction: Transaction, orbitRef: DocumentReference) -> Orbit: """ Description Note that this cannot take place if transaction already received write operations. "If a transaction is used and it already has write operations added, this method cannot be used (i.e. read-after-write is not allowed)." """ try: snapshot: DocumentSnapshot = orbitRef.get(transaction=transaction) snapshotDict: dict = snapshot.to_dict() orbit = Orbit.from_dict(snapshotDict) orbit.set_firestore_ref(orbitRef) return orbit except google.cloud.exceptions.NotFound: raise Exception('No such document! ' + str(orbitRef.id))
def get_with_transaction(transaction: Transaction, eventRef: DocumentReference) -> Type[Event]: """ Description Note that this cannot take place if transaction already received write operations. "If a transaction is used and it already has write operations added, this method cannot be used (i.e. read-after-write is not allowed)." :type transaction:Transaction: :param transaction:Transaction: firestore transaction :type eventRef:DocumentReference: :param eventRef:DocumentReference: firestore document reference of the event to get :raises: :rtype: """ try: snapshot: DocumentSnapshot = eventRef.get( transaction=transaction) snapshotDict: dict = snapshot.to_dict() event = Event.from_dict(snapshotDict) event.set_firestore_ref(eventRef) return event except google.cloud.exceptions.NotFound: raise Exception('No such document! ' + str(eventRef.id))
def get_user_with_transaction(transaction, userRef: DocumentReference): """ Description Note that this cannot take place if transaction already received write operation :type self: :param self: :type transaction:Transaction: :param transaction:Transaction: :type userRef:DocumentReference: :param userRef:DocumentReference: :raises: :rtype: """ userExists = UserDao.user_exists(userRef) if userExists: snapshot = userRef.get(transaction=transaction) userDict = snapshot.to_dict() _get_auth_info(userRef.id, userDict) user = User.from_dict(userDict) user.set_firestore_ref(userRef) return user else: return None
def user_exists(userRef: DocumentReference): snapshot: DocumentSnapshot = userRef.get() if snapshot.exists: return True else: return False
def get(self, rideRequestRef: DocumentReference): snapshot: DocumentSnapshot = rideRequestRef.get() snapshotDict: dict = snapshot.to_dict() rideRequest = RideRequest.from_dict(snapshotDict) rideRequest.set_firestore_ref(rideRequestRef) return rideRequest
def set(self, location, location_ref: DocumentReference): location_ref.set(location.to_dict())
def get(self, orbitRef: DocumentReference): snapshot: DocumentSnapshot = orbitRef.get() snapshot_dict: dict = snapshot.to_dict() orbit = Orbit.from_dict(snapshot_dict) orbit.set_firestore_ref(orbitRef) return orbit