… | |
… | |
15 | package DC::DB; |
15 | package DC::DB; |
16 | |
16 | |
17 | use strict; |
17 | use strict; |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
|
|
20 | use File::Path (); |
20 | use Carp (); |
21 | use Carp (); |
21 | use Storable (); |
22 | use Storable (); |
22 | use Config; |
23 | use Config; |
23 | use BDB; |
24 | use BDB; |
24 | |
25 | |
25 | use DC; |
26 | use DC; |
26 | |
27 | |
27 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
28 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
30 | |
31 | |
|
|
32 | unless (-d $DB_HOME) { |
31 | if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { |
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
32 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
33 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
35 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
34 | } |
|
|
35 | |
|
|
36 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { |
36 | } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") { |
37 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
37 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
38 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
38 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
|
|
39 | } else { |
|
|
40 | File::Path::mkpath [$DB_HOME] |
|
|
41 | or die "unable to create database directory $DB_HOME: $!"; |
|
|
42 | } |
39 | } |
43 | } |
40 | |
44 | |
41 | BDB::max_poll_time 0.03; |
45 | BDB::max_poll_time 0.03; |
42 | BDB::max_parallel 1; |
46 | BDB::max_parallel 1; |
43 | |
47 | |
44 | our $DB_ENV; |
48 | our $DB_ENV; |
45 | our $DB_STATE; |
49 | our $DB_STATE; |
46 | our %DB_TABLE; |
50 | our %DB_TABLE; |
|
|
51 | our $TILE_SEQ; |
47 | |
52 | |
48 | sub try_open_db { |
53 | sub try_open_db { |
49 | mkdir $DB_HOME, 0777; |
54 | File::Path::mkpath [$DB_HOME]; |
50 | |
55 | |
51 | $DB_ENV = db_env_create; |
56 | my $env = db_env_create; |
52 | |
57 | |
53 | $DB_ENV->set_errfile (\*STDERR); |
58 | $env->set_errfile (\*STDERR); |
54 | $DB_ENV->set_msgfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
55 | $DB_ENV->set_verbose (-1, 1); |
60 | $env->set_verbose (-1, 1); |
56 | |
61 | |
57 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
62 | $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
|
|
63 | $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
|
|
64 | $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
|
|
65 | |
|
|
66 | $env->set_timeout (3, BDB::SET_TXN_TIMEOUT); |
|
|
67 | $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT); |
|
|
68 | |
58 | $DB_ENV->set_cachesize (0, 2048 * 1024, 0); |
69 | $env->set_cachesize (0, 2048 * 1024, 0); |
59 | |
70 | |
60 | db_env_open $DB_ENV, $DB_HOME, |
71 | db_env_open $env, $DB_HOME, |
61 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
72 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
62 | 0666; |
73 | 0666; |
63 | |
74 | |
64 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
75 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
|
|
76 | |
|
|
77 | $DB_ENV = $env; |
65 | |
78 | |
66 | 1 |
79 | 1 |
67 | } |
80 | } |
68 | |
81 | |
69 | sub table($) { |
82 | sub table($) { |
… | |
… | |
86 | |
99 | |
87 | ############################################################################# |
100 | ############################################################################# |
88 | |
101 | |
89 | our $WATCHER; |
102 | our $WATCHER; |
90 | our $SYNC; |
103 | our $SYNC; |
91 | our $tilemap; |
104 | our $facemap; |
92 | |
105 | |
93 | sub exists($$$) { |
106 | sub exists($$$) { |
94 | my ($db, $key, $cb) = @_; |
107 | my ($db, $key, $cb) = @_; |
95 | |
108 | |
96 | my $data; |
109 | my $data; |
… | |
… | |
138 | my ($name, $cb) = @_; |
151 | my ($name, $cb) = @_; |
139 | |
152 | |
140 | my $table = table "facemap"; |
153 | my $table = table "facemap"; |
141 | my $id; |
154 | my $id; |
142 | |
155 | |
143 | db_get $table, undef, $name, $id, 0; |
156 | db_get $table, undef, $name => $id, 0; |
144 | return $cb->($id) unless $!; |
157 | $! or return $cb->($id); |
145 | |
158 | |
146 | for (1..100) { |
159 | unless ($TILE_SEQ) { |
147 | my $txn = $DB_ENV->txn_begin; |
160 | $TILE_SEQ = $table->sequence; |
148 | db_get $table, $txn, id => $id, 0; |
161 | $TILE_SEQ->initial_value (64); |
|
|
162 | $TILE_SEQ->set_cachesize (0); |
|
|
163 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
|
|
164 | } |
149 | |
165 | |
150 | $id = 64 if $id < 64; |
166 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
151 | |
167 | |
152 | ++$id; |
168 | die "unable to allocate tile id: $!" |
153 | |
169 | if $!; |
154 | db_put $table, $txn, id => $id, 0; |
|
|
155 | db_txn_finish $txn; |
|
|
156 | |
|
|
157 | $SYNC->again unless $SYNC->is_active; |
|
|
158 | |
|
|
159 | return $cb->($id) unless $!; |
|
|
160 | |
|
|
161 | select undef, undef, undef, 0.01 * rand; |
|
|
162 | } |
170 | |
|
|
171 | db_put $table, undef, $name => $id, 0; |
|
|
172 | $cb->($id); |
163 | |
173 | |
164 | die "maximum number of transaction retries reached - database problems?"; |
|
|
165 | } |
174 | } |
166 | |
175 | |
167 | sub get_tile_id_sync($) { |
176 | sub get_tile_id_sync($) { |
168 | my ($name) = @_; |
177 | my ($name) = @_; |
169 | |
178 | |
170 | $tilemap->{$name} ||= do { |
179 | $facemap->{$name} ||= do { |
171 | my $id; |
180 | my $id; |
172 | do_get_tile_id $name, sub { |
181 | do_get_tile_id $name, sub { |
173 | $id = $_[0]; |
182 | $id = $_[0]; |
174 | }; |
183 | }; |
175 | BDB::flush; |
184 | BDB::flush; |
… | |
… | |
400 | close $FH; |
409 | close $FH; |
401 | } |
410 | } |
402 | |
411 | |
403 | package DC::DB; |
412 | package DC::DB; |
404 | |
413 | |
|
|
414 | sub nuke_db { |
|
|
415 | File::Path::mkpath [$DB_HOME]; |
|
|
416 | eval { File::Path::rmtree $DB_HOME }; |
|
|
417 | } |
|
|
418 | |
405 | sub open_db { |
419 | sub open_db { |
406 | unless (eval { try_open_db }) { |
420 | unless (eval { try_open_db }) { |
407 | warn "$@";#d# |
421 | warn "$@";#d# |
408 | eval { File::Path::rmtree $DB_HOME }; |
422 | eval { nuke_db }; |
409 | try_open_db; |
423 | try_open_db; |
410 | } |
424 | } |
411 | |
425 | |
412 | # fetch the full face table first |
426 | # fetch the full face table first |
413 | unless ($tilemap) { |
427 | unless ($facemap) { |
414 | do_table facemap => sub { |
428 | do_table facemap => sub { |
415 | $tilemap = $_[0]; |
429 | $facemap = $_[0]; |
416 | delete $tilemap->{id}; |
430 | delete $facemap->{id}; |
417 | my %maptile = reverse %$tilemap;#d# |
431 | my %maptile = reverse %$facemap;#d# |
418 | if ((scalar keys %$tilemap) != (scalar keys %maptile)) {#d# |
432 | if ((scalar keys %$facemap) != (scalar keys %maptile)) {#d# |
419 | $tilemap = { };#d# |
433 | $facemap = { };#d# |
420 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
434 | DC::error "FATAL: facemap is not a 1:1 mapping, please report this and delete your $DB_HOME directory!\n";#d# |
421 | }#d# |
435 | }#d# |
422 | }; |
436 | }; |
423 | } |
437 | } |
424 | |
438 | |
… | |
… | |
428 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
442 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
429 | }; |
443 | }; |
430 | } |
444 | } |
431 | |
445 | |
432 | END { |
446 | END { |
|
|
447 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
448 | if $DB_ENV; |
|
|
449 | |
|
|
450 | undef $TILE_SEQ; |
433 | %DB_TABLE = (); |
451 | %DB_TABLE = (); |
434 | undef $DB_ENV; |
452 | undef $DB_ENV; |
435 | } |
453 | } |
436 | |
454 | |
437 | 1; |
455 | 1; |