TaskTable.cpp 3.0 KB
Newer Older
W
wxyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*******************************************************************************
 * Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited.
 * Proprietary and confidential.
 ******************************************************************************/

#include "TaskTable.h"
#include <vector>


namespace zilliz {
namespace milvus {
namespace engine {


void
TaskTable::Put(TaskPtr task) {
W
wxyu 已提交
18 19 20 21
    auto item = std::make_shared<TaskTableItem>();
    item->task = std::move(task);
    item->state = TaskTableItemState::LOADED;
    table_.push_back(item);
W
wxyu 已提交
22 23 24 25
}

void
TaskTable::Put(std::vector<TaskPtr> &tasks) {
W
wxyu 已提交
26 27 28 29 30 31
    for (auto &task : tasks) {
        auto item = std::make_shared<TaskTableItem>();
        item->task = std::move(task);
        item->state = TaskTableItemState::LOADED;
        table_.push_back(item);
    }
W
wxyu 已提交
32 33 34
}


W
wxyu 已提交
35
TaskTableItemPtr
W
wxyu 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
TaskTable::Get(uint64_t index) {
    return table_[index];
}

void
TaskTable::Clear() {
// find first task is NOT (done or moved), erase from begin to it;
//        auto iterator = table_.begin();
//        while (iterator->state == TaskTableItemState::EXECUTED or
//            iterator->state == TaskTableItemState::MOVED)
//            iterator++;
//        table_.erase(table_.begin(), iterator);
}

bool
TaskTable::Move(uint64_t index) {
    auto &task = table_[index];

W
wxyu 已提交
54 55 56
    std::lock_guard<std::mutex> lock(task->mutex);
    if (task->state == TaskTableItemState::START) {
        task->state = TaskTableItemState::LOADING;
W
wxyu 已提交
57 58 59 60 61 62 63
        return true;
    }
    return false;
}

bool
TaskTable::Moved(uint64_t index) {
W
wxyu 已提交
64 65 66 67 68 69 70
    auto &task = table_[index];

    std::lock_guard<std::mutex> lock(task->mutex);
    if (task->state == TaskTableItemState::MOVING) {
        task->state = TaskTableItemState::MOVED;
        return true;
    }
W
wxyu 已提交
71 72 73 74 75
    return false;
}

bool
TaskTable::Load(uint64_t index) {
W
wxyu 已提交
76 77 78 79 80 81 82
    auto &task = table_[index];

    std::lock_guard<std::mutex> lock(task->mutex);
    if (task->state == TaskTableItemState::START) {
        task->state = TaskTableItemState::LOADING;
        return true;
    }
W
wxyu 已提交
83 84 85 86 87
    return false;
}

bool
TaskTable::Loaded(uint64_t index) {
W
wxyu 已提交
88 89 90 91 92 93 94
    auto &task = table_[index];

    std::lock_guard<std::mutex> lock(task->mutex);
    if (task->state == TaskTableItemState::LOADING) {
        task->state = TaskTableItemState::LOADED;
        return true;
    }
W
wxyu 已提交
95 96 97 98 99
    return false;
}

bool
TaskTable::Execute(uint64_t index) {
W
wxyu 已提交
100 101 102 103 104 105 106
    auto &task = table_[index];

    std::lock_guard<std::mutex> lock(task->mutex);
    if (task->state == TaskTableItemState::LOADED) {
        task->state = TaskTableItemState::EXECUTING;
        return true;
    }
W
wxyu 已提交
107 108 109 110 111
    return false;
}

bool
TaskTable::Executed(uint64_t index) {
W
wxyu 已提交
112 113 114 115 116 117 118
    auto &task = table_[index];

    std::lock_guard<std::mutex> lock(task->mutex);
    if (task->state == TaskTableItemState::EXECUTING) {
        task->state = TaskTableItemState::EXECUTED;
        return true;
    }
W
wxyu 已提交
119 120 121 122 123 124 125 126 127 128 129
    return false;
}

std::string
TaskTable::Dump() {
    return std::string();
}

}
}
}