Beispiel #1
0
/**
 * Flushes the filter with the given name
 * @arg filter_name The name of the filter to flush
 * @return 0 on success. -1 if the filter does not exist.
 */
int filtmgr_flush_filter(bloom_filtmgr *mgr, char *filter_name) {
    // Get the filter
    bloom_filter_wrapper *filt = take_filter(mgr, filter_name);
    if (!filt) return -1;

    // Flush
    bloomf_flush(filt->filter);
    return 0;
}
Beispiel #2
0
/**
 * Gracefully closes a bloom filter.
 * @arg filter The filter to close
 * @return 0 on success.
 */
int bloomf_close(bloom_filter *filter) {
    // Acquire lock
    pthread_mutex_lock(&filter->sbf_lock);

    // Only act if we are non-proxied
    if (filter->sbf) {
        bloomf_flush(filter);

        bloom_sbf *sbf = (bloom_sbf*)filter->sbf;
        filter->sbf = NULL;

        sbf_close(sbf);
        free(sbf);

        filter->counters.page_outs += 1;
    }

    // Release lock
    pthread_mutex_unlock(&filter->sbf_lock);
    return 0;
}
Beispiel #3
0
/**
 * Initializes a bloom filter wrapper.
 * @arg config The configuration to use
 * @arg filter_name The name of the filter
 * @arg discover Should existing data files be discovered. Otherwise
 * they will be faulted in on-demand.
 * @arg filter Output parameter, the new filter
 * @return 0 on success
 */
int init_bloom_filter(bloom_config *config, char *filter_name, int discover, bloom_filter **filter) {
    // Allocate the buffers
    bloom_filter *f = *filter = calloc(1, sizeof(bloom_filter));

    // Store the things
    f->config = config;
    f->filter_name = strdup(filter_name);

    // Copy filter configs
    f->filter_config.initial_capacity = config->initial_capacity;
    f->filter_config.capacity = config->initial_capacity;
    f->filter_config.default_probability = config->default_probability;
    f->filter_config.scale_size = config->scale_size;
    f->filter_config.probability_reduction = config->probability_reduction;
    f->filter_config.in_memory = config->in_memory;

    // Get the folder name
    char *folder_name = NULL;
    int res;
    res = asprintf(&folder_name, FILTER_FOLDER_NAME, f->filter_name);
    assert(res != -1);

    // Compute the full path
    f->full_path = join_path(config->data_dir, folder_name);
    free(folder_name);

    // Initialize the locks
    INIT_BLOOM_SPIN(&f->counter_lock);
    pthread_mutex_init(&f->sbf_lock, NULL);

    // Try to create the folder path
    res = mkdir(f->full_path, 0755);
    if (res && errno != EEXIST) {
        syslog(LOG_ERR, "Failed to create filter directory '%s'. Err: %d [%d]", f->full_path, res, errno);
        return res;
    }

    // Read in the filter_config
    char *config_name = join_path(f->full_path, (char*)CONFIG_FILENAME);
    res = filter_config_from_filename(config_name, &f->filter_config);
    free(config_name);
    if (res && res != -ENOENT) {
        syslog(LOG_ERR, "Failed to read filter '%s' configuration. Err: %d [%d]", f->filter_name, res, errno);
        return res;
    }

    // Discover the existing filters if we need to
    res = 0;
    if (discover) {
        res = thread_safe_fault(f);
        if (res) {
            syslog(LOG_ERR, "Failed to fault in the filter '%s'. Err: %d", f->filter_name, res);
        }
    }

    // Trigger a flush on first instantiation. This will create
    // a new ini file for first time filters.
    if (!res) {
        res = bloomf_flush(f);
    }

    return res;
}