Esempio n. 1
0
func gdbmOpen(file string) (*gdbm, error) {
	cstr := C.CString(file)
	defer C.free(unsafe.Pointer(cstr))
	dbf := C.gdbm_open(cstr, 0, C.GDBM_WRCREAT, 0644, nil)
	if dbf == nil {
		return nil, err()
	}
	return &gdbm{
		dbf: dbf,
	}, nil
}
Esempio n. 2
0
// Opens a database using the given configuration options.
func OpenConfig(config *Config) (*Database, error) {
	cname := C.CString(config.File)
	defer C.free(unsafe.Pointer(cname))
	mode := C.int(config.Mode)
	if config.Sync {
		mode |= C.GDBM_SYNC
	}
	if config.DontLock {
		mode |= C.GDBM_NOLOCK
	}
	dbf, err := C.gdbm_open(cname, C.int(config.BlockSize), mode, C.int(config.Perm), nil)
	if dbf != nil {
		if config.CacheSize != 0 {
			val := C.int(config.CacheSize)
			C.gdbm_setopt(dbf, C.GDBM_CACHESIZE, &val, C.int(unsafe.Sizeof(val)))
		}
		return &Database{config.File, dbf}, nil
	}
	return nil, err
}
Esempio n. 3
0
// More complex database initialization function that takes in a `DatabaseCfg`
// struct to allow more fine-grained control over database settings.
func OpenWithCfg(filename string, cfg DatabaseCfg) (db *Database, err error) {
	db = new(Database)

	// Convert a human-readable mode string into a libgdbm-usable constant.
	switch cfg.Mode {
	case "r":
		db.mode = C.GDBM_READER
	case "w":
		db.mode = C.GDBM_WRITER
	case "c":
		db.mode = C.GDBM_WRCREAT
	case "n":
		db.mode = C.GDBM_NEWDB
	}

	cs := C.CString(filename)
	defer C.free(unsafe.Pointer(cs))
	db.dbf = C.gdbm_open(cs, C.int(cfg.BlockSize), C.int(db.mode), C.int(cfg.Permissions), nil)
	if db.dbf == nil {
		err = lastError()
	}
	return db, err
}