Skip to content

Commit

Permalink
Add keys method in WorkspaceDictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliu committed Oct 5, 2021
1 parent 3c716eb commit 3666f4d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
33 changes: 33 additions & 0 deletions docs/reference/Workspace.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,39 @@ func fetchWithinASnapshot<T>(_: () -> 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`
Expand Down
2 changes: 1 addition & 1 deletion sourcedocs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions src/sqlite/SQLiteWorkspaceDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,7 @@ struct SQLiteWorkspaceDictionary: WorkspaceDictionary {
storage.unlock(hashValue)
}
}

func synchronize() {
let group = DispatchGroup()
group.enter()
Expand All @@ -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..<Storage.size {
storage.lock(i)
defer { storage.unlock(i) }
keys.formUnion(storage.dictionaries[i].keys)
}
return Array(keys)
}
}

extension SQLiteWorkspaceDictionary.Storage {
Expand Down
10 changes: 10 additions & 0 deletions src/tests/DictionaryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ class DictionaryTests: XCTestCase {
XCTAssertEqual(anotherDict["stringValue", default: "1234"], "1234")
}

func testIterateKeys() {
guard var dictionary = dflat?.dictionary else { return }
dictionary["stringValue"] = "abcde"
dictionary["intValue"] = Int(10)
dictionary["doubleValue"] = Double(12.3)
let keys = dictionary.keys
XCTAssertEqual(Set(keys), Set(["stringValue", "intValue", "doubleValue"]))
}

static let allTests = [
("testReadWriteReadCodableObject", testReadWriteReadCodableObject),
("testReadWriteReadFlatBuffersObject", testReadWriteReadFlatBuffersObject),
Expand All @@ -185,5 +194,6 @@ class DictionaryTests: XCTestCase {
("testReadWriteReadFloat", testReadWriteReadFloat),
("testReadWriteReadDouble", testReadWriteReadDouble),
("testReadWriteReadString", testReadWriteReadString),
("testIterateKeys", testIterateKeys),
]
}

0 comments on commit 3666f4d

Please sign in to comment.