Exemplo n.º 1
0
        /// <summary>
        /// Inserts a the provided entry for the specified key if the key does not already exist.
        /// </summary>
        /// <param name="db">The database to operate on.</param>
        /// <param name="key">The key of the entry to insert.</param>
        /// <param name="entry">The value of the entry.</param>
        /// <returns>true if the key/entry pair was inserted successfully; otherwise false if the key already exists</returns>
        public bool Insert(ushort db, RegsKey key, IRegsEntry entry)
        {
            ValidateDatabaseOrThrow(db);
            ValidateEntryOrThrow(entry);

            return(_databases[db].Value.TryAdd(key, entry));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the specified key to the newly provided entry if they key already exists.
        /// </summary>
        /// <param name="db">The database to operate on.</param>
        /// <param name="key">The key of the entry to insert.</param>
        /// <param name="entry">The value of the entry.</param>
        /// <returns>true if the entry was updated successfully; otherwise false</returns>
        public bool Update(ushort db, RegsKey key, IRegsEntry entry)
        {
            ValidateDatabaseOrThrow(db);
            ValidateEntryOrThrow(entry);

            if (_databases[db].Value.TryGetValue(key, out IRegsEntry existingEntry))
            {
                return(_databases[db].Value.TryUpdate(key, entry, existingEntry));
            }
            return(false);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets the entry for the specified key.
        /// </summary>
        /// <param name="db">The database to operate on.</param>
        /// <param name="key">The key of the entry.</param>
        /// <returns>The entry if the key exists; otherwise null.</returns>
        public IRegsEntry Get(ushort db, RegsKey key)
        {
            ValidateDatabaseOrThrow(db);

            if (_databases[db].Value.TryGetValue(key, out IRegsEntry entry))
            {
                return(entry);
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Insert a new key/entry pair if the key does not already exist, or updates a key/entry pair if the key already exits.
        /// </summary>
        /// <param name="db">The database to operate on.</param>
        /// <param name="key">The key of the entry.</param>
        /// <param name="entry">The value of the entry.</param>m>
        /// <returns>The new entry for the key.</returns>
        public IRegsEntry Upsert(ushort db, RegsKey key, IRegsEntry entry)
        {
            ValidateDatabaseOrThrow(db);
            ValidateEntryOrThrow(entry);

            return(_databases[db].Value.AddOrUpdate(key, entry, (k, existingEntry) =>
            {
                entry.CreatedOn = existingEntry.CreatedOn;
                entry.ModifiedOn = DateTimeOffset.UtcNow;
                return entry;
            }));
        }
        public void InsertShouldReturnTrueGivenValidInsert([Frozen] RegsSettings _,
                                                           [Frozen] IOptionsMonitor <RegsSettings> __,
                                                           string keyValue,
                                                           string entryValue,
                                                           RegsDatabaseManager sut)
        {
            // Arrange
            var key         = new RegsKey(keyValue, RegsDataType.String);
            var insertValue = new RegsStringEntry(entryValue);

            // Act
            var wasInserted = sut.Insert(0, key, insertValue);

            // Assert
            wasInserted.ShouldBeTrue();
        }
        public void GetShouldReturnExpectedGivenValidUpsert([Frozen] RegsSettings _,
                                                            [Frozen] IOptionsMonitor <RegsSettings> __,
                                                            string keyValue,
                                                            string entryValue,
                                                            RegsDatabaseManager sut)
        {
            // Arrange
            var key      = new RegsKey(keyValue, RegsDataType.String);
            var expected = new RegsStringEntry(entryValue);

            // Act
            sut.Upsert(0, key, expected);
            var actual = sut.Get(0, key);

            // Assert
            actual.ShouldBe(expected);
        }
        public void GetShouldReturnNullGivenValidDelete([Frozen] RegsSettings _,
                                                        [Frozen] IOptionsMonitor <RegsSettings> __,
                                                        string keyValue,
                                                        string deleteValue,
                                                        RegsDatabaseManager sut)
        {
            // Arrange
            var key         = new RegsKey(keyValue, RegsDataType.String);
            var toBeDeleted = new RegsStringEntry(deleteValue);

            sut.Insert(0, key, toBeDeleted);

            // Act
            sut.Delete(0, key);
            var actual = sut.Get(0, key);

            // Assert
            actual.ShouldBeNull();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Deletes key/entry pair.
        /// </summary>
        /// <param name="db">The database to operate on.</param>
        /// <param name="key">The key of the entry.</param>
        /// <returns>true if the key/entry pair was deleted successfully; otherwise false.</returns>
        public bool Delete(ushort db, RegsKey key)
        {
            ValidateDatabaseOrThrow(db);

            return(_databases[db].Value.TryRemove(key, out IRegsEntry _));
        }
Exemplo n.º 9
0
 public bool Delete(ushort db, RegsKey key) => _dbManager.Delete(db, key);
Exemplo n.º 10
0
 public IRegsEntry Get(ushort db, RegsKey key) => _dbManager.Get(db, key);
Exemplo n.º 11
0
 public IRegsEntry Set(ushort db, RegsKey key, IRegsEntry entry) => _dbManager.Upsert(db, key, entry);