GithubHelp home page GithubHelp logo

gdbmc.nim's Introduction

This library is a wrapper to C GDBM one

The GNU dbm ('gdbm') is a library of database functions that use extendable hashing and works similarly to the standard UNIX 'dbm' functions.

Instal:

nimble install gdbmc  

Requirement:

By default **libgdbm.so.*** already installed on most distros.  
sudo apt install libgdbm  

Usage:

    block test:
      const filename: string = "test.db"
      let
        db = Open(filename, "n")
      defer: Close(db)

      ## Inserts a key-value pair in the database
      let seqdata = @[("test2", "test2value"), ("test3", "test3value"), ("test4", "test4value"), ("test5", "test5value")]

      for seqitem in seqdata:
        (serr,errl) = Insert(db, seqitem[0], seqitem[1])

      ## Inserts or Updates a key-value pair in the database
      db["test6"] = "test6value"
      value = db["test6"]

      ## Returns of the most recent error encountered when operating on the database dbf
      errl = db.LastError()
      debugEcho "db[test6]=", value, " errl:", $errl

      ## Counts number of records in the database dbf
      let ucnt = db.Count()
      debugEcho "db.Count:", $ucnt

      for pair in db.keyValueIterator():
        stdout.writeLine(pair)

      for key in db.keyIterator():
        stdout.writeLine(key)

For ditails see block test: in gdbmc.nim

Example: memory efficient deduplication for files

    import gdbmc
    from system import quit
    from os import getFileInfo
    from re import re, replace
    from strutils import strip
    from md5 import getMD5

    block:
      let stdin_info = getFileInfo(stdin)
      if stdin_info.id.file == 9:
        echo "Use: cat file.txt|deduplicate"
        quit 1

      const filename: string = "/tmp/dedup-nim.db"
      let
        db = Open(filename, "n")
      defer: Close(db)
      var line = ""
      while stdin.readLine(line):
        line = line.strip.replace(re"(\s+)", " ")
        let md5line = getMD5(line)
        if md5line notin db:
          db[md5line] = ""
          echo line

License

MIT License. You may use any compatible license (essentially any license) for your own programs developed with gdbmc

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.