Skip to content

Commit

Permalink
feat: s3 wasm support (#114)
Browse files Browse the repository at this point in the history
* s3 continue upload

* s3 wasm

* s3 wasm
  • Loading branch information
withchao authored Jul 14, 2023
1 parent 9ad3907 commit 1184313
Show file tree
Hide file tree
Showing 14 changed files with 592 additions and 715 deletions.
75 changes: 75 additions & 0 deletions internal/file/bitmap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package file

func NewBitmap(size int) *Bitmap {
data := make([]uint64, (size+63)/64)
return &Bitmap{data: data, size: size}
}

func ParseBitmap(p []byte, size int) *Bitmap {
data := make([]uint64, len(p)/8)
for i := range data {
data[i] = uint64(p[i*8])<<56 |
uint64(p[i*8+1])<<48 |
uint64(p[i*8+2])<<40 |
uint64(p[i*8+3])<<32 |
uint64(p[i*8+4])<<24 |
uint64(p[i*8+5])<<16 |
uint64(p[i*8+6])<<8 |
uint64(p[i*8+7])
}
return &Bitmap{
data: data,
size: size,
}
}

type Bitmap struct {
data []uint64
size int
}

func (b *Bitmap) Set(index int) {
if index < 0 || index >= b.size {
panic("out of range")
}
wordIndex := index / 64
bitIndex := uint(index % 64)
b.data[wordIndex] |= 1 << bitIndex
}

func (b *Bitmap) Clear(index int) {
if index < 0 || index >= b.size {
panic("out of range")
}
wordIndex := index / 64
bitIndex := uint(index % 64)
b.data[wordIndex] &= ^(1 << bitIndex)
}

func (b *Bitmap) Get(index int) bool {
if index < 0 || index >= b.size {
panic("out of range")
}
wordIndex := index / 64
bitIndex := uint(index % 64)
return (b.data[wordIndex] & (1 << bitIndex)) != 0
}

func (b *Bitmap) Size() int {
return b.size
}

func (b *Bitmap) Serialize() []byte {
p := make([]byte, len(b.data)*8)
for i, word := range b.data {
p[i*8] = byte(word >> 56)
p[i*8+1] = byte(word >> 48)
p[i*8+2] = byte(word >> 40)
p[i*8+3] = byte(word >> 32)
p[i*8+4] = byte(word >> 24)
p[i*8+5] = byte(word >> 16)
p[i*8+6] = byte(word >> 8)
p[i*8+7] = byte(word)
}
return p
}
63 changes: 17 additions & 46 deletions internal/file/cb.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,78 +14,49 @@

package file

//type PutFileCallback interface {
// Open(size int64)
// HashProgress(current, total int64)
// HashComplete(hash string, total int64)
// PutStart(current, total int64)
// PutProgress(save int64, current, total int64)
// PutComplete(total int64, putType int)
//}
//
//type emptyCallback struct{}
//
//func (e emptyCallback) Open(size int64) {}
//
//func (e emptyCallback) HashProgress(current, total int64) {}
//
//func (e emptyCallback) HashComplete(hash string, total int64) {}
//
//func (e emptyCallback) PutStart(current, total int64) {}
//
//func (e emptyCallback) PutProgress(save int64, current, total int64) {}
//
//func (e emptyCallback) PutComplete(total int64, putType int) {}
import "fmt"

type UploadFileCallback interface {
Open(size int64) // 文件打开的大小
PartSize(partSize int64, num int32) // 分片大小,数量
HashPartProgress(index int32, size int64, partHash string) // 每块分片的hash值
PartSize(partSize int64, num int) // 分片大小,数量
HashPartProgress(index int, size int64, partHash string) // 每块分片的hash值
HashPartComplete(partsHash string, fileHash string) // 分块完成,服务端标记hash和文件最终hash
UploadID(uploadID string) // 上传ID
UploadPartComplete(index int32, partSize int64, partHash string) // 上传分片进度
UploadPartComplete(index int, partSize int64, partHash string) // 上传分片进度
UploadComplete(fileSize int64, streamSize int64, storageSize int64) // 整体进度
Complete(size int64, url string, typ int32) // 上传完成
Complete(size int64, url string, typ int) // 上传完成
}

type emptyUploadCallback struct{}

func (e emptyUploadCallback) Open(size int64) {
//TODO implement me

fmt.Println("Callback Open:", size)
}

func (e emptyUploadCallback) PartSize(partSize int64, num int32) {
//TODO implement me

func (e emptyUploadCallback) PartSize(partSize int64, num int) {
fmt.Println("Callback PartSize:", partSize, num)
}

func (e emptyUploadCallback) HashPartProgress(index int32, size int64, partHash string) {
//TODO implement me

func (e emptyUploadCallback) HashPartProgress(index int, size int64, partHash string) {
//fmt.Println("Callback HashPartProgress:", index, size, partHash)
}

func (e emptyUploadCallback) HashPartComplete(partsHash string, fileHash string) {
//TODO implement me

fmt.Println("Callback HashPartComplete:", partsHash, fileHash)
}

func (e emptyUploadCallback) UploadID(uploadID string) {
//TODO implement me

fmt.Println("Callback UploadID:", uploadID)
}

func (e emptyUploadCallback) UploadPartComplete(index int32, partSize int64, partHash string) {
//TODO implement me

func (e emptyUploadCallback) UploadPartComplete(index int, partSize int64, partHash string) {
fmt.Println("Callback UploadPartComplete:", index, partSize, partHash)
}

func (e emptyUploadCallback) UploadComplete(fileSize int64, streamSize int64, storageSize int64) {
//TODO implement me

fmt.Println("Callback UploadComplete:", fileSize, streamSize, storageSize)
}

func (e emptyUploadCallback) Complete(size int64, url string, typ int32) {
//TODO implement me

func (e emptyUploadCallback) Complete(size int64, url string, typ int) {
fmt.Println("Callback Complete:", size, url, typ)
}
Loading

0 comments on commit 1184313

Please sign in to comment.