4 Commits

Author SHA1 Message Date
tursom
1b6648fdda add Delete 2023-04-19 21:09:07 +08:00
tursom
31b16fe2d2 update 2023-04-18 18:01:06 +08:00
tursom
fe15b461b3 update 2023-04-18 15:25:54 +08:00
tursom
04bd5127c3 update 2023-04-18 15:10:17 +08:00
18 changed files with 348 additions and 65 deletions

188
collection/List.go Normal file
View File

@@ -0,0 +1,188 @@
package collection
import (
"bytes"
"encoding/binary"
"github.com/tursom/GoCollections/collections"
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
"gitea.tursom.cn/tursom/kvs/kv"
)
type (
List[T lang.Object] interface {
collections.MutableList[T]
}
ListNode[T any] struct {
lang.BaseObject
prev, next uint32
value T
}
listNodeCodec[T any] struct {
lang.BaseObject
codec kv.Codec[[]byte, T]
}
listImpl[T lang.Object] struct {
kvs kv.Store[uint32, *ListNode[T]]
head, tail uint32
}
listIterator[T any] struct {
kvs kv.Store[uint32, *ListNode[T]]
}
)
func ListNodeCodec[T any](codec kv.Codec[[]byte, T]) kv.Codec[[]byte, *ListNode[T]] {
return &listNodeCodec[T]{
codec: codec,
}
}
func NewList[T lang.Object](kvs kv.Store[uint32, *ListNode[T]]) List[T] {
headPointer, exception := kvs.Get(0)
if exception != nil {
panic(exception)
}
var head uint32
if headPointer == nil {
head = 1
} else {
head = headPointer.next
}
return &listImpl[T]{
kvs: kvs,
head: head,
}
}
func (l *listNodeCodec[T]) Encode(v2 *ListNode[T]) []byte {
if v2 == nil {
return nil
}
buffer := bytes.NewBuffer(nil)
_ = binary.Write(buffer, binary.BigEndian, v2.prev)
_ = binary.Write(buffer, binary.BigEndian, v2.next)
buffer.Write(l.codec.Encode(v2.value))
return buffer.Bytes()
}
func (l *listNodeCodec[T]) Decode(v1 []byte) *ListNode[T] {
if len(v1) == 0 {
return nil
}
return &ListNode[T]{
prev: binary.BigEndian.Uint32(v1),
next: binary.BigEndian.Uint32(v1[4:]),
value: l.codec.Decode(v1[8:]),
}
}
func (l *listImpl[T]) Size() int {
size, err := collections.Size[T](l)
if err != nil {
panic(err)
}
return size
}
func (l *listImpl[T]) IsEmpty() bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Contains(element T) bool {
return collections.Contains[T](l, element)
}
func (l *listImpl[T]) ContainsAll(c collections.Collection[T]) bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Get(index int) (T, exceptions.Exception) {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) SubList(from, to int) collections.List[T] {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Add(element T) bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Remove(element T) exceptions.Exception {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) AddAll(c collections.Collection[T]) bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) RemoveAll(c collections.Collection[T]) bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) RetainAll(c collections.Collection[T]) bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Clear() {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Set(index int, element T) exceptions.Exception {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) AddAtIndex(index int, element T) bool {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) RemoveAt(index int) exceptions.Exception {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) SubMutableList(from, to int) collections.MutableList[T] {
//TODO implement me
panic("implement me")
}
func (l *listImpl[T]) Iterator() collections.Iterator[T] {
return l.MutableListIterator()
}
func (l *listImpl[T]) ListIterator() collections.ListIterator[T] {
return l.MutableListIterator()
}
func (l *listImpl[T]) MutableIterator() collections.MutableIterator[T] {
return l.MutableListIterator()
}
func (l *listImpl[T]) MutableListIterator() collections.MutableListIterator[T] {
//TODO implement me
panic("implement me")
}

27
collection/List_test.go Normal file
View File

@@ -0,0 +1,27 @@
package collection
import (
"math/rand"
"testing"
"gitea.tursom.cn/tursom/kvs/kv"
)
func Test_listNodeCodec(t *testing.T) {
codec := ListNodeCodec(kv.Int32ToByteCodec)
prev := uint32(rand.Int31())
next := uint32(rand.Int31())
value := rand.Int31()
encode := codec.Encode(&ListNode[int32]{
prev: prev,
next: next,
value: value,
})
decode := codec.Decode(encode)
if decode.prev != prev || decode.next != next || decode.value != value {
t.Fatal(decode)
}
}

View File

@@ -21,10 +21,10 @@ func ArrayCodec[V any](codec Codec[io.Reader, V]) Codec[[]byte, []V] {
return &arrayCodec[V]{codec: codec}
}
func (a *arrayCodec[V]) encode(v2 []V) []byte {
func (a *arrayCodec[V]) Encode(v2 []V) []byte {
var bs []byte
for _, v := range v2 {
encode := a.codec.encode(v)
encode := a.codec.Encode(v)
all, err := io.ReadAll(encode)
if err != nil {
panic(exceptions.Package(err))
@@ -36,7 +36,7 @@ func (a *arrayCodec[V]) encode(v2 []V) []byte {
return bs
}
func (a *arrayCodec[V]) decode(v1 []byte) []V {
func (a *arrayCodec[V]) Decode(v1 []byte) []V {
if len(v1) == 0 {
return []V{}
}
@@ -48,7 +48,7 @@ func (a *arrayCodec[V]) decode(v1 []byte) []V {
for func() bool {
defer recover()
v := a.codec.decode(reader)
v := a.codec.Decode(reader)
values = append(values, v)
return reader.Len() > 0

View File

@@ -15,7 +15,7 @@ var (
BoolToByteCodec Codec[[]byte, bool] = &boolCodec{}
)
func (b *boolCodec) encode(v2 bool) []byte {
func (b *boolCodec) Encode(v2 bool) []byte {
if v2 {
return TrueBytes
} else {
@@ -23,7 +23,7 @@ func (b *boolCodec) encode(v2 bool) []byte {
}
}
func (b *boolCodec) decode(v1 []byte) bool {
func (b *boolCodec) Decode(v1 []byte) bool {
if len(v1) == 0 {
return false
}

View File

@@ -17,10 +17,10 @@ func ChainCodec[V1, V2, V3 any](codec1 Codec[V2, V3], codec2 Codec[V1, V2]) Code
}
}
func (c *chainCodec[V1, V2, V3]) encode(v2 V3) V1 {
return c.codec2.encode(c.codec1.encode(v2))
func (c *chainCodec[V1, V2, V3]) Encode(v2 V3) V1 {
return c.codec2.Encode(c.codec1.Encode(v2))
}
func (c *chainCodec[V1, V2, V3]) decode(v1 V1) V3 {
return c.codec1.decode(c.codec2.decode(v1))
func (c *chainCodec[V1, V2, V3]) Decode(v1 V1) V3 {
return c.codec1.Decode(c.codec2.Decode(v1))
}

View File

@@ -26,3 +26,8 @@ func (m *mapKvs) Put(key string, value []byte) exceptions.Exception {
func (m *mapKvs) Get(key string) ([]byte, exceptions.Exception) {
return m.m[key], nil
}
func (m *mapKvs) Delete(key string) exceptions.Exception {
delete(m.m, key)
return nil
}

View File

@@ -73,11 +73,11 @@ var (
ByteToComplex128Codec = InvertCodec[complex128, []byte](&complex128ToByteCodec{})
)
func (u *int8ToByteCodec) encode(v2 int8) []byte {
func (u *int8ToByteCodec) Encode(v2 int8) []byte {
return []byte{byte(v2)}
}
func (u *int8ToByteCodec) decode(v1 []byte) int8 {
func (u *int8ToByteCodec) Decode(v1 []byte) int8 {
if len(v1) == 0 {
return 0
}
@@ -85,11 +85,11 @@ func (u *int8ToByteCodec) decode(v1 []byte) int8 {
return int8(v1[0])
}
func (u *int16ToByteCodec) encode(v2 int16) []byte {
func (u *int16ToByteCodec) Encode(v2 int16) []byte {
return binary.BigEndian.AppendUint16(nil, uint16(v2))
}
func (u *int16ToByteCodec) decode(v1 []byte) int16 {
func (u *int16ToByteCodec) Decode(v1 []byte) int16 {
if len(v1) == 0 {
return 0
}
@@ -97,11 +97,11 @@ func (u *int16ToByteCodec) decode(v1 []byte) int16 {
return int16(binary.BigEndian.Uint16(v1))
}
func (u *int32ToByteCodec) encode(v2 int32) []byte {
func (u *int32ToByteCodec) Encode(v2 int32) []byte {
return binary.BigEndian.AppendUint32(nil, uint32(v2))
}
func (u *int32ToByteCodec) decode(v1 []byte) int32 {
func (u *int32ToByteCodec) Decode(v1 []byte) int32 {
if len(v1) == 0 {
return 0
}
@@ -109,11 +109,11 @@ func (u *int32ToByteCodec) decode(v1 []byte) int32 {
return int32(binary.BigEndian.Uint32(v1))
}
func (u *int64ToByteCodec) encode(v2 int64) []byte {
func (u *int64ToByteCodec) Encode(v2 int64) []byte {
return binary.BigEndian.AppendUint64(nil, uint64(v2))
}
func (u *int64ToByteCodec) decode(v1 []byte) int64 {
func (u *int64ToByteCodec) Decode(v1 []byte) int64 {
if len(v1) == 0 {
return 0
}
@@ -121,11 +121,11 @@ func (u *int64ToByteCodec) decode(v1 []byte) int64 {
return int64(binary.BigEndian.Uint64(v1))
}
func (u *uint8ToByteCodec) encode(v2 uint8) []byte {
func (u *uint8ToByteCodec) Encode(v2 uint8) []byte {
return []byte{v2}
}
func (u *uint8ToByteCodec) decode(v1 []byte) uint8 {
func (u *uint8ToByteCodec) Decode(v1 []byte) uint8 {
if len(v1) == 0 {
return 0
}
@@ -133,11 +133,11 @@ func (u *uint8ToByteCodec) decode(v1 []byte) uint8 {
return v1[0]
}
func (u *uint16ToByteCodec) encode(v2 uint16) []byte {
func (u *uint16ToByteCodec) Encode(v2 uint16) []byte {
return binary.BigEndian.AppendUint16(nil, v2)
}
func (u *uint16ToByteCodec) decode(v1 []byte) uint16 {
func (u *uint16ToByteCodec) Decode(v1 []byte) uint16 {
if len(v1) == 0 {
return 0
}
@@ -145,11 +145,11 @@ func (u *uint16ToByteCodec) decode(v1 []byte) uint16 {
return binary.BigEndian.Uint16(v1)
}
func (u *uint32ToByteCodec) encode(v2 uint32) []byte {
func (u *uint32ToByteCodec) Encode(v2 uint32) []byte {
return binary.BigEndian.AppendUint32(nil, v2)
}
func (u *uint32ToByteCodec) decode(v1 []byte) uint32 {
func (u *uint32ToByteCodec) Decode(v1 []byte) uint32 {
if len(v1) == 0 {
return 0
}
@@ -157,11 +157,11 @@ func (u *uint32ToByteCodec) decode(v1 []byte) uint32 {
return binary.BigEndian.Uint32(v1)
}
func (u *uint64ToByteCodec) encode(v2 uint64) []byte {
func (u *uint64ToByteCodec) Encode(v2 uint64) []byte {
return binary.BigEndian.AppendUint64(nil, v2)
}
func (u *uint64ToByteCodec) decode(v1 []byte) uint64 {
func (u *uint64ToByteCodec) Decode(v1 []byte) uint64 {
if len(v1) == 0 {
return 0
}
@@ -169,11 +169,11 @@ func (u *uint64ToByteCodec) decode(v1 []byte) uint64 {
return binary.BigEndian.Uint64(v1)
}
func (u *float32ToByteCodec) encode(v2 float32) []byte {
func (u *float32ToByteCodec) Encode(v2 float32) []byte {
return binary.BigEndian.AppendUint32(nil, *(*uint32)(unsafe.Pointer(&v2)))
}
func (u *float32ToByteCodec) decode(v1 []byte) float32 {
func (u *float32ToByteCodec) Decode(v1 []byte) float32 {
if len(v1) == 0 {
return 0
}
@@ -182,11 +182,11 @@ func (u *float32ToByteCodec) decode(v1 []byte) float32 {
return *(*float32)(unsafe.Pointer(&u2))
}
func (u *float64ToByteCodec) encode(v2 float64) []byte {
func (u *float64ToByteCodec) Encode(v2 float64) []byte {
return binary.BigEndian.AppendUint64(nil, *(*uint64)(unsafe.Pointer(&v2)))
}
func (u *float64ToByteCodec) decode(v1 []byte) float64 {
func (u *float64ToByteCodec) Decode(v1 []byte) float64 {
if len(v1) == 0 {
return 0
}
@@ -195,11 +195,11 @@ func (u *float64ToByteCodec) decode(v1 []byte) float64 {
return *(*float64)(unsafe.Pointer(&u2))
}
func (u *complex64ToByteCodec) encode(v2 complex64) []byte {
func (u *complex64ToByteCodec) Encode(v2 complex64) []byte {
return binary.BigEndian.AppendUint64(nil, *(*uint64)(unsafe.Pointer(&v2)))
}
func (u *complex64ToByteCodec) decode(v1 []byte) complex64 {
func (u *complex64ToByteCodec) Decode(v1 []byte) complex64 {
if len(v1) == 0 {
return 0
}
@@ -208,7 +208,7 @@ func (u *complex64ToByteCodec) decode(v1 []byte) complex64 {
return *(*complex64)(unsafe.Pointer(&u2))
}
func (u *complex128ToByteCodec) encode(v2 complex128) []byte {
func (u *complex128ToByteCodec) Encode(v2 complex128) []byte {
r := real(v2)
i := imag(v2)
@@ -220,7 +220,7 @@ func (u *complex128ToByteCodec) encode(v2 complex128) []byte {
return bytes
}
func (u *complex128ToByteCodec) decode(v1 []byte) complex128 {
func (u *complex128ToByteCodec) Decode(v1 []byte) complex128 {
if len(v1) == 0 {
return 0
}

25
kv/PrefixCodec.go Normal file
View File

@@ -0,0 +1,25 @@
package kv
import (
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
)
type (
prefixCodec struct {
lang.BaseObject
prefix string
}
)
func PrefixCodec(prefix string) Codec[string, string] {
return &prefixCodec{prefix: prefix}
}
func (p *prefixCodec) Encode(v2 string) string {
return p.prefix + v2
}
func (p *prefixCodec) Decode(v1 string) string {
panic(exceptions.NewIllegalAccessException("unsupported method", nil))
}

View File

@@ -23,7 +23,7 @@ func ProtoDeCodec[V proto.Message](emptyMessage func() V) Codec[V, []byte] {
return InvertCodec(ProtoCodec(emptyMessage))
}
func (p *protoToByteCodec[V]) encode(v2 V) []byte {
func (p *protoToByteCodec[V]) Encode(v2 V) []byte {
bytes, err := proto.Marshal(v2)
if err == nil {
panic(exceptions.Package(err))
@@ -32,7 +32,7 @@ func (p *protoToByteCodec[V]) encode(v2 V) []byte {
return bytes
}
func (p *protoToByteCodec[V]) decode(v1 []byte) V {
func (p *protoToByteCodec[V]) Decode(v1 []byte) V {
message := p.emptyMessage()
if err := proto.Unmarshal(v1, message); err != nil {
panic(exceptions.Package(err))

View File

@@ -39,24 +39,24 @@ func FixedLengthCodec(frameLength uint32) Codec[io.Reader, []byte] {
return &fixedLengthCodec{frameLength: frameLength}
}
func (r *readerCodec[V]) encode(v2 V) io.Reader {
return bytes.NewReader(r.codec.encode(v2))
func (r *readerCodec[V]) Encode(v2 V) io.Reader {
return bytes.NewReader(r.codec.Encode(v2))
}
func (r *readerCodec[V]) decode(v1 io.Reader) V {
func (r *readerCodec[V]) Decode(v1 io.Reader) V {
all, err := io.ReadAll(v1)
if err != nil {
panic(exceptions.Package(err))
}
return r.codec.decode(all)
return r.codec.Decode(all)
}
func (f *fixedLengthCodec) encode(v2 []byte) io.Reader {
func (f *fixedLengthCodec) Encode(v2 []byte) io.Reader {
return bytes.NewReader(v2)
}
func (f *fixedLengthCodec) decode(v1 io.Reader) []byte {
func (f *fixedLengthCodec) Decode(v1 io.Reader) []byte {
bs := make([]byte, f.frameLength)
n, err := v1.Read(bs)
if err != nil {
@@ -66,7 +66,7 @@ func (f *fixedLengthCodec) decode(v1 io.Reader) []byte {
return bs[0:n]
}
func (l *lengthFieldCodec) encode(v2 []byte) io.Reader {
func (l *lengthFieldCodec) Encode(v2 []byte) io.Reader {
buffer := bytes.NewBuffer(nil)
_ = binary.Write(buffer, binary.BigEndian, uint32(len(v2)))
@@ -75,7 +75,7 @@ func (l *lengthFieldCodec) encode(v2 []byte) io.Reader {
return buffer
}
func (l *lengthFieldCodec) decode(v1 io.Reader) []byte {
func (l *lengthFieldCodec) Decode(v1 io.Reader) []byte {
var length uint32
if err := binary.Read(v1, binary.BigEndian, &length); err != nil {
panic(exceptions.Package(err))

View File

@@ -13,11 +13,11 @@ var (
ByteToStringCodec = InvertCodec[string, []byte](&stringToByteCodec{})
)
func (s *stringToByteCodec) encode(v2 string) []byte {
func (s *stringToByteCodec) Encode(v2 string) []byte {
return []byte(v2)
}
func (s *stringToByteCodec) decode(v1 []byte) string {
func (s *stringToByteCodec) Decode(v1 []byte) string {
if len(v1) == 0 {
return ""
}

View File

@@ -8,8 +8,8 @@ import (
type (
Codec[V1, V2 any] interface {
lang.Object
encode(v2 V2) V1
decode(v1 V1) V2
Encode(v2 V2) V1
Decode(v1 V1) V2
}
codecStore[K1, K2, V1, V2 any] struct {
@@ -74,28 +74,36 @@ func VCodecStore[K, V1, V2 any](
}
func (c *codecStore[K1, K2, V1, V2]) Put(key K2, value V2) exceptions.Exception {
return c.kvs.Put(c.kCodec.encode(key), c.vCodec.encode(value))
return c.kvs.Put(c.kCodec.Encode(key), c.vCodec.Encode(value))
}
func (c *codecStore[K1, K2, V1, V2]) Get(key K2) (V2, exceptions.Exception) {
value, exception := c.kvs.Get(c.kCodec.encode(key))
value, exception := c.kvs.Get(c.kCodec.Encode(key))
if exception != nil {
return lang.Nil[V2](), exception
}
return c.vCodec.decode(value), nil
return c.vCodec.Decode(value), nil
}
func (c *codecStore[K1, K2, V1, V2]) Delete(key K2) exceptions.Exception {
return c.kvs.Delete(c.kCodec.Encode(key))
}
func (c *kCodecStore[K1, K2, V]) Put(key K2, value V) exceptions.Exception {
return c.kvs.Put(c.codec.encode(key), value)
return c.kvs.Put(c.codec.Encode(key), value)
}
func (c *kCodecStore[K1, K2, V]) Get(key K2) (V, exceptions.Exception) {
return c.kvs.Get(c.codec.encode(key))
return c.kvs.Get(c.codec.Encode(key))
}
func (c *kCodecStore[K1, K2, V]) Delete(key K2) exceptions.Exception {
return c.kvs.Delete(c.codec.Encode(key))
}
func (c *vCodecStore[K, V1, V2]) Put(key K, value V2) exceptions.Exception {
return c.kvs.Put(key, c.codec.encode(value))
return c.kvs.Put(key, c.codec.Encode(value))
}
func (c *vCodecStore[K, V1, V2]) Get(key K) (V2, exceptions.Exception) {
@@ -104,13 +112,17 @@ func (c *vCodecStore[K, V1, V2]) Get(key K) (V2, exceptions.Exception) {
return lang.Nil[V2](), exception
}
return c.codec.decode(get), nil
return c.codec.Decode(get), nil
}
func (i *invertCodec[V1, V2]) encode(v2 V2) V1 {
return i.codec.decode(v2)
func (c *vCodecStore[K, V1, V2]) Delete(key K) exceptions.Exception {
return c.kvs.Delete(key)
}
func (i *invertCodec[V1, V2]) decode(v1 V1) V2 {
return i.codec.encode(v1)
func (i *invertCodec[V1, V2]) Encode(v2 V2) V1 {
return i.codec.Decode(v2)
}
func (i *invertCodec[V1, V2]) Decode(v1 V1) V2 {
return i.codec.Encode(v1)
}

View File

@@ -10,5 +10,6 @@ type (
lang.Object
Put(key K, value V) exceptions.Exception
Get(key K) (V, exceptions.Exception)
Delete(key K) exceptions.Exception
}
)

View File

@@ -5,7 +5,7 @@ import (
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
"kvs/kv"
"gitea.tursom.cn/tursom/kvs/kv"
)
type (
@@ -35,3 +35,11 @@ func (l *leveldbKVS) Get(key []byte) ([]byte, exceptions.Exception) {
return value, nil
}
func (l *leveldbKVS) Delete(key []byte) exceptions.Exception {
if err := l.db.Delete(key, nil); err != nil {
return exceptions.Package(err)
}
return nil
}

View File

@@ -5,7 +5,7 @@ import (
"github.com/syndtr/goleveldb/leveldb"
"kvs/kv"
"gitea.tursom.cn/tursom/kvs/kv"
)
func Test_leveldbKVS(t *testing.T) {

View File

@@ -5,7 +5,7 @@ import "github.com/tursom/GoCollections/lang"
type (
PipelineHandler[V1, V2 any] interface {
lang.Object
encode1(input []V1) []V2
encode2(input []V2) []V1
Encode1(input []V1) []V2
Encode2(input []V2) []V1
}
)

View File

@@ -8,7 +8,7 @@ import (
"github.com/tursom/GoCollections/exceptions"
"github.com/tursom/GoCollections/lang"
"kvs/kv"
"gitea.tursom.cn/tursom/kvs/kv"
)
type (
@@ -88,3 +88,11 @@ func (s *sqliteKVS) Put(key []byte, value []byte) exceptions.Exception {
return nil
}
func (s *sqliteKVS) Delete(key []byte) exceptions.Exception {
if _, err := s.db.Exec("delete from "+s.table+" where k=?", key); err != nil {
return exceptions.Package(err)
}
return nil
}

View File

@@ -7,7 +7,7 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/tursom/GoCollections/exceptions"
"kvs/kv"
"gitea.tursom.cn/tursom/kvs/kv"
)
func Test_sqliteKVS(t *testing.T) {
@@ -27,4 +27,13 @@ func Test_sqliteKVS(t *testing.T) {
if exception != nil || value != "world!" {
t.Fatal(value, exception)
}
if exception = skvs.Delete("hello"); exception != nil {
t.Fatal(value, exception)
}
value, exception = skvs.Get("hello")
if exception != nil || value != "" {
t.Fatal(value, exception)
}
}