From 3666f4d118c2bf4472c5eedeacd72bb7c5b2a646 Mon Sep 17 00:00:00 2001 From: Liu Liu Date: Mon, 4 Oct 2021 22:52:59 -0400 Subject: [PATCH] Add keys method in WorkspaceDictionary --- docs/reference/Workspace.md | 33 ++++++++++++++++++++++ sourcedocs.sh | 2 +- src/Workspace.swift | 5 ++++ src/sqlite/SQLiteWorkspaceDictionary.swift | 12 ++++++++ src/tests/DictionaryTests.swift | 10 +++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/docs/reference/Workspace.md b/docs/reference/Workspace.md index 42d1f7f0e..4a067e69e 100644 --- a/docs/reference/Workspace.md +++ b/docs/reference/Workspace.md @@ -24,6 +24,39 @@ func fetchWithinASnapshot(_: () -> T) -> T Provide a consistent view for fetching multiple objects at once. +**PROTOCOL** + +# `WorkspaceDictionary` + +```swift +public protocol WorkspaceDictionary +``` + +## Properties +### `keys` + +```swift +var keys: [String] +``` + +Return all keys available in the dictionary. This is an expensive (for this dictionary) +method as it fetches from disk, from in-memory structures, and acquire locks if needed. + +## Methods +### `synchronize()` + +```swift +func synchronize() +``` + +Force current thread to wait until everything has been written to disk. +Note that this function forces wait to disk, but not synchronize across +threads. You could have one thread called synchronize while another thread +is still holding their own lock to update in-memory value. It doesn't guarantee +the first thread will wait the second thread's dictionary[key] = value to finish. +This method only guarantees all writes on current thread done. + + **PROTOCOL** # `Workspace` diff --git a/sourcedocs.sh b/sourcedocs.sh index bc4de7321..92f01042b 100755 --- a/sourcedocs.sh +++ b/sourcedocs.sh @@ -3,6 +3,6 @@ sourcedocs generate --spm-module Dflat sourcedocs generate --spm-module SQLiteDflat -(cat Documentation/Reference/protocols/Queryable.md ; echo -e "\n" ; cat Documentation/Reference/protocols/Workspace.md ; echo -e "\n" ; cat Documentation/Reference/classes/QueryBuilder.md ; echo -e "\n" ; cat Documentation/Reference/protocols/WorkspaceSubscription.md ; echo -e "\n" ; cat Documentation/Reference/enums/SubscribedObject.md ; echo -e "\n" ; cat Documentation/Reference/classes/QueryPublisherBuilder.md) > docs/reference/Workspace.md +(cat Documentation/Reference/protocols/Queryable.md ; echo -e "\n" ; cat Documentation/Reference/protocols/WorkspaceDictionary.md ; echo -e "\n" ; cat Documentation/Reference/protocols/Workspace.md ; echo -e "\n" ; cat Documentation/Reference/classes/QueryBuilder.md ; echo -e "\n" ; cat Documentation/Reference/protocols/WorkspaceSubscription.md ; echo -e "\n" ; cat Documentation/Reference/enums/SubscribedObject.md ; echo -e "\n" ; cat Documentation/Reference/classes/QueryPublisherBuilder.md) > docs/reference/Workspace.md (cat Documentation/Reference/protocols/TransactionContext.md ; echo -e "\n" ; cat Documentation/Reference/extensions/TransactionContext.md ; echo -e "\n" ; cat Documentation/Reference/enums/TransactionError.md) > docs/reference/TransactionContext.md (cat Documentation/Reference/classes/SQLiteWorkspace.md ; echo -e "\n" ; cat Documentation/Reference/enums/SQLiteWorkspace.FileProtectionLevel.md ; echo -e "\n" ; cat Documentation/Reference/enums/SQLiteWorkspace.WriteConcurrency.md ; echo -e "\n" ; cat Documentation/Reference/enums/SQLiteWorkspace.Synchronous.md) > docs/reference/SQLiteWorkspace.md diff --git a/src/Workspace.swift b/src/Workspace.swift index 47d78ef66..055f56a9b 100644 --- a/src/Workspace.swift +++ b/src/Workspace.swift @@ -52,6 +52,11 @@ public protocol WorkspaceDictionary { * This method only guarantees all writes on current thread done. */ func synchronize() + /** + * Return all keys available in the dictionary. This is an expensive (for this dictionary) + * method as it fetches from disk, from in-memory structures, and acquire locks if needed. + */ + var keys: [String] { get } } public protocol Workspace: Queryable { diff --git a/src/sqlite/SQLiteWorkspaceDictionary.swift b/src/sqlite/SQLiteWorkspaceDictionary.swift index f1cc06be5..c559edea2 100644 --- a/src/sqlite/SQLiteWorkspaceDictionary.swift +++ b/src/sqlite/SQLiteWorkspaceDictionary.swift @@ -451,6 +451,7 @@ struct SQLiteWorkspaceDictionary: WorkspaceDictionary { storage.unlock(hashValue) } } + func synchronize() { let group = DispatchGroup() group.enter() @@ -461,6 +462,17 @@ struct SQLiteWorkspaceDictionary: WorkspaceDictionary { }) group.wait() } + + var keys: [String] { + let items = workspace.fetch(for: DictItem.self).where(DictItem.namespace == storage.namespace) + var keys = Set(items.map { $0.key }) + for i in 0..