… | |
… | |
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 | sub FIRST_TILE_ID () { 64 } |
|
|
33 | |
|
|
34 | unless (-d $DB_HOME) { |
31 | if (!-e $DB_HOME and -e "$Deliantra::VARDIR/$ODBDIR") { |
35 | if (-d "$Deliantra::VARDIR/$ODBDIR") { |
32 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
36 | rename "$Deliantra::VARDIR/$ODBDIR", $DB_HOME; |
33 | 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"; |
34 | } |
|
|
35 | |
|
|
36 | if (!-e $DB_HOME and -e "$Deliantra::OLDDIR/$ODBDIR") { |
38 | } elsif (-d "$Deliantra::OLDDIR/$ODBDIR") { |
37 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
39 | rename "$Deliantra::OLDDIR/$DBDIR", $DB_HOME; |
38 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
40 | print STDERR "INFO: moved old database from $Deliantra::OLDDIR/$ODBDIR to $DB_HOME\n"; |
|
|
41 | } else { |
|
|
42 | File::Path::mkpath [$DB_HOME] |
|
|
43 | or die "unable to create database directory $DB_HOME: $!"; |
|
|
44 | } |
39 | } |
45 | } |
40 | |
46 | |
41 | BDB::max_poll_time 0.03; |
47 | BDB::max_poll_time 0.03; |
42 | BDB::max_parallel 1; |
48 | BDB::max_parallel 1; |
43 | |
49 | |
44 | our $DB_ENV; |
50 | our $DB_ENV; |
45 | our $DB_STATE; |
51 | our $DB_STATE; |
46 | our %DB_TABLE; |
52 | our %DB_TABLE; |
|
|
53 | our $TILE_SEQ; |
47 | |
54 | |
48 | sub try_open_db { |
55 | sub try_open_db { |
49 | mkdir $DB_HOME, 0777; |
56 | File::Path::mkpath [$DB_HOME]; |
50 | |
57 | |
51 | $DB_ENV = db_env_create; |
58 | my $env = db_env_create; |
52 | |
59 | |
53 | $DB_ENV->set_errfile (\*STDERR); |
60 | $env->set_errfile (\*STDERR); |
54 | $DB_ENV->set_msgfile (\*STDERR); |
61 | $env->set_msgfile (\*STDERR); |
55 | $DB_ENV->set_verbose (-1, 1); |
62 | $env->set_verbose (-1, 1); |
56 | |
63 | |
57 | $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 | |
58 | $DB_ENV->set_cachesize (0, 2048 * 1024, 0); |
71 | $env->set_cachesize (0, 2048 * 1024, 0); |
59 | |
72 | |
60 | db_env_open $DB_ENV, $DB_HOME, |
73 | db_env_open $env, $DB_HOME, |
61 | 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, |
62 | 0666; |
75 | 0666; |
63 | |
76 | |
64 | $! 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; |
65 | |
80 | |
66 | 1 |
81 | 1 |
67 | } |
82 | } |
68 | |
83 | |
69 | sub table($) { |
84 | sub table($) { |
70 | $DB_TABLE{$_[0]} ||= do { |
85 | $DB_TABLE{$_[0]} ||= do { |
71 | my ($table) = @_; |
86 | my ($table) = @_; |
72 | |
87 | |
73 | $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# |
74 | |
92 | |
75 | my $db = db_create $DB_ENV; |
93 | my $db = db_create $DB_ENV; |
76 | $db->set_flags (BDB::CHKSUM); |
94 | $db->set_flags (BDB::CHKSUM); |
77 | |
95 | |
78 | db_open $db, undef, $table, undef, BDB::BTREE, |
96 | db_open $db, undef, $table, undef, BDB::BTREE, |
… | |
… | |
108 | }; |
126 | }; |
109 | } |
127 | } |
110 | |
128 | |
111 | sub put($$$$) { |
129 | sub put($$$$) { |
112 | my ($db, $key, $data, $cb) = @_; |
130 | my ($db, $key, $data, $cb) = @_; |
113 | warn "put $key ",(length $data),"\n";#d# |
|
|
114 | |
131 | |
115 | db_put table $db, undef, $key, $data, 0, sub { |
132 | db_put table $db, undef, $key, $data, 0, sub { |
116 | $cb->($!); |
133 | $cb->($!); |
117 | $SYNC->again unless $SYNC->is_active; |
134 | $SYNC->again unless $SYNC->is_active; |
118 | }; |
135 | }; |
… | |
… | |
139 | my ($name, $cb) = @_; |
156 | my ($name, $cb) = @_; |
140 | |
157 | |
141 | my $table = table "facemap"; |
158 | my $table = table "facemap"; |
142 | my $id; |
159 | my $id; |
143 | |
160 | |
144 | db_get $table, undef, $name, $id, 0; |
161 | db_get $table, undef, $name => $id, 0; |
145 | $! or return $cb->($id); |
162 | $! or return $cb->($id); |
146 | |
163 | |
147 | for (1..100) { |
164 | unless ($TILE_SEQ) { |
148 | my $txn = $DB_ENV->txn_begin; |
165 | $TILE_SEQ = $table->sequence; |
149 | db_get $table, $txn, id => $id, 0; |
166 | $TILE_SEQ->initial_value (FIRST_TILE_ID); |
|
|
167 | $TILE_SEQ->set_cachesize (0); |
|
|
168 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
|
|
169 | } |
150 | |
170 | |
151 | $id = 64 if $id < 64; |
171 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
152 | |
172 | |
153 | ++$id; |
173 | die "unable to allocate tile id: $!" |
154 | |
174 | if $!; |
155 | db_put $table, $txn, id => $id, 0; |
175 | |
156 | db_txn_finish $txn; |
|
|
157 | |
|
|
158 | unless ($!) { |
|
|
159 | db_put $table, undef, $name => $id; |
176 | db_put $table, undef, $name => $id, 0; |
160 | $SYNC->again unless $SYNC->is_active; |
177 | $cb->($id); |
161 | return $cb->($id); |
|
|
162 | } |
|
|
163 | |
178 | |
164 | select undef, undef, undef, 0.01 * rand; |
|
|
165 | } |
|
|
166 | |
|
|
167 | die "maximum number of transaction retries reached - database problems?"; |
|
|
168 | } |
179 | } |
169 | |
180 | |
170 | sub get_tile_id_sync($) { |
181 | sub get_tile_id_sync($) { |
171 | my ($name) = @_; |
182 | my ($name) = @_; |
172 | |
183 | |
… | |
… | |
403 | close $FH; |
414 | close $FH; |
404 | } |
415 | } |
405 | |
416 | |
406 | package DC::DB; |
417 | package DC::DB; |
407 | |
418 | |
|
|
419 | sub nuke_db { |
|
|
420 | File::Path::mkpath [$DB_HOME]; |
|
|
421 | eval { File::Path::rmtree $DB_HOME }; |
|
|
422 | } |
|
|
423 | |
408 | sub open_db { |
424 | sub open_db { |
409 | unless (eval { try_open_db }) { |
425 | unless (eval { try_open_db }) { |
410 | warn "$@";#d# |
426 | warn "$@";#d# |
411 | eval { File::Path::rmtree $DB_HOME }; |
427 | eval { nuke_db }; |
412 | try_open_db; |
428 | try_open_db; |
413 | } |
429 | } |
414 | |
430 | |
415 | # fetch the full face table first |
431 | # fetch the full face table first |
416 | unless ($facemap) { |
432 | unless ($facemap) { |
… | |
… | |
425 | }; |
441 | }; |
426 | } |
442 | } |
427 | |
443 | |
428 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
444 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
429 | $SYNC = EV::timer_ns 0, 60, sub { |
445 | $SYNC = EV::timer_ns 0, 60, sub { |
430 | warn "SYNC\n";#d# |
|
|
431 | $_[0]->stop; |
446 | $_[0]->stop; |
432 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
447 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
433 | }; |
448 | }; |
434 | } |
449 | } |
435 | |
450 | |
436 | END { |
451 | END { |
|
|
452 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0 |
|
|
453 | if $DB_ENV; |
|
|
454 | |
|
|
455 | undef $TILE_SEQ; |
437 | %DB_TABLE = (); |
456 | %DB_TABLE = (); |
438 | undef $DB_ENV; |
457 | undef $DB_ENV; |
439 | } |
458 | } |
440 | |
459 | |
441 | 1; |
460 | 1; |