Skip to content

Commit

Permalink
fix memory increase (#3806)
Browse files Browse the repository at this point in the history
  • Loading branch information
cangfengzhs authored Jan 26, 2022
1 parent e80c45a commit d113f4a
Show file tree
Hide file tree
Showing 18 changed files with 134 additions and 91 deletions.
22 changes: 5 additions & 17 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,32 +340,20 @@ bool MetaClient::loadSchemas(GraphSpaceID spaceId,
EdgeSchemas edgeSchemas;
TagID lastTagId = -1;

auto addSchemaField = [&spaceInfoCache](NebulaSchemaProvider* schema,
const cpp2::ColumnDef& col) {
auto addSchemaField = [](NebulaSchemaProvider* schema, const cpp2::ColumnDef& col) {
bool hasDef = col.default_value_ref().has_value();
auto& colType = col.get_type();
size_t len = colType.type_length_ref().has_value() ? *colType.get_type_length() : 0;
cpp2::GeoShape geoShape =
colType.geo_shape_ref().has_value() ? *colType.get_geo_shape() : cpp2::GeoShape::ANY;
bool nullable = col.nullable_ref().has_value() ? *col.get_nullable() : false;
Expression* defaultValueExpr = nullptr;
std::string defaultValueExprStr = "";
if (hasDef) {
auto encoded = *col.get_default_value();
defaultValueExpr = Expression::decode(&(spaceInfoCache->pool_),
folly::StringPiece(encoded.data(), encoded.size()));

if (defaultValueExpr == nullptr) {
LOG(ERROR) << "Wrong expr default value for column name: " << col.get_name();
hasDef = false;
}
defaultValueExprStr = *col.get_default_value();
}

schema->addField(col.get_name(),
colType.get_type(),
len,
nullable,
hasDef ? defaultValueExpr : nullptr,
geoShape);
schema->addField(
col.get_name(), colType.get_type(), len, nullable, defaultValueExprStr, geoShape);
};

for (auto& tagIt : tagItemVec) {
Expand Down
2 changes: 0 additions & 2 deletions src/clients/meta/MetaClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ struct SpaceInfoCache {
Indexes tagIndexes_;
Indexes edgeIndexes_;
Listeners listeners_;
// objPool used to decode when adding field
ObjectPool pool_;
std::unordered_map<PartitionID, TermID> termOfPartition_;
};

Expand Down
6 changes: 4 additions & 2 deletions src/codec/RowWriterV2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,9 @@ WriteResult RowWriterV2::checkUnsetFields() noexcept {

WriteResult r = WriteResult::SUCCEEDED;
if (field->hasDefault()) {
auto expr = field->defaultValue()->clone();
ObjectPool pool;
auto& exprStr = field->defaultValue();
auto expr = Expression::decode(&pool, folly::StringPiece(exprStr.data(), exprStr.size()));
auto defVal = Expression::eval(expr, expCtx);
switch (defVal.type()) {
case Value::Type::NULLVALUE:
Expand Down Expand Up @@ -816,7 +818,7 @@ WriteResult RowWriterV2::checkUnsetFields() noexcept {
default:
LOG(FATAL) << "Unsupported default value type: " << defVal.typeName()
<< ", default value: " << defVal
<< ", default value expr: " << field->defaultValue()->toString();
<< ", default value expr: " << field->defaultValue();
}
} else {
// Set NULL
Expand Down
10 changes: 5 additions & 5 deletions src/codec/test/ResultSchemaProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ResultSchemaProvider::ResultSchemaField::ResultSchemaField(std::string name,
bool nullable,
int32_t offset,
size_t nullFlagPos,
Expression* defaultValue,
std::string defaultValue,
meta::cpp2::GeoShape geoShape)
: name_(std::move(name)),
type_(type),
Expand All @@ -37,13 +37,13 @@ const char* ResultSchemaProvider::ResultSchemaField::name() const { return name_

PropertyType ResultSchemaProvider::ResultSchemaField::type() const { return type_; }

bool ResultSchemaProvider::ResultSchemaField::hasDefault() const {
return defaultValue_ != nullptr;
}
bool ResultSchemaProvider::ResultSchemaField::hasDefault() const { return defaultValue_ != ""; }

bool ResultSchemaProvider::ResultSchemaField::nullable() const { return nullable_; }

Expression* ResultSchemaProvider::ResultSchemaField::defaultValue() const { return defaultValue_; }
const std::string& ResultSchemaProvider::ResultSchemaField::defaultValue() const {
return defaultValue_;
}

size_t ResultSchemaProvider::ResultSchemaField::size() const { return size_; }

Expand Down
6 changes: 3 additions & 3 deletions src/codec/test/ResultSchemaProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ class ResultSchemaProvider : public meta::SchemaProviderIf {
bool nullable,
int32_t offset,
size_t nullFlagPos,
Expression* defaultValue = nullptr,
std::string defaultValue = "",
meta::cpp2::GeoShape = meta::cpp2::GeoShape::ANY);

const char* name() const override;
meta::cpp2::PropertyType type() const override;
bool nullable() const override;
bool hasDefault() const override;
Expression* defaultValue() const override;
const std::string& defaultValue() const override;
size_t size() const override;
size_t offset() const override;
size_t nullFlagPos() const override;
Expand All @@ -41,7 +41,7 @@ class ResultSchemaProvider : public meta::SchemaProviderIf {
bool nullable_;
int32_t offset_;
size_t nullFlagPos_;
Expression* defaultValue_;
std::string defaultValue_;
meta::cpp2::GeoShape geoShape_;
};

Expand Down
10 changes: 8 additions & 2 deletions src/codec/test/SchemaWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ SchemaWriter& SchemaWriter::appendCol(folly::StringPiece name,
nullFlagPos = numNullableFields_++;
}

columns_.emplace_back(
name.toString(), type, size, nullable, offset, nullFlagPos, defaultValue, geoShape);
columns_.emplace_back(name.toString(),
type,
size,
nullable,
offset,
nullFlagPos,
defaultValue ? defaultValue->encode() : "",
geoShape);
nameIndex_.emplace(std::make_pair(hash, columns_.size() - 1));

return *this;
Expand Down
4 changes: 2 additions & 2 deletions src/common/meta/NebulaSchemaProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void NebulaSchemaProvider::addField(folly::StringPiece name,
cpp2::PropertyType type,
size_t fixedStrLen,
bool nullable,
Expression* defaultValue,
std::string defaultValue,
cpp2::GeoShape geoShape) {
size_t size = fieldSize(type, fixedStrLen);

Expand All @@ -108,7 +108,7 @@ void NebulaSchemaProvider::addField(folly::StringPiece name,
fields_.emplace_back(name.toString(),
type,
nullable,
defaultValue != nullptr,
defaultValue != "",
defaultValue,
size,
offset,
Expand Down
8 changes: 4 additions & 4 deletions src/common/meta/NebulaSchemaProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NebulaSchemaProvider : public SchemaProviderIf {
cpp2::PropertyType type,
bool nullable,
bool hasDefault,
Expression* defaultValue,
std::string defaultValue,
size_t size,
size_t offset,
size_t nullFlagPos,
Expand All @@ -48,7 +48,7 @@ class NebulaSchemaProvider : public SchemaProviderIf {

bool hasDefault() const override { return hasDefault_; }

Expression* defaultValue() const override { return defaultValue_; }
const std::string& defaultValue() const override { return defaultValue_; }

size_t size() const override { return size_; }

Expand All @@ -66,7 +66,7 @@ class NebulaSchemaProvider : public SchemaProviderIf {
cpp2::PropertyType type_;
bool nullable_;
bool hasDefault_;
Expression* defaultValue_;
std::string defaultValue_;
size_t size_;
size_t offset_;
size_t nullFlagPos_;
Expand Down Expand Up @@ -97,7 +97,7 @@ class NebulaSchemaProvider : public SchemaProviderIf {
cpp2::PropertyType type,
size_t fixedStrLen = 0,
bool nullable = false,
Expression* defaultValue = nullptr,
std::string defaultValue = "",
cpp2::GeoShape geoShape = cpp2::GeoShape::ANY);

static std::size_t fieldSize(cpp2::PropertyType type, std::size_t fixedStrLimit);
Expand Down
2 changes: 1 addition & 1 deletion src/common/meta/SchemaProviderIf.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class SchemaProviderIf {
virtual cpp2::PropertyType type() const = 0;
virtual bool nullable() const = 0;
virtual bool hasDefault() const = 0;
virtual Expression* defaultValue() const = 0;
virtual const std::string& defaultValue() const = 0;
// This method returns the number of bytes the field will occupy
// when the field is persisted on the storage medium
// For the variant length string, the size will return 8
Expand Down
8 changes: 4 additions & 4 deletions src/graph/util/SchemaUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ Status SchemaUtil::validateProps(const std::vector<SchemaPropItem *> &schemaProp

// static
std::shared_ptr<const meta::NebulaSchemaProvider> SchemaUtil::generateSchemaProvider(
ObjectPool *pool, const SchemaVer ver, const meta::cpp2::Schema &schema) {
const SchemaVer ver, const meta::cpp2::Schema &schema) {
auto schemaPtr = std::make_shared<meta::NebulaSchemaProvider>(ver);
for (auto col : schema.get_columns()) {
bool hasDef = col.default_value_ref().has_value();
Expression *defaultValueExpr = nullptr;
std::string exprStr;
if (hasDef) {
defaultValueExpr = Expression::decode(pool, *col.default_value_ref());
exprStr = *col.get_default_value();
}
schemaPtr->addField(col.get_name(),
col.get_type().get_type(),
col.type.type_length_ref().value_or(0),
col.nullable_ref().value_or(false),
hasDef ? defaultValueExpr : nullptr,
exprStr,
col.type.geo_shape_ref().value_or(meta::cpp2::GeoShape::ANY));
}
return schemaPtr;
Expand Down
2 changes: 1 addition & 1 deletion src/graph/util/SchemaUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SchemaUtil final {
meta::cpp2::Schema& schema);

static std::shared_ptr<const meta::NebulaSchemaProvider> generateSchemaProvider(
ObjectPool* pool, const SchemaVer ver, const meta::cpp2::Schema& schema);
const SchemaVer ver, const meta::cpp2::Schema& schema);

static Status setTTLDuration(SchemaPropItem* schemaProp, meta::cpp2::Schema& schema);

Expand Down
6 changes: 2 additions & 4 deletions src/graph/validator/MaintainValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,7 @@ Status CreateTagValidator::validateImpl() {
NG_RETURN_IF_ERROR(validateColumns(sentence->columnSpecs(), schema));
NG_RETURN_IF_ERROR(SchemaUtil::validateProps(sentence->getSchemaProps(), schema));
// Save the schema in validateContext
auto pool = qctx_->objPool();
auto schemaPro = SchemaUtil::generateSchemaProvider(pool, 0, schema);
auto schemaPro = SchemaUtil::generateSchemaProvider(0, schema);
vctx_->addSchema(name, schemaPro);
createCtx_->name = std::move(name);
createCtx_->schema = std::move(schema);
Expand All @@ -177,8 +176,7 @@ Status CreateEdgeValidator::validateImpl() {
NG_RETURN_IF_ERROR(validateColumns(sentence->columnSpecs(), schema));
NG_RETURN_IF_ERROR(SchemaUtil::validateProps(sentence->getSchemaProps(), schema));
// Save the schema in validateContext
auto pool = qctx_->objPool();
auto schemaPro = SchemaUtil::generateSchemaProvider(pool, 0, schema);
auto schemaPro = SchemaUtil::generateSchemaProvider(0, schema);
vctx_->addSchema(name, schemaPro);
createCtx_->name = std::move(name);
createCtx_->schema = std::move(schema);
Expand Down
9 changes: 6 additions & 3 deletions src/graph/validator/MutateValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,12 @@ Status InsertEdgesValidator::prepareEdges() {
auto iter = std::find(propNames_.begin(), propNames_.end(), propName);
if (iter == propNames_.end()) {
if (field->hasDefault()) {
auto *defaultValue = field->defaultValue();
DCHECK(!!defaultValue);
auto v = defaultValue->eval(QueryExpressionContext()(nullptr));
auto &defaultValue = field->defaultValue();
DCHECK(!defaultValue.empty());
ObjectPool pool;
auto expr = Expression::decode(
&pool, folly::StringPiece(defaultValue.data(), defaultValue.size()));
auto v = expr->eval(QueryExpressionContext()(nullptr));
entirePropValues.emplace_back(v);
} else {
if (!field->nullable()) {
Expand Down
Loading

0 comments on commit d113f4a

Please sign in to comment.