newdb: github.com/anydemo/newdb Index | Files | Directories

package newdb

import "github.com/anydemo/newdb"

Index

Package Files

db.go encode_decode.go operation.go page.go tuple.go tx.go utils.go

Variables

var (
    // DB singleton db
    DB  = NewDatabase()

    // DefaultPageSize the os dependency pagesize
    DefaultPageSize = os.Getpagesize()
    // DefaultPageNum default page num
    DefaultPageNum = 50
)
var (

    // IntType enum of Type int
    IntType = &Type{Name: reflect.TypeOf(int64(0)).Name(), Len: Sizeof(int64(0))}
    // StringType enum of Type string
    StringType = &Type{Name: reflect.TypeOf("").Name(), Len: 16}
)
var (
    // DefaultOrder default binary.LittleEndian
    DefaultOrder = binary.LittleEndian
)

func HeapPageCreateEmptyPageData Uses

func HeapPageCreateEmptyPageData() []byte

HeapPageCreateEmptyPageData create emptyPageDate

func Int64ToRaw Uses

func Int64ToRaw(num int64) ([]byte, error)

Int64ToRaw int64 to byte

func ParseInt64 Uses

func ParseInt64(raw []byte) (int64, error)

ParseInt64 unmarshalint64

func PutInt64 Uses

func PutInt64(buf []byte, num int64) error

PutInt64 marshal int64

func RandString Uses

func RandString(length int) string

RandString rand string with len

func Seed Uses

func Seed(seed int64)

Seed defaultRand.Seed

func Sizeof Uses

func Sizeof(t interface{}) uintptr

Sizeof t's sizeof

type BufferPool Uses

type BufferPool struct {

    // PageID2Page k is PageID.ID()
    PageID2Page map[string]Page
    // contains filtered or unexported fields
}

BufferPool BufferPool manages the reading and writing of pages into memory from disk. Access methods call into it to retrieve pages, and it fetches pages from the appropriate location. <p> The BufferPool is also responsible for locking; when a transaction fetches a page, BufferPool checks that the transaction has the appropriate locks to read/write the page.

@Threadsafe, all fields are final

func NewBufferPool Uses

func NewBufferPool(size int) *BufferPool

NewBufferPool return BufferPool

func (*BufferPool) GetPage Uses

func (bp *BufferPool) GetPage(tx *TxID, pid PageID, perm Permission) (ret Page, err error)

GetPage Retrieve the specified page with the associated permissions. Will acquire a lock and may block if that lock is held by another transaction. <p> The retrieved page should be looked up in the buffer pool. If it is present, it should be returned. If it is not present, it should be added to the buffer pool and returned. If there is insufficient space in the buffer pool, a page should be evicted and the new page should be added in its place.

func (*BufferPool) InsertTuple Uses

func (bp *BufferPool) InsertTuple(txID *TxID, tableID string, tuple *Tuple) error

InsertTuple insert tuple to page

func (BufferPool) PageSize Uses

func (bp BufferPool) PageSize() int

PageSize get the os dependencied page size

type Catalog Uses

type Catalog struct {
    TableID2DBFile map[string]DBFile
    Name2ID        map[string]string
}

Catalog The Catalog keeps track of all available tables in the database and their associated schemas.

func NewCatalog Uses

func NewCatalog() *Catalog

NewCatalog new Catalog

func (Catalog) AddTable Uses

func (c Catalog) AddTable(file DBFile, name string)

AddTable add DBFile/Table

func (Catalog) GetTableByID Uses

func (c Catalog) GetTableByID(tableID string) DBFile

GetTableByID get DBFile/Table by tableID

func (Catalog) GetTableByName Uses

func (c Catalog) GetTableByName(name string) DBFile

GetTableByName get DBFile/Table by name

func (*Catalog) LoadSchema Uses

func (c *Catalog) LoadSchema(r io.Reader) (ret []string, err error)

LoadSchema load Catalog from file, and return slice of TableID

type CatalogSchema Uses

