func main() { var defaultMaxprocs int var err error if defaultMaxprocs, err = strconv.Atoi(os.Getenv("GOMAXPROCS")); err != nil { defaultMaxprocs = runtime.NumCPU() * 2 } var mode, dir string var port, maxprocs int flag.StringVar(&mode, "mode", "", "[v1|v2|v3|bench|durable-bench|example]") flag.StringVar(&dir, "dir", "", "database directory") flag.IntVar(&port, "port", 0, "listening port number") flag.IntVar(&maxprocs, "gomaxprocs", defaultMaxprocs, "GOMAXPROCS") flag.Parse() if mode == "" { flag.PrintDefaults() return } runtime.GOMAXPROCS(maxprocs) log.Printf("GOMAXPROCS is set to %d", maxprocs) if maxprocs < runtime.NumCPU() { log.Printf("GOMAXPROCS (%d) is less than number of CPUs (%d), this may affect performance. You can change it via environment variable GOMAXPROCS or by passing CLI parameter -gomaxprocs", maxprocs, runtime.NumCPU()) } switch mode { case "v1": fallthrough case "v2": fallthrough case "v3": if dir == "" { log.Fatal("Please specify database directory, for example -dir=/tmp/db") } if port == 0 { log.Fatal("Please specify port number, for example -port=8080") } db, err := db.OpenDB(dir) if err != nil { log.Fatal(err) } if mode == "v1" { v1.Start(db, port) } else if mode == "v2" { v2.Start(db, port) } else if mode == "v3" { v3.Start(db, port) } case "bench": benchmark() case "durable-bench": durableBenchmark() case "example": embeddedExample() default: flag.PrintDefaults() return } }
func TestAPIV2(t *testing.T) { tmp := "/tmp/tiedot_apiv2_test" os.RemoveAll(tmp) defer os.RemoveAll(tmp) db, err := db.OpenDB(tmp) if err != nil { t.Fatal(err) } }
func main() { var defaultMaxprocs int var err error if defaultMaxprocs, err = strconv.Atoi(os.Getenv("GOMAXPROCS")); err != nil { defaultMaxprocs = runtime.NumCPU() * 2 } var mode, dir string var port, maxprocs int flag.StringVar(&mode, "mode", "", "[v1|bench|example]") flag.StringVar(&dir, "dir", "", "database directory") flag.IntVar(&port, "port", 0, "listening port number") flag.IntVar(&maxprocs, "gomaxprocs", defaultMaxprocs, "GOMAXPROCS") flag.Parse() if mode == "" { log.Fatal("tiedot -mode=[v1|bench|example] -gomaxprocs=MAX_NUMBER_OF_GOPROCS") } runtime.GOMAXPROCS(maxprocs) log.Printf("GOMAXPROCS is set to %d", maxprocs) switch mode { case "v1": if dir == "" { log.Fatal("Please specify database directory, for example -dir=/tmp/db") } if port == 0 { log.Fatal("Please specify port number, for example -port=8080") } db, err := db.OpenDB(dir) if err != nil { log.Fatal(err) } v1.Start(db, port) case "bench": benchmark() case "example": embeddedExample() default: log.Fatal("tiedot -mode=[v1|bench] -gomaxprocs=MAX_NUMBER_OF_GOPROCS") } }
func embeddedExample() { dir := "/tmp/MyDatabase" os.RemoveAll(dir) defer os.RemoveAll(dir) // Open database myDB, err := db.OpenDB(dir) if err != nil { panic(err) } // Create collection if err := myDB.Create("A"); err != nil { panic(err) } if err := myDB.Create("B"); err != nil { panic(err) } // Rename collection if err := myDB.Rename("B", "C"); err != nil { panic(err) } // Which collections do I have? for name := range myDB.StrCol { fmt.Printf("I have a collection called %s\n", name) } // Drop collection if err := myDB.Drop("C"); err != nil { panic(err) } // Start using collection A := myDB.Use("A") // Collection insert/update/delete operations require the document to be a map[string]interface{} // Otherwise index may not work docID, err := A.Insert(map[string]interface{}{"Url": "http://google.com", "Owner": "Google Inc."}) if err != nil { panic(err) } fmt.Printf("Inserted document at %d (document ID)\n", docID) // Update document (you can still use struct, but this example uses generic interface{}) var doc map[string]interface{} json.Unmarshal([]byte(`{"Url": "http://www.google.com.au", "Owner": "Google Inc."}`), &doc) newID, err := A.Update(docID, doc) // newID may or may not be the same! if err != nil { panic(err) } fmt.Printf("Updated document %d to %v, new ID is %d\n", docID, doc, newID) // Read document var readback map[string]interface{} if err := A.Read(newID, &readback); err != nil { panic(err) } fmt.Printf("Read document ID %d: %v\n", newID, readback) // Delete document A.Delete(123) // passing invalid ID to it will not harm your data /* Collection insert/update/delete have their dedicated "durable" calls: - durableInsert - durableUpdate - durableDelete Those operations ensure a disk flush after each call to guarantee data durability on disk. However - those operations are 10000x more expensive than ordinary insert/update/delete! */ // Create index if err := A.Index([]string{"a", "b", "c"}); err != nil { panic(err) } // Which indexes do I have on collection A? for path := range A.StrHT { fmt.Printf("I have an index on path %s\n", path) } // Remove index if err := A.Unindex([]string{"a", "b", "c"}); err != nil { panic(err) } // Execute query result := make(map[uint64]struct{}) var query interface{} json.Unmarshal([]byte(`"all"`), &query) if err := db.EvalQueryV2(query, A, &result); err != nil { panic(err) } for id := range result { // map keys are query results - result document IDs fmt.Printf("Query returned document ID %d\n", id) } // Gracefully close database myDB.Close() }
func embeddedExample() { dir := "/tmp/MyDatabase" os.RemoveAll(dir) defer os.RemoveAll(dir) // Open database myDB, err := db.OpenDB(dir) if err != nil { panic(err) } // Create collection if err := myDB.Create("A"); err != nil { panic(err) } if err := myDB.Create("B"); err != nil { panic(err) } // Rename collection if err := myDB.Rename("B", "C"); err != nil { panic(err) } // Which collections do I have? for name := range myDB.StrCol { fmt.Printf("I have a collection called %s\n", name) } // Drop collection if err := myDB.Drop("C"); err != nil { panic(err) } // Start using collection A := myDB.Use("A") // Insert document var doc interface{} json.Unmarshal([]byte(`{"a": 1, "b": 2}`), &doc) docID, err := A.Insert(doc) if err != nil { panic(err) } fmt.Printf("Inserted document %v at %d (document ID)\n", doc, docID) // Update document json.Unmarshal([]byte(`{"a": 2, "b": 3}`), &doc) newID, err := A.Update(docID, doc) // newID may or may not be the same if err != nil { panic(err) } fmt.Printf("Updated document %d to %v, new ID is %d\n", docID, doc, newID) // Delete document A.Delete(123) // passing invalid ID to it will not harm your data // Create index if err := A.Index([]string{"a", "b", "c"}); err != nil { panic(err) } // Which indexes do I have on collection A? for path := range A.StrHT { fmt.Printf("I have an index on path %s\n", path) } // Remove index if err := A.Unindex([]string{"a", "b", "c"}); err != nil { panic(err) } // Execute query result := make(map[uint64]bool) var query interface{} json.Unmarshal([]byte(`["all"]`), &query) if err := db.EvalQuery(query, A, &result); err != nil { panic(err) } for id := range result { // query results are in map keys fmt.Printf("Query returned document ID %d\n", id) } // Gracefully close database myDB.Close() }
func embeddedExample() { dir := "/tmp/MyDatabase" os.RemoveAll(dir) defer os.RemoveAll(dir) // Open database myDB, err := db.OpenDB(dir) if err != nil { panic(err) } // Create collection if err := myDB.Create("A"); err != nil { panic(err) } if err := myDB.Create("B"); err != nil { panic(err) } // Rename collection if err := myDB.Rename("B", "C"); err != nil { panic(err) } // Which collections do I have? for name := range myDB.StrCol { fmt.Printf("I have a collection called %s\n", name) } // Drop collection if err := myDB.Drop("C"); err != nil { panic(err) } // Start using collection A := myDB.Use("A") /* * Insert document. * You may insert/update any interface{} to collection, for example: * * var doc interface{} * json.Unmarshal([]byte(`{"a": 1, "b": 2}`), &doc) * A.Insert(doc) * * And here is an example using struct: */ type Document struct { Url, Owner string } docID, err := A.Insert(Document{"http://google.com", "Google Inc."}) if err != nil { panic(err) } fmt.Printf("Inserted document at %d (document ID)\n", docID) // Update document (you can still use struct, but this example uses generic interface{}) var doc interface{} json.Unmarshal([]byte(`{"Url": "http://www.google.com.au", "Owner": "Google Inc."}`), &doc) newID, err := A.Update(docID, doc) // newID may or may not be the same! if err != nil { panic(err) } fmt.Printf("Updated document %d to %v, new ID is %d\n", docID, doc, newID) // Read document var readback Document if err := A.Read(newID, &readback); err != nil { panic(err) } fmt.Printf("Read document ID %d: %v\n", newID, readback) // Delete document A.Delete(123) // passing invalid ID to it will not harm your data // Create index if err := A.Index([]string{"a", "b", "c"}); err != nil { panic(err) } // Which indexes do I have on collection A? for path := range A.StrHT { fmt.Printf("I have an index on path %s\n", path) } // Remove index if err := A.Unindex([]string{"a", "b", "c"}); err != nil { panic(err) } // Execute query result := make(map[uint64]bool) var query interface{} json.Unmarshal([]byte(`["all"]`), &query) if err := db.EvalQuery(query, A, &result); err != nil { panic(err) } for id := range result { // query results are in map keys fmt.Printf("Query returned document ID %d\n", id) } // Gracefully close database myDB.Close() }