bTree

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2023 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Nil pointer in a database.
	BNil = FilePos(0)

	// Sizes of data in bytes.
	BOS = 1 // bool size.
	BYS = 1 // byte size.
	SIS = 2 // int16 size.
	INS = 4 // int32 size.
	LIS = 8 // int64 size.
	SRS = 4 // float32 size.
	RES = 8 // float64 size.

)
View Source
const (

	// Comp; result of a comparaison.
	Lt = Comp(-1) // Less than.
	Eq = Comp(0)  // Equal.
	Gt = Comp(+1) // Greater than.

)

Variables

This section is empty.

Functions

This section is empty.

Types

type Bytes

type Bytes []byte

Slice of byte(s).

type Comp

type Comp = A.Comp

Result of a comparaison.

type Data

type Data interface {
	Read(r *Reader)
	Write(w *Writer)
}

Content of a page, used to store data or key.

type DataFac

type DataFac interface {
	New(size int) Data
}

Factory of Data.

type DataMan

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

Manager of Data

func (*DataMan) AllocateData

func (m *DataMan) AllocateData(data Data) FilePos

Consider using WriteAllocateData instead. Allocate a cluster for data, managed by m, in the database of m, and return its position. Warning: AllocateData calls data.Write to find the size of data, be sure the value of data can be written and has its correct size.

func (*DataMan) AllocateSize

func (m *DataMan) AllocateSize(size int) FilePos

Consider using AllocateData or, better, WriteAllocateData instead. Allocate a cluster of size size, managed by m, in the database of m, and return its position.

func (*DataMan) EraseData

func (m *DataMan) EraseData(ptr FilePos)

Erase data managed by m at position ptr in the database of m.

func (*DataMan) ReadData

func (m *DataMan) ReadData(ptr FilePos) Data

Read user data m in the database of m at position ptr.

func (*DataMan) WriteAllocateData

func (m *DataMan) WriteAllocateData(data Data) FilePos

Allocate a cluster for data, managed by m, in the database of m, and write data into it; return the position of the allocated cluster.

func (*DataMan) WriteData

func (m *DataMan) WriteData(ptr FilePos, data Data)

Write data, managed by m, at the position ptr in the database of m.

type Database

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

A database

func (*Database) CloseBase

func (base *Database) CloseBase()

Close the database base.

func (*Database) CreateDataMan

func (base *Database) CreateDataMan(fac DataFac) *DataMan

Create and return a new manager of the data created by fac in the database base.

func (*Database) CreateIndex

func (base *Database) CreateIndex(keySize int) FilePos

Create a new index in the database base and return its reference, but do not open it. keySize is the size of keys. If keys have a fixed size, put this size in keySize. If the size of keys does not vary much, put the greatest size in keySize. If the greatest size of keys is unknown, or if this size vary much, or if you want to use prefixes, put zero in keySize: you'll have to fix the size of each key later.

func (*Database) DeleteIndex

func (base *Database) DeleteIndex(ref FilePos)

Delete the index at position ref in the database base.

func (*Database) End

func (base *Database) End() FilePos

Return the length (number of bytes) of base.

func (*Database) OpenIndex

func (base *Database) OpenIndex(ref FilePos, man *KeyManager, f DataFac) *Index

Open and return an index created with the reference ref by Database.CreateIndex in the database base. man is the key manager of the index and f is the factory of its keys. The current position of the index is reset.

func (*Database) PlaceNb

func (base *Database) PlaceNb() int

Return the number of fixed places in base.

func (*Database) ReadPlace

func (base *Database) ReadPlace(place int) int64

Read and return the content of the fixed place place of the database base. The first place has number 0.

func (*Database) UpdateBase

func (base *Database) UpdateBase()

Update the database base on disk.

func (*Database) WritePlace

func (base *Database) WritePlace(place int, val int64)

Write val in the fixed place place of the database base. The first place has number 0.

type Factorer

