diff --git a/src/core/field_extractor.h b/src/core/field_extractor.h index d9ae1c7547..5802cdbfec 100644 --- a/src/core/field_extractor.h +++ b/src/core/field_extractor.h @@ -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_) { @@ -418,14 +418,14 @@ class FieldExtractor { } } - uint16_t GetRecordCount(const Value& record) const { - return ::lgraph::_detail::UnalignedGet(record.Data() + count_offset_); + FieldId GetRecordCount(const Value& record) const { + return ::lgraph::_detail::UnalignedGet(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. @@ -437,7 +437,7 @@ class FieldExtractor { return ::lgraph::_detail::UnalignedGet(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; diff --git a/src/core/schema.cpp b/src/core/schema.cpp index 671d6fb1f9..a70654130c 100644 --- a/src/core/schema.cpp +++ b/src/core/schema.cpp @@ -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_) { @@ -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(ptr + offset, ::lgraph::_detail::SCHEMA_VERSION); offset += sizeof(VersionId); @@ -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(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(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 { @@ -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(v.Data(), ::lgraph::_detail::SCHEMA_VERSION); - ::lgraph::_detail::UnalignedSet(v.Data() + ::lgraph::_detail::LABEL_OFFSET, label_id_); + ::lgraph::_detail::UnalignedSet(v.Data() + sizeof(VersionId), label_id_); return v; } @@ -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(), diff --git a/src/core/schema_common.h b/src/core/schema_common.h index 98f9e1d072..edebce0270 100644 --- a/src/core/schema_common.h +++ b/src/core/schema_common.h @@ -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.", diff --git a/test/test_field_extractor.cpp b/test/test_field_extractor.cpp index 0ef0008a98..6454881b40 100644 --- a/test/test_field_extractor.cpp +++ b/test/test_field_extractor.cpp @@ -365,4 +365,4 @@ TEST_F(TestFieldExtractor, ConvertDataTest) { float resultf; test_conversion(resultf, fdp, std::numeric_limits::max()); test_conversion(resultf, fdn, std::numeric_limits::min()); -} \ No newline at end of file +} diff --git a/test/test_schema.cpp b/test/test_schema.cpp index f24878e291..9f7cf4ca64 100644 --- a/test/test_schema.cpp +++ b/test/test_schema.cpp @@ -84,14 +84,14 @@ TEST_F(TestSchema, SetSchema) { s.SetSchema(true, std::vector({FieldSpec("int16", FieldType::NUL, true)}), "int16", "", {}, {}), FieldCannotBeNullType); - UT_EXPECT_THROW_CODE( - s.SetSchema(true, - std::vector({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("int16", FieldType::INT16, true, 0), + FieldSpec("int16", FieldType::INT16, true, 1), + FieldSpec("int16", FieldType::INT16, true, 1), + }), + "int16", "", {}, {}), + FieldIdConflict); std::vector fs; for (size_t i = 0; i < _detail::MAX_NUM_FIELDS + 1; i++) fs.emplace_back(UT_FMT("f_{}", i), FieldType::INT16, true); @@ -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()); @@ -187,7 +187,7 @@ TEST_F(TestSchema, DumpRecord) { std::vector value{"marko", "300"}; // missing weight field UT_EXPECT_THROW_CODE(schema.CreateRecord(fid.size(), fid.data(), value.data()), - FieldCannotBeSetNull); + FieldCannotBeSetNull); } std::vector fid = schema.GetFieldIds({"name", "uid", "weight", "age", "addr"}); @@ -209,6 +209,4 @@ TEST_F(TestSchema, DumpRecord) { TEST_F(TestSchema, ParseAndSet) { Value value("value"); Schema schema(true); - } -