// loadTestData adds all the test data into the database. func loadTestData(context interface{}, db *db.DB) error { // Make sure the old data is clear. if err := unloadTestData(context, db); err != nil { if !graph.IsQuadNotExist(err) { return err } } // ----------------------------------------------------------- // Load example items, relationships, views, and patterns. items, rels, vs, pats, err := wirefix.Get() if err != nil { return err } if err := wirefix.Add(context, db, items, rels, vs, pats); err != nil { return err } // ----------------------------------------------------------- // Build the example graph. mongoURI := cfg.MustURL("MONGO_URI") if err := cayleyshelf.InitQuadStore(mongoURI.String()); err != nil { return err } var quads []quad.Quad quads = append(quads, quad.Make(wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"authored", wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"flagged", wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", "")) tx := cayley.NewTransaction() for _, quad := range quads { tx.AddQuad(quad) } if err := db.NewCayley(tests.Context, tests.TestSession); err != nil { return err } store, err := db.GraphHandle(tests.Context) if err != nil { return err } defer store.Close() if err := store.ApplyTransaction(tx); err != nil { return err } return nil }
// loadItems adds items to run tests. func loadItems(context interface{}, db *db.DB) error { store, err := db.GraphHandle(context) if err != nil { return err } items, err := itemfix.Get() if err != nil { return err } for _, itm := range items { if err := sponge.Import(context, db, store, &itm); err != nil { return err } } return nil }
// unloadTestData removes all the test data from the database. func unloadTestData(context interface{}, db *db.DB) error { // ------------------------------------------------------------ // Clear items, relationships, and views. wirefix.Remove("context", db, wirePrefix) // ------------------------------------------------------------ // Clear cayley graph. var quads []quad.Quad quads = append(quads, quad.Make(wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", wirePrefix+"on", wirePrefix+"c1b2bbfe-af9f-4903-8777-bd47c4d5b20a", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"d1dfa366-d2f7-4a4a-a64f-af89d4c97d82", "")) quads = append(quads, quad.Make(wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", wirePrefix+"authored", wirePrefix+"6eaaa19f-da7a-4095-bbe3-cee7a7631dd4", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"authored", wirePrefix+"d16790f8-13e9-4cb4-b9ef-d82835589660", "")) quads = append(quads, quad.Make(wirePrefix+"a63af637-58af-472b-98c7-f5c00743bac6", wirePrefix+"flagged", wirePrefix+"80aa936a-f618-4234-a7be-df59a14cf8de", "")) tx := cayley.NewTransaction() for _, quad := range quads { tx.RemoveQuad(quad) } if err := db.NewCayley(tests.Context, tests.TestSession); err != nil { return err } store, err := db.GraphHandle(tests.Context) if err != nil { return err } defer store.Close() if err := store.ApplyTransaction(tx); err != nil { return err } return nil }
// materializeView executes a view, creates a temporary collection for the view, and // modifies the query to query the temporary collection. func materializeView(context interface{}, db *db.DB, q *query.Query, vars map[string]string) (string, error) { // Make sure we have a valid connection to the graph. graph, err := db.GraphHandle(context) if err != nil { return "", err } // Make sure we have the information we need to execute the view. viewName, ok := vars["view"] if !ok { return "", fmt.Errorf("Vars does not include \"view\".") } itemKey, ok := vars["item"] if !ok { return "", fmt.Errorf("Vars does not include \"item\".") } // Generate a unique name for the collection. viewCol := uuid.New() // Prepare the parameters for executing the view. viewParams := wire.ViewParams{ ViewName: viewName, ItemKey: itemKey, ResultsCollection: viewCol, } // Execute the view. if _, err := wire.Execute(context, db, graph, &viewParams); err != nil { return viewCol, err } // Provide the query with the temporary collection name. q.Collection = viewCol return viewCol, nil }
func main() { // Initialize the configuration if err := cfg.Init(cfg.EnvProvider{Namespace: "WIRE"}); err != nil { wire.Println("Unable to initialize configuration") os.Exit(1) } // Initialize the logging logLevel := func() int { ll, err := cfg.Int(cfgLoggingLevel) if err != nil { return log.NONE } return ll } log.Init(os.Stderr, logLevel, log.Ldefault) wire.Println("Using log level", logLevel()) // Pull options from the config. var mgoDB *db.DB var graphDB *cayley.Handle // Configure MongoDB. wire.Println("Configuring MongoDB") mongoURI := cfg.MustURL(cfgMongoURI) err := db.RegMasterSession("startup", mongoURI.Path, mongoURI.String(), 0) if err != nil { wire.Println("Unable to initialize MongoDB") os.Exit(1) } mgoDB, err = db.NewMGO("startup", mongoURI.Path) if err != nil { wire.Println("Unable to get MongoDB session") os.Exit(1) } defer mgoDB.CloseMGO("") // Configure Cayley. wire.Println("Configuring Cayley") if err := mgoDB.NewCayley("startup", mongoURI.Path); err != nil { wire.Println("Unable to get Cayley support") } graphDB, err = mgoDB.GraphHandle("startup") if err != nil { wire.Println("Unable to get Cayley handle") os.Exit(1) } // Add the graph and view commands to the CLI tool. wire.AddCommand( cmdview.GetCommands(mgoDB, graphDB), ) // Execute the command. wire.Execute() }