Skip to content

Commit

Permalink
fix get func bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinLeeo committed Oct 15, 2024
1 parent d1be61a commit 4627878
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 34 deletions.
10 changes: 5 additions & 5 deletions src/core/field_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class FieldExtractor {
memcpy(data, record.Data() + off, size);
}

char* GetNullArray(const Value& record) const { return record.Data() + nullarray_offset_; }
char* GetNullArray(const Value& record) const { return record.Data() + nullarray_offset_; }

size_t GetDataSize(const Value& record) const {
if (is_vfield_) {
Expand All @@ -418,14 +418,14 @@ class FieldExtractor {
}
}

uint16_t GetRecordCount(const Value& record) const {
return ::lgraph::_detail::UnalignedGet<uint16_t>(record.Data() + count_offset_);
FieldId GetRecordCount(const Value& record) const {
return ::lgraph::_detail::UnalignedGet<FieldId>(record.Data() + count_offset_);
}

/** Retrieve the starting position of the Field data for the given ID.
* Note that both fixed-length and variable-length data are not distinguished here.
*/
size_t GetFieldOffset(const Value& record, const FieldId id) const {
size_t GetFieldOffset(const Value& record, const FieldId id) const {
const uint16_t count = GetRecordCount(record);
if (0 == id) {
// The starting position of Field0 is at the end of the offset section.
Expand All @@ -437,7 +437,7 @@ class FieldExtractor {
return ::lgraph::_detail::UnalignedGet<DataOffset>(record.Data() + offset);
}

size_t GetOffsetPosition(const Value& record, const FieldId id) {
size_t GetOffsetPosition(const Value& record, const FieldId id) const {
const FieldId count = GetRecordCount(record);
if (0 == id) {
return 0;
Expand Down
24 changes: 11 additions & 13 deletions src/core/schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,10 +464,8 @@ Value Schema::CreateEmptyRecord(size_t size_hint) const {
Value v(size_hint);
size_t num_fields = fields_.size();
// version - [label] - count - null_array - offset_array
size_t min_size =
sizeof(VersionId) + label_in_record_
? sizeof(LabelId)
: 0 + sizeof(FieldId) + (num_fields + 7) / 8 + num_fields * sizeof(DataOffset);
size_t min_size = sizeof(VersionId) + (label_in_record_ ? sizeof(LabelId) : 0) +
sizeof(FieldId) + (num_fields + 7) / 8 + num_fields * sizeof(DataOffset);
// Fixed-value and Variable-value. Variable-value will store an offset at Fixed-value area and
// assume the length of every variable value is 0;
for (const auto& field : fields_) {
Expand All @@ -480,7 +478,6 @@ Value Schema::CreateEmptyRecord(size_t size_hint) const {
char* ptr = v.Data();
DataOffset offset = 0;


// 2. Set version id.
::lgraph::_detail::UnalignedSet<VersionId>(ptr + offset, ::lgraph::_detail::SCHEMA_VERSION);
offset += sizeof(VersionId);
Expand All @@ -501,25 +498,25 @@ Value Schema::CreateEmptyRecord(size_t size_hint) const {

// 6. Set fields' offset.
DataOffset offset_begin = offset;
DataOffset data_offset = offset + num_fields * sizeof(DataOffset); // data area begin.
char* offset_ptr = ptr + offset_begin; // offset area begin.
DataOffset data_offset = offset + num_fields * sizeof(DataOffset); // data area begin.
char* offset_ptr = ptr + offset_begin; // offset area begin.

// field0 do not need to store its offset.
for (size_t i = 1; i < num_fields; i++) {
data_offset += fields_[i - 1].IsFixedType() ? fields_[i - 1].TypeSize() : sizeof(DataOffset);
data_offset +=
fields_[i - 1].IsFixedType() ? fields_[i - 1].TypeSize() : sizeof(DataOffset);
::lgraph::_detail::UnalignedSet<DataOffset>(offset_ptr, data_offset);
offset_ptr += sizeof(DataOffset);
}
// the latest offset marks the end of the fixed-area.
data_offset +=
fields_[num_fields - 1].IsFixedType() ? fields_[num_fields - 1].TypeSize() : sizeof(DataOffset);
data_offset += fields_[num_fields - 1].IsFixedType() ? fields_[num_fields - 1].TypeSize()
: sizeof(DataOffset);
::lgraph::_detail::UnalignedSet<DataOffset>(offset_ptr, data_offset);


// 7. Set variable fields offset. They are stored at fixed-area, and their sizes are all zero.
for (const auto& field : fields_) {
if (!field.IsFixedType()) {
DataOffset var_offset = 0; // variable fields offset.
DataOffset var_offset = 0; // variable fields offset.
if (field.GetFieldId() == 0) {
var_offset = offset + num_fields * sizeof(DataOffset);
} else {
Expand Down Expand Up @@ -941,7 +938,7 @@ void Schema::ParseAndSet(Value& record, const std::string& data,
Value Schema::CreateRecordWithLabelId() const {
Value v(sizeof(LabelId) + sizeof(VersionId));
::lgraph::_detail::UnalignedSet<VersionId>(v.Data(), ::lgraph::_detail::SCHEMA_VERSION);
::lgraph::_detail::UnalignedSet<LabelId>(v.Data() + ::lgraph::_detail::LABEL_OFFSET, label_id_);
::lgraph::_detail::UnalignedSet<LabelId>(v.Data() + sizeof(VersionId), label_id_);
return v;
}

Expand Down Expand Up @@ -1045,6 +1042,7 @@ void Schema::SetSchema(bool is_vertex, size_t n_fields, const FieldSpec* fields,
fields_.reserve(n_fields);
for (size_t i = 0; i < n_fields; i++) {
fields_.emplace_back(fields[i]);
fields_[i].SetLabelInRecord(label_in_record_);
name_to_idx_[fields[i].name] = fields[i].id;
}
std::sort(fields_.begin(), fields_.end(),
Expand Down
4 changes: 2 additions & 2 deletions src/core/schema_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class FieldAlreadyExistsException : public LgraphException {
};

class FieldIdConflictException : public LgraphException {
public:
explicit FieldIdConflictException(const std::string& fidname1,
public:
explicit FieldIdConflictException(const std::string& fidname1,
const std::string& fidname2)
: LgraphException(ErrorCode::FieldIdConflict,
"Field [#{}] and Field [#P{}] id conflict.",
Expand Down
2 changes: 1 addition & 1 deletion test/test_field_extractor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,4 +365,4 @@ TEST_F(TestFieldExtractor, ConvertDataTest) {
float resultf;
test_conversion(resultf, fdp, std::numeric_limits<float>::max());
test_conversion(resultf, fdn, std::numeric_limits<float>::min());
}
}
24 changes: 11 additions & 13 deletions test/test_schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ TEST_F(TestSchema, SetSchema) {
s.SetSchema(true, std::vector<FieldSpec>({FieldSpec("int16", FieldType::NUL, true)}),
"int16", "", {}, {}),
FieldCannotBeNullType);
UT_EXPECT_THROW_CODE(
s.SetSchema(true,
std::vector<FieldSpec>({FieldSpec("int16", FieldType::INT16, true, 0),
FieldSpec("int16", FieldType::INT16, true, 1),
FieldSpec("int16", FieldType::INT16, true, 1),
}),
"int16", "", {}, {}),
FieldIdConflict);
UT_EXPECT_THROW_CODE(s.SetSchema(true,
std::vector<FieldSpec>({
FieldSpec("int16", FieldType::INT16, true, 0),
FieldSpec("int16", FieldType::INT16, true, 1),
FieldSpec("int16", FieldType::INT16, true, 1),
}),
"int16", "", {}, {}),
FieldIdConflict);
std::vector<FieldSpec> fs;
for (size_t i = 0; i < _detail::MAX_NUM_FIELDS + 1; i++)
fs.emplace_back(UT_FMT("f_{}", i), FieldType::INT16, true);
Expand Down Expand Up @@ -159,11 +159,11 @@ TEST_F(TestSchema, DumpRecord) {
schema.SetSchema(true, fds, "uid", "", {}, {});
Value va_tmp = schema.CreateEmptyRecord();
UT_EXPECT_THROW_CODE(schema_1.SetField(va_tmp, (std::string) "name", FieldData()),
FieldCannotBeSetNull);
FieldCannotBeSetNull);
UT_EXPECT_THROW(schema_1.SetField(va_tmp, (std::string) "age", FieldData(256)),
lgraph::ParseFieldDataException);
UT_EXPECT_THROW_CODE(schema_1.SetField(va_tmp, (std::string) "name", FieldData(256)),
ParseIncompatibleType);
ParseIncompatibleType);
UT_EXPECT_TRUE(schema_1.GetField(va_tmp, (std::string) "does_not_exist",
[](const BlobManager::BlobKey&) { return Value(); }) ==
FieldData());
Expand All @@ -187,7 +187,7 @@ TEST_F(TestSchema, DumpRecord) {
std::vector<std::string> value{"marko", "300"};
// missing weight field
UT_EXPECT_THROW_CODE(schema.CreateRecord(fid.size(), fid.data(), value.data()),
FieldCannotBeSetNull);
FieldCannotBeSetNull);
}

std::vector<size_t> fid = schema.GetFieldIds({"name", "uid", "weight", "age", "addr"});
Expand All @@ -209,6 +209,4 @@ TEST_F(TestSchema, DumpRecord) {
TEST_F(TestSchema, ParseAndSet) {
Value value("value");
Schema schema(true);

}

0 comments on commit 4627878

Please sign in to comment.