Commit 6f5da415 authored by Ignacio Corderi's avatar Ignacio Corderi
Browse files

Cleaner NotFound support

parent 8091ef8e
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@
		3EFB7F221B7A7A8000988886 /* Utils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F211B7A7A7F00988886 /* Utils.swift */; };
		3EFB7F251B7A977500988886 /* Common.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F241B7A977500988886 /* Common.swift */; };
		3EFB7F271B7AA45A00988886 /* Get.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F261B7AA45A00988886 /* Get.swift */; };
		3EFB7F291B7AB06800988886 /* Delete.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3EFB7F281B7AB06800988886 /* Delete.swift */; };
		921636AE408C3549DE7A1141 /* Pods.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 382D1C00FC0EC4F1BF676411 /* Pods.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
/* End PBXBuildFile section */

@@ -48,6 +49,7 @@
		3EFB7F211B7A7A7F00988886 /* Utils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utils.swift; sourceTree = "<group>"; };
		3EFB7F241B7A977500988886 /* Common.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Common.swift; sourceTree = "<group>"; };
		3EFB7F261B7AA45A00988886 /* Get.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Get.swift; sourceTree = "<group>"; };
		3EFB7F281B7AB06800988886 /* Delete.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Delete.swift; sourceTree = "<group>"; };
		80777030020DBD5A301CA196 /* Pods.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.debug.xcconfig; path = "Pods/Target Support Files/Pods/Pods.debug.xcconfig"; sourceTree = "<group>"; };
		FB4A7FCA365ECAF75E20A4A9 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
@@ -131,6 +133,7 @@
				3EFB7F241B7A977500988886 /* Common.swift */,
				3EFB7F1D1B7A6A9800988886 /* Put.swift */,
				3EFB7F261B7AA45A00988886 /* Get.swift */,
				3EFB7F281B7AB06800988886 /* Delete.swift */,
			);
			name = Commands;
			sourceTree = "<group>";
@@ -290,6 +293,7 @@
				3EFB7F1C1B7A55D100988886 /* Core.swift in Sources */,
				3EFB7F201B7A6C3300988886 /* Errors.swift in Sources */,
				3EFB7F1E1B7A6A9800988886 /* Put.swift in Sources */,
				3EFB7F291B7AB06800988886 /* Delete.swift in Sources */,
				3EDAAB581B66D47200F30808 /* Client.swift in Sources */,
				3EFB7F271B7AA45A00988886 /* Get.swift in Sources */,
				3E07EA7F1B78E3B500DAB3F1 /* Kinetic.proto.swift in Sources */,
+15 −10
Original line number Diff line number Diff line
@@ -20,12 +20,12 @@

// @author: Ignacio Corderi

public struct EmptyResponse : ChannelResponse {
public struct NoResponse : ChannelResponse {
    public let success: Bool
    public let error: KineticRemoteError?
    
    public static func parse(raw: RawResponse) -> EmptyResponse {
        return EmptyResponse(success: raw.command.status.code == .Success,
    public static func parse(raw: RawResponse) -> NoResponse {
        return NoResponse(success: raw.command.status.code == .Success,
            error: KineticRemoteError.fromStatus(raw.command.status))
    }
}
@@ -34,17 +34,20 @@ public struct ValueResponse : ChannelResponse {
    public let success: Bool
    public let error: KineticRemoteError?
    public let value: Bytes?
    
    public var hasValue: Bool { return value != nil }
    public let exists: Bool
    public var hasValue: Bool { return value != nil && value!.count > 0 }
    
    public static func parse(raw: RawResponse) -> ValueResponse {
        switch raw.command.status.code {
        case .Success, .NotFound:
            return ValueResponse(success: true, error: nil, value: raw.value)
        case .Success:
            return ValueResponse(success: true, error: nil, value: raw.value, exists: true)
        case .NotFound:
            return ValueResponse(success: true, error: nil, value: nil, exists: false)
        default:
            return ValueResponse(success: false,
            error: KineticRemoteError.fromStatus(raw.command.status),
            value: raw.value)
            value: raw.value,
            exists: false)
        }
    }
    
@@ -56,8 +59,10 @@ public struct ValueResponse : ChannelResponse {
                } else {
                    return "Success (Empty)"
                }
            } else if self.error!.message.isEmpty {
                return "\(self.error!.code)"
            } else {
                return "\(self.error?.code): \(self.error?.message)"
                return "\(self.error!.code): \(self.error!.message)"
            }
        }
    }
+3 −1
Original line number Diff line number Diff line
@@ -83,8 +83,10 @@ public extension ChannelResponse {
        get {
            if self.success {
                return "Success"
            } else if self.error!.message.isEmpty {
                return "\(self.error!.code)"
            } else {
                return "\(self.error?.code): \(self.error?.message)"
                return "\(self.error!.code): \(self.error!.message)"
            }
        }
    }
+93 −9
Original line number Diff line number Diff line
//
//  Delete.swift
//  Kinetic
//
//  Created by Ignacio Corderi on 8/12/15.
//  Copyright © 2015 Seagate. All rights reserved.
//

import Foundation
// Copyright (c) 2015 Seagate Technology

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

// @author: Ignacio Corderi

public class DeleteCommand : ChannelCommand {
    
    public typealias ResponseType = EmptyResponse
    
    public let key: NSData
    
    public init(key: NSData) {
        self.key = key
    }
    
    public convenience init(key: String) {
        self.init(key: key.toNSData())
    }
    
    public func build(builder: Builder) -> Builder {
        builder.header.messageType = .Delete
        builder.keyValue.key = self.key
        return builder
    }
    
}

public struct EmptyResponse : ChannelResponse {
    public let success: Bool
    public let error: KineticRemoteError?
    public let exists: Bool
    
    public static func parse(raw: RawResponse) -> EmptyResponse {
        switch raw.command.status.code {
        case .Success:
            return EmptyResponse(success: true, error: nil, exists: true)
        case .NotFound:
            return EmptyResponse(success: true, error: nil, exists: false)
        default:
            return EmptyResponse(success: false,
                error: KineticRemoteError.fromStatus(raw.command.status),
                exists: false)
        }
    }
    
    public var description: String {
        get {
            if self.success {
                if self.exists {
                    return "Success"
                } else {
                    return "Success (NotFound)"
                }
            } else if self.error!.message.isEmpty {
                    return "\(self.error!.code)"
                } else {
                    return "\(self.error!.code): \(self.error!.message)"
            }
        }
    }
}

extension DeleteCommand: CustomStringConvertible {
    public var description: String {
        get {
            return "Delete (key: \(self.key.toUtf8()))"
        }
    }
}

public extension SynchornousChannel {
    func delete(key: String) throws -> DeleteCommand.ResponseType {
        let cmd = DeleteCommand(key: key)
        return try cmd.sendTo(self)
    }
}
 No newline at end of file