type CatalogSchema struct {
    Filename  string            `json:"filename,omitempty"`
    TD        []CatalogTDSchema `json:"td,omitempty"`
    TableName string            `json:"table_name,omitempty"`
}

CatalogSchema for load Catalog from file

type CatalogTDSchema Uses

type CatalogTDSchema struct {
    Name string `json:"name,omitempty"`
    Type string `json:"type,omitempty"`
}

CatalogTDSchema for CatalogSchema

type DBFile Uses

type DBFile interface {
    ID() string

    ReadPage(pid PageID) (Page, error)
    WritePage(p Page) error

    InsertTuple(*TxID, *Tuple) ([]Page, error)
    DeleteTuple(*TxID, *Tuple) ([]Page, error)
    TupleDesc() *TupleDesc
    Iterator(*TxID) DbFileIterator
}

DBFile The interface for database files on disk. Each table is represented by a single DBFile. DbFiles can fetch pages and iterate through tuples. Each file has a unique id used to store metadata about the table in the Catalog. DbFiles are generally accessed through the buffer pool, rather than directly by operators.

type Database Uses

type Database struct {
    Catalog    *Catalog
    BufferPool *BufferPool
}

Database singleton struct

func NewDatabase Uses

func NewDatabase() *Database

NewDatabase return new

func (*Database) B Uses

func (db *Database) B() *BufferPool

B get BufferPool

func (*Database) C Uses

func (db *Database) C() *Catalog

C get Catalog

type DbFileIterator Uses

type DbFileIterator interface {
    Iterator
}

DbFileIterator DbFileIterator is the iterator interface that all newDB Dbfile should implement.

type Field Uses

type Field interface {
    fmt.Stringer
    encoding.BinaryMarshaler
    encoding.BinaryUnmarshaler
    Type() *Type
    Compare(Op, Field) bool
}

Field identify one filed like int 1

func NewIntField Uses

func NewIntField(val int64) Field

NewIntField constructor of IntField

type Filter Uses

type Filter struct {
    Child OpIterator
    Pred  *Predicate

    Err error
    // contains filtered or unexported fields
}

Filter is an operator that implements a relational projection.

func NewFilter Uses

func NewFilter(predicate *Predicate, child OpIterator) *Filter

NewFilter create new filter

func (*Filter) Close Uses

func (f *Filter) Close()

Close close iterator

func (*Filter) Error Uses

func (f *Filter) Error() error

func (*Filter) HasNext Uses

func (f *Filter) HasNext() (ret bool)

HasNext if has next elem

func (*Filter) Next Uses

func (f *Filter) Next() (ret *Tuple)

Next next tuple

func (*Filter) Open Uses

func (f *Filter) Open() error

Open open iterator see #OpIterator

func (*Filter) Rewind Uses

func (f *Filter) Rewind() error

Rewind restart the iterator

func (Filter) TupleDesc Uses

func (f Filter) TupleDesc() *TupleDesc

TupleDesc returns the TupleDesc associated with this OpIterator.

type HeapFile Uses

type HeapFile struct {
    File *os.File
    TD   *TupleDesc
}

HeapFile HeapFile

file format:

[Page][Page][Page][Page]...

func NewHeapFile Uses

func NewHeapFile(file *os.File, td *TupleDesc) *HeapFile

NewHeapFile new HeapFile

func (*HeapFile) DeleteTuple Uses

func (hf *HeapFile) DeleteTuple(*TxID, *Tuple) ([]Page, error)

DeleteTuple del tuple to the HeapPage

func (HeapFile) ID Uses

func (hf HeapFile) ID() string

ID string

func (*HeapFile) InsertTuple Uses

func (hf *HeapFile) InsertTuple(txID *TxID, tuple *Tuple) (ret []Page, err error)

InsertTuple insert tuple to the HeapPage

func (*HeapFile) Iterator Uses

func (hf *HeapFile) Iterator(txID *TxID) DbFileIterator

Iterator DbFileIterator

func (HeapFile) NumPagesInFile Uses

func (hf HeapFile) NumPagesInFile() int64

NumPagesInFile get real num pages in file

func (HeapFile) ReadPage Uses