type Factorer interface {

	// Create a file with name nF. Don't open this file.
	Create(nF string) bool

	// Open a file with name nF and return this file. Return nil if the file does not exist.
	Open(nF string) *File
}

Interface for a Factory.

type Factory

type Factory struct {
	Factorer
}

Factory for files and databases.

func (*Factory) CloseBase

func (fac *Factory) CloseBase(nF string)

Close, with the help of fac, the database created in the file of name nF, if it exists and is open. It is a rescue func. Use it only in case of an accidentally kept open database. Normally, use Database.CloseBase.

func (*Factory) CreateBase

func (fac *Factory) CreateBase(nF string, placeNb int) bool

Create a new file of name nF, with the help of fac, and a database inside this file, with placeNb fixed places. Don't open this database. Fixed places are locations where can be recorded integers, i.e. data pointers.

func (*Factory) OpenBase

func (fac *Factory) OpenBase(nF string, pageNb int) *Database

Open and return, with the help of fac, the database created in the file of name nF. pageNb is the maximal number of allocated buffer pages. Return nil if the file does not exit or can't be opened.

func (*Factory) TestBase

func (fac *Factory) TestBase(nF string) bool

Verify, with the help of fac, if the file nF exists and if it contains a database; return true in this case; the base must be closed.

type File

type File struct {
	Filer
}

A file containing a database.

type FilePos

type FilePos int64

Position in a file, in bytes.

type Filer

type Filer interface {

	// Close the file.
	Close()

	// Flush the file on disk.
	Flush()

	// Set the reading position of the file to pos; the first position is 0.
	PosReader(pos FilePos)

	// Read a slice of n bytes from the file at reading position.
	Read(n int) Bytes

	// Set the writing position of the file to pos; the first position is 0.
	PosWriter(pos FilePos)

	// Write the slice b to the file at writing position.
	Write(b Bytes)

	// Return the length of the file.
	End() FilePos

	// Truncate the file at the length end.
	Truncate(end FilePos)

	// Read and return a float64 from the slice b at position pos; put pos at the position following the float64.
	BytesToFloat64(b Bytes, pos *int) float64

	// Read and return a float32 from the slice b at position pos; put pos at the position following the float32.
	BytesToFloat32(b Bytes, pos *int) float32

	// Return a pointer to a slice of bytes coding for f.
	Float64ToBytes(f float64) Bytes

	// Return a pointer to a slice of bytes coding for f.
	Float32ToBytes(f float32) Bytes
}

Interface for file management.

type Index

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

Index, structured as a btree.

func (*Index) Height

func (ind *Index) Height() int

Return the height of 'ind'

func (*Index) IsEmpty

func (ind *Index) IsEmpty() bool

Test the emptiness of ind

func (*Index) NewReader

func (ind *Index) NewReader() *IndexReader

Create a new Reader for ind. A Reader keeps a position in an Index and can update this position or read values there. Just after being created, its position is reseted.

func (*Index) NumberOfKeys

func (ind *Index) NumberOfKeys() int

Return the number of different keys in ind

func (*Index) Writer

func (ind *Index) Writer() *IndexWriter

Return the Writer of ind. A Writer keeps a position in an Index and can update this position or read/write values there. Just after the opening of an index, the position of its writer is reseted.

type IndexReader

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

Reader of an index; each index may create several independant readers.

func (*IndexReader) Clone

func (ir *IndexReader) Clone() *IndexReader

Duplicate ir.

func (*IndexReader) CurrentKey

func (ir *IndexReader) CurrentKey() Data

Return the value of the key in the current position of ir, or nil if the current position of ir is reset.

func (*IndexReader) Ind

func (ir *IndexReader) Ind() *Index

Return the Index of ir.

func (*IndexReader) Next

func (ir *IndexReader) Next()

Position ir on the next key. If the current position is reset, ir is positioned on the first key. If ir is positioned on the last key, its current position becomes reset.

func (*IndexReader) PosSet

func (ir *IndexReader) PosSet() bool

