func read_multi_cfs() error { dbOpts := gorocksdb.NewDefaultOptions() defaultRO := gorocksdb.NewDefaultReadOptions() db, handles, err := gorocksdb.OpenDbColumnFamilies( dbOpts, "/tmp/multicf_db", []string{"default", "0", "1", "2", "3"}, []*gorocksdb.Options{dbOpts, dbOpts, dbOpts, dbOpts, dbOpts}, ) iters, err := gorocksext.NewIterators(defaultRO, db, handles) if err != nil { return err } for i, iter := range iters { fmt.Printf("COUTING FOR ITER: %d\n", i) iter.SeekToFirst() for iter.Valid() { fmt.Println(string(iter.Key().Data())) defer iter.Key().Free() iter.Next() } } db.Close() return nil }
func openDB() (*OpenchainDB, error) { if isOpen { return openchainDB, nil } dbPath := getDBPath() opts := gorocksdb.NewDefaultOptions() defer opts.Destroy() opts.SetCreateIfMissing(false) opts.SetCreateIfMissingColumnFamilies(true) cfNames := []string{"default"} cfNames = append(cfNames, columnfamilies...) var cfOpts []*gorocksdb.Options for _ = range cfNames { cfOpts = append(cfOpts, opts) } db, cfHandlers, err := gorocksdb.OpenDbColumnFamilies(opts, dbPath, cfNames, cfOpts) if err != nil { fmt.Println("Error opening DB", err) return nil, err } isOpen = true // XXX should we close cfHandlers[0]? return &OpenchainDB{db, cfHandlers[1], cfHandlers[2], cfHandlers[3], cfHandlers[4], cfHandlers[5]}, nil }
// Open open underlying rocksdb func (openchainDB *OpenchainDB) Open() { openchainDB.mux.Lock() if openchainDB.dbState == opened { openchainDB.mux.Unlock() return } defer openchainDB.mux.Unlock() dbPath := getDBPath() missing, err := dirMissingOrEmpty(dbPath) if err != nil { panic(fmt.Sprintf("Error while trying to open DB: %s", err)) } dbLogger.Debugf("Is db path [%s] empty [%t]", dbPath, missing) if missing { err = os.MkdirAll(path.Dir(dbPath), 0755) if err != nil { panic(fmt.Sprintf("Error making directory path [%s]: %s", dbPath, err)) } } opts := gorocksdb.NewDefaultOptions() defer opts.Destroy() opts.SetCreateIfMissing(missing) opts.SetCreateIfMissingColumnFamilies(true) cfNames := []string{"default"} cfNames = append(cfNames, columnfamilies...) var cfOpts []*gorocksdb.Options for range cfNames { cfOpts = append(cfOpts, opts) } db, cfHandlers, err := gorocksdb.OpenDbColumnFamilies(opts, dbPath, cfNames, cfOpts) if err != nil { panic(fmt.Sprintf("Error opening DB: %s", err)) } openchainDB.DB = db openchainDB.BlockchainCF = cfHandlers[1] openchainDB.StateCF = cfHandlers[2] openchainDB.StateDeltaCF = cfHandlers[3] openchainDB.IndexesCF = cfHandlers[4] openchainDB.PersistCF = cfHandlers[5] openchainDB.dbState = opened }
func openDB() (*OpenchainDB, error) { if isOpen { return openchainDB, nil } dbPath := getDBPath() opts := gorocksdb.NewDefaultOptions() opts.SetCreateIfMissing(false) db, cfHandlers, err := gorocksdb.OpenDbColumnFamilies(opts, dbPath, []string{"default", blockchainCF, stateCF, stateDeltaCF, indexesCF}, []*gorocksdb.Options{opts, opts, opts, opts, opts}) if err != nil { fmt.Println("Error opening DB", err) return nil, err } isOpen = true return &OpenchainDB{db, cfHandlers[1], cfHandlers[2], cfHandlers[3], cfHandlers[4]}, nil }
// NewStore returns the Store a rocksdb wrapper func NewStore(options StoreOptions) (*Store, error) { options.SetDefaults() if options.Directory == "" { return nil, fmt.Errorf("Empty directory of store options") } if options.IsDebug { log.EnableDebug() } s := &Store{ directory: options.Directory, useTailing: !options.DisableTailing, cfHandles: make(map[string]*rocks.ColumnFamilyHandle), queues: make(map[string]*Queue), } opts := rocks.NewDefaultOptions() opts.SetCreateIfMissing(true) opts.IncreaseParallelism(options.Parallel) opts.SetMergeOperator(&_CountMerger{}) opts.SetMaxSuccessiveMerges(64) opts.SetWriteBufferSize(options.WriteBufferSize) opts.SetMaxWriteBufferNumber(options.WriteBufferNumber) opts.SetTargetFileSizeBase(options.FileSizeBase) opts.SetLevel0FileNumCompactionTrigger(8) opts.SetLevel0SlowdownWritesTrigger(16) opts.SetLevel0StopWritesTrigger(24) opts.SetNumLevels(4) opts.SetMaxBytesForLevelBase(512 * 1024 * 1024) opts.SetMaxBytesForLevelMultiplier(8) opts.SetCompression(options.Compression) opts.SetDisableAutoCompactions(options.DisableAutoCompaction) bbto := rocks.NewDefaultBlockBasedTableOptions() bbto.SetBlockCache(rocks.NewLRUCache(options.MemorySize)) bbto.SetFilterPolicy(rocks.NewBloomFilter(10)) opts.SetBlockBasedTableFactory(bbto) opts.SetMaxOpenFiles(-1) opts.SetMemtablePrefixBloomBits(8 * 1024 * 1024) var err error if err = os.MkdirAll(options.Directory, 0755); err != nil { log.Errorf("Failed to mkdir %q, %s", options.Directory, err) return nil, err } cfNames, err := rocks.ListColumnFamilies(opts, options.Directory) if err != nil { // FIXME: we need to be sure if this means the db does not exist for now // so that we cannot list the column families log.Errorf("Failed to collect the column family names, %s", err) } else { log.Debugf("Got column family names for the existing db, %+v", cfNames) } if len(cfNames) == 0 { // We create the default column family to get the column family handle cfNames = []string{"default"} } cfOpts := make([]*rocks.Options, len(cfNames)) for i := range cfNames { cfOpts[i] = opts } db, cfHandles, err := rocks.OpenDbColumnFamilies(opts, options.Directory, cfNames, cfOpts) if err != nil { log.Errorf("Failed to open rocks database, %s", err) return nil, err } s.DB = db s.dbOpts = opts s.ro = rocks.NewDefaultReadOptions() s.ro.SetFillCache(false) s.ro.SetTailing(!options.DisableTailing) s.wo = rocks.NewDefaultWriteOptions() s.wo.DisableWAL(options.DisableWAL) s.wo.SetSync(options.Sync) if len(cfNames) > 0 { for i := range cfNames { s.cfHandles[cfNames[i]] = cfHandles[i] } } return s, nil }