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
- func Delete(name string) error
- func GenerateBucketNames(alphabet string, length int) []string
- type Collection
- func (c *Collection) Clear(name string) error
- func (c *Collection) Close() error
- func (c *Collection) Create(name string, data interface{}) error
- func (c *Collection) CreateAsJSON(name string, src []byte) error
- func (c *Collection) Delete(name string) error
- func (c *Collection) DocPath(name string) (string, error)
- func (c *Collection) Keys() []string
- func (c *Collection) Lists() []string
- func (c *Collection) Read(name string, data interface{}) error
- func (c *Collection) ReadAsJSON(name string) ([]byte, error)
- func (c *Collection) Select(params ...string) (*SelectList, error)
- func (c *Collection) Update(name string, data interface{}) error
- func (c *Collection) UpdateAsJSON(name string, src []byte) error
- type SelectList
- func (s SelectList) First() string
- func (s *SelectList) Last() string
- func (s *SelectList) Len() int
- func (s *SelectList) Length() int
- func (s *SelectList) Less(i, j int) bool
- func (s *SelectList) List() []string
- func (s *SelectList) Pop() string
- func (s *SelectList) Push(val string)
- func (s *SelectList) Rest() []string
- func (s *SelectList) Reverse()
- func (s *SelectList) SaveList() error
- func (s *SelectList) Shift() string
- func (s *SelectList) Sort(direction int)
- func (s *SelectList) String() string
- func (s *SelectList) Swap(i, j int)
- func (s *SelectList) Unshift(val string)
Constants ¶
const ( // Version of the dataset package Version = "v0.0.1-beta3" // License for dataset package License = `` /* 1530-byte string literal not displayed */ DefaultAlphabet = `abcdefghijklmnopqrstuvwxyz` ASC = iota DESC = iota )
Variables ¶
This section is empty.
Functions ¶
func GenerateBucketNames ¶
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 or replaces 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"`
CustomLessFn func([]string, int, int) bool
}
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) Len ¶
func (s *SelectList) Len() int
Len returns the number of keys in the select list
func (*SelectList) Length ¶
func (s *SelectList) Length() int
Length returns the number of items in the select list
func (*SelectList) Less ¶
func (s *SelectList) Less(i, j int) bool
Less compare two elements returning true if first is less than second, false otherwise
func (*SelectList) List ¶
func (s *SelectList) List() []string
List returns all the keys in the select list (non-destructively)
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) Reverse ¶
func (s *SelectList) Reverse()
Reverse flips the order of a select list
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) Sort ¶
func (s *SelectList) Sort(direction int)
Sort sorts the keys in in ascending order alphabetically
func (*SelectList) String ¶
func (s *SelectList) String() string
String returns the Keys portion of the select list as a string delimited with new lines
func (*SelectList) Swap ¶
func (s *SelectList) Swap(i, j int)
Swap updates the position of two compared keys
func (*SelectList) Unshift ¶
func (s *SelectList) Unshift(val string)
Unshift select list inserts an element at the start of an array