Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Addressed issues uncovered during a personal review
Browse files Browse the repository at this point in the history
  • Loading branch information
jalextowle committed Aug 20, 2020
1 parent 499b272 commit 1df11d5
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test-key-value-stores-go:

.PHONY: test-key-value-stores-wasm
test-key-value-stores-wasm:
WASM_INIT_FILE="$$(pwd)/packages/mesh-browser-shim/dist/browser_shim.js" GOOS=js GOARCH=wasm ENABLE_KEY_VALUE_TESTS=true go test ./db -tags=browser -exec="$$GOPATH/bin/wasmbrowsertest"
WASM_INIT_FILE="$$(pwd)/packages/mesh-browser-shim/dist/browser_shim.js" GOOS=js GOARCH=wasm ENABLE_KEY_VALUE_TESTS=true go test ./db -timeout 20m -tags=browser -exec="$$GOPATH/bin/wasmbrowsertest"


.PHONY: test-go-serial
Expand Down
89 changes: 61 additions & 28 deletions db/dexie_datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package db

import (
"context"
"fmt"
"syscall/js"

"github.com/0xProject/0x-mesh/packages/mesh-browser/go/jsutil"
Expand Down Expand Up @@ -54,12 +53,11 @@ type Operation struct {
}

func (o *Operation) JSValue() js.Value {
uint8Array := js.Global().Get("Uint8Array").New(len(o.value))
js.CopyBytesToJS(uint8Array, o.value)
jsBytes, _ := jsutil.CopyBytesToJS(o.value)
return js.ValueOf(map[string]interface{}{
"operationType": int(o.operationType),
"key": o.key.String(),
"value": uint8Array,
"value": jsBytes,
})
}

Expand Down Expand Up @@ -97,66 +95,87 @@ func (b *Batch) Delete(key ds.Key) error {

// Commit performs a batch of operations on the Dexie datastore. In this implementation,
// all of these operations occur in the same transactional context.
func (b *Batch) Commit() error {
func (b *Batch) Commit() (err error) {
defer func() {
if r := recover(); r != nil {
err = jsutil.RecoverError(r)
}
}()

convertibleOperations := make([]interface{}, len(b.operations))
for i, operation := range b.operations {
convertibleOperations[i] = interface{}(operation)
}
_, err := jsutil.AwaitPromiseContext(b.ctx, b.dexieStore.Call("commitAsync", convertibleOperations))
_, err = jsutil.AwaitPromiseContext(b.ctx, b.dexieStore.Call("commitAsync", convertibleOperations))
if err != nil {
return convertJSError(err)
}
return nil
}

func (d *Datastore) Get(key ds.Key) ([]byte, error) {
// FIXME - Remove Defer
var jsResult js.Value
func (d *Datastore) Get(key ds.Key) (_ []byte, err error) {
defer func() {
if r := recover(); r != nil {
fmt.Println(jsResult.String())
panic(r)
err = jsutil.RecoverError(r)
}
}()
var err error
jsResult, err = jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("getAsync", key.String()))

jsResult, err := jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("getAsync", key.String()))
if err != nil {
return nil, convertJSError(err)
}
result := make([]byte, jsResult.Get("length").Int())
js.CopyBytesToGo(result, jsResult)
return result, nil
return jsutil.CopyBytesToGo(jsResult)
}

func (d *Datastore) Has(key ds.Key) (bool, error) {
func (d *Datastore) Has(key ds.Key) (_ bool, err error) {
defer func() {
if r := recover(); r != nil {
err = jsutil.RecoverError(r)
}
}()

jsResult, err := jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("hasAsync", key.String()))
if err != nil {
return false, convertJSError(err)
}
return jsResult.Bool(), nil
}

func (d *Datastore) GetSize(key ds.Key) (size int, err error) {
func (d *Datastore) GetSize(key ds.Key) (_ int, err error) {
defer func() {
if r := recover(); r != nil {
err = jsutil.RecoverError(r)
}
}()

jsResult, err := jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("getSizeAsync", key.String()))
if err != nil {
return -1, convertJSError(err)
}
return jsResult.Int(), nil
}

func (d *Datastore) Query(q dsq.Query) (dsq.Results, error) {
func (d *Datastore) Query(q dsq.Query) (_ dsq.Results, err error) {
defer func() {
if r := recover(); r != nil {
err = jsutil.RecoverError(r)
}
}()

jsResults, err := jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("queryAsync", q.Prefix))
if err != nil {
return nil, convertJSError(err)
}
entries := make([]dsq.Entry, jsResults.Get("length").Int())
for i := 0; i < jsResults.Get("length").Int(); i++ {
jsResult := jsResults.Index(i)
value := make([]byte, jsResult.Get("size").Int())
js.CopyBytesToGo(value, jsResult.Get("value"))
jsBytes, err := jsutil.CopyBytesToGo(jsResult.Get("value"))
if err != nil {
return nil, err
}
entries[i] = dsq.Entry{
Key: jsResult.Get("key").String(),
Value: value,
Value: jsBytes,
Size: jsResult.Get("size").Int(),
}
}
Expand Down Expand Up @@ -185,18 +204,32 @@ func (d *Datastore) Query(q dsq.Query) (dsq.Results, error) {
return dsq.ResultsWithEntries(q, filteredEntries), nil
}

func (d *Datastore) Put(key ds.Key, value []byte) error {
uint8Array := js.Global().Get("Uint8Array").New(len(value))
js.CopyBytesToJS(uint8Array, value)
_, err := jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("putAsync", key.String(), uint8Array))
func (d *Datastore) Put(key ds.Key, value []byte) (err error) {
defer func() {
if r := recover(); r != nil {
err = jsutil.RecoverError(r)
}
}()

jsBytes, err := jsutil.CopyBytesToJS(value)
if err != nil {
return err
}
_, err = jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("putAsync", key.String(), jsBytes))
if err != nil {
return convertJSError(err)
}
return nil
}

func (d *Datastore) Delete(key ds.Key) error {
_, err := jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("deleteAsync", key.String()))
func (d *Datastore) Delete(key ds.Key) (err error) {
defer func() {
if r := recover(); r != nil {
err = jsutil.RecoverError(r)
}
}()

_, err = jsutil.AwaitPromiseContext(d.ctx, d.dexieStore.Call("deleteAsync", key.String()))
if err != nil {
return convertJSError(err)
}
Expand Down
51 changes: 18 additions & 33 deletions db/dexie_implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"math/big"
"path/filepath"
"runtime/debug"
"syscall/js"
"time"

Expand Down Expand Up @@ -66,7 +65,7 @@ func New(ctx context.Context, opts *Options) (database *DB, err error) {
}
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
newDexieDatabase := js.Global().Get("__mesh_dexie_newDatabase__")
Expand Down Expand Up @@ -110,7 +109,7 @@ func (db *DB) DHTStore() ds.Batching {
func (db *DB) AddOrders(orders []*types.OrderWithMetadata) (alreadyStored []common.Hash, added []*types.OrderWithMetadata, removed []*types.OrderWithMetadata, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand Down Expand Up @@ -146,7 +145,7 @@ func (db *DB) AddOrders(orders []*types.OrderWithMetadata) (alreadyStored []comm
func (db *DB) GetOrder(hash common.Hash) (order *types.OrderWithMetadata, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -165,7 +164,7 @@ func (db *DB) GetOrder(hash common.Hash) (order *types.OrderWithMetadata, err er
func (db *DB) GetOrderStatuses(hashes []common.Hash) (statuses []*StoredOrderStatus, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand Down Expand Up @@ -201,7 +200,7 @@ func (db *DB) FindOrders(query *OrderQuery) (orders []*types.OrderWithMetadata,
}
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -224,7 +223,7 @@ func (db *DB) CountOrders(query *OrderQuery) (count int, err error) {
}
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -240,7 +239,7 @@ func (db *DB) CountOrders(query *OrderQuery) (count int, err error) {
func (db *DB) DeleteOrder(hash common.Hash) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -258,7 +257,7 @@ func (db *DB) DeleteOrders(query *OrderQuery) (deletedOrders []*types.OrderWithM
}
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -278,7 +277,7 @@ func (db *DB) DeleteOrders(query *OrderQuery) (deletedOrders []*types.OrderWithM
func (db *DB) UpdateOrder(hash common.Hash, updateFunc func(existingOrder *types.OrderWithMetadata) (updatedOrder *types.OrderWithMetadata, err error)) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand Down Expand Up @@ -311,7 +310,7 @@ func (db *DB) UpdateOrder(hash common.Hash, updateFunc func(existingOrder *types
func (db *DB) AddMiniHeaders(miniHeaders []*types.MiniHeader) (added []*types.MiniHeader, removed []*types.MiniHeader, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -331,7 +330,7 @@ func (db *DB) AddMiniHeaders(miniHeaders []*types.MiniHeader) (added []*types.Mi
func (db *DB) ResetMiniHeaders(newMiniHeaders []*types.MiniHeader) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -347,7 +346,7 @@ func (db *DB) ResetMiniHeaders(newMiniHeaders []*types.MiniHeader) (err error) {
func (db *DB) GetMiniHeader(hash common.Hash) (miniHeader *types.MiniHeader, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -365,7 +364,7 @@ func (db *DB) FindMiniHeaders(query *MiniHeaderQuery) (miniHeaders []*types.Mini
}
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -381,7 +380,7 @@ func (db *DB) FindMiniHeaders(query *MiniHeaderQuery) (miniHeaders []*types.Mini
func (db *DB) DeleteMiniHeader(hash common.Hash) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -399,7 +398,7 @@ func (db *DB) DeleteMiniHeaders(query *MiniHeaderQuery) (deleted []*types.MiniHe
}
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -415,7 +414,7 @@ func (db *DB) DeleteMiniHeaders(query *MiniHeaderQuery) (deleted []*types.MiniHe
func (db *DB) GetMetadata() (metadata *types.Metadata, err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -434,7 +433,7 @@ func (db *DB) GetMetadata() (metadata *types.Metadata, err error) {
func (db *DB) SaveMetadata(metadata *types.Metadata) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -454,7 +453,7 @@ func (db *DB) SaveMetadata(metadata *types.Metadata) (err error) {
func (db *DB) UpdateMetadata(updateFunc func(oldmetadata *types.Metadata) (newMetadata *types.Metadata)) (err error) {
defer func() {
if r := recover(); r != nil {
err = recoverError(r)
err = jsutil.RecoverError(r)
}
}()
start := time.Now()
Expand All @@ -481,20 +480,6 @@ func (db *DB) UpdateMetadata(updateFunc func(oldmetadata *types.Metadata) (newMe
return nil
}

func recoverError(e interface{}) error {
if e != nil {
debug.PrintStack()
}
switch e := e.(type) {
case error:
return e
case string:
return errors.New(e)
default:
return fmt.Errorf("unexpected JavaScript error: (%T) %v", e, e)
}
}

func convertJSError(e error) error {
switch e := e.(type) {
case js.Error:
Expand Down
Loading

0 comments on commit 1df11d5

Please sign in to comment.