Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ljcui committed Jul 25, 2024
1 parent 66c8318 commit 396e30a
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
4 changes: 4 additions & 0 deletions include/lgraph/lgraph_txn.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,10 @@ class Transaction {
EdgeIndexIterator GetEdgeIndexIterator(size_t label_id, size_t field_id,
const FieldData& key_start, const FieldData& key_end);

EdgeIndexIterator GetEdgePairUniqueIndexIterator(size_t label_id, size_t field_id,
int64_t src_vid, int64_t dst_vid,
const FieldData& key_start, const FieldData& key_end);

/**
* @brief Gets vertex index iterator. The iterator has field value [key_start, key_end]. So
* key_start=key_end=v returns an iterator pointing to all vertexes that has field
Expand Down
17 changes: 17 additions & 0 deletions src/core/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,23 @@ EdgeIndexIterator Transaction::GetEdgeIndexIterator(size_t label_id, size_t fiel
return index->GetIterator(this, std::move(ks), std::move(ke), 0);
}

EdgeIndexIterator Transaction::GetEdgePairUniqueIndexIterator(
size_t label_id, size_t field_id, VertexId src_vid, VertexId dst_vid,
const FieldData& key_start, const FieldData& key_end) {
EdgeIndex* index = GetEdgeIndex(label_id, field_id);
if (!index || !index->IsReady()) {
THROW_CODE(InputError, "EdgeIndex is not created for this field");
}
Value ks, ke;
if (!key_start.IsNull()) {
ks = field_data_helper::FieldDataToValueOfFieldType(key_start, index->KeyType());
}
if (!key_end.IsNull()) {
ke = field_data_helper::FieldDataToValueOfFieldType(key_end, index->KeyType());
}
return index->GetIterator(this, std::move(ks), std::move(ke), src_vid, dst_vid);
}

EdgeIndexIterator Transaction::GetEdgeIndexIterator(const std::string& label,
const std::string& field,
const std::string& key_start = "",
Expand Down
4 changes: 4 additions & 0 deletions src/core/transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,10 @@ class Transaction {
const FieldData& key_start = FieldData(),
const FieldData& key_end = FieldData());

EdgeIndexIterator GetEdgePairUniqueIndexIterator(
size_t label_id, size_t field_id, VertexId src_vid, VertexId dst_vid,
const FieldData& key_start, const FieldData& key_end);

EdgeIndexIterator GetEdgeIndexIterator(const std::string& label, const std::string& field,
const std::string& key_start, const std::string& key_end);

Expand Down
11 changes: 11 additions & 0 deletions src/lgraph_api/lgraph_txn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ EdgeIndexIterator Transaction::GetEdgeIndexIterator(size_t label_id, size_t fiel
txn_);
}

EdgeIndexIterator Transaction::GetEdgePairUniqueIndexIterator(size_t label_id, size_t field_id,
int64_t src_vid, int64_t dst_vid,
const FieldData& key_start,
const FieldData& key_end) {
ThrowIfInvalid();
return EdgeIndexIterator(txn_->GetEdgePairUniqueIndexIterator(
label_id, field_id, src_vid, dst_vid, key_start, key_end),
txn_);
}

VertexIndexIterator Transaction::GetVertexIndexIterator(const std::string& label,
const std::string& field,
const FieldData& key_start,
Expand Down Expand Up @@ -501,6 +511,7 @@ OutEdgeIterator Transaction::GetEdgeByUniqueIndex(size_t label_id, size_t field_
euid = eit.GetUid();
return GetOutEdgeIterator(euid, false);
}

size_t Transaction::GetNumVertices() {
ThrowIfInvalid();
return txn_->graph_->GetLooseNumVertex(txn_->GetTxn());
Expand Down
31 changes: 29 additions & 2 deletions test/test_lgraph_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,7 @@ TEST_F(TestLGraphApi, pairUniqueIndex) {
std::vector<FieldSpec>({{"id", FieldType::INT32, false}}),
eo));
UT_EXPECT_TRUE(db.AddEdgeIndex("like", "id", IndexType::PairUniqueIndex));

std::vector<std::string> vp{"id"};
std::vector<std::string> ep{"id"};
auto txn = db.CreateWriteTxn();
Expand All @@ -1219,8 +1220,34 @@ TEST_F(TestLGraphApi, pairUniqueIndex) {
}
for (int i = 0; i < vids.size()-1; i++) {
txn.AddEdge(vids[i], vids[i+1],
std::string("Relation"), vp,
{FieldData::Int32(i), FieldData::Int32(i), FieldData::Int32(i)});
std::string("like"), vp,
{FieldData::Int32(i)});
txn.AddEdge(vids[i], vids[i+1],
std::string("know"), vp,
{FieldData::Int32(i)});
}
txn.Commit();
for (int i = 0; i < vids.size()-1; i++) {
txn = db.CreateWriteTxn();
UT_EXPECT_THROW_MSG(txn.AddEdge(vids[i], vids[i+1],
std::string("like"), vp,
{FieldData::Int32(i)}), "index value already exists");
txn.Abort();
}
txn = db.CreateWriteTxn();
for (int i = 0; i < vids.size()-1; i++) {
txn.AddEdge(vids[i], vids[i+1], std::string("know"), vp, {FieldData::Int32(i)});
}
txn.Commit();
for (int i = 0; i < vids.size()-1; i++) {
txn = db.CreateWriteTxn();
auto like_lid = txn.GetEdgeLabelId("like");
auto fid = txn.GetEdgeFieldId(like_lid, "id");
auto iter = txn.GetEdgePairUniqueIndexIterator(like_lid, fid, vids[i], vids[i+1], FieldData::Int32(i), FieldData::Int32(i));
UT_EXPECT_TRUE(iter.IsValid());
auto euid = iter.GetUid();
auto iter2 = txn.GetOutEdgeIterator(euid);
UT_EXPECT_EQ(iter2.GetField("id"), FieldData::Int32(i));
txn.Abort();
}
}

0 comments on commit 396e30a

Please sign in to comment.