indexed

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2019 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

This is supporting package for future developing: code generation, databases and so on. There is no restrictions to use it manually, however, interface could be too low-level. Also be careful for details!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Index

type Index interface {
	// Link secondary key to primary key (like Email -> User ID)
	Link(primaryKey, secondaryKey []byte) error
	// Unlink secondary key
	Unlink(primaryKey, secondaryKey []byte) error
	// Find primary keys by secondary key
	Find(secondaryKey []byte) ([][]byte, error)
	// Iterate over entries in index. Order depends of underlying storage
	Iterate(handler func(primaryKey, secondaryKey []byte) error) error
}

Index for secondary keys

func NewIndex

func NewIndex(storage storages.Storage) Index

Creates new non-unique index backed by dedicated storage. Multiple links with a same secondary key will be packed to array (encoded by GOB, but it could be changed). Important! Due to process of appending/modify arrays is in memory, user should be aware of badly distributed secondary keys (where there are a lot of same secondary keys).

Example
package main

import (
	"encoding/json"
	"fmt"
	"github.com/reddec/storages/std/memstorage"
)

func main() {
	type User struct {
		ID    string // primary key
		Email string // unique
		Year  string // non-unique, but indexed
	}
	alice := User{
		ID:    "1",
		Email: "alice@example.com",
		Year:  "1935",
	}

	bob := User{
		ID:    "2",
		Email: "bob@example.com",
		Year:  "1935",
	}

	// create primary index (storage)
	primary := memstorage.New()
	byEmail := NewUniqueIndex(memstorage.New())
	byYear := NewIndex(memstorage.New())

	// save data, indexed by ID
	_ = primary.Put([]byte(alice.ID), toJSON(alice))
	_ = primary.Put([]byte(bob.ID), toJSON(bob))

	// add refs to indexes
	_ = byEmail.Link([]byte(alice.ID), []byte(alice.Email))
	_ = byEmail.Link([]byte(bob.ID), []byte(bob.Email))

	_ = byYear.Link([]byte(alice.ID), []byte(alice.Year))
	_ = byYear.Link([]byte(bob.ID), []byte(bob.Year))

	// find alice by email
	pKeys, _ := byEmail.Find([]byte(alice.Email))
	userData, _ := primary.Get(pKeys[0])

	var user User
	fromJSON(userData, &user)
	fmt.Println("records:", len(pKeys))
	fmt.Println("user id:", user.ID)
	fmt.Println("")

	// find all users with year 1935
	pKeys, _ = byYear.Find([]byte("1935"))
	fmt.Println("records:", len(pKeys))
	for _, pk := range pKeys {
		userData, _ = primary.Get(pk)
		var user User
		fromJSON(userData, &user)
		fmt.Println("user id:", user.ID)
	}
}

func toJSON(data interface{}) []byte {
	d, err := json.Marshal(data)
	if err != nil {
		panic(err)
	}
	return d
}

func fromJSON(data []byte, item interface{}) {
	err := json.Unmarshal(data, item)
	if err != nil {
		panic(err)
	}
}
Output:

records: 1
user id: 1

records: 2
user id: 1
user id: 2

func NewUniqueIndex

func NewUniqueIndex(storage storages.Storage) Index

Creates new unique index backed by dedicated storage. Multiple links with a same secondary key will overwrite each other.

Jump to

Keyboard shortcuts

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