Skip to content

Commit

Permalink
composite index to cypher procedure (#505)
Browse files Browse the repository at this point in the history
* composite index to cypher procedure

* composite index to cypher procedure

* add test procedure

* composite index to cypher procedure

* modify composite index iterator

* add composite index iterator

* add composite index iterator

* add composite index iterator

* add composite index iterator

* add composite index iterator

* composite index test pass for int64 kv

* composite index test pass for STRING kv

* add composite index to listIndexes

* add composite index to listIndexes

* add composite index to deleteIndexes

* fix cpplint

* fix build

* fix add vertex to composite index

* fix add vertex to composite index

* fix type transfer

* add field alter to composite index

* enrich composite index interface

* fix cpplint

* add ut

* add ut

* add ut

* add ut

* fix ut asan

* fix cpplint

* fix asan
  • Loading branch information
lipanpan03 authored May 27, 2024
1 parent e1eef05 commit fec3d91
Show file tree
Hide file tree
Showing 31 changed files with 1,291 additions and 92 deletions.
10 changes: 10 additions & 0 deletions include/lgraph/lgraph_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,10 @@ class GraphDB {
*/
bool AddEdgeIndex(const std::string &label, const std::string &field, IndexType type);

bool AddVertexCompositeIndex(const std::string& label,
const std::vector<std::string>& fields,
CompositeIndexType type);

/**
* @brief Check if this vertex_label:field is indexed.
*
Expand All @@ -473,6 +477,9 @@ class GraphDB {
*/
bool IsEdgeIndexed(const std::string &label, const std::string &field);

bool IsVertexCompositeIndexed(const std::string &label,
const std::vector<std::string> &field);

/**
* @brief Deletes the index to 'vertex_label:field'
*
Expand All @@ -488,6 +495,9 @@ class GraphDB {
*/
bool DeleteVertexIndex(const std::string &label, const std::string &field);

bool DeleteVertexCompositeIndex(const std::string& label,
const std::vector<std::string>& fields);

/**
* @brief Deletes the index to 'edge_label:field'
*
Expand Down
35 changes: 35 additions & 0 deletions include/lgraph/lgraph_txn.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class OutEdgeIterator;
class InEdgeIterator;
class VertexIndexIterator;
class EdgeIndexIterator;
class VertexCompositeIndexIterator;

/**
* @brief TuGraph operations happen in transactions. A transaction is sequence of operations
Expand Down Expand Up @@ -431,6 +432,13 @@ class Transaction {
*/
std::vector<IndexSpec> ListVertexIndexes();

/**
* @brief List indexes
*
* @returns A vector of vertex composite index specs.
*/
std::vector<CompositeIndexSpec> ListVertexCompositeIndexes();

/**
* @brief List indexes
*
Expand All @@ -454,6 +462,11 @@ class Transaction {
const FieldData& key_start,
const FieldData& key_end);

VertexCompositeIndexIterator GetVertexCompositeIndexIterator(size_t label_id,
const std::vector<size_t>& field_id,
const std::vector<FieldData>& key_start,
const std::vector<FieldData>& key_end);

/**
* @brief Gets edge index iterator. The iterator has field value [key_start, key_end]. So
* key_start=key_end=v returns an iterator pointing to all edges that has field
Expand Down Expand Up @@ -485,6 +498,11 @@ class Transaction {
const FieldData& key_start,
const FieldData& key_end);

VertexCompositeIndexIterator GetVertexCompositeIndexIterator(const std::string& label,
const std::vector<std::string>& field,
const std::vector<FieldData>& key_start,
const std::vector<FieldData>& key_end);

/**
* @brief Gets index iterator. The iterator has field value [key_start, key_end]. So
* key_start=key_end=v returns an iterator pointing to all edges that has field
Expand Down Expand Up @@ -516,6 +534,11 @@ class Transaction {
const std::string& key_start,
const std::string& key_end);

VertexCompositeIndexIterator GetVertexCompositeIndexIterator(const std::string& label,
const std::vector<std::string>& field,
const std::vector<std::string>& key_start,
const std::vector<std::string>& key_end);

/**
* @brief Gets index iterator. The iterator has field value [key_start, key_end]. So
* key_start=key_end=v returns an iterator pointing to all edges that has field
Expand Down Expand Up @@ -583,6 +606,10 @@ class Transaction {
const std::string& field_name,
const std::string& field_value_string);

VertexIterator GetVertexByUniqueCompositeIndex(const std::string& label_name,
const std::vector<std::string>& field_name,
const std::vector<std::string>& field_value_string);

/**
* @brief Gets edge by unique index. Throws exception if there is no such vertex.
*
Expand All @@ -609,6 +636,10 @@ class Transaction {
const std::string& field_name,
const FieldData& field_value);

VertexIterator GetVertexByUniqueCompositeIndex(const std::string& label_name,
const std::vector<std::string>& field_name,
const std::vector<FieldData>& field_value);

/**
* @brief Gets edge by unique index. Throws exception if there is no such vertex.
*
Expand All @@ -634,6 +665,10 @@ class Transaction {
VertexIterator GetVertexByUniqueIndex(size_t label_id, size_t field_id,
const FieldData& field_value);

VertexIterator GetVertexByUniqueCompositeIndex(size_t label_id,
const std::vector<size_t>& field_id,
const std::vector<FieldData>& field_value);

/**
* @brief Gets edge by unique index. Throws exception if there is no such vertex.
*
Expand Down
9 changes: 9 additions & 0 deletions include/lgraph/lgraph_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,15 @@ struct IndexSpec {
IndexType type;
};

/** @brief A composite index specifier. */
struct CompositeIndexSpec {
/** @brief label name */
std::string label;
/** @brief fields name */
std::vector<std::string> fields;
CompositeIndexType type;
};

struct EdgeUid {
EdgeUid() : src(0), dst(0), lid(0), tid(0), eid(0) {}
EdgeUid(int64_t s, int64_t d, uint16_t l, int64_t t, int64_t e)
Expand Down
93 changes: 93 additions & 0 deletions include/lgraph/lgraph_vertex_composite_index_iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2022 AntGroup CO., Ltd.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

#pragma once
#include <memory>
#include "lgraph/lgraph_types.h"

namespace lgraph {

class Transaction;
class CompositeIndexIterator;

} // namespace lgraph

namespace lgraph_api {
class Transaction;

/**
* @brief VertexIndexIterator can be used to access a set of vertices that has the same indexed
* value. If the index is unique (that is, each vertex has a unique index value), then
* each VertexIndexIterator will only have one VertexId, and will become invalid after
* Next()
* is called.
*
* An VertexIndexIterator is valid iff it points to a valid (index_value, vid) pair,
* otherwise it is invalid. Calling member function on an invalid VertexIndexIterator
* throws an exception, except for the IsValid() function.
*/
class VertexCompositeIndexIterator {
friend class Transaction;

std::unique_ptr<lgraph::CompositeIndexIterator> it_;
std::shared_ptr<lgraph::Transaction> txn_;

VertexCompositeIndexIterator(lgraph::CompositeIndexIterator &&it,
const std::shared_ptr<lgraph::Transaction> &txn);

VertexCompositeIndexIterator(const VertexCompositeIndexIterator &) = delete;
VertexCompositeIndexIterator &operator=(const VertexCompositeIndexIterator &) = delete;

public:
VertexCompositeIndexIterator(VertexCompositeIndexIterator &&rhs);
VertexCompositeIndexIterator &operator=(VertexCompositeIndexIterator &&);
~VertexCompositeIndexIterator();

/**
* @brief Closes this iterator
*/
void Close();

/**
* @brief Query if this iterator is valid, i.e. the Key and Vid can be queried.
*
* @returns True if valid, false if not.
*/
bool IsValid() const;

/**
* @brief Move to the next vertex id in the list, which consists of all the valid vertex
* ids of the iterator and is sorted from small to large. If we hit the end of the
* list, iterator will become invalid and false is returned.
*
* @returns True if it succeeds, otherwise false.
*/
bool Next();

/**
* @brief Gets the current index value. The vids are sorted in (IndexValue, Vid) order.
* When Next() is called, the iterator moves from one vid to next, possibly moving
* from one IndexValue to another. This function tells the IndexValue currently
* pointed to.
*
* @returns The key.
*/
std::vector<FieldData> GetIndexValue() const;

std::vector<FieldType> GetIndexType() const;

/**
* @brief Gets the current vertex id.
*
* @returns The current vertex id.
*/
int64_t GetVid() const;
};

} // namespace lgraph_api
3 changes: 2 additions & 1 deletion src/BuildLGraphApi.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ set(LGRAPH_API_SRC
lgraph_api/lgraph_vertex_iterator.cpp
lgraph_api/lgraph_result.cpp
lgraph_api/lgraph_exceptions.cpp
lgraph_api/result_element.cpp)
lgraph_api/result_element.cpp
lgraph_api/lgraph_vertex_composite_index_iterator.cpp)

set(TARGET_LGRAPH lgraph)

Expand Down
Loading

0 comments on commit fec3d91

Please sign in to comment.