Skip to content

Commit

Permalink
- absl::Status Tuple::SetField -> void Tuple::SetField
Browse files Browse the repository at this point in the history
  • Loading branch information
korbiniak committed May 23, 2023
1 parent 2faae15 commit d0ef9fc
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 87 deletions.
11 changes: 3 additions & 8 deletions komfydb/common/tuple.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,12 @@ absl::StatusOr<std::unique_ptr<Field>> Tuple::ReleaseField(int i) {
return std::move(fields[i]);
}

absl::Status Tuple::SetField(int i, std::unique_ptr<Field> f) {
if (fields.size() <= i || i < 0) {
return absl::InvalidArgumentError("Index out of range");
}

void Tuple::SetField(int i, std::unique_ptr<Field> f) {
fields[i] = std::move(f);
return absl::OkStatus();
}

absl::Status Tuple::SetField(int i, Field* f) {
return SetField(i, f->CreateCopy());
void Tuple::SetField(int i, Field* f) {
SetField(i, f->CreateCopy());
}

Tuple::operator std::string() const {
Expand Down
4 changes: 2 additions & 2 deletions komfydb/common/tuple.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ class Tuple {

absl::StatusOr<std::unique_ptr<Field>> ReleaseField(int i);

absl::Status SetField(int i, std::unique_ptr<Field> f);
void SetField(int i, std::unique_ptr<Field> f);

absl::Status SetField(int i, Field* f);
void SetField(int i, Field* f);

operator std::string() const;

Expand Down
38 changes: 19 additions & 19 deletions komfydb/common/tuple_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ TEST(Tuple, StringConversion) {

Tuple tuple(tv.size());

EXPECT_TRUE(tuple.SetField(0, std::make_unique<IntField>(1)).ok());
tuple.SetField(0, std::make_unique<IntField>(1));

Field* f1 = tuple.GetField(0);
int i = (static_cast<IntField*>(f1))->GetValue();
Expand All @@ -30,23 +30,23 @@ TEST(Tuple, Comparison) {

Tuple t1(types1.size()), t2(types1.size()), t3(types1.size()),
t4(types2.size());
ASSERT_TRUE(t1.SetField(0, std::make_unique<IntField>(1)).ok());
ASSERT_TRUE(t1.SetField(1, std::make_unique<StringField>("a")).ok());
ASSERT_TRUE(t1.SetField(2, std::make_unique<IntField>(2)).ok());
ASSERT_TRUE(t1.SetField(3, std::make_unique<StringField>("b")).ok());
t1.SetField(0, std::make_unique<IntField>(1));
t1.SetField(1, std::make_unique<StringField>("a"));
t1.SetField(2, std::make_unique<IntField>(2));
t1.SetField(3, std::make_unique<StringField>("b"));

ASSERT_TRUE(t2.SetField(0, std::make_unique<IntField>(1)).ok());
ASSERT_TRUE(t2.SetField(1, std::make_unique<StringField>("a")).ok());
ASSERT_TRUE(t2.SetField(2, std::make_unique<IntField>(2)).ok());
ASSERT_TRUE(t2.SetField(3, std::make_unique<StringField>("b")).ok());
t2.SetField(0, std::make_unique<IntField>(1));
t2.SetField(1, std::make_unique<StringField>("a"));
t2.SetField(2, std::make_unique<IntField>(2));
t2.SetField(3, std::make_unique<StringField>("b"));

ASSERT_TRUE(t3.SetField(0, std::make_unique<IntField>(1)).ok());
ASSERT_TRUE(t3.SetField(1, std::make_unique<StringField>("a")).ok());
ASSERT_TRUE(t3.SetField(2, std::make_unique<IntField>(2)).ok());
ASSERT_TRUE(t3.SetField(3, std::make_unique<StringField>("c")).ok());
t3.SetField(0, std::make_unique<IntField>(1));
t3.SetField(1, std::make_unique<StringField>("a"));
t3.SetField(2, std::make_unique<IntField>(2));
t3.SetField(3, std::make_unique<StringField>("c"));

ASSERT_TRUE(t4.SetField(0, std::make_unique<IntField>(1)).ok());
ASSERT_TRUE(t4.SetField(1, std::make_unique<StringField>("a")).ok());
t4.SetField(0, std::make_unique<IntField>(1));
t4.SetField(1, std::make_unique<StringField>("a"));

EXPECT_EQ(t1, t1);
EXPECT_EQ(t1, t2);
Expand All @@ -60,10 +60,10 @@ TEST(Tuple, CopyAssignment) {
const std::vector<Type> types1 = {Type::INT, Type::STRING, Type::INT,
Type::STRING};
Tuple t1(types1.size());
ASSERT_TRUE(t1.SetField(0, std::make_unique<IntField>(1)).ok());
ASSERT_TRUE(t1.SetField(1, std::make_unique<StringField>("a")).ok());
ASSERT_TRUE(t1.SetField(2, std::make_unique<IntField>(2)).ok());
ASSERT_TRUE(t1.SetField(3, std::make_unique<StringField>("b")).ok());
t1.SetField(0, std::make_unique<IntField>(1));
t1.SetField(1, std::make_unique<StringField>("a"));
t1.SetField(2, std::make_unique<IntField>(2));
t1.SetField(3, std::make_unique<StringField>("b"));

Tuple t2(types1.size());
t2 = t1;
Expand Down
23 changes: 11 additions & 12 deletions komfydb/custom_hash_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace {

using komfydb::common::Field;
using komfydb::common::IntField;
using komfydb::common::StringField;
using komfydb::common::Tuple;
Expand Down Expand Up @@ -65,17 +64,17 @@ TEST(Tuple, SupportsAbslHash) {
std::unique_ptr<StringField> str4 = std::make_unique<StringField>("");
Tuple t1(types.size()), t2(types.size()), t3(types2.size()),
t4(types2.size()), t5(types3.size());
ASSERT_TRUE(t1.SetField(0, std::move(int1)).ok());
ASSERT_TRUE(t1.SetField(1, std::move(str1)).ok());
ASSERT_TRUE(t1.SetField(2, std::move(int2)).ok());
ASSERT_TRUE(t2.SetField(0, std::move(int3)).ok());
ASSERT_TRUE(t2.SetField(1, std::move(str2)).ok());
ASSERT_TRUE(t2.SetField(2, std::move(int4)).ok());
ASSERT_TRUE(t3.SetField(0, std::move(str3)).ok());
ASSERT_TRUE(t3.SetField(1, std::move(int5)).ok());
ASSERT_TRUE(t4.SetField(0, std::move(str4)).ok());
ASSERT_TRUE(t4.SetField(1, std::move(int6)).ok());
ASSERT_TRUE(t5.SetField(0, std::move(int7)).ok());
t1.SetField(0, std::move(int1));
t1.SetField(1, std::move(str1));
t1.SetField(2, std::move(int2));
t2.SetField(0, std::move(int3));
t2.SetField(1, std::move(str2));
t2.SetField(2, std::move(int4));
t3.SetField(0, std::move(str3));
t3.SetField(1, std::move(int5));
t4.SetField(0, std::move(str4));
t4.SetField(1, std::move(int6));
t5.SetField(0, std::move(int7));

EXPECT_TRUE(absl::VerifyTypeImplementsAbslHashCorrectly({
t1,
Expand Down
7 changes: 3 additions & 4 deletions komfydb/execution/aggregate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ absl::Status FillGroupIdFields(Tuple& group_id,
std::vector<Type>& groupby_types,
Record* record) {
for (int i = 0; i < groupby_fields.size(); i++) {
RETURN_IF_ERROR(group_id.SetField(i, record->GetField(groupby_fields[i])));
group_id.SetField(i, record->GetField(groupby_fields[i]));
}
return absl::OkStatus();
}
Expand All @@ -52,12 +52,11 @@ absl::Status InitializeGroup(AggregateTuple* group,
for (int i = 0; i < aggregate_fields.size(); i++) {
switch (aggregate_types[i]) {
case AggregateType::COUNT: {
RETURN_IF_ERROR(group->SetField(i, std::make_unique<IntField>(1)));
group->SetField(i, std::make_unique<IntField>(1));
break;
}
default: {
RETURN_IF_ERROR(
group->SetField(i, record->GetField(aggregate_fields[i])));
group->SetField(i, record->GetField(aggregate_fields[i]));
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions komfydb/execution/delete.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ absl::Status Delete::FetchNext() {
RETURN_IF_NOT_OOR(record.status());
RETURN_IF_ERROR(bufferpool->RemoveTuples(ids, table_id, tid));
next_record = std::make_unique<Record>(1, RecordId(PageId(0, 0), -1));
RETURN_IF_ERROR(next_record->SetField(0, std::make_unique<IntField>(count)));
next_record->SetField(0, std::make_unique<IntField>(count));
return absl::OkStatus();
}

}; // namespace komfydb::execution
}; // namespace komfydb::execution
2 changes: 1 addition & 1 deletion komfydb/execution/insert.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ absl::Status Insert::FetchNext() {
RETURN_IF_NOT_OOR(record.status());
RETURN_IF_ERROR(bufferpool->InsertTuples(std::move(tuples), table_id, tid));
next_record = std::make_unique<Record>(1, RecordId(PageId(0, 0), -1));
RETURN_IF_ERROR(next_record->SetField(0, std::make_unique<IntField>(count)));
next_record->SetField(0, std::make_unique<IntField>(count));
return absl::OkStatus();
}

Expand Down
13 changes: 1 addition & 12 deletions komfydb/execution/project.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,7 @@ absl::Status Project::FetchNext() {
if (record_field == nullptr) {
continue;
}
switch (tuple_desc.GetFieldType(i).value().GetValue()) {
case Type::INT:
RETURN_IF_ERROR(
new_tuple.SetField(i, std::make_unique<IntField>(
*dynamic_cast<IntField*>(record_field))));
break;
case Type::STRING:
RETURN_IF_ERROR(new_tuple.SetField(
i, std::make_unique<StringField>(
*dynamic_cast<StringField*>(record_field))));
break;
}
new_tuple.SetField(i, record_field);
}
next_record = std::make_unique<Record>(new_tuple, record->GetId());
return absl::OkStatus();
Expand Down
6 changes: 2 additions & 4 deletions komfydb/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,15 @@ absl::StatusOr<Tuple> GetTuple(std::vector<hsql::Expr*>* values,
return absl::InvalidArgumentError(absl::StrCat(
"Invalid value ", i + 1, " type, string instead of int."));
}
RETURN_IF_ERROR(
result.SetField(i, std::make_unique<IntField>(value->ival)));
result.SetField(i, std::make_unique<IntField>(value->ival));
break;
}
case hsql::ExprType::kExprLiteralString: {
if (tditem.field_type != Type::STRING) {
return absl::InvalidArgumentError(absl::StrCat(
"Invalid value ", i + 1, " type, int instead of string."));
}
RETURN_IF_ERROR(
result.SetField(i, std::make_unique<StringField>(value->name)));
result.SetField(i, std::make_unique<StringField>(value->name));
break;
}
default: {
Expand Down
15 changes: 11 additions & 4 deletions komfydb/storage/heap_page.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,18 @@ absl::StatusOr<std::unique_ptr<HeapPage>> HeapPage::Create(
} else {
for (int j = 0; j < n_fields; j++) {
ASSIGN_OR_RETURN(Type field_type, tuple_desc->GetFieldType(j));
if (field_type.GetValue() == Type::INT) {
RETURN_IF_ERROR(record.SetField(j, ParseInt(data, data_idx)));
} else if (field_type.GetValue() == Type::STRING) {
RETURN_IF_ERROR(record.SetField(j, ParseString(data, data_idx)));
std::unique_ptr<Field> parsed;
switch (field_type.GetValue()) {
case Type::INT: {
parsed = ParseInt(data, data_idx);
break;
}
case Type::STRING: {
parsed = ParseString(data, data_idx);
break;
}
}
record.SetField(j, std::move(parsed));
}
}

Expand Down
31 changes: 12 additions & 19 deletions komfydb/storage/heap_page_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace {

using namespace komfydb::storage;
using komfydb::common::Field;
using komfydb::common::IntField;
using komfydb::common::StringField;
using komfydb::common::Type;
Expand Down Expand Up @@ -76,18 +75,12 @@ TEST_F(HeapPageTest, Records) {
EXPECT_EQ(record.GetId(), RecordId(pid, i));

Record comp_record(tuple_desc->Length(), pid, i);
ASSERT_TRUE(
comp_record.SetField(0, std::make_unique<IntField>(i + 1)).ok());
ASSERT_TRUE(
comp_record
.SetField(1, std::make_unique<StringField>(std::string(i + 1, 'a')))
.ok());
ASSERT_TRUE(
comp_record.SetField(2, std::make_unique<IntField>(i + 1)).ok());
ASSERT_TRUE(
comp_record
.SetField(3, std::make_unique<StringField>(std::string(i + 1, 'b')))
.ok());
comp_record.SetField(0, std::make_unique<IntField>(i + 1));
comp_record.SetField(
1, std::make_unique<StringField>(std::string(i + 1, 'a')));
comp_record.SetField(2, std::make_unique<IntField>(i + 1));
comp_record.SetField(
3, std::make_unique<StringField>(std::string(i + 1, 'b')));
EXPECT_EQ(record, comp_record);
}
}
Expand All @@ -109,14 +102,14 @@ TEST_F(HeapPageTest, AddAndRemoveTuples) {

Tuple t[3] = {Tuple(tuple_desc->Length()), Tuple(tuple_desc->Length()),
Tuple(tuple_desc->Length())};
ASSERT_TRUE(t[0].SetField(0, std::make_unique<IntField>(0)).ok());
ASSERT_TRUE(t[0].SetField(1, std::make_unique<StringField>("a")).ok());
ASSERT_TRUE(t[0].SetField(2, std::make_unique<IntField>(1)).ok());
ASSERT_TRUE(t[0].SetField(3, std::make_unique<StringField>("b")).ok());
t[0].SetField(0, std::make_unique<IntField>(0));
t[0].SetField(1, std::make_unique<StringField>("a"));
t[0].SetField(2, std::make_unique<IntField>(1));
t[0].SetField(3, std::make_unique<StringField>("b"));
t[1] = t[0];
t[2] = t[0];
ASSERT_TRUE(t[1].SetField(0, std::make_unique<IntField>(2)).ok());
ASSERT_TRUE(t[2].SetField(1, std::make_unique<StringField>("c")).ok());
t[1].SetField(0, std::make_unique<IntField>(2));
t[2].SetField(1, std::make_unique<StringField>("c"));
std::unique_ptr<Tuple> t_pointers[3] = {std::make_unique<Tuple>(t[0]),
std::make_unique<Tuple>(t[1]),
std::make_unique<Tuple>(t[2])};
Expand Down

0 comments on commit d0ef9fc

Please sign in to comment.