@ -11,10 +11,13 @@
# define __STDC_FORMAT_MACROS
# define __STDC_FORMAT_MACROS
# endif
# endif
# include <algorithm>
# include <condition_variable>
# include <inttypes.h>
# include <inttypes.h>
# include <string>
# include <string>
# include <vector>
# include <vector>
# include <algorithm>
# include <mutex>
# include <thread>
# include <set>
# include <set>
# include <unordered_set>
# include <unordered_set>
@ -561,27 +564,49 @@ class SpatialDBImpl : public SpatialDB {
return Write ( write_options , & batch ) ;
return Write ( write_options , & batch ) ;
}
}
virtual Status Compact ( ) override {
virtual Status Compact ( int num_threads ) override {
// TODO(icanadi) maybe do this in parallel?
std : : vector < ColumnFamilyHandle * > column_families ;
Status s , t ;
column_families . push_back ( data_column_family_ ) ;
for ( auto & iter : name_to_index_ ) {
for ( auto & iter : name_to_index_ ) {
t = Flush ( FlushOptions ( ) , iter . second . column_family ) ;
column_families . push_back ( iter . second . column_family ) ;
if ( ! t . ok ( ) ) {
s = t ;
}
}
t = CompactRange ( iter . second . column_family , nullptr , nullptr ) ;
if ( ! t . ok ( ) ) {
std : : mutex state_mutex ;
s = t ;
std : : condition_variable cv ;
Status s ;
int threads_running = 0 ;
std : : vector < std : : thread > threads ;
for ( auto cfh : column_families ) {
threads . emplace_back ( [ & , cfh ] {
{
std : : unique_lock < std : : mutex > lk ( state_mutex ) ;
cv . wait ( lk , [ & ] { return threads_running < num_threads ; } ) ;
threads_running + + ;
}
}
Status t = Flush ( FlushOptions ( ) , cfh ) ;
if ( t . ok ( ) ) {
t = CompactRange ( cfh , nullptr , nullptr ) ;
}
}
t = Flush ( FlushOptions ( ) , data_column_family_ ) ;
if ( ! t . ok ( ) ) {
{
std : : unique_lock < std : : mutex > lk ( state_mutex ) ;
threads_running - - ;
if ( s . ok ( ) & & ! t . ok ( ) ) {
s = t ;
s = t ;
}
}
t = CompactRange ( data_column_family_ , nullptr , nullptr ) ;
cv . notify_one ( ) ;
if ( ! t . ok ( ) ) {
s = t ;
}
}
} ) ;
}
for ( auto & t : threads ) {
t . join ( ) ;
}
return s ;
return s ;
}
}