func (hf HeapFile) ReadPage(pid PageID) (Page, error)

ReadPage read one page

func (HeapFile) TupleDesc Uses

func (hf HeapFile) TupleDesc() *TupleDesc

TupleDesc return TupleDesc

func (*HeapFile) WritePage Uses

func (hf *HeapFile) WritePage(page Page) error

WritePage write one page

type HeapPage Uses

type HeapPage struct {
    PID         *HeapPageID
    TD          *TupleDesc
    Head        []byte
    Tuples      []*Tuple
    TxMarkDirty *TxID
}

HeapPage heap page

file format:

| header bit set | [Tuple][Tuple][Tuple][Tuple] |

func NewHeapPage Uses

func NewHeapPage(pid *HeapPageID, data []byte) (*HeapPage, error)

NewHeapPage new HeapPage

func (*HeapPage) Bitset Uses

func (hp *HeapPage) Bitset() bitset.BitSet

Bitset get bitset

func (HeapPage) EmptyTupleNum Uses

func (hp HeapPage) EmptyTupleNum() (ret int)

EmptyTupleNum num of empty tuple

func (HeapPage) HeaderSize Uses

func (hp HeapPage) HeaderSize() int

HeaderSize computes the number of bytes in the header of a page in a HeapFile with each tuple occupying tupleSize bytes

func (*HeapPage) InsertTuple Uses

func (hp *HeapPage) InsertTuple(tuple *Tuple) error

InsertTuple insert one tuple one the page

func (HeapPage) IsDirty Uses

func (hp HeapPage) IsDirty() *TxID

IsDirty if return *TxID != nil, is dirty

func (*HeapPage) Iterator Uses

func (hp *HeapPage) Iterator(txID *TxID) DbFileIterator

Iterator iterator

func (*HeapPage) MarkDirty Uses

func (hp *HeapPage) MarkDirty(txID *TxID)

MarkDirty mark the page dirty if TxID is nil, Mark not dirty

func (HeapPage) MarshalBinary Uses

func (hp HeapPage) MarshalBinary() (data []byte, err error)

MarshalBinary implement encoding.BinaryMarshaler

func (HeapPage) NumOfTuples Uses

func (hp HeapPage) NumOfTuples() int

NumOfTuples retrieve the number of tuples on this page.

func (HeapPage) PageID Uses

func (hp HeapPage) PageID() PageID

PageID get pageID

func (HeapPage) TupleDesc Uses

func (hp HeapPage) TupleDesc() *TupleDesc

TupleDesc get the tuple desc

type HeapPageDbFileIterator Uses

type HeapPageDbFileIterator struct {
    Err error
    // contains filtered or unexported fields
}

HeapPageDbFileIterator HeapPage HeapPageDbFileIterator

func NewHeapPageDbFileIterator Uses

func NewHeapPageDbFileIterator(txID *TxID, hf *HeapFile) *HeapPageDbFileIterator

NewHeapPageDbFileIterator new HeapPageDbFileIterator with txID

func (*HeapPageDbFileIterator) Close Uses

func (it *HeapPageDbFileIterator) Close()

Close close

func (HeapPageDbFileIterator) Error Uses

func (it HeapPageDbFileIterator) Error() error

Error return err

func (*HeapPageDbFileIterator) HasNext Uses

func (it *HeapPageDbFileIterator) HasNext() bool

HasNext has next

func (*HeapPageDbFileIterator) Next Uses

func (it *HeapPageDbFileIterator) Next() (ret *Tuple)

Next next

func (*HeapPageDbFileIterator) Open Uses

func (it *HeapPageDbFileIterator) Open() error

Open open the iterator

func (*HeapPageDbFileIterator) Rewind Uses

func (it *HeapPageDbFileIterator) Rewind() error

Rewind rewind the iterator

type HeapPageID Uses

type HeapPageID struct {
    // TID TableID
    TID string
    // PNum PageNum
    PNum int
}

HeapPageID HeapPageID

func NewHeapPageID Uses

func NewHeapPageID(tID string, pn int) *HeapPageID

