82 lines
1.3 KiB
Go
82 lines
1.3 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
"sync"
|
||
|
"sync/atomic"
|
||
|
|
||
|
"github.com/tecbot/gorocksdb"
|
||
|
)
|
||
|
|
||
|
func NewEasyDataBase() *EasyDataBase {
|
||
|
|
||
|
edb := &EasyDataBase{}
|
||
|
db, cfs := OpenDataBase()
|
||
|
edb.DB = db
|
||
|
edb.CFS = cfs
|
||
|
return edb
|
||
|
|
||
|
}
|
||
|
|
||
|
type EasyDataBase struct {
|
||
|
DB *gorocksdb.DB
|
||
|
CFS gorocksdb.ColumnFamilyHandles
|
||
|
|
||
|
Tables map[uint32]*Table
|
||
|
TableDict map[string]uint32
|
||
|
|
||
|
Metadata
|
||
|
}
|
||
|
|
||
|
type Metadata struct {
|
||
|
tidCount uint32 // int16
|
||
|
}
|
||
|
|
||
|
// Table 表结构
|
||
|
type Table struct {
|
||
|
tableLock *sync.Mutex
|
||
|
|
||
|
Name string
|
||
|
Type int
|
||
|
|
||
|
ID uint32 // uint16
|
||
|
|
||
|
IsAllKey bool
|
||
|
Fields []*Field
|
||
|
|
||
|
fidCount uint16 // uint16
|
||
|
idxCount uint64
|
||
|
rawCount uint64
|
||
|
delCount uint64
|
||
|
}
|
||
|
|
||
|
// CreateField 创建字段
|
||
|
func (table *Table) CreateField(key []byte, vt ValueType, isIndex bool, isUnique bool) *Field {
|
||
|
|
||
|
table.tableLock.Lock()
|
||
|
defer table.tableLock.Unlock()
|
||
|
|
||
|
field := &Field{}
|
||
|
field.ID = table.fidCount
|
||
|
field.IsIndex = isIndex
|
||
|
field.IsUnique = isUnique
|
||
|
field.Key = key
|
||
|
field.VT = vt
|
||
|
|
||
|
table.fidCount++
|
||
|
|
||
|
return field
|
||
|
}
|
||
|
|
||
|
// CreateTable 创建表
|
||
|
func CreateTable(name string, fields []*Field) {
|
||
|
if _, ok := EDB.TableDict[name]; !ok {
|
||
|
ntid := atomic.AddUint32(&EDB.Metadata.tidCount, 1)
|
||
|
table := &Table{Name: name, ID: ntid, Type: 1}
|
||
|
table.Fields = fields
|
||
|
} else {
|
||
|
log.Println("table name is exists")
|
||
|
}
|
||
|
|
||
|
}
|