linedb

package
v0.0.0-...-92360a8 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package linedb provides an OO-like interface to operate on simple line-record databases backed by a plain text file.

The rules are as follows:

  • One file, one database;
  • One line, one record;
  • An empty (0 bytes) database file contains exactly zero records;
  • In all other cases, an empty line is a valid record;
  • No assumptions are made about the inner structure of each record, even in terms of white space characters;
  • The only special character is the record separator, '\n'.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Database

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

Database represents a handle to a given backing file.

func Open

func Open(filePath string) (*Database, error)

Open creates a handle to the given backing file.

func (*Database) All

func (db *Database) All() []Rec

All returns all the records with their respective number

func (*Database) Delete

func (db *Database) Delete(number int) (string, error)

Delete removes the number-th record from the database.

func (*Database) Insert

func (db *Database) Insert(number int, record string) error

Insert creates a new record at position n, pushing the n-th record and all subsequent records one position forward, i.e, Insert(1, ...) would create a new record at the beginning of the file.

Special case: Insert(0, ...) places the new record at the end of the file.

func (*Database) Length

func (db *Database) Length() int

Length returns the number of records in the database.

func (*Database) Record

func (db *Database) Record(number int) (string, error)

Record returns the number-th record in the database. Like lines in files, records are 1-indexed.

func (*Database) Select

func (db *Database) Select(filter func(Rec) bool) []Rec

Select returns the records which cause the filter function to return true.

func (*Database) Update

func (db *Database) Update(number int, record string) (string, error)

Update replaces the text of the number-th record, and returns the old text.

type Rec

type Rec struct {
	Number int
	Text   string
}

Rec pairs the text of a record with its line number in the backing file.

Jump to

Keyboard shortcuts

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