add Delete
This commit is contained in:
parent
31b16fe2d2
commit
1b6648fdda
188
collection/List.go
Normal file
188
collection/List.go
Normal 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
27
collection/List_test.go
Normal 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)
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
|
25
kv/PrefixCodec.go
Normal file
25
kv/PrefixCodec.go
Normal 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))
|
||||
}
|
12
kv/codec.go
12
kv/codec.go
@ -86,6 +86,10 @@ func (c *codecStore[K1, K2, V1, V2]) Get(key K2) (V2, exceptions.Exception) {
|
||||
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)
|
||||
}
|
||||
@ -94,6 +98,10 @@ func (c *kCodecStore[K1, K2, V]) Get(key K2) (V, exceptions.Exception) {
|
||||
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))
|
||||
}
|
||||
@ -107,6 +115,10 @@ func (c *vCodecStore[K, V1, V2]) Get(key K) (V2, exceptions.Exception) {
|
||||
return c.codec.Decode(get), nil
|
||||
}
|
||||
|
||||
func (c *vCodecStore[K, V1, V2]) Delete(key K) exceptions.Exception {
|
||||
return c.kvs.Delete(key)
|
||||
}
|
||||
|
||||
func (i *invertCodec[V1, V2]) Encode(v2 V2) V1 {
|
||||
return i.codec.Decode(v2)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user