在Firebase队列中扇出数据的正确方法

时间:2016-05-30 20:07:13

标签: firebase firebase-queue

我想将数据扇出到4个不同的节点。 由于firebase事务处理过程可能失败,确保保存所有4个数据的正确方法是什么?

假设我有4个用于保存数据的函数,每个函数都会调用一个事务。

编辑:

function saveToFirstNode(data, response) {
    /*
     * @param
     * [response] is a callback function 
     * that indicates whether the save was successful
     */ 
    firebase.database().ref('cars/' + data.carId)
        .transaction(function(currentData) {
        currentData.distance += data.distance;
        return currentData;
    }, function(error, commited, snapshot) {
        if (error) {
            response(false);
        } else if (commited) {
            response(true)
        }
    }, false);
}
function saveToSecondNode(data, response) {...}
function saveToThirdNode(data, response) {...}
function saveToFourthNode(data, response) {...}

1。使用第一种方法

此方法使用嵌套回调函数,并在保存成功时调用progress() 缺点是,如果其中一个保存过程失败,它将重新保存从第一个到第四个的所有数据。

var queue = new Queue(ref, options, function(data, progress, resolve, reject) {
    saveToFirstNode(data, function(success) {
        if (success) {
            progress(25);
            saveToSecondNode(data, function(success) {
                if (success) {
                    progress(50);
                    saveToThirdNode(data, function(success) {
                        if (success) {
                            progress(75);
                            saveToFourthNode(data, function(success) {
                                if(success) {
                                    resolve(data);
                                } else {
                                    reject();
                                }
                            });
                        } else {
                            reject();
                        }
                    });
                } else {
                    reject();
                }
            });
        } else {
            reject();
        }
    });
});

2。使用第二种方法

此方法可确保保存所有数据。当保存过程失败时,它将从失败的规范重试,而不是从第一个到第四个重试。但是你认为仅仅因为做4次救援是一种矫枉过正吗?这是正确的做法吗?

在队列/规范中定义规范:

{
    "save_to_first_node": {
        "in_progress_state": "save_to_first_node_in_progress",
        "finished_state": "save_to_first_node_finished"
    }, 
    "save_to_second_node": {
        "start_state": "save_to_first_node_finished",
        "in_progress_state": "save_to_second_node_in_progress",
        "finished_state": "save_to_second_node_finished"
    }, 
    "save_to_third_node": {
        "start_state": "save_to_second_node_finished",
        "in_progress_state": "save_to_third_node_in_progress",
        "finished_state": "save_to_third_node_finished"
    }, 
    "save_to_fourth_node": {
        "start_state": "save_to_third_node_finished",
        "in_progress_state": "save_to_fourth_node_in_progress"
    }
}

队列代码:

var saveToFirstNodeOptions = {'specId': 'save_to_first_node'};
var saveToFirstNodeQueue = new Queue(ref, saveToFirstNodeOptions, function(data, progress, resolve, reject) {
    saveToFirstNode(data, function(success) {
        if (success) resolve(data);
        else reject();
    });
});

var saveToSecondNodeOptions = {'specId': 'save_to_second_node'};
var saveToSecondNodeQueue = new Queue(ref, saveToSecondNodeOptions, function(data, progress, resolve, reject) {
    saveToSecondNode(data, function(success) {
        if (success) resolve(data);
        else reject();
    });
});

var saveToThirdNodeOptions = {'specId': 'save_to_third_node'};
var saveToThirdNodeQueue = new Queue(ref, saveToThirdNodeOptions, function(data, progress, resolve, reject) {
    saveToThirdNode(data, function(success) {
        if (success) resolve(data);
        else reject();
    });
});

var saveToFourthNodeOptions = {'specId': 'save_to_fourth_node'};
var saveToFourthNodeQueue = new Queue(ref, saveToFourthNodeOptions, function(data, progress, resolve, reject) {
    saveToFourthNode(data, function(success) {
        if (success) resolve(data);
        else reject();
    });
});

哪种方法正确?这个问题不是偏好,因为这两者是完全不同的方法,对性能和效果都有很大的影响。

0 个答案:

没有答案