Test if ir is positioned on a key (return true) or if its current position is reset (false returned).

func (*IndexReader) Previous

func (ir *IndexReader) Previous()

Position ir on the previous key. If the current position is reset, ir is positioned on the last key. If ir is positioned on the first key, its current position becomes reset.

func (*IndexReader) ReadValue

func (ir *IndexReader) ReadValue() FilePos

Read, at the current position of ir, the associated reference of the Data of its Index and return it. When a new key is inserted, the reference which is initially attached to it has the value BNil. The reset position of an index has, itself, an associated reference, legible by IndexReader.ReadValue.

func (*IndexReader) ResetPos

func (ir *IndexReader) ResetPos()

Reset the current position of ir. After this action, ir.PosSet() returns false.

func (*IndexReader) Search

func (ir *IndexReader) Search(key Data) bool

Seek in the index of ir the key key. The result indicates if the key was found. Fix the current position of ir on the found key or on the key which is immediately after the sought key in the event of unfruitful search.

type IndexWriter

type IndexWriter struct {
	IndexReader // An IndexWriter can read too.
}

Writer of an index; each index owns only one writer.

func (*IndexWriter) Erase

func (iw *IndexWriter) Erase(key Data) bool

Erase from the index of iw the key key. If key does not belong to the index, ind.Erase does nothing and returns false, otherwise it returns true. If the current position of iw was on the erased key, it is reset.

func (*IndexWriter) SearchIns

func (iw *IndexWriter) SearchIns(key Data) bool

Seek in the index of iw the key key and insert it if it is not there already. The result indicates if the key was found. Fix the current position of iw on the key found or inserted.

func (*IndexWriter) WriteValue

func (iw *IndexWriter) WriteValue(val FilePos)

Write on the current position of iw the associated reference val of a Data into its Index. When a new key is inserted, the data which is initially attached to it has the value BNil. The reset position of an index has, itself, a associated data, modifiable by IndexWriter.WriteValue.

type KeyManager

type KeyManager struct {
	KeyManagerer
	// contains filtered or unexported fields
}

Manager of index keys.

func MakeKM

func MakeKM(k KeyManagerer) *KeyManager

Make a KeyManager out of a KeyManagerer.

func StringKeyManager

func StringKeyManager() *KeyManager

Create a KeyManeger for String(s).

type KeyManagerer

type KeyManagerer interface {

	// Comparison method. s1 and s2 are the keys to be compared. The result can be Lt (s1 < s2), Eq (s1 = s2) or Gt (s1 > s2). KeyManagerer.CompP must induce a total order on the set of keys.
	CompP(s1, s2 Data) Comp

	// Create a prefix of a key. On input key1 and key2 are two keys, with key1 < key2. On output, key2 may be modified, but cannot be enlarged, so that it becomes a prefix of its previous value. A prefix Pref(key1, key2) of  the key key2 in relation to the key key1 is defined by:
	//	1) Pref(key1, key2) is a key, which can be compared to other keys by the mean of the method KeyManagerer.CompP;
	//	2) Pref(key1, key2) is the shortest key with key1 < Pref(key1, key2) <= key2;
	//	3) key1 may have a null length and, in this case, must be considered less than any other key.
	// Prefixes are useful if keys are long or if their lengths vary much. In this case, if KeyManagerer.PrefP is instantiated, database is shorter and searches are faster.
	PrefP(key1 Data, key2 *Data)
}

Interface for managers of index keys.

type Reader

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

Reader of a byte stream.

func (*Reader) InBool

func (r *Reader) InBool() bool

Read a bool from the input stream.

func (*Reader) InByte

func (r *Reader) InByte() byte

Read a byte from the input stream.

func (*Reader) InBytes

func (r *Reader) InBytes(len int) Bytes

Read an array of bytes from the input stream.

func (*Reader) InFilePos

func (r *Reader) InFilePos() FilePos

Read a FilePos from the input stream.

