newdb

package module
v0.0.0-...-0b5b9fe Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2019 License: Apache-2.0 Imports: 16 Imported by: 0

README

newdb Build Status GoDoc Widget CII Best Practices

new db write by golang. Support mysql TIBD sql

Getting Started

Prerequisites

see go.mod

Installing

Makefile will tell you how to install it and use it

run one demo

make help
make demo

Running the tests

make test-verbose test-race test-coverage test

Built With

  • go mod - Dependency Management

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

Authors

  • Exfly - Initial work - Exfly

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Acknowledgments

references

Documentation

Index

Constants

This section is empty.

Variables

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

	// DefaultPageSize the os dependency pagesize
	DefaultPageSize = os.Getpagesize()
	// DefaultPageNum default page num
	DefaultPageNum = 50
)
View Source
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}
)
View Source
var (
	// DefaultOrder default binary.LittleEndian
	DefaultOrder = binary.LittleEndian
)

Functions

func HeapPageCreateEmptyPageData

func HeapPageCreateEmptyPageData() []byte

HeapPageCreateEmptyPageData create emptyPageDate

func Int64ToRaw

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

Int64ToRaw int64 to byte

func ParseInt64

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

ParseInt64 unmarshalint64

func PutInt64

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

PutInt64 marshal int64

func RandString

func RandString(length int) string

RandString rand string with len

func Seed

func Seed(seed int64)

Seed defaultRand.Seed

func Sizeof

func Sizeof(t interface{}) uintptr

Sizeof t's sizeof

Types

type BufferPool

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

func NewBufferPool(size int) *BufferPool

NewBufferPool return BufferPool

func (*BufferPool) GetPage

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

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

InsertTuple insert tuple to page

func (BufferPool) PageSize

func (bp BufferPool) PageSize() int

PageSize get the os dependencied page size

type Catalog

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

func NewCatalog() *Catalog

NewCatalog new Catalog

func (Catalog) AddTable

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

AddTable add DBFile/Table

func (Catalog) GetTableByID

func (c Catalog) GetTableByID(tableID string) DBFile

GetTableByID get DBFile/Table by tableID

func (Catalog) GetTableByName

func (c Catalog) GetTableByName(name string) DBFile

GetTableByName get DBFile/Table by name

func (*Catalog) LoadSchema

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

LoadSchema load Catalog from file, and return slice of TableID

type CatalogSchema

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

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

CatalogTDSchema for CatalogSchema

type DBFile

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

type Database struct {
	Catalog    *Catalog
	BufferPool *BufferPool
}

Database singleton struct

func NewDatabase

func NewDatabase() *Database

NewDatabase return new

func (*Database) B

func (db *Database) B() *BufferPool

B get BufferPool

func (*Database) C

func (db *Database) C() *Catalog

C get Catalog

type DbFileIterator

type DbFileIterator interface {
	Iterator
}

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

type Field

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

Field identify one filed like int 1

func NewIntField

func NewIntField(val int64) Field

NewIntField constructor of IntField

type Filter

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

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

NewFilter create new filter

func (*Filter) Close

func (f *Filter) Close()

Close close iterator

func (*Filter) Error

func (f *Filter) Error() error

func (*Filter) HasNext

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

HasNext if has next elem

func (*Filter) Next

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

Next next tuple

func (*Filter) Open

func (f *Filter) Open() error

Open open iterator see #OpIterator

func (*Filter) Rewind

func (f *Filter) Rewind() error

Rewind restart the iterator

func (Filter) TupleDesc

func (f Filter) TupleDesc() *TupleDesc

TupleDesc returns the TupleDesc associated with this OpIterator.

type HeapFile

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

HeapFile HeapFile

file format:

PagePagePagePage...

func NewHeapFile

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

NewHeapFile new HeapFile

func (*HeapFile) DeleteTuple

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

DeleteTuple del tuple to the HeapPage

func (HeapFile) ID

func (hf HeapFile) ID() string

ID string

func (*HeapFile) InsertTuple

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

InsertTuple insert tuple to the HeapPage

func (*HeapFile) Iterator

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

Iterator DbFileIterator

func (HeapFile) NumPagesInFile

func (hf HeapFile) NumPagesInFile() int64

NumPagesInFile get real num pages in file

func (HeapFile) ReadPage

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

ReadPage read one page

func (HeapFile) TupleDesc

func (hf HeapFile) TupleDesc() *TupleDesc

TupleDesc return TupleDesc

func (*HeapFile) WritePage

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

WritePage write one page

type HeapPage

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

HeapPage heap page

file format:

| header bit set | TupleTupleTupleTuple |

func NewHeapPage

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

NewHeapPage new HeapPage

func (*HeapPage) Bitset

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

Bitset get bitset

func (HeapPage) EmptyTupleNum

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

EmptyTupleNum num of empty tuple

func (HeapPage) HeaderSize

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

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

InsertTuple insert one tuple one the page

func (HeapPage) IsDirty

func (hp HeapPage) IsDirty() *TxID

IsDirty if return *TxID != nil, is dirty

func (*HeapPage) Iterator

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

Iterator iterator

func (*HeapPage) MarkDirty

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

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

func (HeapPage) MarshalBinary

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

MarshalBinary implement encoding.BinaryMarshaler

func (HeapPage) NumOfTuples

func (hp HeapPage) NumOfTuples() int

NumOfTuples retrieve the number of tuples on this page.

func (HeapPage) PageID

func (hp HeapPage) PageID() PageID

PageID get pageID

func (HeapPage) TupleDesc