NewHeapPageID new HeapPageID

func (HeapPageID) ID Uses

func (hid HeapPageID) ID() string

ID ${TableID}-${PageNum} identify the PageID

func (HeapPageID) PageNum Uses

func (hid HeapPageID) PageNum() int

PageNum page num

func (HeapPageID) TableID Uses

func (hid HeapPageID) TableID() string

TableID table ID

type IntField Uses

type IntField struct {
    Val      int64
    TypeReal *Type
}

IntField int filed

func (IntField) Compare Uses

func (i IntField) Compare(op Op, val Field) (ret bool)

Compare Compare the specified field to the value of this Field. Return semantics are as specified by Field.compare

func (IntField) MarshalBinary Uses

func (i IntField) MarshalBinary() (data []byte, err error)

MarshalBinary implement encoding.BinaryMarshaler

func (IntField) String Uses

func (i IntField) String() string

String the readable IntField

func (IntField) Type Uses

func (i IntField) Type() *Type

Type the type of int

func (*IntField) UnmarshalBinary Uses

func (i *IntField) UnmarshalBinary(data []byte) error

UnmarshalBinary implement encoding.BinaryUnmarshaler

type Iterator Uses

type Iterator interface {
    // Open opens the iterator. This must be called before any of the other methods.
    Open() error
    // Close Closes the iterator. When the iterator is closed, calling next(), hasNext(), or rewind() should return error
    Close()
    // HasNext Returns true if the iterator has more tuples.
    HasNext() bool
    // Next Returns the next tuple from the operator (typically implementing by reading
    // from a child operator or an access method).
    Next() *Tuple
    // Rewind Resets the iterator to the start.
    Rewind() error
    // Error return err
    Error() error
}

Iterator iterator

type Op Uses

type Op int

Op enum of Op

const (
    // OpEquals ==
    OpEquals Op  = iota
    // OpGreaterThan >
    OpGreaterThan
    // OpLessThan <
    OpLessThan
    // OpGreaterThanOrEq >=
    OpGreaterThanOrEq
    // OpLessThanOrEq <=
    OpLessThanOrEq
    // OpLike LIKE
    OpLike
    // OpNotEquals !=
    OpNotEquals
)

func (Op) String Uses

func (op Op) String() (ret string)

type OpIterator Uses

type OpIterator interface {
    Iterator
    // TupleDesc Returns the TupleDesc associated with this OpIterator.
    TupleDesc() *TupleDesc
}

OpIterator operation iterator interface

type Page Uses

type Page interface {
    encoding.BinaryMarshaler
    // PageID get the PageID
    PageID() PageID
    // MarkDirty mark the page dirty
    // if TxID is nil, Mark not dirty
    MarkDirty(*TxID)
    TupleDesc() *TupleDesc
}

Page page

type PageID Uses

type PageID interface {
    ID() string
    PageNum() int
    TableID() string
}

PageID page id

type Permission Uses

type Permission int

Permission perm

const (
    // PermReadOnly 0
    PermReadOnly Permission = iota
    // PermReadWrite 1
    PermReadWrite
)

func (Permission) String Uses

func (p Permission) String() (ret string)

type Predicate Uses

type Predicate struct {
    Field   int
    Op      Op
    Operand Field
}

Predicate compares tuples to a specified Field value

func (Predicate) Filter Uses

func (p Predicate) Filter(tuple *Tuple) bool

Filter compares the field number of t specified in the constructor to the operand field specified in the constructor using the operator specific in the constructor. The comparison can be made through Field's compare method.

func (Predicate) String Uses

func (p Predicate) String() string

type RecordID Uses

type RecordID struct {
    PID      PageID
    TupleNum int
}

RecordID record id: PageID + TupleNum

func NewRecordID Uses

func NewRecordID(pid PageID, tupleNum int) *RecordID

NewRecordID NewRecordID Pointer

type SeqScan Uses

type SeqScan struct {
    TxID       *TxID
    TableID    string
    TableAlias string
    DBFile     DBFile
    Iter       DbFileIterator

    Err error
}

SeqScan sequence scan

