-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVolatileStoreCollection.swift
62 lines (51 loc) · 1.27 KB
/
VolatileStoreCollection.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Storage.swift
// Noze.io / mod_swift / MacroExpress
//
// Created by Helge Hess on 03/06/16.
// Copyright © 2016-2021 ZeeZide GmbH. All rights reserved.
//
/// A collection store which just stores everything in memory. Data added will
/// be gone when the process is stopped.
///
/// Note: The operations in here are all synchronous for simplicity. This is not
/// how a regular store would usually work.
/// Check out todo-mvc-redis for a store with async operations.
///
class VolatileStoreCollection<T> {
var sequence = 1337
var changeCounter = 0
var objects = [ Int : T ]()
init() {}
func nextKey() -> Int {
sequence += 1
return sequence
}
func getAll() -> [ T ] {
return Array(objects.values)
}
func get(ids keys: [ Int ]) -> [ T ] {
var matches = [T]()
for key in keys {
if let object = objects[key] {
matches.append(object)
}
}
return matches
}
func get(id key: Int) -> T? {
return objects[key]
}
func delete(id key: Int) {
changeCounter += 1
objects.removeValue(forKey: key)
}
func update(id key: Int, value v: T) {
changeCounter += 1
objects[key] = v // value type!
}
func deleteAll() {
changeCounter += 1
objects.removeAll()
}
}