… | |
… | |
18 | use utf8; |
18 | use utf8; |
19 | |
19 | |
20 | use File::Path (); |
20 | use File::Path (); |
21 | use Carp (); |
21 | use Carp (); |
22 | use Storable (); |
22 | use Storable (); |
|
|
23 | use AnyEvent::Util (); |
23 | use Config; |
24 | use Config; |
24 | use BDB; |
25 | use BDB; |
25 | |
26 | |
26 | use DC; |
27 | use DC; |
27 | |
28 | |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
29 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
30 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
31 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
|
|
32 | |
|
|
33 | sub FIRST_TILE_ID () { 64 } |
31 | |
34 | |
32 | unless (-d $DB_HOME) { |
35 | unless (-d $DB_HOME) { |
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
36 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
37 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
35 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
38 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
… | |
… | |
57 | |
60 | |
58 | $env->set_errfile (\*STDERR); |
61 | $env->set_errfile (\*STDERR); |
59 | $env->set_msgfile (\*STDERR); |
62 | $env->set_msgfile (\*STDERR); |
60 | $env->set_verbose (-1, 1); |
63 | $env->set_verbose (-1, 1); |
61 | |
64 | |
62 | $env->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
65 | $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
|
|
66 | $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
|
|
67 | $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
|
|
68 | |
|
|
69 | $env->set_timeout (3, BDB::SET_TXN_TIMEOUT); |
|
|
70 | $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT); |
|
|
71 | |
63 | $env->set_cachesize (0, 2048 * 1024, 0); |
72 | $env->set_cachesize (0, 2048 * 1024, 0); |
64 | |
73 | |
65 | db_env_open $env, $DB_HOME, |
74 | db_env_open $env, $DB_HOME, |
66 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
75 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
67 | 0666; |
76 | 0666; |
… | |
… | |
76 | sub table($) { |
85 | sub table($) { |
77 | $DB_TABLE{$_[0]} ||= do { |
86 | $DB_TABLE{$_[0]} ||= do { |
78 | my ($table) = @_; |
87 | my ($table) = @_; |
79 | |
88 | |
80 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
89 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
90 | |
|
|
91 | $DB_ENV#d# |
|
|
92 | or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
81 | |
93 | |
82 | my $db = db_create $DB_ENV; |
94 | my $db = db_create $DB_ENV; |
83 | $db->set_flags (BDB::CHKSUM); |
95 | $db->set_flags (BDB::CHKSUM); |
84 | |
96 | |
85 | db_open $db, undef, $table, undef, BDB::BTREE, |
97 | db_open $db, undef, $table, undef, BDB::BTREE, |
… | |
… | |
150 | db_get $table, undef, $name => $id, 0; |
162 | db_get $table, undef, $name => $id, 0; |
151 | $! or return $cb->($id); |
163 | $! or return $cb->($id); |
152 | |
164 | |
153 | unless ($TILE_SEQ) { |
165 | unless ($TILE_SEQ) { |
154 | $TILE_SEQ = $table->sequence; |
166 | $TILE_SEQ = $table->sequence; |
155 | $TILE_SEQ->initial_value (64); |
167 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
156 | $TILE_SEQ->set_cachesize (0); |
168 | $TILE_SEQ->set_cachesize (0); |
157 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
169 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
158 | } |
170 | } |
159 | |
171 | |
160 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
172 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
… | |
… | |
344 | |
356 | |
345 | print { $LOG_FH{$path} } "$ts $line\n" |
357 | print { $LOG_FH{$path} } "$ts $line\n" |
346 | } |
358 | } |
347 | |
359 | |
348 | sub run { |
360 | sub run { |
349 | ($FH, my $fh) = DC::socketpipe; |
361 | ($FH, my $fh) = AnyEvent::Util::portable_socketpair |
|
|
362 | or die "unable to create database socketpair: $!"; |
350 | |
363 | |
351 | my $oldfh = select $FH; $| = 1; select $oldfh; |
364 | my $oldfh = select $FH; $| = 1; select $oldfh; |
352 | my $oldfh = select $fh; $| = 1; select $oldfh; |
365 | my $oldfh = select $fh; $| = 1; select $oldfh; |
353 | |
366 | |
354 | my $pid = fork; |
367 | my $pid = fork; |
… | |
… | |
436 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
449 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
437 | }; |
450 | }; |
438 | } |
451 | } |
439 | |
452 | |
440 | END { |
453 | END { |
|
|
454 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
455 | if $DB_ENV; |
|
|
456 | |
441 | undef $TILE_SEQ; |
457 | undef $TILE_SEQ; |
442 | %DB_TABLE = (); |
458 | %DB_TABLE = (); |
443 | undef $DB_ENV; |
459 | undef $DB_ENV; |
444 | } |
460 | } |
445 | |
461 | |