Skip to content

Commit 8df1ccc

Browse files
authored
feat(ffi-refactor): replace sequence id with pointer to proposals (8/8) (#1221)
This is the final PR in the series to refactor the FFI layer to be more rust and also a bit more Gooey. This replaces the serial atomic id for proposals with a pointer to the actual structures. The wrapper that includes the db handle is only necessary in order to do the cached view shenanigans. I will come back to that later.
1 parent 14ed185 commit 8df1ccc

File tree

11 files changed

+603
-764
lines changed

11 files changed

+603
-764
lines changed

ffi/firewood.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ func (db *Database) Propose(keys, vals [][]byte) (*Proposal, error) {
152152
return nil, err
153153
}
154154

155-
val := C.fwd_propose_on_db(db.handle, kvp)
156-
return newProposal(db.handle, &val)
155+
return getProposalFromProposalResult(C.fwd_propose_on_db(db.handle, kvp), db)
157156
}
158157

159158
// Get retrieves the value for the given key. It always returns a nil error.
@@ -231,3 +230,25 @@ func (db *Database) Revision(root []byte) (*Revision, error) {
231230

232231
return &Revision{database: db, root: root}, nil
233232
}
233+
234+
// Close releases the memory associated with the Database.
235+
//
236+
// This is not safe to call while there are any outstanding Proposals. All proposals
237+
// must be freed or committed before calling this.
238+
//
239+
// This is safe to call if the pointer is nil, in which case it does nothing. The
240+
// pointer will be set to nil after freeing to prevent double free. However, it is
241+
// not safe to call this method concurrently from multiple goroutines.
242+
func (db *Database) Close() error {
243+
if db.handle == nil {
244+
return nil
245+
}
246+
247+
if err := getErrorFromVoidResult(C.fwd_close_db(db.handle)); err != nil {
248+
return fmt.Errorf("unexpected error when closing database: %w", err)
249+
}
250+
251+
db.handle = nil // Prevent double free
252+
253+
return nil
254+
}

ffi/firewood.h

Lines changed: 137 additions & 102 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)