AtteanX-Store-LMDB
view release on metacpan or search on metacpan
lib/AtteanX/Store/LMDB.pm view on Meta::CPAN
sub get_graphs {
my $self = shift;
my $txn = $self->env->BeginTxn(MDB_RDONLY);
my $graphs = $txn->OpenDB({ dbname => 'graphs', });
my $i2t = $txn->OpenDB({ dbname => 'id_to_term', });
my ($key, $value);
my @graph_ids;
$self->iterate_database($graphs, sub {
my ($key, $value) = @_;
my ($gid) = unpack('Q>', $key);
push(@graph_ids, $gid);
});
my $sub = sub {
my $txn = $self->env->BeginTxn(MDB_RDONLY);
my $i2t = $txn->OpenDB({ dbname => 'id_to_term', });
GRAPH: while (my $gid = shift(@graph_ids)) {
my $key = pack('Q>', $gid);
my $termdata = $i2t->get($key);
next QUAD unless ($termdata);
my $term = $self->_parse_term($termdata);
next GRAPH unless ($term);
return $term;
}
return;
};
return Attean::CodeIterator->new( generator => $sub, item_type => 'Attean::API::Term' );
}
lib/AtteanX/Store/LMDB.pm view on Meta::CPAN
return;
}
my $next_quad = 'next_unassigned_quad_id';
my ($next) = unpack('Q>', $stats->get($next_quad));
# warn "next quad id: $next\n";
my $qid_value = $next++;
my $qid = pack('Q>', $qid_value);
my $qids = pack('Q>4', @ids);
my $gid = pack('Q>', $ids[3]);
my $graphs_dbi = $txn->open('graphs');
my $quads_dbi = $txn->open('quads');
my $stats_dbi = $txn->open('stats');
$txn->put($quads_dbi, $qid, $qids);
my $graphs_cursor = $graphs->Cursor;
my $key = $gid;
my $empty = '';
eval {
local($LMDB_File::die_on_err) = 0;
if (my $err = $graphs_cursor->get($key, $empty, MDB_SET_RANGE)) {
$graphs_cursor->put($gid, $empty);
} else {
if ($key ne $gid) {
$graphs_cursor->put($gid, $empty);
}
}
};
$self->_add_quad_to_indexes($qid, \@ids, $txn);
$txn->put($stats_dbi, $next_quad, pack('Q>', $next));
$txn->commit();
}
sub add_quad_with_txn {
lib/AtteanX/Store/LMDB.pm view on Meta::CPAN
my $qid = unpack('Q>', $key);
my (@ids) = unpack('Q>4', $value);
if ($ids[0] == $remove_ids[0] and $ids[1] == $remove_ids[1] and $ids[2] == $remove_ids[2] and $ids[3] == $remove_ids[3]) {
my $g = $ids[3];
$self->_remove_quad_to_indexes($qid, \@ids, $txn);
$cursor->del();
unless ($self->_graph_id_exists_with_txn($txn, $quads, $t2i, $g)) {
# no more quads with this graph, so delete it from the graphs table
my $graphs_cursor = $graphs->Cursor;
my $gid = pack('Q>', $g);
my $key = $gid;
my $empty = '';
unless ($graphs_cursor->get($key, $empty, MDB_SET_RANGE)) {
if ($gid eq $key) {
$graphs_cursor->del();
}
}
}
$txn->commit();
return;
}
} continue {
last if $cursor->get($key, $value, MDB_NEXT);
lib/AtteanX/Store/LMDB.pm view on Meta::CPAN
$txn->put($quads_dbi, $qid, $qids);
$self->_add_quad_to_indexes($qid, \@ids, $txn);
} else {
$self->add_quad($q);
}
}
if ($BULK_LOAD) {
my $empty = '';
my $graphs_cursor = $graphs_dbi->Cursor;
foreach my $gid (values %graphs) {
my $key = $gid;
eval {
local($LMDB_File::die_on_err) = 0;
if (my $err = $graphs_cursor->get($key, $empty, MDB_SET_RANGE)) {
$graphs_cursor->put($gid, $empty);
} else {
if ($key ne $gid) {
$graphs_cursor->put($gid, $empty);
}
}
};
}
$txn->put($stats_dbi, $next_quad, pack('Q>', $next));
$txn->commit();
}
}
sub _quad_exists_with_txn {
lib/AtteanX/Store/LMDB.pm view on Meta::CPAN
my $quadids = $self->_get_unordered_matching_quads($quads, \%bound);
return scalar(@$quadids);
}
sub _graph_id_exists_with_txn {
my $self = shift;
my $txn = shift;
my $quads = shift;
my $t2i = shift;
my $gid = shift;
my %bound = (3 => $gid);
if (my $best = $self->_best_index(\%bound, $txn)) {
my ($index, $score) = @$best;
my $order = $self->indexes->{$index};
my @positions = @$order[0..$score-1];
my @prefix = map { $bound{$_} } @positions;
my @lower = @prefix;
my @upper = @prefix;
$upper[-1]++;
my $quadids = $self->_get_ordered_matching_quads($txn, \%bound, $index, \@lower, \@upper);
return scalar(@$quadids);
( run in 0.568 second using v1.01-cache-2.11-cpan-5735350b133 )