… | |
… | |
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 | |
… | |
… | |
26 | |
27 | |
27 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $ODBDIR = "cfplus-" . BDB::VERSION . "-$Config{archname}"; |
28 | our $DBDIR = "client-" . BDB::VERSION . "-$Config{archname}"; |
29 | our $DBDIR = "client-" . BDB::VERSION . "-$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 | $DB_ENV = db_env_create; |
52 | |
57 | |
53 | $DB_ENV->set_errfile (\*STDERR); |
58 | $DB_ENV->set_errfile (\*STDERR); |
54 | $DB_ENV->set_msgfile (\*STDERR); |
59 | $DB_ENV->set_msgfile (\*STDERR); |
… | |
… | |
108 | }; |
113 | }; |
109 | } |
114 | } |
110 | |
115 | |
111 | sub put($$$$) { |
116 | sub put($$$$) { |
112 | my ($db, $key, $data, $cb) = @_; |
117 | my ($db, $key, $data, $cb) = @_; |
113 | warn "put $key ",(length $data),"\n";#d# |
|
|
114 | |
118 | |
115 | db_put table $db, undef, $key, $data, 0, sub { |
119 | db_put table $db, undef, $key, $data, 0, sub { |
116 | $cb->($!); |
120 | $cb->($!); |
117 | $SYNC->again unless $SYNC->is_active; |
121 | $SYNC->again unless $SYNC->is_active; |
118 | }; |
122 | }; |
… | |
… | |
139 | my ($name, $cb) = @_; |
143 | my ($name, $cb) = @_; |
140 | |
144 | |
141 | my $table = table "facemap"; |
145 | my $table = table "facemap"; |
142 | my $id; |
146 | my $id; |
143 | |
147 | |
144 | db_get $table, undef, $name, $id, 0; |
148 | db_get $table, undef, $name => $id, 0; |
145 | $! or return $cb->($id); |
149 | $! or return $cb->($id); |
146 | |
150 | |
147 | for (1..100) { |
151 | unless ($TILE_SEQ) { |
148 | my $txn = $DB_ENV->txn_begin; |
152 | $TILE_SEQ = $table->sequence; |
149 | db_get $table, $txn, id => $id, 0; |
153 | $TILE_SEQ->initial_value (64); |
|
|
154 | $TILE_SEQ->set_cachesize (0); |
|
|
155 | db_sequence_open $TILE_SEQ, undef, "id", BDB::CREATE; |
|
|
156 | } |
150 | |
157 | |
151 | $id = 64 if $id < 64; |
158 | db_sequence_get $TILE_SEQ, undef, 1, my $id; |
152 | |
159 | |
153 | ++$id; |
160 | die "unable to allocate tile id: $!" |
154 | |
161 | if $!; |
155 | db_put $table, $txn, id => $id, 0; |
162 | |
156 | db_txn_finish $txn; |
|
|
157 | |
|
|
158 | unless ($!) { |
|
|
159 | db_put $table, undef, $name => $id; |
163 | db_put $table, undef, $name => $id, 0; |
160 | $SYNC->again unless $SYNC->is_active; |
164 | $cb->($id); |
161 | return $cb->($id); |
|
|
162 | } |
|
|
163 | |
165 | |
164 | select undef, undef, undef, 0.01 * rand; |
|
|
165 | } |
|
|
166 | |
|
|
167 | die "maximum number of transaction retries reached - database problems?"; |
|
|
168 | } |
166 | } |
169 | |
167 | |
170 | sub get_tile_id_sync($) { |
168 | sub get_tile_id_sync($) { |
171 | my ($name) = @_; |
169 | my ($name) = @_; |
172 | |
170 | |
… | |
… | |
403 | close $FH; |
401 | close $FH; |
404 | } |
402 | } |
405 | |
403 | |
406 | package DC::DB; |
404 | package DC::DB; |
407 | |
405 | |
|
|
406 | sub nuke_db { |
|
|
407 | File::Path::mkpath [$DB_HOME]; |
|
|
408 | eval { File::Path::rmtree $DB_HOME }; |
|
|
409 | } |
|
|
410 | |
408 | sub open_db { |
411 | sub open_db { |
409 | unless (eval { try_open_db }) { |
412 | unless (eval { try_open_db }) { |
410 | warn "$@";#d# |
413 | warn "$@";#d# |
411 | eval { File::Path::rmtree $DB_HOME }; |
414 | eval { nuke_db }; |
412 | try_open_db; |
415 | try_open_db; |
413 | } |
416 | } |
414 | |
417 | |
415 | # fetch the full face table first |
418 | # fetch the full face table first |
416 | unless ($facemap) { |
419 | unless ($facemap) { |
… | |
… | |
425 | }; |
428 | }; |
426 | } |
429 | } |
427 | |
430 | |
428 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
431 | $WATCHER = EV::io BDB::poll_fileno, EV::READ, \&BDB::poll_cb; |
429 | $SYNC = EV::timer_ns 0, 60, sub { |
432 | $SYNC = EV::timer_ns 0, 60, sub { |
430 | warn "SYNC\n";#d# |
|
|
431 | $_[0]->stop; |
433 | $_[0]->stop; |
432 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
434 | db_env_txn_checkpoint $DB_ENV, 0, 0, 0, sub { }; |
433 | }; |
435 | }; |
434 | } |
436 | } |
435 | |
437 | |
436 | END { |
438 | END { |
|
|
439 | undef $TILE_SEQ; |
437 | %DB_TABLE = (); |
440 | %DB_TABLE = (); |
438 | undef $DB_ENV; |
441 | undef $DB_ENV; |
439 | } |
442 | } |
440 | |
443 | |
441 | 1; |
444 | 1; |