Skip to content

Commit

Permalink
Update and complete documentation (#11)
Browse files Browse the repository at this point in the history
* docs: Fix typos and what not.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* Add favicon and update README

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* docs: Add tutorial.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* Fix headings for tutorial and add concepts section.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* docs: Improvements in tutorial.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* docs: Add advanced section.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* docs: Add concepts (store, values and stdkiwi).

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* Add contribution guidelines to docs.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* Add docs link to README.

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* docs: Add 'new value type' section

* docs: Minor changes to docs

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

* Add badges for gh-actions

* Update doc.go

Signed-off-by: Vaibhav <vrongmeal@gmail.com>

Co-authored-by: murex971 <nupur202000@gmail.com>
  • Loading branch information
vrongmeal and murex971 authored Aug 28, 2020
1 parent d280a48 commit 2d1877e
Show file tree
Hide file tree
Showing 20 changed files with 1,162 additions and 107 deletions.
114 changes: 48 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,93 +1,75 @@
# kiwi
![Kiwi Logo](./docs/.vuepress/public/kiwi-logo.png)

> A minimalistic in-memory key value store.
Each key is thread safe as it is protected by its own mutex, though different keys can be accessed by various threads.
![Go CI](https://github.com/sdslabs/kiwi/workflows/Go%20CI/badge.svg) ![Docs CI](https://github.com/sdslabs/kiwi/workflows/Docs%20CI/badge.svg) ![Docs CD](https://github.com/sdslabs/kiwi/workflows/Docs%20CD/badge.svg)

To get started, create a store with the NewStore function and add keys to it using AddKey.
Each key is associated with a value which has a specific type.
These types are extendible and can be created by implementing the Value interface.
## Overview

Store can also be initialised with a schema, which is basically a map of keys and value types.
You can think of Kiwi as thread safe global variables. This kind of library
comes in helpful when you need to manage state accross your application which
can be mutated with multiple threads. Kiwi protects your keys with mutex locks
so you don't have to.

```go
// If you have a pre-defined schema
schema := kiwi.Schema{
"key1": str.Type, // string key
// ...
}
Head over to [kiwi.sdslabs.co](https://kiwi.sdslabs.co) for more details and
documentation.

store, err := kiwi.NewStoreFromSchema(schema)
if err != nil {
// handle error
}
## Installation

// If you want to add/update keys dynamically
store.AddKey("key2", str.Type)
store.UpdateKey("key1", list.Type)
> Kiwi requires [Go](https://golang.org/) >= 1.14
// Invoke an action, say updating a string
_, err := store.Do("key2", str.Update, "abc")
if err != nil {
// handle error
}
Kiwi can be integrated with your application just like any other go library.

// or if you need to get the string
s, err := store.Do("key2", str.Get)
if err != nil {
// handle error
}
```sh
go get -u github.com/sdslabs/kiwi
```

myString := s.(string)
// use myString
Now you can import kiwi any where in your code.

```go
import "github.com/sdslabs/kiwi"
```

Package `github.com/sdslabs/kiwi/stdkiwi` implements type safe methods with standard types registered.
These types include:
- [x] str (`github.com/sdslabs/kiwi/values/str`): String value
- [x] list (`github.com/sdslabs/kiwi/values/list`): A list of strings
- [x] set (`github.com/sdslabs/kiwi/values/set`): An exclusive set of strings
- [x] hash (`github.com/sdslabs/kiwi/values/hash`): A string-string hashmap
- [x] zset (`github.com/sdslabs/kiwi/values/zset`): A set with each element having scores
## Basic usage

If you only require the aforementioned value types, use the `stdkiwi` package.
The above example using the `stdkiwi` package is as follows:
Create a store, add key and play with it. It's that easy!

```go
// If you have a pre-defined schema
schema := kiwi.Schema{
"key1": str.Type, // string key
// ...
}
store := stdkiwi.NewStore()

store, err := stdkiwi.NewStoreFromSchema(schema)
if err != nil {
// handle error
if err := store.AddKey("my_string", "str"); err != nil {
// handle error
}

myString := store.Str("my_string")

// If you want to add/update keys dynamically
store.AddKey("key2", str.Typ)
store.UpdateKey("key1", list.Typ)
if err := myString.Update("Hello, World!"); err != nil {
// handle error
}

// Invoke an action, say updating a string
err = store.Str("key2").Update("abc")
str, err := myString.Get()
if err != nil {
// handle error
// handle error
}

mystring := store.Str("key2").Get()
// use mystring
fmt.Println(str) // Hello, World!
```

// If the schema is known while starting the app, to avoid unnecessary errors during app runtime,
// use Guard method for the values when initializing the store.
str.List("key1").Guard() // panics on error
## Contributing

err = str.Str("key2").GuardE() // returns error
if err != nil {
// handle error
}
```
We are always open for contributions. If you find any feature missing, or just
want to report a bug, feel free to open an issue and/or submit a pull request
regarding the same.

For more information on contribution, check out our
[docs](https://kiwi.sdslabs.co/docs/contribution-guide.html).

## Contact

If you have a query regarding the product or just want to say hello then feel
free to visit [chat.sdslabs.co](https://chat.sdslabs.co) or drop a mail at
[contact@sdslabs.co.in](mailto:contact@sdslabs.co.in)

---

To implement a custom value, all that is required is to implement the `kiwi.Value` interface and register
it with the kiwi store using `kiwi.RegisterValue`. Look at the `github.com/sdslabs/kiwi/values` for examples.
Made by [SDSLabs](https://sdslabs.co)
24 changes: 24 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,28 @@
//
// Store can also be initialized with a schema, which is basically a map of keys
// and value types.
//
//
// Get Started
//
// Create a store, add key and play with it. It's that easy!
//
// store := kiwi.NewStore()
//
// if err := store.AddKey("my_string", "str"); err != nil {
// // handle error
// }
//
// if _, err := store.Do("my_string", "UPDATE", "Hello, World!"); err != nil {
// // handle error
// }
//
// v, err := store.Do("my_string", "GET")
// if err != nil {
// // handle error
// }
//
// fmt.Println(v.(string)) // Hello, World!
//
// For documentation visit https://kiwi.sdslabs.co/docs/
package kiwi
39 changes: 37 additions & 2 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ module.exports = {
head: [
['meta', { name: 'theme-color', content: '#A1CE48' }],
['meta', { name: 'apple-mobile-web-app-capable', content: 'yes' }],
['meta', { name: 'apple-mobile-web-app-status-bar-style', content: 'black' }]
['meta', { name: 'apple-mobile-web-app-status-bar-style', content: 'black' }],
['link', { rel: "icon", type: "image/png", sizes: "32x32", href: "/favicon-32.png"}],
['link', { rel: "icon", type: "image/png", sizes: "16x16", href: "/favicon-16.png"}]
],

/**
Expand All @@ -38,7 +40,7 @@ module.exports = {
link: '/docs/',
},
{
text: 'Go Reference',
text: 'pkg.go.dev',
link: 'https://pkg.go.dev/github.com/sdslabs/kiwi'
},
],
Expand All @@ -51,6 +53,39 @@ module.exports = {
'',
'get-started',
]
},
{
title: 'Tutorial',
collapsable: false,
children: [
'tutorial-store',
'tutorial-value-and-actions',
'tutorial-stdkiwi',
'tutorial-json'
]
},
{
title: 'Concepts',
collapsable: false,
children: [
'concepts-store',
'concepts-values',
'concepts-stdkiwi'
]
},
{
title: 'Advanced',
collapsable: false,
children: [
'add-new-value-type'
]
},
{
title: 'Developer',
collapsable: false,
children: [
'contribution-guide'
]
}
],
}
Expand Down
Binary file added docs/.vuepress/public/favicon-16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/.vuepress/public/favicon-32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/.vuepress/public/kiwi-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/.vuepress/styles/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@

.home .hero img
max-width 450px!important

h1#main-title
display none!important
29 changes: 20 additions & 9 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
---
home: true
heroImage: /sdslabs-logo.png
tagline: A minimalistic in-memory key value store.
actionText: Dive Into Docs →
heroImage: /kiwi-logo.png
actionText: Get Started →
actionLink: /docs/
features:
- title: Extendable
details: Custom data structures can be added with ease.
- title: One Binary
- title: Extendable types
details: Keys have static data types that can be plugged in with ease.
- title: One binary
details: Kiwi can be directly plugged in as a go library which produces one binary for your application.
- title: In-Memory
details: All the data is stored in memory for fast access.
- title: JSON compatible
details: Data can be imported from or exported to JSON very comfortably.
footer: MIT Licensed | Copyright © 2020 SDSLabs
---

Expand All @@ -25,12 +24,24 @@ if err := store.AddKey("my_string", "str"); err != nil {

myString := store.Str("my_string")

if err := myString.Update("hello"); err != nil {
if err := myString.Update("Hello, World!"); err != nil {
// handle error
}

str, err := myString.Get()
if err != nil {
// handle error
}

fmt.Println(str) // Hello, World!
```

***

### Contact

If you have a query regarding the product or just want to say hello then feel
free to visit [chat.sdslabs.co](https://chat.sdslabs.co) or drop a mail at
[contact@sdslabs.co.in](mailto:contact@sdslabs.co.in)

<p align="center">Made by <a href="https://sdslabs.co" target="_blank">SDSLabs<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p>
40 changes: 14 additions & 26 deletions docs/docs/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Introduction

You can think of kiwi as thread safe global variables. This kind of library
You can think of Kiwi as thread safe global variables. This kind of library
comes in helpful when you need to manage state accross your application which
can be mutated with multiple threads. Kiwi protects your keys with mutex locks
so you don't have to.
Expand All @@ -16,21 +16,21 @@ can take place on the `value` associated with the key.

Let's take an example to understand what we mean by above paragraph:

_Say, you have multiple servers with which your application interacts and you_
_need to store their IP addresses (which are dynamic). So essentially you need_
_a `set` of IP addresses where you can add or remove IP addresses._
Say, you have multiple servers with which your application interacts and you
need to store their IP addresses (which are dynamic). So essentially you need
a `set` of IP addresses where you can add or remove IP addresses.

![How it works](./images/how-it-works-chart.jpg)

For implementing the above example in Kiwi, you will create a `store`, add the
key `"ip_addresses"` with `value` of `set` type. To interact with the key, you
`key = "ip_addresses"` with `value` of `set` type. To interact with the key, you
will invoke (or `Do`) an `action`, i.e., add IP address or remove IP address
in this case.

## Features

1. **Supports various types:** All values in Kiwi have a type. It is not restricted
to a string value. So if you need to store a map, no need to store a JSON as
to a string value. So if you need to store a map, you don't need to store a JSON as
a string. You can use the `hash` type.

2. **Extendable types:** Even though the core package comes with a limited number
Expand All @@ -43,24 +43,12 @@ in this case.
a go application directly without any extra moving parts. Kiwi, with your application
results in a single binary.

4. **JSON compatible:** Kiwi store and values are all JSON compatible, i.e., they can
be converted into JSON or loaded from JSON. So in-case if you need to store some
value in persistent storage or just backup your store in case of failure, you can
do so in JSON format.
::: tip Did you know
Kiwi is a result of us trying to minimize moving parts in another application
we're developing. Want to know more about that project?
Read [this blog post](https://blog.sdslabs.co/2019/09/status-internal-hackathon).
:::

## When to use it

Kiwi helps in managing application state. If you're familiar with [Redux](https://redux.js.org/),
what Redux does for a react application, Kiwi does for an application written
in Golang.

The example in [How it works](./#how-it-works) section is a valid use-case for
using Kiwi in your application. When an application is divided into multiple
services, and it is horizontally scaled into multiple instances, you need to
keep a track of all the instances. This is where Kiwi comes in handy.

::: tip Did you know
Kiwi is a result of us trying to minimize moving parts in another application
we're developing. Want to know more about that project?
Read [this blog post](https://blog.sdslabs.co/2019/09/status-internal-hackathon).
:::
4. **JSON compatible:** Kiwi's store and values are all JSON compatible, i.e., they can
be converted into JSON or loaded from JSON. In-case you need to store some value in
persistent storage or just backup your store in case of failure, you can do so as JSON.
Loading

0 comments on commit 2d1877e

Please sign in to comment.