@@ -1053,7 +1053,7 @@ bool cfs_control_gc(bool enabled)
1053
1053
/* ----------------------------------------------------------------
1054
1054
* Section 5: Garbage collection user's functions.
1055
1055
*
1056
- * TODO add description. reorder functions.
1056
+ * TODO add descriptions
1057
1057
* ----------------------------------------------------------------
1058
1058
*/
1059
1059
PG_FUNCTION_INFO_V1 (cfs_start_gc );
@@ -1083,7 +1083,8 @@ Datum cfs_start_gc(PG_FUNCTION_ARGS)
1083
1083
cfs_state -> n_workers = PG_GETARG_INT32 (0 );
1084
1084
handles = (BackgroundWorkerHandle * * )palloc (cfs_state -> n_workers * sizeof (BackgroundWorkerHandle * ));
1085
1085
1086
- for (i = 0 ; i < cfs_state -> n_workers ; i ++ ) {
1086
+ for (i = 0 ; i < cfs_state -> n_workers ; i ++ )
1087
+ {
1087
1088
BackgroundWorker worker ;
1088
1089
MemSet (& worker , 0 , sizeof (worker ));
1089
1090
sprintf (worker .bgw_name , "cfs-worker-%d" , i );
@@ -1092,13 +1093,12 @@ Datum cfs_start_gc(PG_FUNCTION_ARGS)
1092
1093
worker .bgw_restart_time = BGW_NEVER_RESTART ;
1093
1094
worker .bgw_main = cfs_gc_bgworker_main ;
1094
1095
worker .bgw_main_arg = Int32GetDatum (i );
1095
- if (!RegisterDynamicBackgroundWorker (& worker , & handles [i ])) {
1096
+ if (!RegisterDynamicBackgroundWorker (& worker , & handles [i ]))
1096
1097
break ;
1097
- }
1098
1098
}
1099
- for (j = 0 ; j < i ; j ++ ) {
1099
+ for (j = 0 ; j < i ; j ++ )
1100
1100
WaitForBackgroundWorkerShutdown (handles [j ]);
1101
- }
1101
+
1102
1102
pfree (handles );
1103
1103
pg_atomic_clear_flag (& cfs_state -> gc_started );
1104
1104
}
@@ -1122,35 +1122,41 @@ Datum cfs_estimate(PG_FUNCTION_ARGS)
1122
1122
Relation rel = try_relation_open (oid , AccessShareLock );
1123
1123
double avgRatio = 0.0 ;
1124
1124
1125
- if (rel != NULL ) {
1125
+ if (rel != NULL )
1126
+ {
1126
1127
char * path = relpathbackend (rel -> rd_node , rel -> rd_backend , MAIN_FORKNUM );
1127
1128
int fd = open (path , O_RDONLY |PG_BINARY , 0 );
1128
- if (fd >= 0 ) {
1129
+
1130
+ if (fd >= 0 )
1131
+ {
1129
1132
int i ;
1130
1133
char origBuffer [BLCKSZ ];
1131
1134
char compressedBuffer [CFS_MAX_COMPRESSED_SIZE (BLCKSZ )];
1132
1135
uint32 compressedSize ;
1133
1136
off_t rc = lseek (fd , 0 , SEEK_END );
1134
- if (rc >= 0 ) {
1137
+
1138
+ if (rc >= 0 )
1139
+ {
1135
1140
off_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
+ {
1137
1143
rc = lseek (fd , step * i , SEEK_SET );
1138
- if (rc < 0 ) {
1144
+ if (rc < 0 )
1139
1145
break ;
1140
- }
1141
- if (!cfs_read_file (fd , origBuffer , BLCKSZ )) {
1146
+
1147
+ if (!cfs_read_file (fd , origBuffer , BLCKSZ ))
1142
1148
break ;
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 ))
1146
1153
avgRatio += (double )BLCKSZ /compressedSize ;
1147
- } else {
1154
+ else
1148
1155
avgRatio += 1 ;
1149
- }
1150
1156
}
1151
- if (i != 0 ) {
1157
+
1158
+ if (i != 0 )
1152
1159
avgRatio /= i ;
1153
- }
1154
1160
}
1155
1161
close (fd );
1156
1162
}
@@ -1166,39 +1172,43 @@ Datum cfs_compression_ratio(PG_FUNCTION_ARGS)
1166
1172
uint64 virtSize = 0 ;
1167
1173
uint64 physSize = 0 ;
1168
1174
1169
- if (rel != NULL ) {
1175
+ if (rel != NULL )
1176
+ {
1170
1177
char * path = relpathbackend (rel -> rd_node , rel -> rd_backend , MAIN_FORKNUM );
1171
1178
char * map_path = (char * )palloc (strlen (path ) + 16 );
1172
1179
int i = 0 ;
1173
1180
1174
- while (true) {
1181
+ while (true)
1182
+ {
1175
1183
int md ;
1176
1184
FileMap * map ;
1177
1185
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
+
1183
1191
md = open (map_path , O_RDWR |PG_BINARY , 0 );
1184
- if (md < 0 ) {
1192
+
1193
+ if (md < 0 )
1185
1194
break ;
1186
- }
1195
+
1187
1196
map = cfs_mmap (md );
1188
- if (map == MAP_FAILED ) {
1197
+ if (map == MAP_FAILED )
1198
+ {
1189
1199
elog (LOG , "cfs_compression_ration failed to map file %s: %m" , map_path );
1190
1200
close (md );
1191
1201
break ;
1192
1202
}
1203
+
1193
1204
virtSize += pg_atomic_read_u32 (& map -> virtSize );
1194
1205
physSize += pg_atomic_read_u32 (& map -> physSize );
1195
1206
1196
- if (cfs_munmap (map ) < 0 ) {
1207
+ if (cfs_munmap (map ) < 0 )
1197
1208
elog (LOG , "CFS failed to unmap file %s: %m" , map_path );
1198
- }
1199
- if (close (md ) < 0 ) {
1209
+ if (close (md ) < 0 )
1200
1210
elog (LOG , "CFS failed to close file %s: %m" , map_path );
1201
- }
1211
+
1202
1212
i += 1 ;
1203
1213
}
1204
1214
pfree (path );
@@ -1215,39 +1225,41 @@ Datum cfs_fragmentation(PG_FUNCTION_ARGS)
1215
1225
uint64 usedSize = 0 ;
1216
1226
uint64 physSize = 0 ;
1217
1227
1218
- if (rel != NULL ) {
1228
+ if (rel != NULL )
1229
+ {
1219
1230
char * path = relpathbackend (rel -> rd_node , rel -> rd_backend , MAIN_FORKNUM );
1220
1231
char * map_path = (char * )palloc (strlen (path ) + 16 );
1221
1232
int i = 0 ;
1222
1233
1223
- while (true) {
1234
+ while (true)
1235
+ {
1224
1236
int md ;
1225
1237
FileMap * map ;
1226
1238
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
+
1232
1244
md = open (map_path , O_RDWR |PG_BINARY , 0 );
1233
- if (md < 0 ) {
1245
+ if (md < 0 )
1234
1246
break ;
1235
- }
1247
+
1236
1248
map = cfs_mmap (md );
1237
- if (map == MAP_FAILED ) {
1249
+ if (map == MAP_FAILED )
1250
+ {
1238
1251
elog (LOG , "cfs_compression_ration failed to map file %s: %m" , map_path );
1239
1252
close (md );
1240
1253
break ;
1241
1254
}
1242
1255
usedSize += pg_atomic_read_u32 (& map -> usedSize );
1243
1256
physSize += pg_atomic_read_u32 (& map -> physSize );
1244
1257
1245
- if (cfs_munmap (map ) < 0 ) {
1258
+ if (cfs_munmap (map ) < 0 )
1246
1259
elog (LOG , "CFS failed to unmap file %s: %m" , map_path );
1247
- }
1248
- if (close (md ) < 0 ) {
1260
+ if (close (md ) < 0 )
1249
1261
elog (LOG , "CFS failed to close file %s: %m" , map_path );
1250
- }
1262
+
1251
1263
i += 1 ;
1252
1264
}
1253
1265
pfree (path );
@@ -1261,21 +1273,22 @@ Datum cfs_gc_relation(PG_FUNCTION_ARGS)
1261
1273
{
1262
1274
cfs_gc_processed_segments = 0 ;
1263
1275
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 ))
1265
1277
{
1266
1278
Oid oid = PG_GETARG_OID (0 );
1267
1279
Relation rel = try_relation_open (oid , AccessShareLock );
1268
1280
1269
- if (rel != NULL ) {
1281
+ if (rel != NULL )
1282
+ {
1270
1283
char * path = relpathbackend (rel -> rd_node , rel -> rd_backend , MAIN_FORKNUM );
1271
1284
char * map_path = (char * )palloc (strlen (path ) + 16 );
1272
1285
int i = 0 ;
1273
1286
sprintf (map_path , "%s.cfm" , path );
1274
1287
1275
- while (true) {
1276
- if (!cfs_gc_file (map_path )) {
1288
+ while (true)
1289
+ {
1290
+ if (!cfs_gc_file (map_path ))
1277
1291
break ;
1278
- }
1279
1292
sprintf (map_path , "%s.%u.cfm" , path , ++ i );
1280
1293
}
1281
1294
pfree (path );
0 commit comments