KeyValueDB 1.2.0
dotnet add package KeyValueDB --version 1.2.0
NuGet\Install-Package KeyValueDB -Version 1.2.0
<PackageReference Include="KeyValueDB" Version="1.2.0" />
paket add KeyValueDB --version 1.2.0
#r "nuget: KeyValueDB, 1.2.0"
// Install KeyValueDB as a Cake Addin
#addin nuget:?package=KeyValueDB&version=1.2.0
// Install KeyValueDB as a Cake Tool
#tool nuget:?package=KeyValueDB&version=1.2.0
Table of content
Example
The following example load, update, read and remove a simple key value object
using System;
using Io.Github.Thecarisma;
namespace Sample
{
class Test
{
public static void Main(string[] args)
{
//initialize the key-value
KeyValueDB keyValueDB = new KeyValueDB("Greet=Hello World,Project=KeyValueDB", true, '=', ',', false);
//get an object
Console.WriteLine(keyValueDB.Get("Greet"));
//remove an object
keyValueDB.Remove("Greet");
//add an object
keyValueDB.Add("What", "i don't know what to write here");
//print all the objects
foreach (var kvo in keyValueDB) {
Console.WriteLine(kvo);
}
}
}
}
Legends
kvp - Key Value Pair
kvdb - Key value Database
pss - Possibly
kvo - Key Value Object
API
Creating/loading a document
You can use the package to update and create an existing key value database. This library does not read the database from a file which means you have to find a way to read a string from the file.
Create a new keyValueDB. The default seperator between the key and value is =
and the delimeter between the kvp is \n
(newline).
KeyValueDB keyValueDB = new KeyValueDB();
To load existing KeyValueDB
KeyValueDB keyValueDB = new KeyValueDB(
"Greet=Hello World,Project=KeyValueDB", //pss read string from file
true, //case sensitive is true
'=', //the seperator from key and value
',', //the delimeter for the key-value-pair
false //error tolerance if true no exception is thrown
);
Inserting Data
The only accepted type that can be inserted is a valid KeyValueObject
and String
. The method Add
can be used to add a new kvp into the object.
Add a kvp with it key and value
keyValueDB.Add("Greet", "Hello World");
Add a kvp using the KeyValueObject
class.
KeyValueObject keyValueObject = new KeyValueObject("Greet", "Hello World");
keyValueDB.Add(keyValueObject);
Finding Data
There are several way to find and get a value from the kvdb object. The value or the KeyValueObject can be gotten using the methods below
Get KeyValue Object
You can get the kvo using either the key or index. If the corresponding kvo is not found, an empty kvo is added to the db and then returned but not in the case when requested with the integer index. If a fallback kvo is sent as second parameter then when the request kvo is not found the fallback second parameter is added to the kvdb and then returned.
Get the kvo using it integer index
keyValueDB.GetKeyValueObject(0);
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Greet,Value=Hello World
Get the kvo using it key
keyValueDB.GetKeyValueObject("Greet");
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Greet,Value=Hello World
Get the kvo using it key with fallback kvo
KeyValueObject keyValueObject = new KeyValueObject("Name", "Adewale Azeez");
keyValueDB.GetKeyValueObject("Name", keyValueObject);
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Name,Value=Adewale Azeez
Get
You can get a kvdb value using either the key or index. If the corresponding value is not found, an empty string is added to the db and then returned but not in the case when requested with the integer index.
If a fallback kvo is sent as second parameter then when the request key is not found the fallback second parameter is added to the kvdb and then value is returned. If a string value is sent as the second value it is returned if the key is not found in the kvdb.
Get a value using it integer index
keyValueDB.Get(0);
//"Hello World"
Get the value using it key
keyValueDB.Get("Greet");
//"Hello World"
Get the kvo using it key with fallback value
keyValueDB.Get("Licence", "The MIT Licence");
//"The MIT Licence"
Get the kvo using it key with fallback kvo
KeyValueObject keyValueObject = new KeyValueObject("Licence", "The MIT Licence");
keyValueDB.Get("Name", keyValueObject);
//"The MIT Licence"
Updating Data
There are various way to update a kvp in the kvdb, the value can be changed directly or set to a new KeyValueObject. If you try to set a kvo that does not exist in the kvdb using it key, it is added to the kvdb.
Set
The Set
method is used to change the value of the kvo using the index of the kvo or a kvo key.
Set a kvo value using it index
keyValueDB.Set(0, "Hello World from thecarisma");
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Greet,Value=Hello World from thecarisma
Set a kvo value using it key
keyValueDB.Set("Greet", "Hello World from thecarisma");
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Greet,Value=Hello World from thecarisma
Set KeyValue Object
Completely change a KeyValueObject in the kvdb using either it index or it key. The kvo is completly replaced which means unique fields like the hashcode of the kvo changes. When the kvo is set using it key if the corresponding kvo does not exist it is added into the kvdb. Note that this method completly changes the kvo so it can be used to replace a kvo.
Set a kvo using it index
KeyValueObject keyValueObject = new KeyValueObject("Licence", "The MIT Licence");
keyValueDB.SetKeyValueObject(0, keyValueObject);
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Licence,Value=The MIT Licence
Set a kvo value using it key
KeyValueObject keyValueObject = new KeyValueObject("Licence", "The MIT Licence");
keyValueDB.SetKeyValueObject("Greet", keyValueObject);
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Licence,Value=The MIT Licence
Inserting Data
A new kvp can be inserted by invoking the Add
method. The kvp can be added using it key and value or by directly adding the KeyValueObject to the kvdb.
Add a new kvp using the key and value
keyValueDB.Add("Key", "This is the value");
Add a new kvp using a new KeyValueObject
KeyValueObject keyValueObject = new KeyValueObject("Key", "This is the value");
keyValueDB.Add(keyValueObject);
Removing Data
Remove a kvp completely from the kvdb using either it key of the integer index. The kvp that was removed is returned from the method. If the index does not exist out of bound error occur and if a kvo with the key is not present nothing is done but an empty kvo is returned.
Remove a kvp using integer index
keyValueDB.Remove(0);
//removes the first kvp in the kvdb
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Greet,Value=Hello World
Remove a kvp using it key
keyValueDB.Remove("Greet");
//removes the first kvp in the kvdb
//Io.Github.Thecarisma.KeyValueObject@55915408:Key=Greet,Value=Hello World
Iterating collection
The KeyValueDB object can be iterated natively using the foreach..in
loop expression.
foreach (var kvo in keyValueDB) {
//operate on the KeyValueObject
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Add, Remove, Set, Clear, IsEmpty API implemented.