Skip to content

Database

emli edited this page Nov 22, 2017 · 31 revisions

db

Gorjun uses bolt. Bolt an embedded key/value database for Go. Bucket is table in bolt.

Gorjun database directory is /opt/gorjun/data/db/my.db. If it not exist it will created automatically. Before create directory opt/gorjun.

  • MyBucket - store information about all files in Gorjun. Here key - is a file UUID.
  • Users - store all users registered in the system. Key is a username, the value is a bucket with multiple GPG-keys.
  • SearchIndex - store a search index for fast accessing artifacts. Key - filename, value - bucket with files UUIDs.
  • Tokens - store authentication tokens for user sessions. Key - token, value bucket with username and date of creation.
  • AuthID - store AuthID(random string) that user should sign to get token. Key - authID, value - username.
  • Tags - another search index that is used to group templates by tags. Key - tag, value - template UUIDs

Transactions

db.Update - read-write transactions

db.View - read-only transactions

Users

  1. How create bucket to new user?

b := tx.Bucket(users).Bucket([]byte(user))

  1. How put keys to user?

b := b.CreateBucketIfNotExists([]byte("keys")) b.Put(key, nil)

Working with Tokens

  1. How to get tokens?

b := tx.Bucket(tokens).Bucket([]byte(token))

As you see above tokens is bucket that store token as key, value bucket with username and date of creation. Token is key.

  1. How to put values?

b.Put([]byte("name"), []byte(name))

b.Put([]byte("date"), now)

  1. How to get date and username for token? After assign bucket to b, we can get value by keys.

b.Get([]byte("date"))

b.Get([]byte("name"))