Documentation
¶
Overview ¶
Usage is a dummy package to showcase how to use gocrud with various examples.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Search ¶
func Search()
Example ¶
package main
import (
"fmt"
"io/ioutil"
"time"
_ "github.com/manishrjain/gocrud/drivers/leveldb"
_ "github.com/manishrjain/gocrud/drivers/memsearch"
"github.com/manishrjain/gocrud/indexer"
"github.com/manishrjain/gocrud/req"
"github.com/manishrjain/gocrud/search"
"github.com/manishrjain/gocrud/store"
"github.com/manishrjain/gocrud/x"
)
var log = x.Log("usage")
type SimpleIndexer struct {
}
func (si SimpleIndexer) OnUpdate(e x.Entity) (result []x.Entity) {
result = append(result, e)
return
}
func (si SimpleIndexer) Regenerate(e x.Entity) (rdoc x.Doc) {
rdoc.Id = e.Id
rdoc.Kind = e.Kind
rdoc.NanoTs = time.Now().UnixNano()
result, err := store.NewQuery(e.Id).Run()
if err != nil {
x.LogErr(log, err).Fatal("While querying store")
return
}
data := result.ToMap()
rdoc.Data = data
return
}
var particles = [...]string{
"up", "charm", "top", "gluon", "down", "strange",
"bottom", "photon", "boson", "higgs boson",
}
func main() {
path, err := ioutil.TempDir("", "gocrudldb_")
if err != nil {
x.LogErr(log, err).Fatal("Opening file")
return
}
store.Get().Init(path) // leveldb
search.Get().Init() // memsearch
// Run indexer to update entities in search engine in real time.
c := req.NewContextWithUpdates(10, 100)
indexer.Register("Child", SimpleIndexer{})
indexer.Run(c, 2)
u := store.NewUpdate("Root", "bigbang").SetSource("author")
for i := 0; i < 10; i++ {
child := u.AddChild("Child").Set("pos", i).Set("particle", particles[i])
if i == 5 {
child.MarkDeleted() // This shouldn't be retrieved anymore.
}
}
if err = u.Execute(c); err != nil {
x.LogErr(log, err).Fatal("While updating")
return
}
indexer.WaitForDone(c) // Block until indexing is done.
docs, err := search.Get().NewQuery("Child").Order("-data.pos").Run()
if err != nil {
x.LogErr(log, err).Fatal("While searching")
return
}
fmt.Println("docs:", len(docs))
for _, doc := range docs {
m := doc.Data.(map[string]interface{})
fmt.Println(m["pos"], m["particle"])
}
}
Output: docs: 9 9 higgs boson 8 boson 7 photon 6 bottom 4 down 3 gluon 2 top 1 charm 0 up
func Store ¶
func Store()
Example ¶
package main
import (
"fmt"
"io/ioutil"
_ "github.com/manishrjain/gocrud/drivers/leveldb"
_ "github.com/manishrjain/gocrud/drivers/memsearch"
"github.com/manishrjain/gocrud/req"
"github.com/manishrjain/gocrud/store"
"github.com/manishrjain/gocrud/x"
)
var log = x.Log("usage")
func main() {
path, err := ioutil.TempDir("", "gocrudldb_")
if err != nil {
x.LogErr(log, err).Fatal("Opening file")
return
}
store.Get().Init(path) // leveldb
// Update some data.
c := req.NewContext(10) // 62^10 permutations
err = store.NewUpdate("Root", "bigbang").SetSource("author").
Set("when", "13.8 billion years ago").Set("explosive", true).Execute(c)
if err != nil {
x.LogErr(log, err).Fatal("Commiting update")
return
}
// Retrieve that data
result, err := store.NewQuery("bigbang").Run()
if err != nil {
x.LogErr(log, err).Fatal("While querying store")
return
}
fmt.Println(result.Kind) // Root
fmt.Println(result.Id) // bigbang
data := result.ToMap()
{
val, ok := data["explosive"]
if !ok {
log.Fatal("creator should be set")
return
}
fmt.Println(val) // true
}
{
val, ok := data["when"]
if !ok {
log.Fatal("creator should be set")
return
}
fmt.Println(val)
}
}
Output: Root bigbang true 13.8 billion years ago
Types ¶
This section is empty.
Directories
¶
| Path | Synopsis |
|---|---|
|
drivers
|
|
|
cassandra
Package cassandra contains Cassandra driver for Gocrud.
|
Package cassandra contains Cassandra driver for Gocrud. |
|
elastic
command
|
|
|
examples
|
|
|
social
command
Package main to demonstrate usage of the gocrud apis, using a social network as an example.
|
Package main to demonstrate usage of the gocrud apis, using a social network as an example. |
|
Package indexer provides two different methods to run real time incremental indexing for entities.
|
Package indexer provides two different methods to run real time incremental indexing for entities. |
|
This package is the initialization point for the api.
|
This package is the initialization point for the api. |
|
Package search provides a way to index entities and run relatively complicated search queries, best served outside of data stores, and by specialized search engines like ElasticSearch or Solr etc.
|
Package search provides a way to index entities and run relatively complicated search queries, best served outside of data stores, and by specialized search engines like ElasticSearch or Solr etc. |
|
Package store provides an interface for data store operations, to allow for easy extensibility to support various datastores.
|
Package store provides an interface for data store operations, to allow for easy extensibility to support various datastores. |
|
Package x stores utility functions, mostly for internal usage.
|
Package x stores utility functions, mostly for internal usage. |
Click to show internal directories.
Click to hide internal directories.
