// File: yangwanyuan[P]HLKVDS/src/DS_MultiVolume_Impl.cc
// Function: hlkvds::DS_MultiVolume_Impl::WriteBatchData(hlkvds::WriteBatch*)
Status DS_MultiVolume_Impl::WriteBatchData(WriteBatch *batch) {
    uint32_t seg_id = 0;
    bool ret;

    int vol_id = pickVol();
    Volume *vol = volMap_[vol_id];

    //TODO: use GcSegment first, need abstract segment.
    while (!vol->Alloc(seg_id)) {
        ret = vol->ForeGC();
        if (!ret) {
            __ERROR("Cann't get a new Empty Segment.\n");
            return Status::Aborted("Can't allocate a Empty Segment.");
        }
    }

    SegForSlice *seg = new SegForSlice(vol, idxMgr_);
    for (std::list<KVSlice *>::iterator iter = batch->batch_.begin();
            iter != batch->batch_.end(); iter++) {
        if (seg->TryPut(*iter)) {
            seg->Put(*iter);
        }
        else {
            __ERROR("The Batch is too large, can't put in one segment");
            delete seg;
            return Status::Aborted("Batch is too large.");
        }
    }

    seg->SetSegId(seg_id);
    ret = seg->WriteSegToDevice();
    if(!ret) {
        __ERROR("Write batch segment to device failed");
        vol->FreeForFailed(seg_id);
        delete seg;
        return Status::IOError("could not write batch segment to device ");
    }

    uint32_t free_size = seg->GetFreeSize();
    vol->Use(seg_id, free_size);
    seg->UpdateToIndex();
    delete seg;
    return Status::OK();
}

