<commit_before>//
//  DefaultPeerManager.swift
//  Clink
//
//  Created by <NAME> on 7/13/17.
//

import Foundation


public class DefaultPeerManager: ClinkPeerManager {    
    public func createPeer<T: ClinkPeer>(withId peerId: String) -> T {
        let peer = Clink.Peer(id: peerId)
        
        UserDefaults.standard.set(peer.toDict(), forKey: peer.id)
        
        var savedPeerIds = UserDefaults.standard.stringArray(forKey: savedPeerIdsDefaultsKey) ?? []
        
        if savedPeerIds.index(of: peer.id) == nil {
            savedPeerIds.append(peer.id)
            UserDefaults.standard.set(savedPeerIds, forKey: savedPeerIdsDefaultsKey)
        }
        
        return peer
    }
    
    public func update(peerWithId peerId: String, withPeerData data: [String: Any]) {
        var clinkPeer = peer
        
        clinkPeer.data = data
        
        UserDefaults.standard.set(clinkPeer.toDict(), forKey: clinkPeer.id)
    }
    
    public func getPeer<T: ClinkPeer>(withId peerId: String) -> T? {
        guard let peerDict = UserDefaults.standard.dictionary(forKey: peerId) else { return nil }
        
        return Clink.Peer(dict: peerDict)
    }
    
    public func getKnownPeers<T: ClinkPeer>() -> [T] {
        return (UserDefaults.standard.stringArray(forKey: savedPeerIdsDefaultsKey) ?? []).flatMap { peerId in
            return self.getPeer(withId: peerId)
        }
    }
    
    public func delete(peerWithId peerId: String) {
        UserDefaults.standard.removeObject(forKey: peer.id)
    }
}<commit_msg>update default peer manager CRUD methods<commit_after>//
//  DefaultPeerManager.swift
//  Clink
//
//  Created by <NAME> on 7/13/17.
//

import Foundation


public class DefaultPeerManager: ClinkPeerManager {    
    public func createPeer<T: ClinkPeer>(withId peerId: String) -> T {
        let peer = T(id: peerId, peerData: [:])
        
        UserDefaults.standard.set(peer.toDict(), forKey: peer.id)
        
        var savedPeerIds = UserDefaults.standard.stringArray(forKey: savedPeerIdsDefaultsKey) ?? []
        
        if savedPeerIds.index(of: peer.id) == nil {
            savedPeerIds.append(peer.id)
            UserDefaults.standard.set(savedPeerIds, forKey: savedPeerIdsDefaultsKey)
        }
        
        return peer
    }
    
    public func update(peerWithId peerId: String, withPeerData data: [String: Any]) {
        guard let peer: Clink.Peer = self.getPeer(withId: peerId) else { return }
        
        peer.data = data
        
        UserDefaults.standard.set(peer.toDict(), forKey: peer.id)
    }
    
    public func getPeer<T: ClinkPeer>(withId peerId: String) -> T? {
        guard
            let peerDict = UserDefaults.standard.dictionary(forKey: peerId),
            let id = peerDict["id"] as? String,
            let peerData = peerDict["data"] as? [String: Any]
        else {
            return nil
        }
        
        return T(id: id, peerData: peerData)
    }
    
    public func getKnownPeers<T: ClinkPeer>() -> [T] {
        return (UserDefaults.standard.stringArray(forKey: savedPeerIdsDefaultsKey) ?? []).flatMap { peerId in
            return self.getPeer(withId: peerId)
        }
    }
    
    public func delete(peerWithId peerId: String) {
        UserDefaults.standard.removeObject(forKey: peerId)
    }
}
{'fraction_non_alphanumeric': 0.10727163461538461, 'fraction_numerical': 0.0030048076923076925, 'mean_word_length': 2.286278381046397, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '114751', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1001, 'n_words': 220}