ndbm

package
v0.0.0-...-a7c1724 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2026 License: BSD-3-Clause, ISC Imports: 3 Imported by: 0

Documentation

Overview

Package ndbm implements the ndbm(3) interface of Berkeley DB 1.85 on top of a hash database, file compatible with dbm_open(3).

Example
package main

import (
	"fmt"
	"log"
	"os"
	"path/filepath"

	"github.com/dim13/db"
	"github.com/dim13/db/ndbm"
)

func main() {
	name := filepath.Join(os.TempDir(), "example") // creates example.db
	defer os.Remove(name + ndbm.Suffix)

	d, err := ndbm.Open(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
	if err != nil {
		log.Fatal(err)
	}
	defer d.Close()

	if err := d.Store([]byte("key"), []byte("content"), db.DBMReplace); err != nil {
		log.Fatal(err)
	}
	v, err := d.Fetch([]byte("key"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%s\n", v)
}
Output:
content

Index

Examples

Constants

View Source
const Suffix = ".db"

Suffix is appended to the name given to Open, as in C.

Variables

This section is empty.

Functions

This section is empty.

Types

type DBM

type DBM struct {
	// contains filtered or unexported fields
}

DBM is an open ndbm database, safe for concurrent use. FirstKey and NextKey share a single cursor.

func Open

func Open(name string, flag int, perm os.FileMode) (*DBM, error)

Open opens or creates name with Suffix appended. The flag and perm are those of os.OpenFile; a flag without os.O_RDWR opens read-only. The caller must Close the database to write changes.

func (*DBM) Close

func (d *DBM) Close() error

Close writes changes and closes the file.

func (*DBM) Delete

func (d *DBM) Delete(key []byte) error

Delete deletes key, or returns db.ErrNotFound.

func (*DBM) DirFd

func (d *DBM) DirFd() uintptr

DirFd returns the file descriptor of the database file, as dbm_dirfno(3).

func (*DBM) Fetch

func (d *DBM) Fetch(key []byte) ([]byte, error)

Fetch returns the content stored under key, or db.ErrNotFound.

func (*DBM) FirstKey

func (d *DBM) FirstKey() ([]byte, error)

FirstKey returns the first key in hash order, or db.ErrNotFound if the database is empty.

func (*DBM) NextKey

func (d *DBM) NextKey() ([]byte, error)

NextKey returns the key after the one returned last, or db.ErrNotFound at the end.

func (*DBM) Store

func (d *DBM) Store(key, content []byte, mode db.Mode) error

Store stores content under key. With db.DBMInsert an existing key is kept and db.ErrKeyExist returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL