Documentation
¶
Overview ¶
Package jsonwall provides an interface to work with database files in the simple "jsonwall" format, which consists of a text file with one JSON object per line, where both the individual JSON fields and the lines themselves are sorted to optimize for searching and iteration.
For example, the following content represents a valid jsonwall database:
{"jsonwall":"1.0","count":3} {"kind":"app","name":"chisel","version":"1.0"} {"kind":"app","name":"pebble","version":"1.2"}
The entries in this database might be manipulated with a type such as:
type AppEntry struct { Kind string `json:"kind"` Name string `json:"name,omitempty"` Version string `json:"version,omitempty"` }
Such data types have two important characteristics: fields must be defined in the order that will be used when searching, and every optional field must be tagged with `omitempty`.
With that in place, the database may be accessed as:
app := AppEntry{Kind: "app", Name: "chisel"} if db.Get(&app) == nil { fmt.Println(app.Name, "version:", app.Version) }
Iteration works similarly:
app := AppEntry{Kind: "app"} if iter, err := db.Iter(&app); err == nil { for iter.Next() { if iter.Get(&app) == nil { fmt.Println(app.Name, "version:", app.Version) } } }
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = fmt.Errorf("value not found in database")
Functions ¶
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB holds an in-memory read-only database ready for querying.
func (*DB) Get ¶
Get encodes the provided value as JSON, finds the first entry in the database with initial fields exactly matching that encoding, and then decodes the entry back into the provided value.
func (*DB) Iterate ¶
Iterate encodes the provided value as JSON and returns an iterator that will go over every entry in the database with initial fields that exactly match that encoding. An iterator is still returned even if no entries match the provided value.
func (*DB) IteratePrefix ¶
IteratePrefix works similarly to Iterate, except that after encoding the provided value as JSON, its last encoded field must be a string that will be matched as a prefix of the respective database entry field, instead of being matched exactly.
type DBWriter ¶
type DBWriter struct {
// contains filtered or unexported fields
}
DBWriter holds in memory the state of a database while it's being prepared for serialization and implements the WriterTo interface for assembling it.
func NewDBWriter ¶
func NewDBWriter(options *DBWriterOptions) *DBWriter
NewDBWriter returns a database writer that can assemble new databases.
type DBWriterOptions ¶
type DBWriterOptions struct { // Schema is included in the database header to help the decoding // process. The value is made available when reading, and is not // internally interpreted. Schema string }