<commit_before>// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

use rocksdb::{DB, Options};
use rocksdb::rocksdb_ffi::DBCFHandle;

pub fn get_cf_handle<'a>(db: &'a DB, cf: &str) -> Result<&'a DBCFHandle, String> {
    db.cf_handle(cf)
        .ok_or_else(|| format!("cf {} not found.", cf))
}

pub fn open(path: &str, cfs: &[&str]) -> Result<DB, String> {
    let mut opts = Options::new();
    opts.create_if_missing(false);
    let mut cfs_opts = vec![];
    for _ in 0..cfs.len() {
        cfs_opts.push(Options::new());
    }
    open_opt(&opts, path, cfs, cfs_opts)
}

pub fn open_opt(opts: &Options,
                path: &str,
                cfs: &[&str],
                cfs_opts: Vec<Options>)
                -> Result<DB, String> {
    let cfs_ref_opts: Vec<&Options> = cfs_opts.iter().collect();
    DB::open_cf(opts, path, cfs, &cfs_ref_opts)
}

pub fn new_engine(path: &str, cfs: &[&str]) -> Result<DB, String> {
    let opts = Options::new();
    let mut cfs_opts = vec![];
    for _ in 0..cfs.len() {
        cfs_opts.push(Options::new());
    }
    new_engine_opt(opts, path, cfs, cfs_opts)
}

pub fn new_engine_opt(mut opts: Options,
                      path: &str,
                      cfs: &[&str],
                      cfs_opts: Vec<Options>)
                      -> Result<DB, String> {
    // Currently we support 1) Create new db. 2) Open a db with CFs we want. 3) Open db with no
    // CF.
    // TODO: Support open db with incomplete CFs.

    // opts is used as Rocksdb's DBOptions when call DB::open_cf
    opts.create_if_missing(false);
    let cfs_ref_opts: Vec<&Options> = cfs_opts.iter().collect();
    match DB::open_cf(&opts, path, cfs, &cfs_ref_opts) {
        Ok(db) => return Ok(db),
        Err(e) => warn!("open rocksdb fail: {}", e),
    }

    // opts is used as Rocksdb's Options(include DBOptions and ColumnFamilyOptions)
    // when call DB::open
    opts.create_if_missing(true);
    let mut db = match DB::open(&opts, path) {
        Ok(db) => db,
        Err(e) => return Err(e),
    };
    for (&cf, &cf_opts) in cfs.iter().zip(&cfs_ref_opts) {
        if cf == "default" {
            continue;
        }
        if let Err(e) = db.create_cf(cf, cf_opts) {
            return Err(e);
        }
    }
    Ok(db)
}
<commit_msg>util: remove unnecessary log. (#972)<commit_after>// Copyright 2016 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

use rocksdb::{DB, Options};
use rocksdb::rocksdb_ffi::DBCFHandle;

pub fn get_cf_handle<'a>(db: &'a DB, cf: &str) -> Result<&'a DBCFHandle, String> {
    db.cf_handle(cf)
        .ok_or_else(|| format!("cf {} not found.", cf))
}

pub fn open(path: &str, cfs: &[&str]) -> Result<DB, String> {
    let mut opts = Options::new();
    opts.create_if_missing(false);
    let mut cfs_opts = vec![];
    for _ in 0..cfs.len() {
        cfs_opts.push(Options::new());
    }
    open_opt(&opts, path, cfs, cfs_opts)
}

pub fn open_opt(opts: &Options,
                path: &str,
                cfs: &[&str],
                cfs_opts: Vec<Options>)
                -> Result<DB, String> {
    let cfs_ref_opts: Vec<&Options> = cfs_opts.iter().collect();
    DB::open_cf(opts, path, cfs, &cfs_ref_opts)
}

pub fn new_engine(path: &str, cfs: &[&str]) -> Result<DB, String> {
    let opts = Options::new();
    let mut cfs_opts = vec![];
    for _ in 0..cfs.len() {
        cfs_opts.push(Options::new());
    }
    new_engine_opt(opts, path, cfs, cfs_opts)
}

pub fn new_engine_opt(mut opts: Options,
                      path: &str,
                      cfs: &[&str],
                      cfs_opts: Vec<Options>)
                      -> Result<DB, String> {
    // Currently we support 1) Create new db. 2) Open a db with CFs we want. 3) Open db with no
    // CF.
    // TODO: Support open db with incomplete CFs.

    // opts is used as Rocksdb's DBOptions when call DB::open_cf
    opts.create_if_missing(false);
    let cfs_ref_opts: Vec<&Options> = cfs_opts.iter().collect();
    if let Ok(db) = DB::open_cf(&opts, path, cfs, &cfs_ref_opts) {
        return Ok(db);
    }

    // opts is used as Rocksdb's Options(include DBOptions and ColumnFamilyOptions)
    // when call DB::open
    opts.create_if_missing(true);
    let mut db = match DB::open(&opts, path) {
        Ok(db) => db,
        Err(e) => return Err(e),
    };
    for (&cf, &cf_opts) in cfs.iter().zip(&cfs_ref_opts) {
        if cf == "default" {
            continue;
        }
        if let Err(e) = db.create_cf(cf, cf_opts) {
            return Err(e);
        }
    }
    Ok(db)
}

{'fraction_non_alphanumeric': 0.14604685212298682, 'fraction_numerical': 0.005307467057101025, 'mean_word_length': 2.4028642590286426, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2929114', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1850, 'n_words': 540}