func NewSeqScan Uses

func NewSeqScan(txID *TxID, tableID string, tableAlias string) *SeqScan

NewSeqScan new SeqScan

func (*SeqScan) Close Uses

func (s *SeqScan) Close()

Close close

func (SeqScan) Error Uses

func (s SeqScan) Error() error

Error return error

func (*SeqScan) HasNext Uses

func (s *SeqScan) HasNext() bool

HasNext hasNext

func (*SeqScan) Next Uses

func (s *SeqScan) Next() *Tuple

Next next tuple

func (*SeqScan) Open Uses

func (s *SeqScan) Open() error

Open open

func (*SeqScan) Rewind Uses

func (s *SeqScan) Rewind() error

Rewind rewind the iterator

func (SeqScan) TupleDesc Uses

func (s SeqScan) TupleDesc() *TupleDesc

TupleDesc TupleDesc

type TdItem Uses

type TdItem struct {
    Type *Type
    Name string
}

TdItem tuple desc item

func (TdItem) String Uses

func (ti TdItem) String() string

type Tuple Uses

type Tuple struct {
    Fields   []Field
    TD       *TupleDesc
    RecordID *RecordID
}

Tuple one record

Marshal format field-val1 | field-val2 |...

func (Tuple) MarshalBinary Uses

func (tp Tuple) MarshalBinary() (data []byte, err error)

MarshalBinary marshal tuple

func (Tuple) String Uses

func (tp Tuple) String() string

type TupleDesc Uses

type TupleDesc struct {
    TdItems []TdItem
}

TupleDesc the tuple descrition

func NewTupleDesc Uses

func NewTupleDesc(types []*Type, names []string) *TupleDesc

NewTupleDesc create one TupleDesc

func (TupleDesc) Equal Uses

func (td TupleDesc) Equal(target *TupleDesc) (ret bool)

Equal 2 tuple desc is equal

func (TupleDesc) Size Uses

func (td TupleDesc) Size() int

Size get size of fields

func (TupleDesc) String Uses

func (td TupleDesc) String() string

type TupleIterator Uses

type TupleIterator struct {
    Tuples []*Tuple

    TD  *TupleDesc
    Err error
    // contains filtered or unexported fields
}

TupleIterator Implements a OpIterator

func NewTupleIterator Uses

func NewTupleIterator(td *TupleDesc, tuples []*Tuple) *TupleIterator

NewTupleIterator new TupleIterator

func (*TupleIterator) Close Uses

func (it *TupleIterator) Close()

Close close

func (TupleIterator) Error Uses

func (it TupleIterator) Error() error

Error return error

func (*TupleIterator) HasNext Uses

func (it *TupleIterator) HasNext() bool

HasNext hasNext

func (*TupleIterator) Next Uses

func (it *TupleIterator) Next() (ret *Tuple)

Next next tuple; before call Next, should call HasNext

func (*TupleIterator) Open Uses

func (it *TupleIterator) Open() error

Open open

func (*TupleIterator) Rewind Uses

func (it *TupleIterator) Rewind() error

Rewind rewind the iterator

func (TupleIterator) TupleDesc Uses

func (it TupleIterator) TupleDesc() *TupleDesc

TupleDesc TupleDesc

type Tx Uses

type Tx struct {
    TxID *TxID
}

Tx transaction

func NewTx Uses

func NewTx() *Tx

NewTx new Tx with NewTxID

func (*Tx) Finish Uses

func (tx *Tx) Finish()

Finish clean Tx

type TxID Uses

type TxID struct {
    ID uint64
}

TxID transaction id

func NewTxID Uses

func NewTxID() *TxID

NewTxID new one *TxID

type Type Uses

type Type struct {
    Name string
    Len  uintptr
}

Type type of fields

func (Type) Parse Uses

func (t Type) Parse(r io.Reader) (Field, error)

Parse parse the real Field

func (Type) String Uses

func (t Type) String() string

Directories

PathSynopsis
pkg/bitset

Package newdb imports 16 packages (graph). Updated 2019-08-16. Refresh now. Tools for package owners.