| 1 | package FalkorDB; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Redis::Fast; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Carp qw(croak); | ||
| 7 | use FalkorDB::Graph; | ||
| 8 | |||
| 9 | our $VERSION = '0.01'; | ||
| 10 | |||
| 11 | sub new { | ||
| 12 | my ( $class, %args ) = @_; | ||
| 13 | |||
| 14 | my $redis; | ||
| 15 | if ( $args{redis} ) { | ||
| 16 | $redis = $args{redis}; | ||
| 17 | } | ||
| 18 | else { | ||
| 19 | my $host = $args{host} // 'falkordb'; | ||
| 20 | my $port = $args{port} // 6379; | ||
| 21 | my $server = "$host:$port"; | ||
| 22 | |||
| 23 | my %redis_args = ( server => $server, ); | ||
| 24 | |||
| 25 | # Only add password if it is defined and not empty | ||
| 26 | if ( defined $args{password} && $args{password} ne '' ) { | ||
| 27 | $redis_args{password} = $args{password}; | ||
| 28 | } | ||
| 29 | |||
| 30 | $redis = Redis::Fast->new(%redis_args); | ||
| 31 | |||
| 32 | # If username is provided, we can attempt AUTH (RESP3 uses AUTH username password) | ||
| 33 | # But for empty username/password as requested, we don't need to authenticate. | ||
| 34 | if ( defined $args{username} | ||
| 35 | && $args{username} ne '' | ||
| 36 | && defined $args{password} | ||
| 37 | && $args{password} ne '' ) | ||
| 38 | { | ||
| 39 | $redis->auth( $args{username}, $args{password} ); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | my $self = { redis => $redis, }; | ||
| 44 | |||
| 45 | return bless $self, $class; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub redis { | ||
| 49 | my ($self) = @_; | ||
| 50 | return $self->{redis}; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub select_graph { | ||
| 54 | my ( $self, $name ) = @_; | ||
| 55 | return FalkorDB::Graph->new( $self, $name ); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub graph { | ||
| 59 | my ( $self, $name ) = @_; | ||
| 60 | return $self->select_graph($name); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub delete_graph { | ||
| 64 | my ( $self, $name ) = @_; | ||
| 65 | my ( $res, $error ) = $self->{redis}->__std_cmd( "GRAPH.DELETE", $name ); | ||
| 66 | if ( defined $error ) { | ||
| 67 | croak("[GRAPH.DELETE] $error"); | ||
| 68 | } | ||
| 69 | return defined $res && $res eq 'OK'; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub list_graphs { | ||
| 73 | my ($self) = @_; | ||
| 74 | my ( $res, $error ) = $self->{redis}->__std_cmd("GRAPH.LIST"); | ||
| 75 | if ( defined $error ) { | ||
| 76 | croak("[GRAPH.LIST] $error"); | ||
| 77 | } | ||
| 78 | return ref $res eq 'ARRAY' ? $res : []; | ||
| 79 | } | ||
| 80 | |||
| 81 | 1; | ||
| 82 | __END__ | ||