Skip to content

Commit

Permalink
update Authorization information
Browse files Browse the repository at this point in the history
  • Loading branch information
heiyeluren authored Mar 1, 2022
1 parent 771cfa9 commit 4861f57
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 100 deletions.
180 changes: 93 additions & 87 deletions xmap/concurrent_hash_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,58 +22,59 @@ import (
"errors"
"reflect"
"unsafe"

// "xds"
"github.com/heiyeluren/xmm"
"github.com/heiyeluren/xds"
)

// 定义 map 结构的类型
// map[keyKind]valKind

type Kind uint
// type Kind uint

const (
Invalid Kind = iota
Bool
Int
Int8
Int16
Int32
Int64
Uint
Uint8
Uint16
Uint32
Uint64
Uintptr
Float32
Float64
Complex64
Complex128
Array
Chan
Func
Interface
Map
Ptr
ByteSlice
String
Struct
UnsafePointer
)
// const (
// Invalid Kind = iota
// Bool
// Int
// Int8
// Int16
// Int32
// Int64
// Uint
// Uint8
// Uint16
// Uint32
// Uint64
// Uintptr
// Float32
// Float64
// Complex64
// Complex128
// Array
// Chan
// Func
// Interface
// Map
// Ptr
// ByteSlice
// String
// Struct
// UnsafePointer
// )

var InvalidType = errors.New("type Error") // 类型错误
// var InvalidType = errors.New("type Error") // 类型错误

type ConcurrentHashMap struct {
keyKind Kind
valKind Kind
keyKind xds.Kind
valKind xds.Kind
data *ConcurrentRawHashMap
}

