# File hstore_compact.rb, line 44
def compact(hstore_name, backup_dir, descriptor=nil)
   filename = hstore_name
   raise "#{storename.inspect} does not exist" if (!File.exists?(filename))
   raise "#{filename+'.new'} already exists and is needed by compact. Please remove this file and try again" if (File.exists?(filename+'.new'))
   raise "#{filename+'.newi'} already exists and is needed by compact. Please remove this file and try again" if (File.exists?(filename+'.newi'))
   export_filename = nil
   export_count = import_count = errors = 0
   count = 0
   HStore.new(hstore_name).open {|store|
      descriptor = store.get_descriptor if (!descriptor)
      count = store.size
      export_filename, export_count, errors = store.export(nil, backup_dir)
   }
   if (count > 0 && export_count == 0)
      # delete the export and error files

      File.delete(File.join(backup_dir, export_filename)) if (File.exists?(File.join(backup_dir, export_filename)))
      File.delete(File.join(backup_dir, export_filename+'.err')) if (File.exists?(File.join(backup_dir, export_filename+'.err')))
      puts "#{errors} error#{errors!=1?'s':''} occurred during the export of the hstore so the compact was aborted."
      puts "This error typically occurs because hstore_compact could not determine the type of the objects stored in the HStore.  Try:"
      puts "   ruby -r module_name hstore_compact.rb #{hstore_name} #{export_filename}"
      puts "   where module_name is the module that defines the objects store in the HStore."
      puts 'For example: ruby -r notes.rb ../hstore_compact.rb notes.hs "Locana Notes" "1.00"'
      raise "The compact was canceled because none of data in the HStore was exported.  See note above for details"
   end
   HStore.new(filename+'.new').open {|hs|
      import_filename, import_count = hs.import(export_filename, backup_dir, descriptor)
   }
   begin
      File.mv(filename+'.new', filename)
   rescue Exception
      # This original HStore should still be okay

      puts "The HStore was exported and imported but could not be moved to it's original location.  "
      puts "This could happen if another process is accessing the HStore."
      puts "The HStore should be okay by was NOT compacted.  Fix the error and try again."
      raise
   end
   begin
      File.mv(filename+'.newi', filename+'i')
   rescue Exception
      # This HStore is now corrupt so we need to remove it

      File.delete(filename) if (File.exists?(filename))
      File.delete(filename+'i') if (File.exists?(filename+'i'))
      puts "The HStore was exported and imported but could not be moved to it's original location."
      puts "The HStore is now corrupt and you need to restore it by copying the HStore files from the backup dir. "
      puts "This should work:"
      puts " cp #{backup_dir}/#{hstore_name} #{filename}"
      puts " cp #{backup_dir}/#{hstore_name}i #{filename}i"
      raise
   end
   # these file are no longer usable

   File.delete(filename+'~') if (File.exists?(filename+'~'))
   File.delete(filename+'i~') if (File.exists?(filename+'i~'))  # this is an older style backup replaced by 'i.bak' on 10/2002

   File.delete(filename+'i.bak') if (File.exists?(filename+'i.bak'))
   @@we_are_compacting = nil
   return export_filename, count, export_count, import_count, errors
rescue Exception
   File.delete(filename+'.new') if (File.exists?(filename+'.new'))
   File.delete(filename+'.newi') if (File.exists?(filename+'.newi'))
   raise
end