ResourceOperations.h 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

#pragma once

#include "db/snapshot/Operations.h"

namespace milvus {
namespace engine {
namespace snapshot {

class CollectionCommitOperation : public CommitOperation<CollectionCommit> {
 public:
    using BaseT = CommitOperation<CollectionCommit>;
    CollectionCommitOperation(OperationContext context, ScopedSnapshotT prev_ss) : BaseT(context, prev_ss) {
    }
    CollectionCommitOperation(OperationContext context, ID_TYPE collection_id, ID_TYPE commit_id = 0)
        : BaseT(context, collection_id, commit_id) {
    }

    CollectionCommitPtr
    GetPrevResource() const override {
        return prev_ss_->GetCollectionCommit();
    }

34
    Status
35 36 37 38 39 40 41 42 43 44 45 46
    DoExecute(Store&) override;
};

class PartitionCommitOperation : public CommitOperation<PartitionCommit> {
 public:
    using BaseT = CommitOperation<PartitionCommit>;
    PartitionCommitOperation(const OperationContext& context, ScopedSnapshotT prev_ss);
    PartitionCommitOperation(const OperationContext& context, ID_TYPE collection_id, ID_TYPE commit_id = 0);

    PartitionCommitPtr
    GetPrevResource() const override;

47
    Status
48
    DoExecute(Store&) override;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

    Status
    PreCheck() override;
};

class PartitionOperation : public CommitOperation<Partition> {
 public:
    using BaseT = CommitOperation<Partition>;
    PartitionOperation(const PartitionContext& context, ScopedSnapshotT prev_ss);
    PartitionOperation(const PartitionContext& context, ID_TYPE collection_id, ID_TYPE commit_id = 0);

    Status
    DoExecute(Store& store) override;

    Status
    PreCheck() override;

 protected:
    PartitionContext context_;
68 69 70 71 72 73 74 75 76 77 78
};

class SegmentCommitOperation : public CommitOperation<SegmentCommit> {
 public:
    using BaseT = CommitOperation<SegmentCommit>;
    SegmentCommitOperation(const OperationContext& context, ScopedSnapshotT prev_ss);
    SegmentCommitOperation(const OperationContext& context, ID_TYPE collection_id, ID_TYPE commit_id = 0);

    SegmentCommit::Ptr
    GetPrevResource() const override;

79
    Status
80
    DoExecute(Store&) override;
81 82 83

    Status
    PreCheck() override;
84 85 86 87 88 89 90 91
};

class SegmentOperation : public CommitOperation<Segment> {
 public:
    using BaseT = CommitOperation<Segment>;
    SegmentOperation(const OperationContext& context, ScopedSnapshotT prev_ss);
    SegmentOperation(const OperationContext& context, ID_TYPE collection_id, ID_TYPE commit_id = 0);

92
    Status
93
    DoExecute(Store& store) override;
94 95 96

    Status
    PreCheck() override;
97 98 99 100 101 102 103 104
};

class SegmentFileOperation : public CommitOperation<SegmentFile> {
 public:
    using BaseT = CommitOperation<SegmentFile>;
    SegmentFileOperation(const SegmentFileContext& sc, ScopedSnapshotT prev_ss);
    SegmentFileOperation(const SegmentFileContext& sc, ID_TYPE collection_id, ID_TYPE commit_id = 0);

105
    Status
106 107 108 109 110 111 112 113 114 115 116 117 118
    DoExecute(Store& store) override;

 protected:
    SegmentFileContext context_;
};

template <>
class LoadOperation<Collection> : public Operations {
 public:
    explicit LoadOperation(const LoadOperationContext& context)
        : Operations(OperationContext(), ScopedSnapshotT()), context_(context) {
    }

119
    Status
120
    ApplyToStore(Store& store) override {
121 122 123 124 125
        if (done_) {
            Done();
            return status_;
        }
        Status status;
126
        if (context_.id == 0 && context_.name != "") {
127
            status = store.GetCollection(context_.name, resource_);
128
        } else {
129
            status = store.GetResource<Collection>(context_.id, resource_);
130
        }
131
        SetStatus(status);
132
        Done();
133
        return status_;
134 135
    }

136 137 138 139 140 141 142 143 144 145 146 147 148
    Status
    GetResource(CollectionPtr& res, bool wait = true) {
        if (wait) {
            WaitToFinish();
        }
        auto status = DoneRequired();
        if (!status.ok())
            return status;
        if (!resource_) {
            return Status(SS_NOT_FOUND_ERROR, "No specified resource");
        }
        res = resource_;
        return status;
149 150 151 152 153 154 155 156 157 158
    }

 protected:
    LoadOperationContext context_;
    CollectionPtr resource_;
};

}  // namespace snapshot
}  // namespace engine
}  // namespace milvus