// NewLevelDBDatabase creates a persistent key-value database without a freezer
// moving immutable chain segments into cold storage.
357828
func NewLevelDBDatabase(file string, cache int, handles int, namespace string) (ethdb.Database, error) {
	db, err := leveldb.New(file, cache, handles, namespace)
	if err != nil {
		return nil, err
	}
	return NewDatabase(db), nil
}
904907
func NewDB(db *bolt.DB, cs content.Store, ss map[string]snapshots.Snapshotter, opts ...DBOpt) *DB {
	m := &DB{
		db:      db,
		ss:      make(map[string]*snapshotter, len(ss)),
		dirtySS: map[string]struct{}{},
		dbopts: dbOptions{
			shared: true,
		},
	}

	for _, opt := range opts {
		opt(&m.dbopts)
	}

	// Initialize data stores
	m.cs = newContentStore(m, m.dbopts.shared, cs)
	for name, sn := range ss {
		m.ss[name] = newSnapshotter(m, name, sn)
	}

	return m
}
1322645
func newDB(c *Context) (*empire.DB, error) {
	db, err := empire.OpenDB(c.String(FlagDB))
	if err != nil {
		return nil, err
	}

	db.Schema = &empire.Schema{
		InstancePortPool: &empire.InstancePortPool{
			Start: uint(c.Int(FlagInstancePortPoolStart)),
			End:   uint(c.Int(FlagInstancePortPoolEnd)),
		},
	}

	return db, nil
}
803936
func NewNodeBase(loc LocationRange, freeVariables Identifiers) NodeBase {
	return NodeBase{
		loc:           loc,
		freeVariables: freeVariables,
	}
}
271564
func (c *CacheLazyFill) normaliseAndSaveEntityToDatabase(entityType, uuid string, nv *entity) error {
	// Strip name suffixes if applicable. This is intended for blue green deployments,
	// so that things like -venerable can be stripped from renamed apps
	if entityType == "apps" {
		for _, suffix := range c.config.StripAppSuffixes {
			if strings.HasSuffix(nv.Name, suffix) {
				nv.Name = nv.Name[:len(nv.Name)-len(suffix)]
				break
			}
		}
	}

	// Set TTL to value between 75% and 125% of desired amount. This is to spread out cache invalidations
	nv.TTL = time.Now().Add(time.Duration(float64(c.config.CacheInvalidateTTL.Nanoseconds()) * (0.75 + (rand.Float64() / 2.0))))

	// Write to DB
	return c.store.Set(makeCacheStorageKey(entityType, uuid), nv)
}
338517
func NewDB(conn *sql.DB) (*DB, error) {
	db, err := gorm.Open(DBDriver, conn)
	if err != nil {
		return nil, err
	}

	m := migrate.NewPostgresMigrator(conn)
	// Run all migrations in a single transaction, so they will be rolled
	// back as one. This is almost always the behavior that users would want
	// when upgrading Empire. If a new release has multiple migrations, and
	// one of those fails, it's easier for them if the entire upgrade rolls
	// back instead of getting stuck in failed state.
	m.TransactionMode = migrate.SingleTransaction

	return &DB{
		DB:       &db,
		migrator: m,
	}, nil
}
1351374
func newVersionedDB(db *leveldbhelper.DBHandle, dbName string) *versionedDB {
	return &versionedDB{db, dbName}
}
213043
func newPersistentDB(path string) (*DB, error) {
	opts := &opt.Options{OpenFilesCacheCapacity: 5}
	db, err := leveldb.OpenFile(path, opts)
	if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
		db, err = leveldb.RecoverFile(path, nil)
	}
	if err != nil {
		return nil, err
	}
	// The nodes contained in the cache correspond to a certain protocol version.
	// Flush all nodes if the version doesn't match.
	currentVer := make([]byte, binary.MaxVarintLen64)
	currentVer = currentVer[:binary.PutVarint(currentVer, int64(dbVersion))]

	blob, err := db.Get([]byte(dbVersionKey), nil)
	switch err {
	case leveldb.ErrNotFound:
		// Version not found (i.e. empty cache), insert it
		if err := db.Put([]byte(dbVersionKey), currentVer, nil); err != nil {
			db.Close()
			return nil, err
		}

	case nil:
		// Version present, flush if different
		if !bytes.Equal(blob, currentVer) {
			db.Close()
			if err = os.RemoveAll(path); err != nil {
				return nil, err
			}
			return newPersistentDB(path)
		}
	}
	return &DB{lvl: db, quit: make(chan struct{})}, nil
}
602707
func NewCloneDatabaseValue(db *Database) *CreateDatabaseValue {
	return &CreateDatabaseValue{
		DatabaseName:    db.Remark.DBConf.Common.DatabaseName,
		DatabaseVersion: db.Remark.DBConf.Common.DatabaseVersion,
		SourceAppliance: NewResource(db.ID),
	}
}
327798
func MakeChainDatabase(ctx *cli.Context, stack *node.Node) ethdb.Database {
	var (
		cache   = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100
		handles = makeDatabaseHandles()
	)
	name := "chaindata"
	if ctx.GlobalString(SyncModeFlag.Name) == "light" {
		name = "lightchaindata"
	}
	chainDb, err := stack.OpenDatabase(name, cache, handles, "")
	if err != nil {
		Fatalf("Could not open database: %v", err)
	}
	return chainDb
}
192970
func newMemoryDB() (*DB, error) {
	db, err := leveldb.Open(storage.NewMemStorage(), nil)
	if err != nil {
		return nil, err
	}
	return &DB{lvl: db, quit: make(chan struct{})}, nil
}
1221764
func newMemoryNodeDB(self NodeID) (*nodeDB, error) {
	db, err := leveldb.Open(storage.NewMemStorage(), nil)
	if err != nil {
		return nil, err
	}
	return &nodeDB{
		lvl:  db,
		self: self,
		quit: make(chan struct{}),
	}, nil
}
100006
func (s *persistentSequencer) initDB() error {
	return s.db.Update(func(tx *bbolt.Tx) error {
		_, err := tx.CreateBucketIfNotExists(nextPaymentIDKey)
		return err
	})
}
456141
func NewDB(dbType, dbpath string, debugSQL bool) (DB, bool, error) {
	switch dbType {
	case dialectSqlite3, dialectMysql, dialectPostgreSQL:
		return NewRDB(dbType, dbpath, debugSQL)
	case dialectRedis:
		return NewRedis(dbType, dbpath, debugSQL)
	}
	return nil, false, fmt.Errorf("Invalid database dialect, %s", dbType)
}
1100394
func NewDB() *DB {
	return &DB{
		UserDB: UserDB{
			ids:    make(map[string]*models.User),
			emails: make(map[string]*models.User),
		},
	}
}
755198
func (c *Client) CreateDB(db string) error {
	u := *c.url
	q := u.Query()
	q.Del("db")
	u.Path = "/query"
	u.RawQuery = q.Encode()

	r, err := c.http.Post(u.String(), "application/x-www-form-urlencoded", strings.NewReader(
		fmt.Sprintf("q=CREATE DATABASE %q", db),
	))
	if err != nil {
		return err
	}
	return readResponse(r)
}
649949
func NewRelocationBatchArgBase(Entries []*RelocationPath) *RelocationBatchArgBase {
	s := new(RelocationBatchArgBase)
	s.Entries = Entries
	s.Autorename = false
	return s
}