// NewDefaultConcurrentHashMap 类似于make(map[keyKind]valKind)
// mm 内存分配模块
// keyKind: map中key的类型
// valKind: map中value的类型
func NewDefaultConcurrentHashMap(mm xmm.XMemory, keyKind, valKind Kind) (*ConcurrentHashMap, error) {
func NewDefaultConcurrentHashMap(mm xmm.XMemory, keyKind, valKind xds.Kind) (*ConcurrentHashMap, error) {
return NewConcurrentHashMap(mm, 16, 0.75, 8, keyKind, valKind)
}

Expand All @@ -84,7 +85,7 @@ func NewDefaultConcurrentHashMap(mm xmm.XMemory, keyKind, valKind Kind) (*Concur
// fact:负载因子,当存放的元素超过该百分比,就会触发扩容。
// treeSize:bucket中的链表长度达到该值后,会转换为红黑树。
// valKind: map中value的类型
func NewConcurrentHashMap(mm xmm.XMemory, cap uintptr, fact float64, treeSize uint64, keyKind, valKind Kind) (*ConcurrentHashMap, error) {
func NewConcurrentHashMap(mm xmm.XMemory, cap uintptr, fact float64, treeSize uint64, keyKind, valKind xds.Kind) (*ConcurrentHashMap, error) {
chm, err := NewConcurrentRawHashMap(mm, cap, fact, treeSize)
if err != nil {
return nil, err
Expand All @@ -93,82 +94,87 @@ func NewConcurrentHashMap(mm xmm.XMemory, cap uintptr, fact float64, treeSize ui
}

func (chm *ConcurrentHashMap) Get(key interface{}) (val interface{}, keyExists bool, err error) {
k, err := chm.Marshal(chm.keyKind, key)
// k, err := chm.Marshal(chm.keyKind, key)
k, err := xds.RawToByte(chm.keyKind, key)
if err != nil {
return nil, false, err
}
valBytes, exists, err := chm.data.Get(k)
if err != nil {
return nil, exists, err
}
ret, err := chm.UnMarshal(chm.valKind, valBytes)
// ret, err := chm.UnMarshal(chm.valKind, valBytes)
ret, err := xds.ByteToRaw(chm.valKind, valBytes)
if err != nil {
return nil, false, err
}
return ret, true, nil
}

func (chm *ConcurrentHashMap) Put(key interface{}, val interface{}) (err error) {
k, err := chm.Marshal(chm.keyKind, key)
// k, err := chm.Marshal(chm.keyKind, key)
k, err := xds.RawToByte(chm.keyKind, key)
if err != nil {
return err
}
v, err := chm.Marshal(chm.valKind, val)
// v, err := chm.Marshal(chm.valKind, val)
v, err := xds.RawToByte(chm.valKind, val)
return chm.data.Put(k, v)
}

func (chm *ConcurrentHashMap) Del(key interface{}) (err error) {
k, err := chm.Marshal(chm.keyKind, key)
// k, err := chm.Marshal(chm.keyKind, key)
k, err := xds.RawToByte(chm.keyKind, key)
if err != nil {
return err
}
return chm.data.Del(k)
}

// 序列化,将来考虑基本类型一次访问
func (chm *ConcurrentHashMap) Marshal(kind Kind, content interface{}) (data []byte, err error) {
switch kind {
case String:
data, ok := content.(string)
if !ok {
return nil, InvalidType
}
sh := (*reflect.StringHeader)(unsafe.Pointer(&data))
return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len})), nil
case ByteSlice:
data, ok := content.([]byte)
if !ok {
return nil, InvalidType
}
return data, nil
case Int:
h, ok := content.(int)
if !ok {
return nil, InvalidType
}
return (*[8]byte)(unsafe.Pointer(&h))[:], nil
case Uintptr:
h, ok := content.(uintptr)
if !ok {
return nil, InvalidType
}
return (*[8]byte)(unsafe.Pointer(&h))[:], nil
}
return
}
// // 序列化,将来考虑基本类型一次访问
// func (chm *ConcurrentHashMap) Marshal(kind Kind, content interface{}) (data []byte, err error) {
// switch kind {
// case xds.String:
// data, ok := content.(string)
// if !ok {
// return nil, xds.InvalidType
// }
// sh := (*reflect.StringHeader)(unsafe.Pointer(&data))
// return *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{Data: sh.Data, Len: sh.Len, Cap: sh.Len})), nil
// case xds.ByteSlice:
// data, ok := content.([]byte)
// if !ok {
// return nil, xds.InvalidType
// }
// return data, nil
// case xds.Int:
// h, ok := content.(int)
// if !ok {
// return nil, xds.InvalidType
// }
// return (*[8]byte)(unsafe.Pointer(&h))[:], nil
// case xds.Uintptr:
// h, ok := content.(uintptr)
// if !ok {
// return nil, xds.InvalidType
// }
// return (*[8]byte)(unsafe.Pointer(&h))[:], nil
// }
// return
// }

func (chm *ConcurrentHashMap) UnMarshal(kind Kind, data []byte) (content interface{}, err error) {
switch kind {
case String:
return *(*string)(unsafe.Pointer(&data)), nil
case Uintptr:
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
return *(*uintptr)(unsafe.Pointer(sh.Data)), nil
case Int:
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
return *(*int)(unsafe.Pointer(sh.Data)), nil
case ByteSlice:
return data, nil
}
return
}
// func (chm *ConcurrentHashMap) UnMarshal(kind Kind, data []byte) (content interface{}, err error) {
// switch kind {
// case xds.String:
// return *(*string)(unsafe.Pointer(&data)), nil
// case xds.Uintptr:
// sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
// return *(*uintptr)(unsafe.Pointer(sh.Data)), nil
// case xds.Int:
// sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
// return *(*int)(unsafe.Pointer(sh.Data)), nil
// case xds.ByteSlice:
// return data, nil
// }
// return
// }
2 changes: 0 additions & 2 deletions xmap/concurrent_hash_map_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import (
"testing"
"time"
"unsafe"

// "xds/xmap/entry"
// "github.com/spf13/cast"
"github.com/heiyeluren/xmm"

"github.com/heiyeluren/xds/xmap/entry"
)

Expand Down
5 changes: 1 addition & 4 deletions xmap/concurrent_hash_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ import (
"testing"
"time"
"unsafe"

"github.com/heiyeluren/xmm"

// "xds/xmap/entry"
"github.com/heiyeluren/xmm"
"github.com/spf13/cast"

"github.com/heiyeluren/xds/xmap/entry"
)

Expand Down
2 changes: 0 additions & 2 deletions xmap/concurrent_raw_hash_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import (
"sync"
"sync/atomic"
"unsafe"

"github.com/heiyeluren/xmm"

"github.com/heiyeluren/xds/xmap/entry"
)

Expand Down
8 changes: 3 additions & 5 deletions xmap/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ import (
"testing"
"time"
"unsafe"

"github.com/heiyeluren/xmm"

// "xds/xmap/entry"
"github.com/spf13/cast"

"github.com/heiyeluren/xmm"
"github.com/heiyeluren/xds"
"github.com/heiyeluren/xds/xmap/entry"
)

Expand Down Expand Up @@ -260,7 +258,7 @@ func Test_NewDefaultConcurrentHashMap(t *testing.T) {
if err != nil {
t.Fatal(err)
}
chmp, err := NewDefaultConcurrentHashMap(mm, Uintptr, Uintptr)
chmp, err := NewDefaultConcurrentHashMap(mm, xds.Uintptr, xds.Uintptr)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4861f57

Please sign in to comment.