Skip to content

Commit

Permalink
Optimize inserts. We don't need to fetch the just-inserted rows
Browse files Browse the repository at this point in the history
because we have all the information needed to create the objects.
  • Loading branch information
rcaputo committed May 5, 2008
1 parent 28f85fe commit dc847a3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
7 changes: 5 additions & 2 deletions trunk/ThirdLobe/ArcStore.pm
Expand Up @@ -148,6 +148,8 @@ sub arc_store {
$obj = $self->anchor_store($object);
}

# TODO - Fetch the arc and return it, otherwise add it.

my @arcs = $db->arc_from_arcs($sub, $prd, $obj);
unless (@arcs) {
@arcs = $db->arc_add($sub, $prd, $obj);
Expand Down Expand Up @@ -186,7 +188,7 @@ return sets may consume a lot of memory.
=cut

sub arc_fetch {
my ($self, $subject, $predicate, $object) = @_;
my ($self, $subject, $predicate, $object, $limit) = @_;

my $sub;
if (defined $subject and length $subject) {
Expand Down Expand Up @@ -221,7 +223,7 @@ sub arc_fetch {
}
}

my @arcs = $self->_db()->arc_from_arcs($sub, $prd, $obj);
my @arcs = $self->_db()->arc_from_arcs($sub, $prd, $obj, $limit);

return unless @arcs;
return @arcs if wantarray;
Expand All @@ -243,6 +245,7 @@ anchor_store() is mostly used internally by ThirdLobe::ArcStore.
sub anchor_store {
my ($self, $text) = @_;

# TODO - Return node if it exists, otherwise add and return.
my $node = $self->_db()->node_from_text($text);
return $self->_db()->arc_from_seq($node->arc_seq()) if $node;

Expand Down
46 changes: 35 additions & 11 deletions trunk/ThirdLobe/Database.pm
Expand Up @@ -56,8 +56,15 @@ sub node_add {
$sth->execute($arc->seq(), $key, $text) or die $sth->errstr();
$sth->finish();

# Fetch the node back out, with a sequence number and all.
return $self->node_from_text($text);
# Return the node represented by the recently inserted row.
return ThirdLobe::Node->new(
{
seq => $dbh->last_insert_id(undef, undef, "node", undef),
arc_seq => $arc->seq(),
key => $key,
val => $text,
}
);
}

=head2 _node_hash TEXT
Expand Down Expand Up @@ -230,8 +237,20 @@ sub arc_add {
or die $sth->errstr();
$sth->finish();

# Fetch the arc back out, with sequence number and all.
return $self->arc_from_arcs($sub_arc, $prd_arc, $obj_arc);
# Return an arc that represents the newly inserted row.
# Since we know the arcs, we can pre-cache them.
return ThirdLobe::Arc->new(
{
db => $self,
seq => $dbh->last_insert_id(undef, undef, "arc", undef),
sub_seq => $sub_arc->seq(),
prd_seq => $prd_arc->seq(),
obj_seq => $obj_arc->seq(),
sub_arc => $sub_arc,
prd_arc => $prd_arc,
obj_arc => $obj_arc,
}
);
}

=head2 arc_from_arcs SUBJECT_ARC, PREDICATE_ARC, OBJECT_ARC
Expand All @@ -247,15 +266,19 @@ Undefined parameters are treated as wildcards.
=cut

sub arc_from_arcs {
my ($self, $sub_arc, $prd_arc, $obj_arc) = @_;
my ($self, $sub_arc, $prd_arc, $obj_arc, $limit) = @_;
my $dbh = $self->[DBH];

my ($where_clause, @values) = $self->build_arc_query(
$sub_arc, $prd_arc, $obj_arc
);

my $sth = $dbh->prepare_cached("SELECT * FROM arc" . $where_clause);
$sth->execute(@values);
my $sql = "SELECT * FROM arc " . $where_clause;
$sql .= " LIMIT $limit" if $limit;
warn $sql;

my $sth = $dbh->prepare_cached($sql) or die $dbh->errstr;
$sth->execute(@values) or die $sth->errstr;

my (%memo, @arcs);
while (my $row = $sth->fetchrow_hashref()) {
Expand Down Expand Up @@ -290,10 +313,11 @@ sub arc_count {
$sub_arc, $prd_arc, $obj_arc
);

my $sth = $dbh->prepare_cached(
"SELECT count(seq) FROM arc" . $where_clause
);
$sth->execute(@values);
my $sql = "SELECT count(seq) FROM arc" . $where_clause;
warn $sql;

my $sth = $dbh->prepare_cached($sql) or die $dbh->errstr;
$sth->execute(@values) or die $sth->errstr;

my @row = $sth->fetchrow_array();
$sth->finish();
Expand Down

0 comments on commit dc847a3

Please sign in to comment.