Example #1
0
    def create_sharing_record(user_id, collection_name, callback):
        """
        Returns:
            -The sharing secret key for the create sharing record
        """

        #check to see whether that particular collection has been shared before
        exsisting_sharing_secret = yield gen.Task(SharingController.get_sharing_secret_from_subscriber_info,
            user_id, collection_name)
        if exsisting_sharing_secret is not None:
            SharingController.__log.info('SharingController - exsiting sharing record found for user %s and collection %s' % (user_id, collection_name))
            callback(exsisting_sharing_secret)

        #we couldn't find the sharing secret so create a new sharing record
        else:
            sharing_secret = SharingController.__generate_sharing_secret()
            sharing_collection = DatabaseFactory.get_sharing_collection()
            sharing_record = {SharingRecord.SECRET_KEY : sharing_secret,
                              SharingRecord.OWNER_KEY : user_id,
                              SharingRecord.COLLECTION_NAME_KEY : collection_name,
                              SharingRecord.SUBSCIRBERS_KEY : [(user_id,collection_name)]}

            #add sharing record to the cache
            yield gen.Task(sharing_collection.insert, sharing_record)
            yield gen.Task(SharingController.add_subscriber, user_id, collection_name,
                sharing_secret)

            #cache both the subscription info and the sharing record itself
            sharing_record_json = json.dumps(sharing_record)
            yield gen.Task(SharingController.__cache.set_sharing_record,
                sharing_secret, sharing_record_json)
            callback(sharing_secret)
Example #2
0
    def get_sharing_record_by_secret(sharing_secret, callback):
        """
        Returns:
            -A Sharing record object containing information of the
            sharing space with the sharing_secret and None if the
            sharing record for the specified sharing secret does not
            exist
        """
        #try the cache
        sharing_record_json_str = yield gen.Task(SharingController.__cache.get_sharing_record,
            sharing_secret)
        #cache hit
        if sharing_record_json_str is not None:
            if sharing_record_json_str == 'false':
                callback(None)
            else:
                sharing_record_obj = json.loads(sharing_record_json_str)
                sharing_record = SharingRecord(
                    sharing_record_obj[SharingRecord.SECRET_KEY],
                    sharing_record_obj[SharingRecord.OWNER_KEY],
                    sharing_record_obj[SharingRecord.COLLECTION_NAME_KEY],
                    sharing_record_obj[SharingRecord.SUBSCIRBERS_KEY])
                callback(sharing_record)

        #cache miss
        else:
            sharing_collection = DatabaseFactory.get_sharing_collection()
            query = {SharingRecord.SECRET_KEY : sharing_secret}
            sharing_records_cursor = yield gen.Task(sharing_collection.find, query)
            result_count = len(sharing_records_cursor[0][0])
            #if we have more sharing spaces with this sharing secret
            #something is horribly wrong
            if result_count >= 2:
                SharingController.__log.warning('SharingController - More than two sharing info found for: %s' % sharing_secret)
                yield gen.Task(SharingController.__cache.set_sharing_record, sharing_secret, 'false')
                callback(None)

            if not result_count:
                SharingController.__log.warning('SharingController - no sharing info found for: %s' % sharing_secret)
                yield gen.Task(SharingController.__cache.set_sharing_record, sharing_secret, 'false')
                callback(None)

            else:
                #FIXME: is there a better way to these in asyncMongo other
                #than these ugly indicies
                sharing_record_bson = sharing_records_cursor[0][0][0]
                try:
                    sharing_record = SharingRecord(
                                    sharing_record_bson[SharingRecord.SECRET_KEY],
                                    sharing_record_bson[SharingRecord.OWNER_KEY],
                                    sharing_record_bson[SharingRecord.COLLECTION_NAME_KEY],
                                    sharing_record_bson[SharingRecord.SUBSCIRBERS_KEY])
                    callback(sharing_record)
                except Exception:
                    SharingController.__log.warning('SharingController - corrupted sharing info found for: %s' % sharing_secret)
                    yield gen.Task(SharingController.__cache.set_sharing_record, sharing_secret, 'false')
                    callback(None)
Example #3
0
    def remove_sharing_record_by_secret(sharing_secret, callback):
        """
        Removes a sharing record identified by the sharing_secret

        Returns:
            - void. The callback will be called
        """
        sharing_collection = DatabaseFactory.get_sharing_collection()
        query = {SharingRecord.SECRET_KEY : sharing_secret}
        #first remove subscribers
        yield gen.Task(SharingController.remove_all_subscribers, sharing_secret)
        #the collection itself
        yield gen.Task(sharing_collection.remove, query)
        #remove the record from the cache too
        yield gen.Task(SharingController.__cache.remove_sharing_record, sharing_secret)
        callback()
Example #4
0
    def __update_sharing_record(sharing_record, callback):
        """
        updates the sharing recrod in the mongoDB database.
        The passed in sharing record will replace the one
        in the db

        This method is very dangerous as the user must make sure that
        they also update the subscribers collection in mongo
        """
        sharing_collection = DatabaseFactory.get_sharing_collection()
        doc_key = {SharingRecord.SECRET_KEY: sharing_record.get_sharing_secret()}
        doc_content = sharing_record.toDictionary()
        sharing_record_json = sharing_record.to_json_str()
        yield gen.Task(sharing_collection.update, doc_key, doc_content)
        #update the cache
        yield gen.Task(SharingController.__cache.set_sharing_record,
            sharing_record.get_sharing_secret(), sharing_record_json)
        callback()