/usr/lib/obs/server/BSDB.pm is in obs-server 2.7.1-10.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 | #
# Copyright (c) 2006, 2007 Michael Schroeder, Novell Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program (see the file COPYING); if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#
################################################################
#
# Simple database for storing hierarchical data, uses BSDBIndex for
# storing data and indices.
# Also supports search via BSXPath/BSXPathKeys.
package BSDB;
use BSDBIndex;
sub opendb {
my ($dbpath, $table) = @_;
my $db = BSDBIndex::opendb($dbpath);
if ($table eq '') {
$db->{'table'} = 'data';
$db->{'index'} = '';
} else {
$db->{'table'} = $table;
$db->{'index'} = "$table/";
}
return bless $db;
}
sub fetch {
my ($db, $key) = @_;
if ($db->{'fetch'}) {
return $db->{'fetch'}->($db, $key);
}
my @v = BSDBIndex::getvalues($db, $db->{'table'}, $key);
return $v[0];
}
sub torel {
my ($path, $key, $v, $rel) = @_;
return unless defined $v;
if (ref($v) eq '') {
push @$rel, [$path, $v, $key, "$path\0$v"] if $v ne '';
return;
}
if (ref($v) eq 'ARRAY') {
for my $vv (@$v) {
if (ref($vv) eq '') {
push @$rel, [$path, $vv, $key, "$path\0$vv"] if $vv ne '';
} else {
torel($path, $key, $vv, $rel);
}
}
return;
}
if (ref($v) eq 'HASH') {
$path .= '/' if $path ne '';
for my $vv (sort keys %$v) {
if (ref($v->{$vv}) eq '') {
push @$rel, ["$path$vv", $v->{$vv}, $key, "$path$vv\0$v->{$vv}"] if $v->{$vv} ne '';
} else {
torel("$path$vv", $key, $v->{$vv}, $rel);
}
}
}
}
sub updateindex_rel {
my ($db, $oldrel, $newrel) = @_;
return unless @$oldrel || @$newrel;
if (@$oldrel + @$newrel > 256) {
while (@$oldrel) {
my @chunk = splice(@$oldrel, 0, 256);
BSDBIndex::modify($db, \@chunk);
}
while (@$newrel) {
my @chunk = splice(@$newrel, 0, 256);
BSDBIndex::modify($db, undef, \@chunk);
}
} else {
BSDBIndex::modify($db, $oldrel, $newrel);
}
}
sub updateindex {
my ($db, $key, $old, $new) = @_;
my $index = $db->{'index'};
$index =~ s/\/$//;
my @oldrel;
my @newrel;
torel($index, $key, $old, \@oldrel);
torel($index, $key, $new, \@newrel);
if ($db->{'noindex'}) {
@newrel = grep {!$db->{'noindex'}->{$_->[0]}} @newrel;
}
# delete all entries that are both in oldrel and newrel
my %in = map {$_->[3] => 1} @oldrel;
%in = map {$_->[3] => 1} grep {$in{$_->[3]}} @newrel;
@oldrel = grep {!$in{$_->[3]}} @oldrel;
@newrel = grep {!$in{$_->[3]}} @newrel;
return unless @oldrel || @newrel;
updateindex_rel($db, \@oldrel, \@newrel);
}
sub store_callback {
my ($db, $rel, $data) = @_;
my $old = ($data || [])[0];
my $new = $rel->[3];
my $key = $rel->[1];
updateindex($db, $key, $old, $new) unless $db->{'noindexatall'};
if (defined($new)) {
@$data = ($new);
} else {
@$data = ();
}
return 1;
}
sub store {
my ($db, $key, $v) = @_;
my $rel = [$db->{'table'}, $key, \&store_callback, $v];
if ($db->{'store'}) {
$db->{'store'}->($db, $key, $v, sub {store_callback($db, $rel, [$_[0]])});
} else {
BSDBIndex::modify($db, [$rel]);
}
}
#
# Search functions
#
sub selectpath {
my ($v, $path) = @_;
$v = [ $v ] unless ref($v) eq 'ARRAY';
my @v = @$v;
my $c;
while(1) {
last if !defined($path) || $path eq '';
($c, $path) = split('/', $path, 2);
for my $vv (splice(@v)) {
next unless ref($vv) eq 'HASH';
$vv = $vv->{$c};
next unless defined($vv);
push @v, ref($vv) eq 'ARRAY' ? @$vv : $vv;
}
}
return @v;
}
sub values {
my ($db, $path, $lkeys) = @_;
if ($db->{'indexfunc'} && $db->{'indexfunc'}->{$path}) {
return $db->{'indexfunc'}->{$path}->($db, $path, undef, $lkeys);
}
if (($db->{'noindex'} && $db->{'noindex'}->{$path}) || $db->{'noindexatall'} || ($lkeys && $db->{'cheapfetch'})) {
$lkeys = [ $db->keys() ] unless $lkeys;
my @v;
for my $k (@$lkeys) {
push @v, selectpath($db->fetch($k), $path);
}
my %v = map {$_ => 1} @v;
return sort keys %v;
}
return BSDBIndex::getkeys($db, "$db->{'index'}$path");
}
sub keys {
my ($db, $path, $value, $lkeys) = @_;
if (!defined($path)) {
return @$lkeys if $lkeys;
$path = $db->{'allkeyspath'};
return BSDBIndex::getkeys($db, $db->{'table'}) unless defined $path;
if ($db->{'indexfunc'} && $db->{'indexfunc'}->{$path}) {
return $db->{'indexfunc'}->{$path}->($db);
}
return map {BSDBIndex::getvalues($db, "$db->{'index'}$path", $_)} BSDBIndex::getkeys($db, "$db->{'index'}$path");
}
if ($db->{'indexfunc'} && $db->{'indexfunc'}->{$path}) {
return $db->{'indexfunc'}->{$path}->($db, $path, $value, $lkeys);
}
if (($db->{'noindex'} && $db->{'noindex'}->{$path}) || $db->{'noindexatall'}) {
$lkeys = [ $db->keys() ] unless $lkeys;
my @v;
my @k;
for my $k (@$lkeys) {
push @k, $k if grep {$_ eq $value} selectpath($db->fetch($k), $path);
}
return @k;
}
return BSDBIndex::getvalues($db, "$db->{'index'}$path", $value);
}
1;
|