// Assembly:
subq $0xc8, %rsp
movq %rdi, 0x48(%rsp)
movq %rdi, %rax
movq %rax, 0x50(%rsp)
movq %rdi, 0xc0(%rsp)
movq %rsi, 0xb8(%rsp)
movq %rdx, 0xb0(%rsp)
movq 0xb8(%rsp), %rdi
movq %rdi, 0x58(%rsp)
movl $0x0, 0xac(%rsp)
callq 0x11fa0
movq 0x58(%rsp), %rdi
movl %eax, 0xa4(%rsp)
addq $0x28, %rdi
leaq 0xa4(%rsp), %rsi
callq 0x15a50
movq (%rax), %rax
movq %rax, 0x98(%rsp)
movq 0x98(%rsp), %rdi
leaq 0xac(%rsp), %rsi
callq 0x50e40
xorb $-0x1, %al
testb $0x1, %al
jne 0x12f61
jmp 0x13010
movq 0x98(%rsp), %rdi
callq 0x51250
andb $0x1, %al
movb %al, 0xab(%rsp)
testb $0x1, 0xab(%rsp)
jne 0x1300b
jmp 0x12f87
movq 0x5e04a(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x40(%rsp)
callq 0x8480
movq 0x40(%rsp), %rdi
movq %rax, %rdx
leaq 0x3f507(%rip), %rsi     # 0x524b1
leaq 0x3f707(%rip), %rcx     # 0x526b8
leaq 0x3f599(%rip), %r8      # 0x52551
movl $0x7c, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x5e00c(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x3f6f1(%rip), %rsi     # 0x526c7
movb $0x0, %al
callq 0x86f0
movq 0x5dff4(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x3fdd7(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movq 0x48(%rsp), %rdi
leaq 0x3f6e7(%rip), %rsi     # 0x526e8
callq 0x163e0
jmp 0x1331b
jmp 0x12f41
movl $0x50, %edi
callq 0x84f0
movq %rax, %rdi
movq 0x58(%rsp), %rax
movq %rdi, %rcx
movq %rcx, 0x38(%rsp)
movq 0x98(%rsp), %rsi
movq 0x20(%rax), %rdx
callq 0x432f0
jmp 0x1303d
movq 0x38(%rsp), %rax
movq %rax, 0x90(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x16410
movq %rax, 0x78(%rsp)
movq 0xb0(%rsp), %rdi
callq 0x16460
movq %rax, 0x70(%rsp)
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x16440
testb $0x1, %al
jne 0x13086
jmp 0x131c1
movq 0x90(%rsp), %rax
movq %rax, 0x30(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x16490
movq 0x30(%rsp), %rdi
movq (%rax), %rsi
callq 0x418e0
testb $0x1, %al
jne 0x130b0
jmp 0x130fc
movq 0x90(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x78(%rsp), %rdi
callq 0x16490
movq 0x28(%rsp), %rdi
movq (%rax), %rsi
callq 0x41960
jmp 0x131a9
movq 0x38(%rsp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x88(%rsp)
movl %eax, 0x84(%rsp)
callq 0x84b0
jmp 0x13328
jmp 0x130fe
movq 0x5ded3(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x20(%rsp)
callq 0x8480
movq 0x20(%rsp), %rdi
movq %rax, %rdx
leaq 0x3f390(%rip), %rsi     # 0x524b1
leaq 0x3f590(%rip), %rcx     # 0x526b8
leaq 0x3f422(%rip), %r8      # 0x52551
movl $0x88, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x5de95(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x3f5bb(%rip), %rsi     # 0x52708
movb $0x0, %al
callq 0x86f0
movq 0x5de7d(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x3fc60(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movq 0x90(%rsp), %rax
movq %rax, 0x18(%rsp)
cmpq $0x0, %rax
je 0x13193
movq 0x18(%rsp), %rdi
callq 0x43240
movq 0x18(%rsp), %rdi
callq 0x84b0
movq 0x48(%rsp), %rdi
leaq 0x3f59a(%rip), %rsi     # 0x52739
callq 0x163e0
jmp 0x1331b
jmp 0x131ab
leaq 0x78(%rsp), %rdi
xorl %esi, %esi
callq 0x164c0
movq %rax, 0x68(%rsp)
jmp 0x1305c
movq 0x90(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x164f0
movq 0x90(%rsp), %rdi
callq 0x41bb0
andb $0x1, %al
movb %al, 0xab(%rsp)
testb $0x1, 0xab(%rsp)
jne 0x132b7
jmp 0x131fb
movq 0x5ddd6(%rip), %rax     # 0x70fd8
movq (%rax), %rax
movq %rax, 0x10(%rsp)
callq 0x8480
movq 0x10(%rsp), %rdi
movq %rax, %rdx
leaq 0x3f293(%rip), %rsi     # 0x524b1
leaq 0x3f493(%rip), %rcx     # 0x526b8
leaq 0x3f325(%rip), %r8      # 0x52551
movl $0x91, %r9d
movb $0x0, %al
callq 0x86f0
movq 0x5dd98(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x3f503(%rip), %rsi     # 0x5274d
movb $0x0, %al
callq 0x86f0
movq 0x5dd80(%rip), %rax     # 0x70fd8
movq (%rax), %rdi
leaq 0x3fb63(%rip), %rsi     # 0x52dc5
movb $0x0, %al
callq 0x86f0
movq 0x98(%rsp), %rdi
movl 0xac(%rsp), %esi
callq 0x511c0
movq 0x90(%rsp), %rax
movq %rax, 0x8(%rsp)
cmpq $0x0, %rax
je 0x132a4
movq 0x8(%rsp), %rdi
callq 0x43240
movq 0x8(%rsp), %rdi
callq 0x84b0
movq 0x48(%rsp), %rdi
leaq 0x3f4c2(%rip), %rsi     # 0x52772
callq 0x16510
jmp 0x1331b
movq 0x90(%rsp), %rdi
callq 0x16540
movl %eax, 0x64(%rsp)
movq 0x98(%rsp), %rdi
movl 0xac(%rsp), %esi
movl 0x64(%rsp), %edx
callq 0x51220
movq 0x90(%rsp), %rdi
callq 0x43330
movq 0x90(%rsp), %rax
movq %rax, (%rsp)
cmpq $0x0, %rax
je 0x13311
movq (%rsp), %rdi
callq 0x43240
movq (%rsp), %rdi
callq 0x84b0
movq 0x48(%rsp), %rdi
callq 0x16560
movq 0x50(%rsp), %rax
addq $0xc8, %rsp
retq
movq 0x88(%rsp), %rdi
callq 0x8820
nopw %cs:(%rax,%rax)