store

package
v0.0.0-...-d1d5330 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2020 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	KEY_LEN    = 8         // float64
	VALUE_LEN  = 8         //  uint64
	RECORD_LEN = int64(16) // KEY_LEN + VALUE_LEN
	HEADER     = 8
)

Variables

This section is empty.

Functions

func FromRecord

func FromRecord(r Record) (key float64, value uint64)

Types

type Record

type Record []byte

Record is a key/value paire

func ToRecord

func ToRecord(key float64, value uint64) Record

func (Record) Key

func (r Record) Key() float64

func (Record) Value

func (r Record) Value() uint64

type Store

type Store struct {
	*os.File
}

Store is a os.File where we store key value paires

Example
tmpDir := os.TempDir()
f, _ := ioutil.TempFile(tmpDir, "*")
store := Store{f}

store.Put(ToRecord(1.99, 0))
store.Put(ToRecord(2.08, 1))
store.Put(ToRecord(2.08, 2))
store.Put(ToRecord(2.33, 3))

fmt.Println("count:", store.RecordCount())
fmt.Println(FromRecord(store.Get(3)))
fmt.Println(FromRecord(store.Get(2)))
fmt.Println(FromRecord(store.Get(1)))
fmt.Println(FromRecord(store.Get(0)))
Output:

count: 4
2.33 3
2.08 2
2.08 1
1.99 0

func (Store) Get

func (s Store) Get(i int64) Record

Get reads the store file at offset i and return a Record byte array

func (Store) Put

func (s Store) Put(r Record)

Put appends a Record to the store file

func (Store) RecordCount

func (s Store) RecordCount() int64

RecordCount reads the first byte as

Jump to

Keyboard shortcuts

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