Implementation of IKeyValueStore that uses the file system to persist data.
Each key in the store is saved to a different file. The name of the file is the key, converted to hexadecimal and suffixed with a file extension e.g. [0x48, 0x65, 0x6C, 0x6C]
becomes 48656C6C.data
.
Files are distributed across a number of subfolders, determined by the FileSystemKeyValueStoreOptions.HashBuckets
property. 20 buckets are used by default. An index file (index.data
) is saved to the store's root folder. This file provides a lookup from key to the file containing the data for that key. Note that the index file will zip its contents if required when writing to disk.
Register FileSystemKeyValueStore
with the IAdapterConfigurationBuilder
when configuring adapter services in the dependency injection container:
public void ConfigureAdapters(IServiceCollection services) {
services
.AddDataCoreAdapterAspNetCoreServices()
.AddHostInfo(HostInfo.Create(
"My Host",
"A brief description of the hosting application",
"0.9.0-alpha", // SemVer v2
VendorInfo.Create("Intelligent Plant", "https://appstore.intelligentplant.com"),
AdapterProperty.Create("Project URL", "https://github.com/intelligentplant/AppStoreConnect.Adapters")
))
.AddKeyValueStore(sp => {
var options = new FileSystemKeyValueStoreOptions() {
Path = Path.Combine(AppContext.BaseDirectory, "Data", "KVStore")
};
return ActivatorUtilities.CreateInstance<FileSystemKeyValueStore>(sp, options);
})
.AddAdapter(sp => {
const string adapterId = "my-adapter-1";
// Assume that MyAdapter's constructor expects an IKeyValueStore instance.
return ActivatorUtilities.CreateInstance<MyAdapter>(
sp,
adapterId,
new MyAdapterOptions()
);
});
}
The IKeyValueStore
can then be injected into an adapter in the same way as any other service. If you are hosting multiple adapters in the same application, or the IKeyValueStore
will be used by other parts of your application, it is recommended to pass a scoped IKeyValueStore
when creating the adapter, to ensure that all items in the store associated with the adapter use a common prefix:
public void ConfigureAdapters(IServiceCollection services) {
services
.AddDataCoreAdapterAspNetCoreServices()
.AddHostInfo(HostInfo.Create(
"My Host",
"A brief description of the hosting application",
"0.9.0-alpha", // SemVer v2
VendorInfo.Create("Intelligent Plant", "https://appstore.intelligentplant.com"),
AdapterProperty.Create("Project URL", "https://github.com/intelligentplant/AppStoreConnect.Adapters")
))
.AddKeyValueStore(sp => {
// Configuration removed for brevity
})
.AddAdapter(sp => {
const string adapterId = "my-adapter-1";
var kvStore = sp.GetRequiredService<IKeyValueStore>();
return ActivatorUtilities.CreateInstance<MyAdapter>(
sp,
adapterId,
new MyAdapterOptions(),
kvStore.CreateScopedStore(adapterId)
);
});
}