@@ -1053,7 +1053,7 @@ bool cfs_control_gc(bool enabled)
10531053/* ----------------------------------------------------------------
10541054 *Section 5: Garbage collection user's functions.
10551055 *
1056- * TODO adddescription. reorder functions.
1056+ * TODO adddescriptions
10571057 * ----------------------------------------------------------------
10581058 */
10591059PG_FUNCTION_INFO_V1 (cfs_start_gc );
@@ -1083,7 +1083,8 @@ Datum cfs_start_gc(PG_FUNCTION_ARGS)
10831083cfs_state -> n_workers = PG_GETARG_INT32 (0 );
10841084handles = (BackgroundWorkerHandle * * )palloc (cfs_state -> n_workers * sizeof (BackgroundWorkerHandle * ));
10851085
1086- for (i = 0 ;i < cfs_state -> n_workers ;i ++ ) {
1086+ for (i = 0 ;i < cfs_state -> n_workers ;i ++ )
1087+ {
10871088BackgroundWorker worker ;
10881089MemSet (& worker ,0 ,sizeof (worker ));
10891090sprintf (worker .bgw_name ,"cfs-worker-%d" ,i );
@@ -1092,13 +1093,12 @@ Datum cfs_start_gc(PG_FUNCTION_ARGS)
10921093worker .bgw_restart_time = BGW_NEVER_RESTART ;
10931094worker .bgw_main = cfs_gc_bgworker_main ;
10941095worker .bgw_main_arg = Int32GetDatum (i );
1095- if (!RegisterDynamicBackgroundWorker (& worker ,& handles [i ])) {
1096+ if (!RegisterDynamicBackgroundWorker (& worker ,& handles [i ]))
10961097break ;
1097- }
10981098}
1099- for (j = 0 ;j < i ;j ++ ) {
1099+ for (j = 0 ;j < i ;j ++ )
11001100WaitForBackgroundWorkerShutdown (handles [j ]);
1101- }
1101+
11021102pfree (handles );
11031103pg_atomic_clear_flag (& cfs_state -> gc_started );
11041104}
@@ -1122,35 +1122,41 @@ Datum cfs_estimate(PG_FUNCTION_ARGS)
11221122Relation rel = try_relation_open (oid ,AccessShareLock );
11231123double avgRatio = 0.0 ;
11241124
1125- if (rel != NULL ) {
1125+ if (rel != NULL )
1126+ {
11261127char * path = relpathbackend (rel -> rd_node ,rel -> rd_backend ,MAIN_FORKNUM );
11271128int fd = open (path ,O_RDONLY |PG_BINARY ,0 );
1128- if (fd >=0 ) {
1129+
1130+ if (fd >=0 )
1131+ {
11291132int i ;
11301133char origBuffer [BLCKSZ ];
11311134char compressedBuffer [CFS_MAX_COMPRESSED_SIZE (BLCKSZ )];
11321135uint32 compressedSize ;
11331136off_t rc = lseek (fd ,0 ,SEEK_END );
1134- if (rc >=0 ) {
1137+
1138+ if (rc >=0 )
1139+ {
11351140off_t step = rc /BLCKSZ /CFS_ESTIMATE_PROBES * BLCKSZ ;
1136- for (i = 0 ;i < CFS_ESTIMATE_PROBES ;i ++ ) {
1141+ for (i = 0 ;i < CFS_ESTIMATE_PROBES ;i ++ )
1142+ {
11371143rc = lseek (fd ,step * i ,SEEK_SET );
1138- if (rc < 0 ) {
1144+ if (rc < 0 )
11391145break ;
1140- }
1141- if (!cfs_read_file (fd ,origBuffer ,BLCKSZ )) {
1146+
1147+ if (!cfs_read_file (fd ,origBuffer ,BLCKSZ ))
11421148break ;
1143- }
1144- compressedSize = (uint32 )cfs_compress (compressedBuffer ,sizeof (compressedBuffer ),origBuffer ,BLCKSZ );
1145- if (compressedSize > 0 && compressedSize < CFS_MIN_COMPRESSED_SIZE (BLCKSZ )) {
1149+
1150+ compressedSize = (uint32 )cfs_compress (compressedBuffer ,
1151+ sizeof (compressedBuffer ),origBuffer ,BLCKSZ );
1152+ if (compressedSize > 0 && compressedSize < CFS_MIN_COMPRESSED_SIZE (BLCKSZ ))
11461153avgRatio += (double )BLCKSZ /compressedSize ;
1147- } else {
1154+ else
11481155avgRatio += 1 ;
1149- }
11501156}
1151- if (i != 0 ) {
1157+
1158+ if (i != 0 )
11521159avgRatio /=i ;
1153- }
11541160}
11551161close (fd );
11561162}
@@ -1166,39 +1172,43 @@ Datum cfs_compression_ratio(PG_FUNCTION_ARGS)
11661172uint64 virtSize = 0 ;
11671173uint64 physSize = 0 ;
11681174
1169- if (rel != NULL ) {
1175+ if (rel != NULL )
1176+ {
11701177char * path = relpathbackend (rel -> rd_node ,rel -> rd_backend ,MAIN_FORKNUM );
11711178char * map_path = (char * )palloc (strlen (path )+ 16 );
11721179int i = 0 ;
11731180
1174- while (true) {
1181+ while (true)
1182+ {
11751183int md ;
11761184FileMap * map ;
11771185
1178- if (i == 0 ) {
1179- sprintf (map_path ,"%s.cfm" ,path );
1180- } else {
1181- sprintf (map_path ,"%s.%u.cfm" ,path ,i );
1182- }
1186+ if (i == 0 )
1187+ sprintf (map_path ,"%s.cfm" ,path );
1188+ else
1189+ sprintf (map_path ,"%s.%u.cfm" ,path ,i );
1190+
11831191md = open (map_path ,O_RDWR |PG_BINARY ,0 );
1184- if (md < 0 ) {
1192+
1193+ if (md < 0 )
11851194break ;
1186- }
1195+
11871196map = cfs_mmap (md );
1188- if (map == MAP_FAILED ) {
1197+ if (map == MAP_FAILED )
1198+ {
11891199elog (LOG ,"cfs_compression_ration failed to map file %s: %m" ,map_path );
11901200close (md );
11911201break ;
11921202}
1203+
11931204virtSize += pg_atomic_read_u32 (& map -> virtSize );
11941205physSize += pg_atomic_read_u32 (& map -> physSize );
11951206
1196- if (cfs_munmap (map )< 0 ) {
1207+ if (cfs_munmap (map )< 0 )
11971208elog (LOG ,"CFS failed to unmap file %s: %m" ,map_path );
1198- }
1199- if (close (md )< 0 ) {
1209+ if (close (md )< 0 )
12001210elog (LOG ,"CFS failed to close file %s: %m" ,map_path );
1201- }
1211+
12021212i += 1 ;
12031213}
12041214pfree (path );
@@ -1215,39 +1225,41 @@ Datum cfs_fragmentation(PG_FUNCTION_ARGS)
12151225uint64 usedSize = 0 ;
12161226uint64 physSize = 0 ;
12171227
1218- if (rel != NULL ) {
1228+ if (rel != NULL )
1229+ {
12191230char * path = relpathbackend (rel -> rd_node ,rel -> rd_backend ,MAIN_FORKNUM );
12201231char * map_path = (char * )palloc (strlen (path )+ 16 );
12211232int i = 0 ;
12221233
1223- while (true) {
1234+ while (true)
1235+ {
12241236int md ;
12251237FileMap * map ;
12261238
1227- if (i == 0 ) {
1228- sprintf (map_path ,"%s.cfm" ,path );
1229- } else {
1230- sprintf (map_path ,"%s.%u.cfm" ,path ,i );
1231- }
1239+ if (i == 0 )
1240+ sprintf (map_path ,"%s.cfm" ,path );
1241+ else
1242+ sprintf (map_path ,"%s.%u.cfm" ,path ,i );
1243+
12321244md = open (map_path ,O_RDWR |PG_BINARY ,0 );
1233- if (md < 0 ) {
1245+ if (md < 0 )
12341246break ;
1235- }
1247+
12361248map = cfs_mmap (md );
1237- if (map == MAP_FAILED ) {
1249+ if (map == MAP_FAILED )
1250+ {
12381251elog (LOG ,"cfs_compression_ration failed to map file %s: %m" ,map_path );
12391252close (md );
12401253break ;
12411254}
12421255usedSize += pg_atomic_read_u32 (& map -> usedSize );
12431256physSize += pg_atomic_read_u32 (& map -> physSize );
12441257
1245- if (cfs_munmap (map )< 0 ) {
1258+ if (cfs_munmap (map )< 0 )
12461259elog (LOG ,"CFS failed to unmap file %s: %m" ,map_path );
1247- }
1248- if (close (md )< 0 ) {
1260+ if (close (md )< 0 )
12491261elog (LOG ,"CFS failed to close file %s: %m" ,map_path );
1250- }
1262+
12511263i += 1 ;
12521264}
12531265pfree (path );
@@ -1261,21 +1273,22 @@ Datum cfs_gc_relation(PG_FUNCTION_ARGS)
12611273{
12621274cfs_gc_processed_segments = 0 ;
12631275
1264- if (cfs_gc_workers == 0 && pg_atomic_test_set_flag (& cfs_state -> gc_started ))
1276+ if (cfs_gc_workers == 0 && pg_atomic_test_set_flag (& cfs_state -> gc_started ))
12651277{
12661278Oid oid = PG_GETARG_OID (0 );
12671279Relation rel = try_relation_open (oid ,AccessShareLock );
12681280
1269- if (rel != NULL ) {
1281+ if (rel != NULL )
1282+ {
12701283char * path = relpathbackend (rel -> rd_node ,rel -> rd_backend ,MAIN_FORKNUM );
12711284char * map_path = (char * )palloc (strlen (path )+ 16 );
12721285int i = 0 ;
12731286sprintf (map_path ,"%s.cfm" ,path );
12741287
1275- while (true) {
1276- if (!cfs_gc_file (map_path )) {
1288+ while (true)
1289+ {
1290+ if (!cfs_gc_file (map_path ))
12771291break ;
1278- }
12791292sprintf (map_path ,"%s.%u.cfm" ,path ,++ i );
12801293}
12811294pfree (path );