From 41f728d8553d4797acfeea85cf59238dbfa84274 Mon Sep 17 00:00:00 2001 From: pnevezhin Date: Wed, 8 Feb 2017 16:08:00 +0300 Subject: [PATCH] refactor: small fixes and addition of comments --- cursor.go | 10 ++++++---- database.go | 9 +++++---- document.go | 8 ++++++-- environment.go | 5 ++++- transaction.go | 7 ++++++- var_store.go | 2 +- wrappers.go | 15 ++------------- 7 files changed, 30 insertions(+), 26 deletions(-) diff --git a/cursor.go b/cursor.go index 5772a35..61efa05 100644 --- a/cursor.go +++ b/cursor.go @@ -56,7 +56,10 @@ func (cur *cursor) Close() error { } cur.doc.Free() cur.closed = true - return spDestroy(cur.ptr) + if !spDestroy(cur.ptr) { + return errors.New("cursor: failed to close") + } + return nil } // Next fetches the next row for the cursor @@ -69,7 +72,6 @@ func (cur *cursor) Next() *Document { if ptr == nil { return nil } - d := newDocument(ptr, 0) - cur.doc = d - return d + cur.doc.ptr = ptr + return cur.doc } diff --git a/database.go b/database.go index a274146..2c4d182 100644 --- a/database.go +++ b/database.go @@ -1,6 +1,7 @@ package sophia import ( + "errors" "fmt" ) @@ -25,12 +26,12 @@ func (db *Database) Document() *Document { // Cursor returns a Cursor for iterating over rows in the database func (db *Database) Cursor(doc *Document) (Cursor, error) { + if nil == doc { + return nil, errors.New("failed to create cursor: nil Document") + } cPtr := spCursor(db.env.ptr) if nil == cPtr { - return nil, fmt.Errorf("failed create cursor: err=%v", db.env.Error()) - } - if nil == doc { - return nil, fmt.Errorf("failed get document: err=%v", db.env.Error()) + return nil, fmt.Errorf("failed to create cursor: err=%v", db.env.Error()) } cur := &cursor{ ptr: cPtr, diff --git a/document.go b/document.go index 2e04b4c..428c3c7 100644 --- a/document.go +++ b/document.go @@ -1,6 +1,7 @@ package sophia import ( + "errors" "unsafe" ) @@ -14,6 +15,9 @@ func newDocument(ptr unsafe.Pointer, size int) *Document { } } -func (d *Document) Destroy() { - spDestroy(d.ptr) +func (d *Document) Destroy() error { + if !spDestroy(d.ptr) { + return errors.New("document: failed to destroy") + } + return nil } diff --git a/environment.go b/environment.go index 907a547..2c7baa2 100644 --- a/environment.go +++ b/environment.go @@ -55,7 +55,10 @@ func (env *Environment) NewDatabase(name string, schema *Schema) (*Database, err // Close on any Environment created with NewEnvironment. func (env *Environment) Close() error { env.Free() - return spDestroy(env.ptr) + if !spDestroy(env.ptr) { + return errors.New("env: failed to close") + } + return nil } // Open opens environment diff --git a/transaction.go b/transaction.go index 5135255..3708aa0 100644 --- a/transaction.go +++ b/transaction.go @@ -1,5 +1,7 @@ package sophia +import "errors" + // TxStatus transactional status type TxStatus int @@ -35,5 +37,8 @@ func (tx *Transaction) Commit() TxStatus { // Rollback rollbacks transaction and destroy transaction object. func (tx *Transaction) Rollback() error { - return spDestroy(tx.ptr) + if !spDestroy(tx.ptr) { + return errors.New("tx: failed to rollback") + } + return nil } diff --git a/var_store.go b/var_store.go index 2950f99..3d17160 100644 --- a/var_store.go +++ b/var_store.go @@ -65,7 +65,6 @@ func (s *varStore) GetString(path string, size *int) string { } func (s *varStore) GetObject(path string) unsafe.Pointer { - return spGetObject(s.ptr, getCStringFromCache(path)) } @@ -74,6 +73,7 @@ func (s *varStore) GetInt(path string) int64 { } // Free frees allocated memory for all C variables, that were in this store +// This always should be called to prevent memory leaks func (s *varStore) Free() { for _, f := range s.pointers { free(f) diff --git a/wrappers.go b/wrappers.go index 1651c8b..d2d2336 100644 --- a/wrappers.go +++ b/wrappers.go @@ -1,7 +1,6 @@ package sophia import ( - "errors" "unsafe" ) @@ -25,23 +24,13 @@ extern void *sp_cursor(void*); extern void *sp_begin(void*); extern int sp_prepare(void*); extern int sp_commit(void*); -char* pointer_to_string(void* ptr) -{ - return (char*)ptr; -} */ import "C" // spDestroy wrapper for sp_destroy // destroys C sophia object -func spDestroy(p unsafe.Pointer) error { - if nil == p { - return nil - } - if 0 != C.sp_destroy(p) { - return errors.New("failed close resource") - } - return nil +func spDestroy(p unsafe.Pointer) bool { + return C.sp_destroy(p) == 0 } // spSetString wrapper for sp_setstring for common cases