World’s Simplest Key-Value Database, Implemented in Two PowerShell Functions

I’ve been spending some time learning from the “Designing Data-Intensive Applications” book. I am reading it for the third time, cover-to-cover. That is a fantastic book.

At some point, the author shares an interesting “database” implementation with two bash functions. Here is my implementation (following the very same ideas), using PowerShell.

function Set-DbValue {
    param(
        [Parameter(
           Mandatory = $true
        )]
        [string]$key,
        [Parameter(
           Mandatory = $true
        )]
        [string]$value
    )
    
    Write-Host "Setting '$key' to '$value'"
    Add-Content Database.txt "$key, $value"
}

function Get-DbValue {
    param(
        [Parameter(
           Mandatory = $true
        )]
        [string]$key
    )
    
    Select-String -Path ".\Database.txt" -Pattern "^$key, (?.*)$" `
    | Select-Object -Last 1 `
    | %{ $_.Matches[0].Groups["x"].Value }
}

Of course, this is not enough for any real-world scenario.

Every call to Set-DbValue function appends to the end of the file (not overwriting values). Every time you need to get a value, you need to look for the last occurrence of the key. The Set-DbValue performance is excellent. But, the Get-DbValue performance is terrible (all calls are O(n)).

IMPORTANT: I am far away from being a PowerShell specialist. If you can improve this code, I will be happy learning from you in the comments.

If you are serious about using a good database, I have an excellent recommendation for you.

Compartilhe este insight:

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Elemar Júnior

Sou fundador e CEO da EximiaCo e atuo como tech trusted advisor ajudando diversas empresas a gerar mais resultados através da tecnologia.

Mais insights para o seu negócio

Veja mais alguns estudos e reflexões que podem gerar alguns insights para o seu negócio:

Na Guiando, os times já possuem certa maturidade com Scrum. Agora, com a adoção progressiva de princípios de Kanban estamos...
Inspecting the code repository of a client, I found something like this: var customer = new { Id = default(int),...
As lojas que podem e que insistem em funcionar, na minha cidade, estão limitando o número de clientes atendidos simultaneamente....
Our goal is to fill a two-dimensional array with 1’s. using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Running; namespace ToArrays { public class Program...
No post anterior, eu mencionei uma série de post do Mario Fusco questionando a forma como desenvolvedores Java estão implementando os design...
Confesso que estava sentindo falta de escrever em primeira pessoa. Há tempos, publicando apenas nos sites da EximiaCo, não encontrava,...
Oferta de pré-venda!

Mentoria em
Arquitetura de Software

Práticas, padrões & técnicas para Arquitetura de Software, de maneira efetiva, com base em cenários reais para profissionais envolvidos no projeto e implantação de software.

× Precisa de ajuda?