Giter VIP home page Giter VIP logo

kvs's Introduction

kvs

kvs is a simple key-value store library for Go.

It offers the following functionality:

  • Get: retrieve a value associated with a given key from the store
  • Set: add or update a key-value pair in the store
  • Delete: remove a key-value pair associated with a given key from the store
  • Keys: retrieve a slice of all the keys in the store

This library defines two interfaces:

Value which defines the methods a value in the key-value store must implement

Store which defines the methods that a key-value store must implement

ErrCode defines an enumeration that represents the error codes that can be returned by the store.

The error codes are:

  • ErrUnknown: represents an unknown error
  • ErrNotFound: represents an error that occurs when the key is not found in the store
  • ErrDuplicate: represents an error that occurs when the key already exists in the store

Installation

Use go get to install kvs.

go get github.com/bay0/kvs

Usage

To use the library, import the kvs package and create a new instance of the KeyValueStore using NewKeyValueStore().

package main

import (
 "fmt"

 "github.com/bay0/kvs"
)

type Person struct {
 Name string
 Age  int
}

func (p *Person) Clone() kvs.Value {
 return &Person{
  Name: p.Name,
  Age:  p.Age,
 }
}

func main() {
 // Create a new key-value store with sharding enabled
 store := kvs.NewKeyValueStore(2)

 // Create a new person value
 person := &Person{
  Name: "John",
  Age:  20,
 }

 // Set the person value in the store
 err := store.Set("person", person)
 if err != nil {
  // Handle the error
 }

 // Get the person value from the store
 val, err := store.Get("person")
 if err != nil {
  // Handle the error
 }

 // Cast the value to a person object
 personVal, ok := val.(*Person)
 if !ok {
  // Handle the error
 }

 // Print the person's name and age
 fmt.Printf("%s is %d years old.\n", personVal.Name, personVal.Age)

 // Delete the person value from the store
 err = store.Delete("person")
 if err != nil {
  // Handle the error
 }

 // Get all keys from the store
 keys := store.Keys()
 fmt.Println("Keys in the store:", keys)
}

kvs's People

Contributors

bay0 avatar

Stargazers

 avatar

Watchers

 avatar

kvs's Issues

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.