func (hp HeapPage) TupleDesc() *TupleDesc

TupleDesc get the tuple desc

type HeapPageDbFileIterator

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

HeapPageDbFileIterator HeapPage HeapPageDbFileIterator

func NewHeapPageDbFileIterator

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

NewHeapPageDbFileIterator new HeapPageDbFileIterator with txID

func (*HeapPageDbFileIterator) Close

func (it *HeapPageDbFileIterator) Close()

Close close

func (HeapPageDbFileIterator) Error

func (it HeapPageDbFileIterator) Error() error

Error return err

func (*HeapPageDbFileIterator) HasNext

func (it *HeapPageDbFileIterator) HasNext() bool

HasNext has next

func (*HeapPageDbFileIterator) Next

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

Next next

func (*HeapPageDbFileIterator) Open

func (it *HeapPageDbFileIterator) Open() error

Open open the iterator

func (*HeapPageDbFileIterator) Rewind

func (it *HeapPageDbFileIterator) Rewind() error

Rewind rewind the iterator

type HeapPageID

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

HeapPageID HeapPageID

func NewHeapPageID

func NewHeapPageID(tID string, pn int) *HeapPageID

NewHeapPageID new HeapPageID

func (HeapPageID) ID

func (hid HeapPageID) ID() string

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

func (HeapPageID) PageNum

func (hid HeapPageID) PageNum() int

PageNum page num

func (HeapPageID) TableID

func (hid HeapPageID) TableID() string

TableID table ID

type IntField

type IntField struct {
	Val      int64
	TypeReal *Type
}

IntField int filed

func (IntField) Compare

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

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

MarshalBinary implement encoding.BinaryMarshaler

func (IntField) String

func (i IntField) String() string

String the readable IntField

func (IntField) Type

func (i IntField) Type() *Type

Type the type of int

func (*IntField) UnmarshalBinary

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

UnmarshalBinary implement encoding.BinaryUnmarshaler

type Iterator

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

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

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

type OpIterator

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

OpIterator operation iterator interface

type Page

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

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

PageID page id

type Permission

type Permission int

Permission perm

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

func (Permission) String

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

type Predicate

type Predicate struct {
	Field   int
	Op      Op
	Operand Field
}

Predicate compares tuples to a specified Field value

func (Predicate) Filter

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

func (p Predicate) String() string

type RecordID

type RecordID struct {
	PID      PageID
	TupleNum int
}

RecordID record id: PageID + TupleNum

func NewRecordID

func NewRecordID(pid PageID, tupleNum int) *RecordID

NewRecordID NewRecordID Pointer

type SeqScan

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

	Err error
}

SeqScan sequence scan

func NewSeqScan

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

NewSeqScan new SeqScan

func (*SeqScan) Close

func (s *SeqScan) Close()

Close close

func (SeqScan) Error

func (s SeqScan) Error() error

Error return error

func (*SeqScan) HasNext

func (s *SeqScan) HasNext() bool

HasNext hasNext

func (*SeqScan) Next

func (s *SeqScan) Next() *Tuple

Next next tuple

func (*SeqScan) Open

func (s *SeqScan) Open() error

Open open

func (*SeqScan) Rewind

func (s *SeqScan) Rewind() error

Rewind rewind the iterator

func (SeqScan) TupleDesc

func (s SeqScan) TupleDesc() *TupleDesc

TupleDesc TupleDesc

type TdItem

type TdItem struct {
	Type *Type
	Name string
}

TdItem tuple desc item

func (TdItem) String

func (ti TdItem) String() string

type Tuple

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

Tuple one record

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

func (Tuple) MarshalBinary

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

MarshalBinary marshal tuple

func (Tuple) String

func (tp Tuple) String() string

type TupleDesc

type TupleDesc struct {
	TdItems []TdItem
}

TupleDesc the tuple descrition

func NewTupleDesc

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

NewTupleDesc create one TupleDesc

func (TupleDesc) Equal

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

Equal 2 tuple desc is equal

func (TupleDesc) Size

func (td TupleDesc) Size() int

Size get size of fields

func (TupleDesc) String

func (td TupleDesc) String() string

type TupleIterator

type TupleIterator struct {
	Tuples []*Tuple

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

TupleIterator Implements a OpIterator

func NewTupleIterator

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

NewTupleIterator new TupleIterator

func (*TupleIterator) Close

func (it *TupleIterator) Close()

Close close

func (TupleIterator) Error

func (it TupleIterator) Error() error

Error return error

func (*TupleIterator) HasNext

func (it *TupleIterator) HasNext() bool

HasNext hasNext

func (*TupleIterator) Next

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

Next next tuple; before call Next, should call HasNext

func (*TupleIterator) Open

func (it *TupleIterator) Open() error

Open open

func (*TupleIterator) Rewind

func (it *TupleIterator) Rewind() error

Rewind rewind the iterator

func (TupleIterator) TupleDesc

func (it TupleIterator) TupleDesc() *TupleDesc

TupleDesc TupleDesc

type Tx

type Tx struct {
	TxID *TxID
}

Tx transaction

func NewTx

func NewTx() *Tx

NewTx new Tx with NewTxID

func (*Tx) Finish

func (tx *Tx) Finish()

Finish clean Tx

type TxID

type TxID struct {
	ID uint64
}

TxID transaction id

func NewTxID

func NewTxID() *TxID

NewTxID new one *TxID

type Type

type Type struct {
	Name string
	Len  uintptr
}

Type type of fields

func (Type) Parse

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

Parse parse the real Field

func (Type) String

func (t Type) String() string

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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