EEDB/Tools/MultiLoader.pm untested

filter: Statementstmt 0.0% 0 / 363 Branchbran 0.0% 0 / 204 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 643 CC 103 SCAR 49.6
CC
103
Cov
0%
store_feature
272.0
store_edge
272.0
build_feature_symbol_links
182.0
CRAP 143.3
1 =head1 NAME - EEDB::Tools::MultiLoader
2  
3 =head1 DESCRIPTION
4  
5 MultiLoader is a single class which wraps up database specific
6 optimizations for speeding up bulk loading. Since eeDB is primarily
7 a data-mining system, most of the data is loaded in bulk. This loader
8 augments the class specific ->store() functions.
9 Currently eeDB supports sqlite and mysql databases and there are
10 specific optimizations here for these two database engines.
11  
12 =head1 AUTHOR
13  
14 Jessica Severin <severin@gsc.riken.jp>
15  
16 =head1 LICENSE
17  
18 * Software License Agreement (BSD License)
19 * EdgeExpressDB [eeDB] system
20 * copyright (c) 2007-2009 Jessica Severin RIKEN OSC
21 * All rights reserved.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions are met:
24 * * Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * * Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 * * Neither the name of Jessica Severin RIKEN OSC nor the
30 * names of its contributors may be used to endorse or promote products
31 * derived from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
34 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 * DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS BE LIABLE FOR ANY
37 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43  
44 =head1 APPENDIX
45  
46 The rest of the documentation details each of the object methods. Internal methods are usually preceded with a _
47  
48 =cut
49  
50  
51 $VERSION = 0.953;
52  
53 package EEDB::Tools::MultiLoader;
54  
55 use strict;
56 use Time::HiRes qw(time gettimeofday tv_interval);
57  
58 use EEDB::Feature;
59 use EEDB::Edge;
60  
61 use MQdb::DBObject;
62 our @ISA = qw(MQdb::DBObject);
63  
64 #################################################
65 # Class methods
66 #################################################
67  
68 sub class { return "MultiLoader"; }
69  
70 #################################################
71 # Instance methods
72 #################################################
73  
74 sub init {
75 my $self = shift;
76 my %args = @_;
77 $self->SUPER::init(@_);
78
79 $self->{'feature_buffer'} = [];
80 $self->{'feature_mdata_buffer'} = [];
81 $self->{'edge_buffer'} = [];
82 $self->{'express_buffer'} = [];
83 $self->{'metadata_buffer'} = [];
84 $self->{'symbol_buffer'} = [];
85 $self->{'do_store'} = 1;
86  
87 return $self;
88 }
89  
90  
91 ##################
92  
93 sub do_store {
94 my $self = shift;
95 return $self->{'do_store'} = shift if(@_);
96 $self->{'do_store'}=1 unless(defined($self->{'do_store'}));
97 return $self->{'do_store'};
98 }
99  
100 sub mirror { #forces load and ignores checks if the data has some previous DB id
101 my $self = shift;
102 return $self->{'mirror'} = shift if(@_);
103 $self->{'mirror'}=0 unless(defined($self->{'mirror'}));
104 return $self->{'mirror'};
105 }
106  
107 ###############################################
108  
109 =head2 flush_buffers
110  
111 Description : MultiLoader uses internal FIFO buffers for speed.
112 After all loading these buffers must be flushed to ensure they are empty
113 and all objects have been written
114 Returntype : none
115 Exceptions : none
116 Example : my $multiLoad = new EEDB::Tools::MultiLoader;
117 $multiLoad->database($eeDB);
118 $multiLoad->do_store(1);
119 foreach my $mydata (@data_array) {
120 my $feature = new EEDB::Feature;
121 # do something with $mydata and $feature to create the feature
122 $multiLoad->store_feature($feature);
123 }
124 #after all store_xxxx methods, must flush any remaining objects from buffers
125 #usually the last thing we do in a script
126 $multiLoad->flush_buffers();
127 exit(1);
128
129 =cut
130  
131 sub flush_buffers {
132 my $self = shift;
133
134 $self->store_metadata();
135 $self->store_symbol();
136 $self->store_feature();
137 $self->store_edge();
138 $self->store_express(); #must be last since it relies on all features having ids
139  
140 if($self->database->driver eq "sqlite") { $self->_sqlite_transaction("FLUSH"); }
141 return $self;
142 }
143  
144  
145 #############
146 #
147 # Features
148 #
149 #############
150  
151 =head2 store_feature
152  
153 Description : MultiLoader uses internal FIFO buffers for speed.
154 This method queues up a Feature into MultiLoader. When the buffer is full
155 it will do a bulk load, otherwise it will return directly. If called
156 with no $feature parameter it will force a buffer flush and write.
157 Parameter[1] : feature [EEDB::Feature] or undef to flush
158 Returntype : none
159 Exceptions : none
160 Example : my $multiLoad = new EEDB::Tools::MultiLoader;
161 $multiLoad->database($eeDB);
162 $multiLoad->do_store(1);
163 foreach my $feature (@feature_array) {
164 $multiLoad->store_feature($feature);
165 }
166 $multiLoad->store_feature(); #no parameter so empties buffers
167 exit(1);
168
169 =cut
170  
171 sub store_feature {
172 my $self = shift;
173 my $feature = shift;
174
175 if(defined($feature)) {
176
177 #first queue up the metadata (and make sure it is loaded if doing as copy)
178 my $mdata_list = $feature->metadataset->metadata_list;
179
180 #now we can reset the primary_id and disconnect from previous database if it was
181 $feature->primary_id(undef);
182
183 foreach my $mdata (@$mdata_list) {
184 if($self->mirror or !defined($mdata->primary_id)) {
185 $mdata->primary_id(undef); #to make sure it really does store and link properly
186 if($mdata->class eq 'Metadata') { $self->store_metadata($mdata); }
187 if($mdata->class eq 'Symbol') { $self->store_symbol($mdata); }
188 }
189 }
190
191 #last queue up the feature
192 push @{$self->{'feature_buffer'}}, $feature;
193 return if(scalar(@{$self->{'feature_buffer'}}) < 200);
194 }
195 return unless(scalar(@{$self->{'feature_buffer'}}) >0);
196 #printf("store feature buffer [%d]\n", scalar(@{$self->{'feature_buffer'}}));
197  
198 #
199 # 1st flush the metadata since I need to have the data in the database before I can link
200 #
201 $self->store_metadata();
202 $self->store_symbol();
203  
204 #
205 # do feature bulk store here
206 #
207 if($self->database->driver eq "sqlite") {
208 $self->_sqlite_transaction("BEGIN");
209 } else {
210 $self->database->do_sql("LOCK TABLE feature WRITE, feature AS f1 READ");
211 }
212 my $fid = $self->database->fetch_col_value("select max(feature_id) from feature AS f1;");
213 if($fid) { $fid += 1; } else { $fid = 1; }
214 my $sql = "INSERT ignore INTO feature (feature_id, feature_source_id, chrom_id, chrom_start, ".
215 "chrom_end, strand, significance, primary_name) VALUES ";
216 my $first=1;
217 foreach my $feat (@{$self->{'feature_buffer'}}) {
218 $feat->primary_id($fid++);
219 my $chrom_id = $feat->chrom_id;
220 if($chrom_id eq '') { $chrom_id = "NULL"; }
221 else { $chrom_id = "\"" . $chrom_id . "\"" };
222  
223 my $values = sprintf("(%s,%s,%s,%d,%d,\"%s\",%s,\"%s\")",
224 $feat->id,
225 $feat->feature_source->id,
226 $chrom_id,
227 $feat->chrom_start,
228 $feat->chrom_end,
229 $feat->strand,
230 $feat->significance,
231 $feat->primary_name);
232 if($self->database->driver eq "sqlite") {
233 $self->database->execute_sql($sql . $values) if($self->do_store);
234 } else {
235 if($first) { $first=0; } else { $sql .= ","; }
236 $sql .= $values;
237 }
238 }
239 #printf("%s\n", $sql);
240 if($self->database->driver eq "sqlite") {
241 $self->_sqlite_transaction("COMMIT");
242 } else {
243 $self->database->execute_sql($sql) if($self->do_store);
244 $self->database->do_sql("UNLOCK TABLES");
245 }
246
247 #
248 # now do the expression
249 #
250 foreach my $feat (@{$self->{'feature_buffer'}}) {
251 my $expr_array = $feat->get_expression_array;
252 foreach my $express (@$expr_array) {
253 if($self->mirror or !defined($express->primary_id)) {
254 $express->primary_id(undef);
255 $self->store_express($express);
256 }
257 }
258 }
259 $self->store_express();
260 #printf("flush the expression buffer\n");
261  
262 #
263 # do the symbol/metadata linkage now
264 #
265 $self->build_feature_symbol_links();
266 $self->build_feature_metadata_links();
267
268 #
269 # then do the chunk edges
270 #
271 $self->build_chunk_links();
272  
273  
274 #done now do cleanup and clear out the buffer
275 foreach $feature (@{$self->{'feature_buffer'}}) {
276 $feature->empty_expression_cache;
277 }
278 $self->{'feature_buffer'} = [];
279 }
280  
281  
282 =head2 update_feature_metadata
283  
284 Description : this method will take a feature which was fetched from a database
285 and which may have new metadata/symbols and it will store that new
286 metadata/symbols and link them to the feature.
287 Parameter[1] : feature [EEDB::Feature]
288 Returntype : none
289 Exceptions : none
290 Example : my $multiLoad = new EEDB::Tools::MultiLoader;
291 $multiLoad->database($eeDB);
292 $multiLoad->do_store(1);
293 foreach my $feature (@feature_array) {
294 #
295 # do some metadata additions here
296 #
297 $multiLoad->update_feature_metadata($feature);
298 }
299 $multiLoad->update_feature_metadata(); #to flush the queues
300
301 =cut
302  
303 sub update_feature_metadata {
304 my $self = shift;
305 my $feature = shift;
306
307 if(defined($feature)) {
308 if($feature->database->url ne $self->database->url) { return; }
309
310 #first queue up the metadata
311 my $mdata_list = $feature->metadataset->metadata_list;
312 my $mdata_count = 0;
313 foreach my $mdata (@$mdata_list) {
314 if(!defined($mdata->primary_id)) {
315 $mdata_count++;
316 if($mdata->class eq 'Metadata') { $self->store_metadata($mdata); }
317 if($mdata->class eq 'Symbol') { $self->store_symbol($mdata); }
318 }
319 }
320 if($mdata_count == 0) { return; }
321
322 #OK this feature has new mdata, so queue it up for linking
323 push @{$self->{'feature_mdata_buffer'}}, $feature;
324 if(scalar(@{$self->{'feature_mdata_buffer'}}) < 500) { return; }
325 }
326 if(scalar(@{$self->{'feature_mdata_buffer'}}) == 0) { return; }
327  
328 # 1st flush the metadata since I need to have the data in the database before I can link
329 $self->store_metadata();
330 $self->store_symbol();
331  
332 # do the symbol/metadata linkage now
333 $self->build_feature_symbol_links();
334 $self->build_feature_metadata_links();
335
336 #done so clear out the buffer
337 $self->{'feature_mdata_buffer'} = [];
338 }
339  
340  
341 sub build_chunk_links {
342 my $self = shift;
343  
344 if($self->database->driver eq "sqlite") { return; }
345  
346 my $sql = "INSERT ignore INTO feature_2_chunk (feature_id, chrom_chunk_id) VALUES ";
347 my $first=1;
348 foreach my $feature (@{$self->{'feature_buffer'}}) {
349 next unless($feature->chrom and ($feature->chrom_start>0) and ($feature->chrom_end>0));
350 my $chunks = EEDB::ChromChunk->fetch_all_by_chrom_range($feature->chrom, $feature->chrom_start, $feature->chrom_end);
351 foreach my $chunk (@{$chunks}) {
352 if($first) { $first=0; } else { $sql .= ","; }
353 $sql .= sprintf("(%s,%s)", $feature->id, $chunk->id);
354 }
355 }
356 if($first) { return; } #no chunks to link
357
358 #printf("%s\n", $sql);
359 $self->database->execute_sql($sql) if($self->do_store);
360 #printf("done\n");
361 }
362  
363  
364 sub build_source_chunk_links {
365 my $self = shift;
366 my $source = shift; #FeatureSource
367  
368 my $chroms = EEDB::Chrom->fetch_all_by_feature_source($source);
369 foreach my $chrom (@$chroms) {
370 $chrom->display_info;
371 my $chunks = EEDB::ChromChunk->fetch_all_by_chrom($chrom);
372 foreach my $chunk (@$chunks) {
373 printf(" "); $chunk->display_info;
374 #
375 # do OverlapCompare against the FeatureSource
376 #
377 }
378 }
379 }
380  
381 #######################################
382 #
383 # Edges
384 #
385 #######################################
386  
387 sub store_edge {
388 my $self = shift;
389 my $edge = shift;
390
391 if(defined($edge)) {
392 #first queue up the metadata
393 my $mdata_list = $edge->metadataset->metadata_list;
394 foreach my $mdata (@$mdata_list) {
395 if($self->mirror or !defined($mdata->primary_id)) {
396 if($mdata->class eq 'Metadata') { $self->store_metadata($mdata); }
397 if($mdata->class eq 'Symbol') { $self->store_symbol($mdata); }
398 }
399 }
400
401 #then queue up the edge
402 push @{$self->{'edge_buffer'}}, $edge;
403 return if(scalar(@{$self->{'edge_buffer'}}) < 1000);
404 }
405 return unless(scalar(@{$self->{'edge_buffer'}}) >0);
406 #printf("store edge buffer\n");
407  
408 #
409 # 1st flush the metadata and features since I need to have the data and ids in the database before I can link
410 #
411 $self->store_metadata();
412 $self->store_symbol();
413 $self->store_feature();
414  
415 #
416 # do Edge bulk store here
417 #
418 if($self->database->driver eq "sqlite") { $self->_sqlite_transaction("BEGIN"); }
419 else { $self->database->do_sql("LOCK TABLE edge WRITE, edge AS e1 READ;") if($self->do_store); }
420 my $eid = $self->database->fetch_col_value("select max(edge_id) from edge AS e1;");
421 if($eid) { $eid += 1; } else { $eid = 1; }
422 my $sql = "INSERT ignore INTO edge ".
423 "(edge_id, edge_source_id, feature1_id, feature2_id, sub_type, direction, weight) VALUES ";
424 my $first=1;
425 foreach $edge (@{$self->{'edge_buffer'}}) {
426 $edge->primary_id($eid++);
427 my $values = sprintf("(%s,%s,%s,%s,'%s','%s',%f)",
428 $edge->id,
429 $edge->edge_source->id,
430 $edge->feature1->id,
431 $edge->feature2->id,
432 $edge->sub_type,
433 $edge->direction,
434 $edge->weight);
435 if($self->database->driver eq "sqlite") {
436 $self->database->execute_sql($sql . $values) if($self->do_store);
437 } else {
438 if($first) { $first=0; } else { $sql .= ","; }
439 $sql .= $values;
440 }
441 }
442 #printf("%s\n", $sql);
443 if($self->database->driver eq "sqlite") {
444 $self->_sqlite_transaction("COMMIT");
445 } else {
446 $self->database->execute_sql($sql) if($self->do_store);
447 $self->database->do_sql("UNLOCK TABLES") if($self->do_store);
448 }
449
450 #
451 # now I need to link the edge to metadata
452 #
453 # TODO
454 #
455
456 #done now so clear out the buffer
457 $self->{'edge_buffer'} = [];
458 }
459  
460 #######################################
461 #
462 # Expression
463 #
464 #######################################
465  
466 sub store_express {
467 #this should be called after features have been stored
468 #so that feature_id is available
469 my $self = shift;
470 my $express = shift;
471
472 if(defined($express)) {
473 push @{$self->{'express_buffer'}}, $express;
474 return if(scalar(@{$self->{'express_buffer'}}) < 500);
475 }
476 return unless(scalar(@{$self->{'express_buffer'}}) >0);
477 #printf("store express buffer [%d]\n", scalar(@{$self->{'express_buffer'}}));
478
479 #
480 # do expression bulk store here
481 #
482 my $sql = "INSERT ignore INTO expression ".
483 "(experiment_id, feature_id, datatype_id, value, sig_error) VALUES ";
484 my $first=1;
485 foreach $express (@{$self->{'express_buffer'}}) {
486 if($first) { $first=0; } else { $sql .= ","; }
487 my $datatype_id = EEDB::Expression->_storeget_expression_datatype_id($self->database, $express->type);
488 $sql .= sprintf("(%s,%d,%d,%s,%s)",
489 $express->experiment->id,
490 $express->feature->id,
491 $datatype_id,
492 $express->value,
493 $express->sig_error);
494 }
495 #printf("%s\n", $sql);
496 $self->database->execute_sql($sql) if($self->do_store);
497 #printf("done\n");
498
499 #done now so clear out the buffer
500 $self->{'express_buffer'} = [];
501 }
502  
503 #######################################
504 #
505 # Metadata and Symbols
506 #
507 #######################################
508  
509 sub store_metadata {
510 my $self = shift;
511 my $mdata = shift;
512
513 if(defined($mdata)) {
514 #return unless($mdata->class eq 'Metadata');
515 push @{$self->{'metadata_buffer'}}, $mdata;
516 return if(scalar(@{$self->{'metadata_buffer'}}) < 500);
517 }
518 return unless(scalar(@{$self->{'metadata_buffer'}}) >0);
519 #printf("store metadata buffer [%d]\n", scalar(@{$self->{'metadata_buffer'}}));
520  
521 #
522 # do bulk store here
523 #
524 if($self->database->driver eq "sqlite") { $self->_sqlite_transaction("BEGIN"); }
525 my $sql = "INSERT ignore INTO metadata (data_type, data) VALUES ";
526 my $first=1;
527 foreach $mdata (@{$self->{'metadata_buffer'}}) {
528 if($self->database->driver eq "sqlite") {
529 $mdata->store($self->database) if($self->do_store);
530 } else {
531 my $data = $mdata->data;
532 $data =~ s/\\/\\\\/g;
533 $data =~ s/\"/\\\"/g;
534 $data =~ s/\'/\\\'/g; #'
535 if($first) { $first=0; } else { $sql .= ","; }
536 $sql .= sprintf("(\"%s\",\"%s\")", $mdata->type, $data);
537 }
538 }
539 #printf("%s\n", $sql);
540 if($self->database->driver eq "sqlite") {
541 $self->_sqlite_transaction("COMMIT");
542 } else {
543 $self->database->execute_sql($sql) if($self->do_store);
544 }
545 #printf("done\n");
546  
547 #done now so clear out the buffer
548 $self->{'metadata_buffer'} = [];
549 }
550  
551  
552 sub store_symbol {
553 my $self = shift;
554 my $symbol = shift;
555
556 if(defined($symbol)) {
557 #if($symbol->check_exists_db($self->database)) { return; }
558 #return unless($symbol->class eq 'Symbol');
559 push @{$self->{'symbol_buffer'}}, $symbol;
560 return if(scalar(@{$self->{'symbol_buffer'}}) < 500);
561 }
562 return unless(scalar(@{$self->{'symbol_buffer'}}) >0);
563 #printf("store symbol buffer [%d]\n", scalar(@{$self->{'symbol_buffer'}}));
564  
565 #
566 # do bulk store here
567 #
568 if($self->database->driver eq "sqlite") { $self->_sqlite_transaction("BEGIN"); }
569 my $sql = "INSERT ignore INTO symbol (sym_type, sym_value) VALUES ";
570 my $first=1;
571 foreach $symbol (@{$self->{'symbol_buffer'}}) {
572 if($self->database->driver eq "sqlite") {
573 $symbol->store($self->database) if($self->do_store);
574 } else {
575 my $data = $symbol->data;
576 $data =~ s/\\/\\\\/g;
577 $data =~ s/\"/\\\"/g;
578 $data =~ s/\'/\\\'/g; #'
579 if($first) { $first=0; } else { $sql .= ","; }
580 $sql .= sprintf("(\"%s\",\"%s\")", $symbol->type, $data);
581 }
582 }
583 #printf("%s\n", $sql);
584 if($self->database->driver eq "sqlite") {
585 $self->_sqlite_transaction("COMMIT");
586 } else {
587 $self->database->execute_sql($sql) if($self->do_store);
588 }
589 #printf("done\n");
590
591  
592 #done now so clear out the buffer
593 $self->{'symbol_buffer'} = [];
594 }
595  
596  
597 sub build_feature_symbol_links {
598 my $self = shift;
599  
600 if($self->database->driver eq "sqlite") { $self->_sqlite_transaction("BEGIN"); }
601 my $sql = "INSERT ignore INTO feature_2_symbol (feature_id, symbol_id) VALUES ";
602 my $insert_count=0;
603 my $first =1;
604 foreach my $feature (@{$self->{'feature_buffer'}}, @{$self->{'feature_mdata_buffer'}}) {
605 my $mdata_list = $feature->metadataset->metadata_list;
606 foreach my $mdata (@$mdata_list) {
607 next unless($mdata->class eq 'Symbol');
608 if($self->do_store) {
609 ##triple check code here, by this time all $mdata should be store and with ID
610 if(!defined($mdata->primary_id) and !$mdata->check_exists_db($self->database)) {
611 #something happened with the bulk database store, so try again with a direct store
612 #this should not happen, something unexpected happened
613 $mdata->store($self->database) if($self->do_store);
614 printf(STDERR "WARN: symbol direct store needed before linking : %s\n", $mdata->display_desc);
615 }
616 if(!defined($mdata->primary_id)) {
617 printf(STDERR "ERROR: mdata did not store : %s\n", $mdata->display_desc);
618 next;
619 }
620 }
621
622 $insert_count++;
623 my $values = sprintf("(%s,%s)", $feature->id, $mdata->id);
624 if($self->database->driver eq "sqlite") {
625 $self->database->execute_sql($sql . $values) if($self->do_store);
626 } else {
627 if($first) { $first=0; } else { $sql .= ","; }
628 $sql .= $values;
629 }
630 }
631 }
632 return if($insert_count == 0);
633 if($self->database->driver eq "sqlite") {
634 $self->_sqlite_transaction("COMMIT");
635 } else {
636 $self->database->execute_sql($sql) if($self->do_store);
637 }
638 }
639  
640  
641 sub build_feature_metadata_links {
642 my $self = shift;
643  
644 if($self->database->driver eq "sqlite") { $self->_sqlite_transaction("BEGIN"); }
645 my $sql = "INSERT ignore INTO feature_2_metadata (feature_id, metadata_id) VALUES ";
646 my $insert_count=0;
647 my $first =1;
648 foreach my $feature (@{$self->{'feature_buffer'}}, @{$self->{'feature_mdata_buffer'}}) {
649 my $mdata_list = $feature->metadataset->metadata_list;
650 foreach my $mdata (@$mdata_list) {
651 next unless($mdata->class eq 'Metadata');
652 if($self->do_store) {
653 ##triple check code here, by this time all $mdata should be store and with ID
654 if(!defined($mdata->primary_id) and !$mdata->check_exists_db($self->database)) {
655 #something happened with the bulk database store, so try again with a direct store
656 $mdata->store($self->database) if($self->do_store);
657 printf(STDERR "WARN: mdata direct store needed before linking : %s\n", $mdata->display_desc);
658 }
659 if(!defined($mdata->primary_id) and ($self->do_store)) {
660 printf(STDERR "ERROR: mdata did not store : %s\n", $mdata->display_desc);
661 next;
662 }
663 }
664  
665 $insert_count++;
666 my $values = sprintf("(%s,%s)", $feature->id, $mdata->id);
667 if($self->database->driver eq "sqlite") {
668 $self->database->execute_sql($sql . $values) if($self->do_store);
669 } else {
670 if($first) { $first=0; } else { $sql .= ","; }
671 $sql .= $values;
672 }
673 }
674 }
675 return if($insert_count == 0);
676 #print($sql,"\n");
677 if($self->database->driver eq "sqlite") {
678 $self->_sqlite_transaction("COMMIT");
679 } else {
680 $self->database->execute_sql($sql) if($self->do_store);
681 }
682 }
683  
684  
685 sub _sqlite_transaction {
686 my $self = shift;
687 my $state = shift;
688
689 if($state eq "BEGIN") {
690 $self->{"_sqlite_transaction_count"}++;
691 if($self->{"_sqlite_transaction_count"} == 1) {
692 $self->database->execute_sql("BEGIN TRANSACTION");
693 }
694 }
695 if($state eq "COMMIT") {
696 if($self->{"_sqlite_transaction_count"} == 1) {
697 $self->database->execute_sql("COMMIT");
698 $self->{"_sqlite_transaction_count"} = 0;
699 }
700 if($self->{"_sqlite_transaction_count"} > 1) {
701 $self->{"_sqlite_transaction_count"}--;
702 }
703 }
704 if($state eq "FLUSH") {
705 if($self->{"_sqlite_transaction_count"} >= 1) {
706 $self->database->execute_sql("COMMIT");
707 $self->{"_sqlite_transaction_count"} = 0;
708 }
709 }
710 }
711  
712 1;
713