comparison lshlib.cpp @ 474:f9d86b1db21c

Fixed memory leaks, added WS --no_unit_norming, and removed capping of LSH_N_POINT_BITS to 15 bits, instead allow any number of bits to encode points, remaining bits encode tracks
author mas01mc
date Fri, 09 Jan 2009 18:05:32 +0000
parents 2d5c3f8e8c22
children a30948382f56
comparison
equal deleted inserted replaced
471:add65705e655 474:f9d86b1db21c
250 #endif 250 #endif
251 delete H::h[ j ][ i ]; 251 delete H::h[ j ][ i ];
252 } 252 }
253 delete[] H::h[ j ]; 253 delete[] H::h[ j ];
254 } 254 }
255 delete[] H::r1; 255 delete[] H::r1;
256 delete[] H::r2; 256 delete[] H::r2;
257 delete[] H::h; 257 delete[] H::h;
258 } 258 }
259 259
260 260
261 // Compute all hash functions for vector v 261 // Compute all hash functions for vector v
262 // #ifdef USE_U_FUNCTIONS use Combination of m \times h_i \in R^{(k/2) \times d} 262 // #ifdef USE_U_FUNCTIONS use Combination of m \times h_i \in R^{(k/2) \times d}
470 indexName(0), 470 indexName(0),
471 lshHeader(0), 471 lshHeader(0),
472 calling_instance(0), 472 calling_instance(0),
473 add_point_callback(0) 473 add_point_callback(0)
474 { 474 {
475 FILE* dbFile = 0;
475 int dbfid = unserialize_lsh_header(filename); 476 int dbfid = unserialize_lsh_header(filename);
476 indexName = new char[O2_INDEX_MAXSTR]; 477 indexName = new char[O2_INDEX_MAXSTR];
477 strncpy(indexName, filename, O2_INDEX_MAXSTR); // COPY THE CONTENTS TO THE NEW POINTER 478 strncpy(indexName, filename, O2_INDEX_MAXSTR); // COPY THE CONTENTS TO THE NEW POINTER
478 H::initialize_lsh_functions(); // Base-class data-structure initialization 479 H::initialize_lsh_functions(); // Base-class data-structure initialization
479 unserialize_lsh_functions(dbfid); // populate with on-disk hashfunction values 480 unserialize_lsh_functions(dbfid); // populate with on-disk hashfunction values
483 unserialize_lsh_hashtables_format1(dbfid); 484 unserialize_lsh_hashtables_format1(dbfid);
484 } 485 }
485 486
486 // Format2 always needs unserializing 487 // Format2 always needs unserializing
487 if(lshHeader->flags&O2_SERIAL_FILEFORMAT2 && lshInCoreFlag){ 488 if(lshHeader->flags&O2_SERIAL_FILEFORMAT2 && lshInCoreFlag){
488 FILE* dbFile = fdopen(dbfid, "rb"); 489 dbFile = fdopen(dbfid, "rb");
489 if(!dbFile) 490 if(!dbFile)
490 error("Cannot open LSH file for reading", filename); 491 error("Cannot open LSH file for reading", filename);
491 unserialize_lsh_hashtables_format2(dbFile); 492 unserialize_lsh_hashtables_format2(dbFile);
492 } 493 }
493 serial_close(dbfid); 494 serial_close(dbfid);
495 if(dbFile){
496 fclose(dbFile);
497 dbFile = 0;
498 }
494 } 499 }
495 500
496 G::~G(){ 501 G::~G(){
497 delete lshHeader; 502 delete lshHeader;
503 delete[] indexName;
498 } 504 }
499 505
500 // single point insertion; inserted values are hash value and pointID 506 // single point insertion; inserted values are hash value and pointID
501 Uns32T G::insert_point(vector<float>& v, Uns32T pp){ 507 Uns32T G::insert_point(vector<float>& v, Uns32T pp){
502 Uns32T collisionCount = 0; 508 Uns32T collisionCount = 0;
678 684
679 void G::serialize(char* filename, Uns32T serialFormat){ 685 void G::serialize(char* filename, Uns32T serialFormat){
680 int dbfid; 686 int dbfid;
681 char* db; 687 char* db;
682 int dbIsNew=0; 688 int dbIsNew=0;
683 689 FILE* dbFile = 0;
684 // Check requested serialFormat 690 // Check requested serialFormat
685 if(!(serialFormat==O2_SERIAL_FILEFORMAT1 || serialFormat==O2_SERIAL_FILEFORMAT2)) 691 if(!(serialFormat==O2_SERIAL_FILEFORMAT1 || serialFormat==O2_SERIAL_FILEFORMAT2))
686 error("Unrecognized serial file format request: ", "serialize()"); 692 error("Unrecognized serial file format request: ", "serialize()");
687 693
688 // Test to see if file exists 694 // Test to see if file exists
715 serialize_lsh_hashfunctions(dbfid); 721 serialize_lsh_hashfunctions(dbfid);
716 // Write the hashtables in the requested format 722 // Write the hashtables in the requested format
717 if(serialFormat == O2_SERIAL_FILEFORMAT1) 723 if(serialFormat == O2_SERIAL_FILEFORMAT1)
718 serialize_lsh_hashtables_format1(dbfid, !dbIsNew); 724 serialize_lsh_hashtables_format1(dbfid, !dbIsNew);
719 else{ 725 else{
720 FILE* dbFile = fdopen(dbfid, "r+b"); 726 dbFile = fdopen(dbfid, "r+b");
721 if(!dbFile) 727 if(!dbFile)
722 error("Cannot open LSH file for writing",filename); 728 error("Cannot open LSH file for writing",filename);
723 serialize_lsh_hashtables_format2(dbFile, !dbIsNew); 729 serialize_lsh_hashtables_format2(dbFile, !dbIsNew);
724 fflush(dbFile); 730 fflush(dbFile);
725 } 731 }
734 lshHeader->flags|=O2_SERIAL_FILEFORMAT1; 740 lshHeader->flags|=O2_SERIAL_FILEFORMAT1;
735 memcpy((char*)db, (char*)lshHeader, sizeof(SerialHeaderT)); 741 memcpy((char*)db, (char*)lshHeader, sizeof(SerialHeaderT));
736 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap 742 serial_munmap(db, O2_SERIAL_HEADER_SIZE); // drop mmap
737 } 743 }
738 serial_close(dbfid); 744 serial_close(dbfid);
745 if(dbFile){
746 fclose(dbFile);
747 dbFile = 0;
748 }
739 } 749 }
740 750
741 // Test to see if core structure and requested format is 751 // Test to see if core structure and requested format is
742 // compatible with currently opened database 752 // compatible with currently opened database
743 int G::serial_can_merge(Uns32T format){ 753 int G::serial_can_merge(Uns32T format){