-
Notifications
You must be signed in to change notification settings - Fork 3
Implement session changeset undo #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
andersmurphy
wants to merge
1
commit into
master
Choose a base branch
from
implement-session-changeset-undo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,134 @@ | ||
| (ns sqlite4clj.session | ||
| (:require | ||
| [coffi.ffi :as ffi :refer [defcfn]] | ||
| [coffi.mem :as mem] | ||
| [sqlite4clj.impl.api :as api])) | ||
|
|
||
| ;; ========= SESSION extension ========= | ||
| ;; https://sqlite.org/session/changegroup.html | ||
|
|
||
| (defcfn session-create | ||
| "sqlite3session_create" | ||
| [::mem/pointer ::mem/c-string ::mem/pointer] ::mem/int | ||
| sqlite3session-create-native | ||
| [pdb] | ||
| (with-open [arena (mem/confined-arena)] | ||
| (let [ppSession (mem/alloc-instance ::mem/pointer arena) | ||
| code (sqlite3session-create-native pdb "main" ppSession)] | ||
| (if (api/sqlite-ok? code) | ||
| (mem/deserialize-from ppSession ::mem/pointer) | ||
| (throw (api/sqlite-ex-info pdb code {})))))) | ||
|
|
||
| (defcfn session-attach | ||
| sqlite3session_attach | ||
| [::mem/pointer ::mem/c-string] ::mem/int) | ||
|
|
||
| (defcfn session-delete | ||
| sqlite3session_delete | ||
| [::mem/pointer] ::mem/int) | ||
|
|
||
| (defcfn session-changeset | ||
| "sqlite3session_changeset" | ||
| [::mem/pointer ::mem/pointer ::mem/pointer] ::mem/int | ||
| sqlite3session-patchset-native | ||
| [pdb pSession] | ||
| (with-open [arena (mem/confined-arena)] | ||
| (let [pnPatchset (mem/alloc-instance ::mem/int arena) | ||
| ppPatchset (mem/alloc-instance ::mem/pointer arena) | ||
| code (sqlite3session-patchset-native pSession | ||
| pnPatchset | ||
| ppPatchset)] | ||
| (if (api/sqlite-ok? code) | ||
| [(mem/deserialize-from pnPatchset ::mem/int) | ||
| (mem/deserialize-from ppPatchset ::mem/pointer)] | ||
| (throw (api/sqlite-ex-info pdb code {})))))) | ||
|
|
||
| (defcfn changeset-invert | ||
| "sqlite3changeset_invert" | ||
| [::mem/int ::mem/pointer | ||
| ::mem/pointer ::mem/pointer] ::mem/int | ||
| sqlite3changeset-invert-native | ||
| [pdb nInSet pInSet] | ||
| (with-open [arena (mem/confined-arena)] | ||
| (let [pnOutSet (mem/alloc-instance ::mem/int arena) | ||
| ppOutSet (mem/alloc-instance ::mem/pointer arena) | ||
| code (sqlite3changeset-invert-native | ||
| nInSet pInSet | ||
| pnOutSet ppOutSet)] | ||
| (if (api/sqlite-ok? code) | ||
| [(mem/deserialize-from pnOutSet ::mem/int) | ||
| (mem/deserialize-from ppOutSet ::mem/pointer)] | ||
| (throw (api/sqlite-ex-info pdb code {})))))) | ||
|
|
||
| (defcfn changeset-apply | ||
| sqlite3changeset_apply | ||
| [::mem/pointer ;; db | ||
| ::mem/int ;; size of changeset | ||
| ::mem/pointer ;; changeset | ||
| ::mem/pointer ;; xFilter | ||
| ::mem/pointer ;; xConflict | ||
| ::mem/pointer ;; First arg to conflict | ||
| ] ::mem/int) | ||
|
|
||
| (defn new-session | ||
| "Creates a session and attaches it to the database." | ||
| [conn] | ||
| (let [pdb (:pdb conn) | ||
| pSession (session-create pdb)] | ||
| (session-attach pSession nil) | ||
| (atom pSession))) | ||
|
|
||
| (defn cancel-session | ||
| "Cancels session without undoing changes." | ||
| [session] | ||
| (when-let [session @session] | ||
| (session-delete session))) | ||
|
|
||
| (defn undo-session | ||
| "Undoes the current session and deletes it." | ||
| [conn session] | ||
| (when-let [pSession @session] | ||
| (let [pdb (:pdb conn) | ||
| [nSet pSet] (session-changeset pdb pSession) | ||
| _ (session-delete pSession) | ||
| [nInvertSet pInvertSet] (changeset-invert pdb nSet pSet)] | ||
| (with-open [arena (mem/confined-arena)] | ||
| (let [x-conflict | ||
| ;; Fails if there's a conflict (there should never be a conflict) | ||
| ;; when using undo-session correctly. | ||
| (mem/serialize (fn [_ _ _] (int 0)) | ||
| [::ffi/fn | ||
| [::mem/pointer ::mem/int ::mem/pointer] | ||
| ::mem/int | ||
| :raw-fn? true] | ||
| arena)] | ||
| (changeset-apply pdb nInvertSet pInvertSet nil x-conflict nil))) | ||
| (api/free pSet) | ||
| (api/free pInvertSet) | ||
| (reset! session nil)))) | ||
|
|
||
| (comment | ||
| (require '[sqlite4clj.core :as d]) | ||
|
|
||
| (defonce db | ||
| (d/init-db! "database.db" | ||
| {:read-only true | ||
| :pool-size 4 | ||
| :pragma {:foreign_keys false}})) | ||
|
|
||
| (def reader (db :reader)) | ||
| (def writer (db :writer)) | ||
|
|
||
| (d/q writer | ||
| ["CREATE TABLE IF NOT EXISTS bar(id INT PRIMARY KEY, data BLOB)"]) | ||
|
|
||
| (let [session (d/with-conn [conn (:writer db)] | ||
| (new-session conn))] | ||
| (println (d/q (:reader db) ["select count(*) from bar"])) | ||
| (d/q (:writer db) | ||
| ["insert into bar (id, data) VALUES (?, ?)" | ||
| (str (random-uuid)) 34]) | ||
| (println (d/q (:reader db) ["select count(*) from bar"])) | ||
| (d/with-conn [conn (:writer db)] | ||
| (undo-session conn session)) | ||
| (println (d/q (:reader db) ["select count(*) from bar"])))) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
think you have the wrong link :)
https://sqlite.org/sessionintro.html