dataset

package module
v0.0.1-alpha2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2017 License: BSD-3-Clause Imports: 6 Imported by: 1

README

dataset

A go package for managing JSON documents stored on disc. dataset is also a command line tool. It stores one of more collections of JSON documents. Typically you'd have a directory that holds collections, each collection holds buckets and each bucket holds some JSON documents. Both the package and command line tool allow you to interact with that logical structure on disc.

layout

  • dataset (directory on file system)
    • collection (directory on file system)
      • collection.json - metadata about collection
        • maps the filename of the JSON blob stored to a bucket in the collection
        • e.g. file "mydocs.jons" stored in bucket "aa" would have a map of {"mydocs.json": "aa"}
      • keys.json - a list of keys in the collection (it is the default select list)
      • BUCKETS - a sequence of alphabet names for buckets holding JSON documents
        • Buckets let supporting common commands like ls, tree, etc. when the doc count is high
      • SELECT_LIST.json - a JSON document holding an array of keys
        • the default select list is "keys", it is not mutable by Push, Pop, Shift and Unshift
        • select lists cannot be named "keys" or "collection"

BUCKETS are names without meaning normally using Alphabetic characters. A dataset defined with four buckets might looks like aa, ab, ba, bb.

operations

  • Collection level
    • Create (collection) - sets up a new disc scripture and creates $DATASET/$COLLECTION_NAME/collection.json
    • Open (collection) - opens an existing collections and reads collection.json into memory
    • Close (collection) - writes changes to collection.json to disc if dirty
    • Delete (collection) - removes a collection from disc
    • Keys (collection) - list of keys in the collection
    • Select (collection) - returns the request select list, will create the list and append keys if not exist
    • Clear (collection) - Removes a select list from a collection and disc
    • List (collection) - returns the names of the available select lists
  • JSON document level
    • Create (JSON document) - saves a new JSON blob to disc with given blob name (sets dirty flag on collection)
    • Read (JSON document)) - finds the JSON document in the buckets and returns the JSON document contents
    • Update (JSON document) - updates an existing blob on disc (sets dirty flag on collection)
    • Delete (JSON document) - removes a JSON blob from its disc (sets the dirty flag on collection)
    • Path (JSON document) - returns the path to the JSON document
  • Select list level
    • Push (select list) - appends one or more keys to an existing select list
    • Last (select list) - returns the value of the last key in the select list (non-distructively)
    • Pop (select list) - returns the last key in select list and removes it
    • Unshift (select list) - inserts one or more new keys at the beginning of the select list
    • First (select list) - returns the value of the first key in the select list (non-distructively)
    • Shift (select list) - returns the first key in a select list and removes it
    • Rest (select list) - returns values of all keys in the select list except the first

Example

Common operations using the dataset command line tool

  • create collection
  • create a JSON document to collection
  • read a JSON document
  • update a JSON document
  • delete a JSON document
    # Create a collection "mystuff" inside the directory called demo
    dataset init demo/mystuff
    # if successful an expression to export the collection name is show
    export DATASET_COLLECTION=demo/mystuff

    # Create a JSON document 
    dataset create freda.json '{"name":"freda","email":"freda@inverness.example.org"}'
    # If successful then you should see an OK or an error message

    # Read a JSON document
    dataset read freda.json

    # Path to JSON document
    dataset path freda.json

    # Update a JSON document
    dataset update freda.json '{"name":"freda","email":"freda@zbs.example.org"}'
    # If successful then you should see an OK or an error message

    # List the keys in the collection
    dataset keys

    # Delete a JSON document
    dataset delete freda.json

    # To remove the collection just use the Unix shell command
    # /bin/rm -fR demo/mystuff

Common operations shown in Golang

  • create collection
  • create a JSON document to collection
  • read a JSON document
  • update a JSON document
  • delete a JSON document
    // Create a collection "mystuff" inside the directory called demo
    collection, err := dataset.Create("demo/mystuff", dataset.GenerateBucketNames("ab", 2))
    if err != nil {
        log.Fatalf("%s", err)
    }
    defer collection.Close()
    // Create a JSON document 
    docName := "freda.json"
    document := map[string]string{"name":"freda","email":"freda@inverness.example.org"}
    if err := collection.Create(docName, document); err != nil {
        log.Fatalf("%s", err)
    }
    // Read a JSON document
    if err := collection.Read(docName, document); err != nil {
        log.Fatalf("%s", err)
    }
    // Update a JSON document
    document["email"] = "freda@zbs.example.org"
    if err := collection.Update(docName, document); err != nil {
        log.Fatalf("%s", err)
    }
    // Delete a JSON document
    if err := collection.Delete(docName); err != nil {
        log.Fatalf("%s", err)
    }

Documentation

Overview

Package dataset is a go package for managing JSON documents stored on disc

@author R. S. Doiel, <rsdoiel@caltech.edu>

