Example #1
0
// 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
}
Example #2
0
// 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

}
Example #3
0
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()
}