Skip to content

Commit

Permalink
refactor: small fixes and addition of comments
Browse files Browse the repository at this point in the history
  • Loading branch information
pnevezhin committed Feb 8, 2017
1 parent 6837550 commit 41f728d
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 26 deletions.
10 changes: 6 additions & 4 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
9 changes: 5 additions & 4 deletions database.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sophia

import (
"errors"
"fmt"
)

Expand All @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions document.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sophia

import (
"errors"
"unsafe"
)

Expand All @@ -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
}
5 changes: 4 additions & 1 deletion environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion transaction.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package sophia

import "errors"

// TxStatus transactional status
type TxStatus int

Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion var_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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)
Expand Down
15 changes: 2 additions & 13 deletions wrappers.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sophia

import (
"errors"
"unsafe"
)

Expand All @@ -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
Expand Down

0 comments on commit 41f728d

Please sign in to comment.