如何在不预先分配数组的情况下压缩多个observable(1kk)

时间:2016-08-04 05:53:53

标签: rx-java reactive-programming rxjs

我有一个函数,表示db:

中的单个异步插入
var someLongArray = ...;

var massInsertion = [];
for (var i = 0; i < someLongArray.length; i++) {
    massInsertion.push(insertInDatabaseRx(someLongArray[i].data);
}

Rx.Observable.zip(massInsertion)
    .subscribe(
        function (x) {
            logger.debug('Next: %s', x);
        },
        function (e) {
            logger.error('Error: %s', e);
        },
        function () {
            //all done
            logger.info('all save done, terminating ... ');
            process.exit();
        });

我需要进行大量插入(1kk +次)并在完成后执行一些操作。我目前的实施:

var massInsertion = [];
for (var i = 0; i < someLongArray.length; i++) {
    massInsertion.push(insertInDatabaseRx(someLongArray[i].data);
}

它有效,但我认为预分配可观察数组的资源(cpu / mem)并不高效:

protected $primaryKey = 'berita_id';

如果没有preservocation数组的可观察数据,如何更有效地执行此任务?

1 个答案:

答案 0 :(得分:0)

您可以使用forkJoin()

 Function SlotsOccupied(Duration, StartTime, Day)


    Dim SlotsUsed() As String
    ReDim SlotsUsed(0 To Duration)  'sets SlotsUsed array size
    Dim StartT(0 To 1)    'sets Array StartT
    Dim StarTarr As Variant
    Dim DurCount as Integer
    Dim SlotCount as Integer
    Dim StartTCount as Integer


                                      'Sets Counts and starting variables
    DurCount = 0
    SlotCount = 0
    StartTCount = StartTime


    StartT(0) = Day                 'sets StartT array values
    StartT(1) = StartTime


    Do Until DurCount = Duration

        StartTarr = Join(StartT, ",")

        Slot = Application.WorksheetFunction.VLookup(StarTarr, Slots.Range("a2:b56"), 2, False)

        SlotsUsed(SlotCount) = Slot

        DurCount = DurCount + 1
        StartTCount = StarTCount + 1
        SlotCount = SlotCount + 1

    Loop

    SlotsOccupied = Join(SlotsUsed, ",")