Skip to content

Commit 435f581

Browse files
committed
fix(storage): sequence of array operation is not right
1 parent b8c2e14 commit 435f581

File tree

1 file changed

+15
-154
lines changed

1 file changed

+15
-154
lines changed

Sources/Foundation/Operation.swift

Lines changed: 15 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -464,152 +464,19 @@ class OperationReducer {
464464
- REMOVE
465465
*/
466466
class Array: OperationReducer {
467-
var operationTable: [Operation.Name:Operation] = [:]
468-
467+
var operationSequence: [Operation] = []
468+
469469
override class func validOperationNames() -> [Operation.Name] {
470470
return [.set, .delete, .add, .addUnique, .remove]
471471
}
472-
472+
473473
override func reduce(_ operation: Operation) throws {
474474
try super.validate(operation)
475-
476-
switch operation.name {
477-
case .set:
478-
reset()
479-
setOperation(operation)
480-
case .delete:
481-
reset()
482-
setOperation(operation)
483-
case .add:
484-
try removeObjects(operation, .remove)
485-
try removeObjects(operation, .addUnique)
486-
487-
if hasOperation(.set) || hasOperation(.delete) {
488-
try addObjects(operation, .set)
489-
} else {
490-
try addObjects(operation, .add)
491-
}
492-
case .addUnique:
493-
try removeObjects(operation, .add)
494-
try removeObjects(operation, .remove)
495-
496-
if hasOperation(.set) || hasOperation(.delete) {
497-
try addObjects(operation, .set, unique: true)
498-
} else {
499-
try addObjects(operation, .addUnique, unique: true)
500-
}
501-
case .remove:
502-
try removeObjects(operation, .set)
503-
try removeObjects(operation, .add)
504-
try removeObjects(operation, .addUnique)
505-
506-
try addObjects(operation, .remove, unique: true)
507-
default:
508-
break
509-
}
475+
self.operationSequence.append(operation)
510476
}
511-
477+
512478
override func operations() -> [Operation] {
513-
var operationTable = self.operationTable
514-
removeEmptyOperation(&operationTable, [.add, .addUnique, .remove])
515-
return Swift.Array(operationTable.values)
516-
}
517-
518-
/**
519-
Remove empty operations from operation table.
520-
521-
- parameter operationTable: The operation table.
522-
- parameter operationNames: A set of operation names that specify which operation should be removed from operation table if it is empty.
523-
*/
524-
func removeEmptyOperation(_ operationTable: inout [Operation.Name:Operation], _ operationNames:Set<Operation.Name>) {
525-
operationNames.forEach { (operationName) in
526-
if let operation = operationTable[operationName] {
527-
if !hasObjects(operation) {
528-
operationTable[operationName] = nil
529-
}
530-
}
531-
}
532-
}
533-
534-
/**
535-
Check whether an operation has objects.
536-
537-
- parameter operation: The operation.
538-
539-
- returns: true if operation has objects, false otherwise.
540-
*/
541-
func hasObjects(_ operation: Operation) -> Bool {
542-
if let array = operation.value as? LCArray {
543-
return !array.value.isEmpty
544-
} else {
545-
return false
546-
}
547-
}
548-
549-
/**
550-
Check whether an operation existed for given operation name.
551-
552-
- parameter name: The operation name.
553-
554-
- returns: true if operation existed for operation name, false otherwise.
555-
*/
556-
func hasOperation(_ name: Operation.Name) -> Bool {
557-
return operationTable[name] != nil
558-
}
559-
560-
/**
561-
Remove objects from operation specified by operation name.
562-
563-
- parameter operation: The operation that contains objects to be removed.
564-
- parameter operationName: The operation name that specifies operation from which the objects will be removed.
565-
*/
566-
func removeObjects(_ operation: Operation, _ operationName: Operation.Name) throws {
567-
guard let rhs = operation.value as? LCArray else {
568-
return
569-
}
570-
guard let lhs = operationTable[operationName]?.value as? LCArray else {
571-
return
572-
}
573-
574-
let operation = try Operation(name: operation.name, key: operation.key, value: try lhs.differ(rhs))
575-
576-
setOperation(operation)
577-
}
578-
579-
/**
580-
Add objects in an operation from operation specified by operation name.
581-
582-
- parameter operation: The operation that contains objects to be removed.
583-
- parameter operationName: The operation name that specifies operation from which the objects will be removed.
584-
*/
585-
func addObjects(_ operation: Operation, _ operationName: Operation.Name, unique: Bool = false) throws {
586-
guard var value = operation.value else {
587-
return
588-
}
589-
590-
if let baseValue = operationTable[operationName]?.value as? LCArray {
591-
value = try baseValue.concatenate(value, unique: unique)
592-
}
593-
594-
let operation = try Operation(name: operationName, key: operation.key, value: value)
595-
596-
setOperation(operation)
597-
}
598-
599-
/**
600-
Set operation to operation table.
601-
602-
- parameter operation: The operation to set.
603-
*/
604-
func setOperation(_ operation: Operation) {
605-
self.operationTable[operation.name] = operation
606-
}
607-
608-
/**
609-
Reset operation table.
610-
*/
611-
func reset() {
612-
self.operationTable = [:]
479+
return self.operationSequence
613480
}
614481
}
615482

@@ -625,21 +492,15 @@ class OperationReducer {
625492
override class func validOperationNames() -> [Operation.Name] {
626493
return [.addRelation, .removeRelation]
627494
}
495+
}
496+
}
628497

629-
override func reduce(_ operation: Operation) throws {
630-
try super.validate(operation)
631-
632-
switch operation.name {
633-
case .addRelation:
634-
try removeObjects(operation, .removeRelation)
635-
try addObjects(operation, .addRelation)
636-
case .removeRelation:
637-
try removeObjects(operation, .addRelation)
638-
try addObjects(operation, .removeRelation, unique: true)
639-
default:
640-
break
641-
}
642-
}
643-
/* Stub class. */
498+
private extension LCError {
499+
500+
static func malformedKey(_ key: String) -> LCError {
501+
return LCError(
502+
code: .malformedData,
503+
reason: "Malformed key.",
504+
userInfo: ["key": key])
644505
}
645506
}

0 commit comments

Comments
 (0)