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:

Outro dia, meu amigo Giovanni Bassi compartilhou o seguinte tuíte: Ele e eu concordamos em discordar muitas vezes. Esta é...
As you probably noted, my old blog posts are gone! It’s sad, but I had to take this way. Main reasons:...
Reduction operations are those that reduce a collection of values to a single value. In this post, I will share...
The example which motivated this post comes from the excellent book Designing Distributed Systems by Brendan Burns (co-founder of the...
What kind of optimizations could we expect from the C# compiler and the JIT? In this post, I would like...
Há tempos, venho pensando sobre “mérito”. Superficialmente, quanto dos meus resultados, positivos e negativos, se devem exclusivamente a mim? Descartando...

Inscrição realizada com sucesso!

No dia da masterclass você receberá um e-mail com um link para acompanhar a aula ao vivo. Até lá!

A sua subscrição foi enviada com sucesso!

Aguarde, em breve entraremos em contato com você para lhe fornecer mais informações sobre como participar da mentoria.

Crie sua conta

Preencha os dados para iniciar o seu cadastro no plano anual do Clube de Estudos:

Crie sua conta

Preencha os dados para iniciar o seu cadastro no plano mensal do Clube de Estudos:

× Precisa de ajuda?