-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
267 additions
and
224 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package db | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/glebarez/go-sqlite" | ||
) | ||
|
||
// ============ BATCH =============== | ||
|
||
var _ Batch = (*sqliteBatch)(nil) | ||
|
||
type sqliteBatch struct { | ||
db *SQLiteDB | ||
tx *sql.Tx | ||
ops []operation | ||
} | ||
|
||
func newSQLiteBatch(db *SQLiteDB) *sqliteBatch { | ||
return &sqliteBatch{ | ||
db: db, | ||
ops: []operation{}, | ||
} | ||
} | ||
|
||
// Set implements Batch. | ||
func (b *sqliteBatch) Set(key, value []byte) error { | ||
if len(key) == 0 { | ||
return errKeyEmpty | ||
} | ||
if value == nil { | ||
return errValueNil | ||
} | ||
b.ops = append(b.ops, operation{opTypeSet, key, value}) | ||
return nil | ||
} | ||
|
||
// Delete implements Batch. | ||
func (b *sqliteBatch) Delete(key []byte) error { | ||
if len(key) == 0 { | ||
return errKeyEmpty | ||
} | ||
b.ops = append(b.ops, operation{opTypeDelete, key, nil}) | ||
return nil | ||
} | ||
|
||
// Write implements Batch. | ||
func (b *sqliteBatch) Write() error { | ||
if b.tx != nil { | ||
return fmt.Errorf("batch already written or not properly closed") | ||
} | ||
tx, err := b.db.db.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
b.tx = tx | ||
err = b.write(false) | ||
if err != nil { | ||
_ = b.tx.Rollback() | ||
b.tx = nil | ||
return err | ||
} | ||
return b.tx.Commit() | ||
} | ||
|
||
// WriteSync implements Batch. | ||
func (b *sqliteBatch) WriteSync() error { | ||
if b.tx != nil { | ||
return fmt.Errorf("batch already written or not properly closed") | ||
} | ||
tx, err := b.db.db.Begin() | ||
if err != nil { | ||
return err | ||
} | ||
b.tx = tx | ||
err = b.write(true) | ||
if err != nil { | ||
_ = b.tx.Rollback() | ||
b.tx = nil | ||
return err | ||
} | ||
return b.tx.Commit() | ||
} | ||
|
||
func (b *sqliteBatch) write(sync bool) error { | ||
if b.tx == nil { | ||
return fmt.Errorf("cannot write to closed batch") | ||
} | ||
|
||
for _, op := range b.ops { | ||
switch op.opType { | ||
case opTypeSet: | ||
_, err := b.tx.Exec("INSERT OR REPLACE INTO kv (key, value) VALUES (?, ?)", op.key, op.value) | ||
if err != nil { | ||
return err | ||
} | ||
case opTypeDelete: | ||
_, err := b.tx.Exec("DELETE FROM kv WHERE key = ?", op.key) | ||
if err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("unknown operation type: %v", op.opType) | ||
} | ||
} | ||
|
||
// Clear the batch after writing | ||
b.ops = nil | ||
|
||
if sync { | ||
return b.tx.Commit() | ||
} else { | ||
return nil | ||
} | ||
} | ||
|
||
// Close implements Batch. | ||
func (b *sqliteBatch) Close() error { | ||
if b.tx != nil { | ||
err := b.tx.Rollback() | ||
b.tx = nil | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
b.ops = nil | ||
return nil | ||
} |
Oops, something went wrong.