… | |
… | |
23 | use Config; |
23 | use Config; |
24 | use BDB; |
24 | use BDB; |
25 | |
25 | |
26 | use DC; |
26 | use DC; |
27 | |
27 | |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION_MAJOR . "." . BDB::VERSION_MINOR . "-$Config{archname}"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
30 | our $DB_HOME = "$Deliantra::VARDIR/$DBDIR"; |
|
|
31 | |
|
|
32 | sub FIRST_TILE_ID () { 64 } |
31 | |
33 | |
32 | unless (-d $DB_HOME) { |
34 | unless (-d $DB_HOME) { |
33 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
35 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
34 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
36 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
35 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
37 | print STDERR "INFO: moved old database from $Deliantra::VARDIR/$ODBDIR to $DB_HOME\n"; |
… | |
… | |
51 | our $TILE_SEQ; |
53 | our $TILE_SEQ; |
52 | |
54 | |
53 | sub try_open_db { |
55 | sub try_open_db { |
54 | File::Path::mkpath [$DB_HOME]; |
56 | File::Path::mkpath [$DB_HOME]; |
55 | |
57 | |
56 | $DB_ENV = db_env_create; |
58 | my $env = db_env_create; |
57 | |
59 | |
58 | $DB_ENV->set_errfile (\*STDERR); |
60 | $env->set_errfile (\*STDERR); |
59 | $DB_ENV->set_msgfile (\*STDERR); |
61 | $env->set_msgfile (\*STDERR); |
60 | $DB_ENV->set_verbose (-1, 1); |
62 | $env->set_verbose (-1, 1); |
61 | |
63 | |
62 | $DB_ENV->set_flags (BDB::AUTO_COMMIT | BDB::LOG_AUTOREMOVE | BDB::TXN_WRITE_NOSYNC); |
64 | $env->set_flags (BDB::AUTO_COMMIT | BDB::REGION_INIT); |
|
|
65 | $env->set_flags (&BDB::LOG_AUTOREMOVE ) if BDB::VERSION v0, v4.7; |
|
|
66 | $env->log_set_config (&BDB::LOG_AUTO_REMOVE) if BDB::VERSION v4.7; |
|
|
67 | |
|
|
68 | $env->set_timeout (3, BDB::SET_TXN_TIMEOUT); |
|
|
69 | $env->set_timeout (3, BDB::SET_LOCK_TIMEOUT); |
|
|
70 | |
63 | $DB_ENV->set_cachesize (0, 2048 * 1024, 0); |
71 | $env->set_cachesize (0, 2048 * 1024, 0); |
64 | |
72 | |
65 | db_env_open $DB_ENV, $DB_HOME, |
73 | db_env_open $env, $DB_HOME, |
66 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
74 | BDB::CREATE | BDB::REGISTER | BDB::RECOVER | BDB::INIT_MPOOL | BDB::INIT_LOCK | BDB::INIT_TXN, |
67 | 0666; |
75 | 0666; |
68 | |
76 | |
69 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
77 | $! and die "cannot open database environment $DB_HOME: " . BDB::strerror; |
|
|
78 | |
|
|
79 | $DB_ENV = $env; |
70 | |
80 | |
71 | 1 |
81 | 1 |
72 | } |
82 | } |
73 | |
83 | |
74 | sub table($) { |
84 | sub table($) { |
75 | $DB_TABLE{$_[0]} ||= do { |
85 | $DB_TABLE{$_[0]} ||= do { |
76 | my ($table) = @_; |
86 | my ($table) = @_; |
77 | |
87 | |
78 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
88 | $table =~ s/([^a-zA-Z0-9_\-])/sprintf "=%x=", ord $1/ge; |
|
|
89 | |
|
|
90 | $DB_ENV#d# |
|
|
91 | or return ::clienterror ("trying to create table $_[0] with empty db_env $DB_ENV" => 1);#d# |
79 | |
92 | |
80 | my $db = db_create $DB_ENV; |
93 | my $db = db_create $DB_ENV; |
81 | $db->set_flags (BDB::CHKSUM); |
94 | $db->set_flags (BDB::CHKSUM); |
82 | |
95 | |
83 | db_open $db, undef, $table, undef, BDB::BTREE, |
96 | db_open $db, undef, $table, undef, BDB::BTREE, |
… | |
… | |
148 | db_get $table, undef, $name => $id, 0; |
161 | db_get $table, undef, $name => $id, 0; |
149 | $! or return $cb->($id); |
162 | $! or return $cb->($id); |
150 | |
163 | |
151 | unless ($TILE_SEQ) { |
164 | unless ($TILE_SEQ) { |
152 | $TILE_SEQ = $table->sequence; |
165 | $TILE_SEQ = $table->sequence; |
153 | $TILE_SEQ->initial_value (64); |
166 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
154 | $TILE_SEQ->set_cachesize (0); |
167 | $TILE_SEQ->set_cachesize (0); |
155 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
168 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
156 | } |
169 | } |
157 | |
170 | |
158 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
171 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
… | |
… | |
434 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
447 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
435 | }; |
448 | }; |
436 | } |
449 | } |
437 | |
450 | |
438 | END { |
451 | END { |
|
|
452 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
453 | if $DB_ENV; |
|
|
454 | |
439 | undef $TILE_SEQ; |
455 | undef $TILE_SEQ; |
440 | %DB_TABLE = (); |
456 | %DB_TABLE = (); |
441 | undef $DB_ENV; |
457 | undef $DB_ENV; |
442 | } |
458 | } |
443 | |
459 | |