func (*Reader) InFloat32

func (r *Reader) InFloat32() float32

Read a float32 from the input stream. Use File.BytesToFloat32.

func (*Reader) InFloat64

func (r *Reader) InFloat64() float64

Read a float64 from the input stream. Use File.BytesToFloat64.

func (*Reader) InInt16

func (r *Reader) InInt16() int16

Read an int16 from the input stream.

func (*Reader) InInt32

func (r *Reader) InInt32() int32

Read an int32 from the input stream.

func (*Reader) InInt64

func (r *Reader) InInt64() int64

Read an int64 from the input stream.

func (*Reader) InInt8

func (r *Reader) InInt8() int8

Read a int8 from the input stream.

func (*Reader) InString

func (r *Reader) InString() string

Read a string from the input stream.

func (*Reader) InStringLen

func (r *Reader) InStringLen() int

Read a string length from the input stream; preserves the position in the stream.

func (*Reader) InUint16

func (r *Reader) InUint16() uint16

Read an uint16 from the input stream.

func (*Reader) InUint32

func (r *Reader) InUint32() uint32

Read an uint32 from the input stream.

func (*Reader) InUint64

func (r *Reader) InUint64() uint64

Read an uint64 from the input stream.

func (*Reader) Len

func (r *Reader) Len() int

Return the length of the input stream owned by r, in bytes.

func (*Reader) Pos

func (r *Reader) Pos() int

Return the position of r in its input stream, in bytes.

type String

type String struct {
	C string
}

String, satisfies Data.

func StringPrefP

func StringPrefP(key1, key2 *String, compare StringComparer) *String

Create a prefix of the String key2, in relation with key1 and the comparison function compare; see PrefP.

func (*String) Read

func (s *String) Read(r *Reader)

Read s.C with the help of the reader r.

func (*String) Write

func (s *String) Write(w *Writer)

Write s.C with the help of the writer w. The length of the production is 2 * (len(s.C)+ 1).

type StringComparer

type StringComparer = func(key1, key2 Data) Comp

Compare two strings.

type StringFac

type StringFac struct {
}

Factory of String; satisfies DataFac.

func (StringFac) New

func (f StringFac) New(size int) Data

Create a String.

type Writer

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

Writer on a byte stream.

func (*Writer) OutBool

func (w *Writer) OutBool(b bool)

Write a bool to the output stream.

func (*Writer) OutByte

func (w *Writer) OutByte(c byte)

Write a byte to the output stream.

func (*Writer) OutBytes

func (w *Writer) OutBytes(b Bytes)

Write a Bytes to the output stream.

func (*Writer) OutFilePos

func (w *Writer) OutFilePos(n FilePos)

Write a FilePos to the output stream.

func (*Writer) OutFloat32

func (w *Writer) OutFloat32(r float32)

Write a float32 to the output stream. Use File.Float32ToBytes.

func (*Writer) OutFloat64

func (w *Writer) OutFloat64(r float64)

Write a float64 to the output stream. Use File.Float64ToBytes.

func (*Writer) OutInt16

func (w *Writer) OutInt16(n int16)

Write a int16 to the output stream.

func (*Writer) OutInt32

func (w *Writer) OutInt32(n int32)

Write a int32 to the output stream.

func (*Writer) OutInt64

func (w *Writer) OutInt64(n int64)

Write a int64 to the output stream.

func (*Writer) OutInt8

func (w *Writer) OutInt8(c int8)

Write an int8 to the output stream.

func (*Writer) OutString

func (w *Writer) OutString(c string)

Write a string to the output stream.

func (*Writer) OutUint16

func (w *Writer) OutUint16(n uint16)

Write a uint16 to the output stream.

func (*Writer) OutUint32

func (w *Writer) OutUint32(n uint32)

Write a uint32 to the output stream.

func (*Writer) OutUint64

func (w *Writer) OutUint64(n uint64)

Write a uint64 to the output stream.

Jump to

Keyboard shortcuts

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