1/**
2 * Copyright 2021 Alibaba, Inc. and its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15
16 * \author Dianzhang.Chen
17 * \date Mar 2021
18 * \brief Implementation of lsn context format
19 */
20
21
22#include "lsn_context_format.h"
23#include <sstream>
24#include "repository/repository_common/error_code.h"
25
26namespace proxima {
27namespace be {
28namespace repository {
29
30LsnContextFormat::LsnContextFormat(const LsnContextFormat &other) {
31 file_name_ = other.file_name_;
32 position_ = other.position_;
33 seq_id_ = other.seq_id_;
34 mode_ = other.mode_;
35}
36
37LsnContextFormat &LsnContextFormat::operator=(const LsnContextFormat &other) {
38 file_name_ = other.file_name_;
39 position_ = other.position_;
40 seq_id_ = other.seq_id_;
41 mode_ = other.mode_;
42 return *this;
43}
44
45int LsnContextFormat::parse_from_string(const std::string &lsn_context) {
46 if (lsn_context.empty()) {
47 return ErrorCode_InvalidLSNContext;
48 }
49 std::size_t start = 0;
50 std::size_t pos = lsn_context.find(";", start);
51 if (pos != std::string::npos) {
52 file_name_ = lsn_context.substr(start, pos - start);
53 start = pos + 1;
54 } else {
55 return ErrorCode_InvalidLSNContext;
56 }
57
58 pos = lsn_context.find(";", start);
59 if (pos != std::string::npos) {
60 try {
61 position_ = std::stoull(lsn_context.substr(start, pos - start));
62 } catch (...) {
63 return ErrorCode_InvalidLSNContext;
64 }
65 start = pos + 1;
66 } else {
67 return ErrorCode_InvalidLSNContext;
68 }
69
70 pos = lsn_context.find(";", start);
71 if (pos != std::string::npos) {
72 try {
73 seq_id_ = std::stoull(lsn_context.substr(start, pos - start));
74 } catch (...) {
75 return ErrorCode_InvalidLSNContext;
76 }
77 start = pos + 1;
78 } else {
79 return ErrorCode_InvalidLSNContext;
80 }
81
82 unsigned long scan_mode = (unsigned long)ScanMode::FULL;
83 try {
84 scan_mode = std::stoul(lsn_context.substr(start));
85 } catch (...) {
86 return ErrorCode_InvalidLSNContext;
87 }
88 mode_ = static_cast<ScanMode>(scan_mode);
89 return 0;
90}
91
92std::string LsnContextFormat::convert_to_string() const {
93 std::stringstream ss;
94 ss << file_name_ << ";" << position_ << ";" << seq_id_ << ";"
95 << (uint32_t)mode_;
96 return ss.str();
97}
98
99const std::string &LsnContextFormat::file_name() const {
100 return file_name_;
101}
102
103uint64_t LsnContextFormat::position() const {
104 return position_;
105}
106
107uint64_t LsnContextFormat::seq_id() const {
108 return seq_id_;
109}
110
111const ScanMode &LsnContextFormat::mode() const {
112 return mode_;
113}
114
115} // end namespace repository
116} // namespace be
117} // end namespace proxima
118