Copyright (c) 2017, Caltech All rights not granted herein are expressly reserved by Caltech.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Examples:

  // Create a collection
  collection, err := dataset.Create("mystuff", "dataset", GenerateBucketNames("abc", 3))
  if err != nil {
      log.Fatalf("%s", err)
	 }
  defer collection.Close()
  // Add a record
  record := map[string]string{"name":"freda","email":"freda@inverness.example.org"}
  if err := collection.Create("freda", record); err != nil {
      log.Fatalf("%s", err)
  }
  // Read a record
  if err := collection.Read("freda", record); err != nil {
      log.Fatalf("%s", err)
  }
  // Update a record
  record["email"] = "freda@zbs.example.org"
  if err := collection.Update("freda", record); err != nil {
      log.Fatalf("%s", err)
  }
  // Delete a record
  if err := collection.Delete("freda"); err != nil {
      log.Fatalf("%s", err)
  }

Index

Constants

View Source
const (
	// Version of the dataset package
	Version = "v0.0.1-alpha2"

	// License for dataset package
	License = `` /* 1530-byte string literal not displayed */

)

Variables

This section is empty.

Functions

func Delete

func Delete(name string) error

Delete an entire collection

func GenerateBucketNames

func GenerateBucketNames(alphabet string, length int) []string

GenerateBucketNames provides a list of permutations of requested length to use as bucket names

Types

type Collection

type Collection struct {
	// Version of collection being stored
	Version string `json:"verison"`
	// Name of collection
	Name string `json:"name"`
	// Dataset is a directory name that holds collections
	Dataset string `json:"dataset"`
	// Buckets is a list of bucket names used by collection
	Buckets []string `json:"buckets"`
	// KeyMap holds the document name to bucket map for the collection
	KeyMap map[string]string `json:"keymap"`
	// SelectLists holds the names of available select lists
	SelectLists []string `json:"select_lists"`
}

Collection is the container holding buckets which in turn hold JSON docs

func Create

func Create(name string, bucketNames []string) (*Collection, error)

Create - create a new collection structure on disc name should be filesystem friendly

func Open

func Open(name string) (*Collection, error)

Open reads in a collection's metadata and returns and new collection structure and err

func (*Collection) Clear

func (c *Collection) Clear(name string) error

Clear removes a select list from disc and the collection

func (*Collection) Close

func (c *Collection) Close() error

Close closes a collection, writing the updated keys to disc

func (*Collection) Create

func (c *Collection) Create(name string, data interface{}) error

Create a JSON doc from an interface{} and adds it to a collection, if problem returns an error name must be unique

func (*Collection) CreateAsJSON

func (c *Collection) CreateAsJSON(name string, src []byte) error

CreateAsJSON adds a JSON doc to a collection, if problem returns an error name must be unique (treated like a key in a key/value store)

func (*Collection) Delete

func (c *Collection) Delete(name string) error

Delete removes a JSON doc from a collection

func (*Collection) DocPath

func (c *Collection) DocPath(name string) (string, error)

DocPath returns a full path to a key or an error if not found

func (*Collection) Keys

func (c *Collection) Keys() []string

Keys returns a list of keys in a collection

func (*Collection) Lists

func (c *Collection) Lists() []string

Lists returns a list of available select lists, should always contain the default keys list

func (*Collection) Read

func (c *Collection) Read(name string, data interface{}) error

Read finds the record in a collection, updates the data interface provide and if problem returns an error name must exist or an error is returned

func (*Collection) ReadAsJSON

func (c *Collection) ReadAsJSON(name string) ([]byte, error)

ReadAsJSON finds a the record in the collection and returns the JSON source

func (*Collection) Select

func (c *Collection) Select(params ...string) (*SelectList, error)

Select returns a select assocaited with a collection, it will be created if neccessary and any keys included will be added before returning the updated list

func (*Collection) Update

func (c *Collection) Update(name string, data interface{}) error

Update JSON doc in a collection from the provided data interface (note: JSON doc must exist or returns an error )

func (*Collection) UpdateAsJSON

func (c *Collection) UpdateAsJSON(name string, src []byte) error

UpdateAsJSON takes a JSON doc and writes it to a collection (note: Record must exist or returns an error)

type SelectList

type SelectList struct {
	FName string   `json:"name"`
	Keys  []string `json:"keys"`
}

SelectList is an ordered set of keys

func (SelectList) First

func (s SelectList) First() string

First select list returns the first item in the list (non-destructively)

func (*SelectList) Last

func (s *SelectList) Last() string

Last select list returns the list item from the list (non-destructively)

func (*SelectList) Length

func (s *SelectList) Length() int

Length returns the number of items in the select list

func (*SelectList) Pop

func (s *SelectList) Pop() string

Pop select list removes from the end of an array returning the element removed

func (*SelectList) Push

func (s *SelectList) Push(val string)

Push select list appends an element to the end of an array

func (*SelectList) Rest

func (s *SelectList) Rest() []string

Rest select list returns all but the first n items of the list (non-destructively)

func (*SelectList) SaveList

func (s *SelectList) SaveList() error

SaveList writes the .Keys to a JSON document named .FName

func (*SelectList) Shift

func (s *SelectList) Shift() string

Shift select list removes from the beginning of and array returning the element removed

func (*SelectList) Unshift

func (s *SelectList) Unshift(val string)

Unshift select list inserts an element at the start of an array

Directories

Path Synopsis
cmds
dataset command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL