func LinkBench() { var db = loge.NewLogeDB(loge.NewLevelDBStore("data/linkbench")) defer db.Close() db.CreateType(loge.NewTypeDef("person", 1, &Person{})) var petDef = loge.NewTypeDef("pet", 1, &Pet{}) petDef.Links = loge.LinkSpec{"owner": "person"} db.CreateType(petDef) fmt.Printf("Inserting...\n") db.Transact(func(t *loge.Transaction) { t.Set("person", "Brendon", &Person{"Brendon", 31, []uint16{}}) for i := 0; i < 10000; i++ { var key = fmt.Sprintf("pet-%04d", i) t.Set("pet", loge.LogeKey(key), &Pet{key, "dog"}) t.AddLink("pet", "owner", loge.LogeKey(key), "Brendon") } }, 0) fmt.Printf("Finding...\n") var count = 0 db.Transact(func(t *loge.Transaction) { var pets = t.Find("pet", "owner", "Brendon") for pets.Valid() { pets.Next() count++ } }, 0) fmt.Printf("Found %d pets\n", count) count = 0 var lastPet loge.LogeKey = "" var loops = 0 for loops < 1000 { db.Transact(func(t *loge.Transaction) { var somePets = t.FindSlice("pet", "owner", "Brendon", lastPet, 100) for somePets.Valid() { lastPet = somePets.Next() count++ } }, 0) loops++ } fmt.Printf("Sliced %d pets\n", count) }
func LinkSandbox() { var db = loge.NewLogeDB(loge.NewLevelDBStore("data/links")) defer db.Close() db.CreateType(loge.NewTypeDef("person", 1, &Person{})) var petDef = loge.NewTypeDef("pet", 1, &Pet{}) petDef.Links = loge.LinkSpec{"owner": "person"} db.CreateType(petDef) db.Transact(func(t *loge.Transaction) { t.Set("person", "Brendon", &Person{"Brendon", 31, []uint16{}}) t.Set("person", "Mike", &Person{"Mike", 38, []uint16{}}) t.Set("pet", "Ted", &Pet{"Ted", "dog"}) t.Set("pet", "Bones", &Pet{"Bones", "dog"}) t.Set("pet", "BaoBao", &Pet{"BaoBao", "dog"}) t.Set("pet", "Ruby", &Pet{"Ruby", "dog"}) t.Set("pet", "HenYou", &Pet{"HenYou", "dog"}) t.Set("pet", "Garcon", &Pet{"Garcon", "dog"}) t.Set("pet", "Flower", &Pet{"Flower", "cat"}) t.SetLinks("pet", "owner", "Ted", []loge.LogeKey{"Brendon"}) t.SetLinks("pet", "owner", "Bones", []loge.LogeKey{"Brendon"}) t.SetLinks("pet", "owner", "BaoBao", []loge.LogeKey{"Brendon"}) t.SetLinks("pet", "owner", "Ruby", []loge.LogeKey{"Brendon"}) t.SetLinks("pet", "owner", "HenYou", []loge.LogeKey{"Mike"}) t.SetLinks("pet", "owner", "Garcon", []loge.LogeKey{"Mike"}) t.SetLinks("pet", "owner", "Flower", []loge.LogeKey{"Mike"}) }, 0) db.Transact(func(t *loge.Transaction) { t.RemoveLink("pet", "owner", "Ruby", "Brendon") t.AddLink("pet", "owner", "Ruby", "Mike") }, 0) db.Transact(func(t *loge.Transaction) { fmt.Printf("Ruby links: %v\n", t.ReadLinks("pet", "owner", "Ruby")) }, 0) for _, pet := range db.Find("pet", "owner", "Brendon") { fmt.Printf("Found Brendon pet: %s\n", pet) } for _, pet := range db.Find("pet", "owner", "Mike") { fmt.Printf("Found Mike pet: %s\n", pet) } for _, pet := range db.Find("pet", "owner", "Nai") { fmt.Printf("Found Nai pet: %s\n", pet) } fmt.Printf("Done\n") }
func DoWrite(cores int, idx int) { var db = loge.NewLogeDB(loge.NewLevelDBStore(fmt.Sprintf("data/bench%d", idx))) defer db.Close() db.CreateType(loge.NewTypeDef("person", 1, &Person{})) var startTime = time.Now() var tokens = make(chan bool, cores) for i := 0; i < cores; i++ { tokens <- true } for startId := 0; startId < TOTAL; startId += BATCH_SIZE { <-tokens var endId = startId + BATCH_SIZE - 1 if endId >= TOTAL { endId = TOTAL - 1 } go WritePeopleBatch(db, startId, endId, tokens) } for i := 0; i < cores; i++ { <-tokens } fmt.Printf("Done in %v\n", time.Since(startTime)) fmt.Printf("Flushed\n") }
func Example() { var db = loge.NewLogeDB(loge.NewLevelDBStore("data/example")) db.CreateType(loge.NewTypeDef("person", 1, &Person{})) // ------------------------------------ // Create an object db.Transact(func(trans *loge.Transaction) { trans.Set("person", "brendon", &Person{Name: "Brendon", Age: 31}) }, 0) // ------------------------------------ // Load some objects db.Transact(func(trans *loge.Transaction) { if trans.Exists("person", "brendon") { var brendon = trans.Write("person", "brendon").(*Person) fmt.Printf("Existing Brendon: %v\n", brendon) // Update brendon.Age = 41 } var defaultObj = trans.Read("person", "someone else").(*Person) fmt.Printf("Default value: %v\n", defaultObj) }, 0) // ------------------------------------ // Check the update db.Transact(func(trans *loge.Transaction) { var brendon = trans.Read("person", "brendon").(*Person) fmt.Printf("Updated Brendon: %v\n", brendon) }, 0) // ------------------------------------ // Intermingle transactions var trans1 = db.CreateTransaction() var trans2 = db.CreateTransaction() trans1.Set("person", "nai", &Person{Name: "Nai Yu", Age: 32}) trans2.Set("person", "nai", &Person{Name: "Not Nai Yu", Age: 16}) fmt.Printf("Commit 1: %v\n", trans1.Commit()) fmt.Printf("Commit 2: %v\n", trans2.Commit()) // ------------------------------------ // Check which succeeded db.Transact(func(trans *loge.Transaction) { var nai = trans.Read("person", "nai") fmt.Printf("Nai: %v\n", nai) }, 0) }
func Sandbox() { var db = loge.NewLogeDB(loge.NewLevelDBStore("data/sandbox")) db.CreateType(loge.NewTypeDef("person", 1, &Version0{})) db.SetOne("person", "brendon", &Version0{"Brendon"}) fmt.Printf("Version 0: %v\n", db.ReadOne("person", "brendon")) var def2 = loge.NewTypeDef("person", 2, &Version1{}) def2.Upgrader = func(v0 interface{}) (interface{}, error) { var obj = v0.(*Version0) return &Version1{obj.Name, 18}, nil } db.CreateType(def2) fmt.Printf("Version 1: %v\n", db.ReadOne("person", "brendon")) fmt.Printf("Version 1: %v\n", db.ReadOne("person", "brendon")) fmt.Printf("Version 1: %v\n", db.ReadOne("person", "brendon")) }
func StartService() { var db = loge.NewLogeDB(loge.NewLevelDBStore("data/links")) defer db.Close() db.CreateType(loge.NewTypeDef("person", 1, &Person{})) var petDef = loge.NewTypeDef("pet", 1, &Pet{}) petDef.Links = loge.LinkSpec{"owner": "person"} db.CreateType(petDef) var serviceCollection = goservice.NewServiceCollection() serviceCollection.AddService(loge.GetService()) var server = &context{ *goservice.NewServer( serviceCollection, goservice.BasicSessionCreator), db, } server.AddEndpoint(goservice.NewHttpRpcEndpoint(":6060", server, nil)) server.AddEndpoint(goservice.NewTelnetEndpoint(":6061", server)) server.Log("Server starting...") var stopper = make(chan os.Signal, 1) signal.Notify(stopper) server.Start() defer server.Stop() <-stopper close(stopper) fmt.Printf("\n") server.Log("Server stopping...") }