Exemplo n.º 1
0
        public static void RemoveValue(this IBotData botData, DataStoreKey key)
        {
            DataStoreEntryAttribute attribute = key.GetDataStoreEntry();

            if (attribute != null)
            {
                switch (attribute.DataStore)
                {
                case DataStore.User:
                    botData.UserData.RemoveValue(key.GetKey());
                    break;

                case DataStore.Conversation:
                    botData.ConversationData.RemoveValue(key.GetKey());
                    break;

                case DataStore.PrivateConversation:
                    botData.PrivateConversationData.RemoveValue(key.GetKey());
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
        public void write_new_value_locally(Partition partition, WriteRequest request)
        {
            DataStoreKey   key   = Utilities.ConvertKeyDtoToDomain(request.ObjectKey);
            DataStoreValue value = Utilities.ConvertValueDtoToDomain(request.Object);

            partition.addNewOrUpdateExisting(key, value);
        }
 public override Task <lockReply> LockObject(lockRequest request, ServerCallContext context)
 {
     lock (this)
     {
         this.current_key = new DataStoreKey(request.PartitionId, request.ObjectId);
         Partition p = server.getPartition(request.PartitionId);
         p.lockObject(current_key, true);
     }
     return(Task.FromResult(new lockReply()));
 }
Exemplo n.º 4
0
        public static DataStoreEntryAttribute GetDataStoreEntry(this DataStoreKey value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DataStoreEntryAttribute[] entryAttributes =
                (DataStoreEntryAttribute[])fi.GetCustomAttributes(typeof(DataStoreEntryAttribute), false);

            if (entryAttributes.Length > 0)
            {
                return(entryAttributes[0]);
            }
            return(null);
        }
Exemplo n.º 5
0
        public static T GetValueOrDefault <T>(this IBotData botData, DataStoreKey key, T defaultValue = default(T))
        {
            DataStoreEntryAttribute attribute = key.GetDataStoreEntry();

            if (attribute != null)
            {
                switch (attribute.DataStore)
                {
                case DataStore.User:
                    return(botData.UserData.GetValueOrDefault(key.GetKey(), defaultValue));

                case DataStore.Conversation:
                    return(botData.ConversationData.GetValueOrDefault(key.GetKey(), defaultValue));

                case DataStore.PrivateConversation:
                    return(botData.PrivateConversationData.GetValueOrDefault(key.GetKey(), defaultValue));

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            return(defaultValue);
        }
Exemplo n.º 6
0
 public static string GetKey(this DataStoreKey value)
 {
     return(value.ToString("G"));
 }
Exemplo n.º 7
0
 public static string GetFriendlyName(this DataStoreKey value)
 {
     return(GetFriendlyNameFromAttribute(value));
 }
Exemplo n.º 8
0
        public void SetKey(string instanceName, string key, string value)
        {
            // Find the instance in question
            // todo: clean this method up a bit..
            var instace = GetCachedInstance(instanceName);

            if (instace == null)
            {
                CreateInstance(instanceName, string.Format(
                    "Automatically generated instance for '{0}'",
                    instanceName));

                instace = GetCachedInstance(instanceName);
                if (instace == null)
                {
                    throw new Exception(string.Format(
                        "Unable to find the '{0}' instance", instanceName));
                }
            }

            var dbKey = new DataStoreKey(key, instace.Id)
                .Get(this);

            // If the key is not null, update it
            if (dbKey != null)
            {
                dbKey.DateUpdated = DateTime.UtcNow;
                dbKey.Value = value;
                dbKey.Update(this);
                return;
            }

            // Otherwise create the new key
            new DataStoreKey
            {
                DateCreated = DateTime.UtcNow,
                InstanceId = instace.Id,
                Key = key,
                Value = value
            }.Add(this);
        }
Exemplo n.º 9
0
        public void Delete(DataStoreKey key)
        {
            // todo: better error handling here
            if (key == null)
            {
                throw new Exception("You need to provide a key to delete");
            }

            key.Delete(this);
        }
Exemplo n.º 10
0
        public DataStoreKey AddOrUpdate(DataStoreKey keyInstance)
        {
            // todo: better error handling here
            // todo: add in test
            if (keyInstance == null)
            {
                throw new Exception("You cannot create an empty key");
            }

            // todo: better error handling here
            // todo: add in test
            if (string.IsNullOrEmpty(keyInstance.Key))
            {
                throw new Exception("You cannot add a key without a name");
            }

            // todo: better error handling here
            // todo: add in test
            if (keyInstance.InstanceId == 0)
            {
                throw new Exception("Your key needs to belong to an instance");
            }

            if (!keyInstance.Exists(this))
            {
                keyInstance.Add(this);
            }
            else
            {
                keyInstance.Update(this);
            }

            return keyInstance.Get(this);
        }