func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Setup a new query with a placeholder
	myQuery := gocb.NewN1qlQuery("SELECT airportname, city, country FROM `travel-sample` " +
		"WHERE type='airport' AND city=$1 ")

	// Setup an array for parameters
	var myParams []interface{}
	myParams = append(myParams, "Reno")

	// Execute Query
	rows, err := bucket.ExecuteN1qlQuery(myQuery, myParams)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Iterate through rows and print output
	var row interface{}
	for rows.Next(&row) {
		fmt.Printf("Results: %+v\n", row)
	}

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
Пример #2
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Create a document
	key := "goDevguideExampleRetrieve"
	val := "Retrieve Test Value"
	_, err = bucket.Upsert(key, &val, 0)
	if err != nil {
		fmt.Println("ERRROR CREATING DOCUMENT:", err)
	}

	// Retrieve Document
	var retValue interface{}
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Setup a new query for building an index
	myQuery := gocb.NewN1qlQuery("CREATE PRIMARY INDEX ON `travel-sample`")
	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}

	// Iterate through rows and print output
	var row interface{}
	for rows.Next(&row) {
		fmt.Printf("Results: %+v\n", row)
	}

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
Пример #4
0
// userById attempts to return a JSON response for the supplied userid
// couchbase errors are handled in the handleError middleware method
func userById(c *gin.Context) {
	myCluster, _ := gocb.Connect("couchbase://localhost")
	myBucket, _ := myCluster.OpenBucket("gamesim-sample", "")
	var value interface{}
	_, err := myBucket.Get(c.Params.ByName("userid"), &value)
	if err != nil {
		c.Error(err).SetType(gin.ErrorTypePublic)
	} else {
		c.JSON(http.StatusOK, &value)
	}
}
Пример #5
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}

	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Create a JSON document
	type Doc struct {
		Item string `json:"item"`
	}
	key := "goDevguideExampleBulkInsert"
	val := Doc{"A bulk insert test value"}

	// Create Array of BulkOps for Insert, and one for Get
	var items []gocb.BulkOp
	var itemsGet []gocb.BulkOp

	// Add 10 items to the array that will be performed as a bulk operation
	for i := 0; i < 10; i++ {
		items = append(items, &gocb.InsertOp{Key: key + "_" + strconv.Itoa(i), Value: &val})
	}

	// Perform the bulk operation to Insert
	err = bucket.Do(items)
	if err != nil {
		fmt.Println("ERRROR PERFORMING BULK INSERT:", err)
	}

	// Retrieve 10 items to the array that will be performed as a bulk operation
	for i := 0; i < 10; i++ {
		itemsGet = append(itemsGet, &gocb.GetOp{Key: key + "_" + strconv.Itoa(i), Value: &Doc{}})
	}

	// Perform the bulk operation to Get
	err = bucket.Do(itemsGet)
	if err != nil {
		fmt.Println("ERRROR PERFORMING BULK GET:", err)
	}

	// Print the output
	for i := 0; i < len(itemsGet); i++ {
		fmt.Println(itemsGet[i].(*gocb.GetOp).Key, itemsGet[i].(*gocb.GetOp).Value.(*Doc).Item)
	}

	// Exiting
	fmt.Println("Example Successful  - Exiting")
}
Пример #6
0
func deleteBucket(host string, bucket string, t *testing.T, noErr bool) {
	connection, err := gocb.Connect(host)
	if err != nil {
		t.Fatal("Unable to connect to cluster: " + err.Error())
	}

	manager := connection.Manager("Administrator", "password")
	if err := manager.RemoveBucket(bucket); err != nil && !noErr {
		t.Fatalf("Error deleting bucket %s", bucket)
	}
}
Пример #7
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}
	fmt.Println("Example Successful - Exiting")
}
func NewGoCBStorageEngine(couchbaseUrl, bucketName string) *GoCBStorageEngine {
	cluster, err := gocb.Connect(couchbaseUrl)
	if err != nil {
		log.Panicf("Could not connect to %v.  Err: %v", couchbaseUrl, err)
	}
	bucket, err := cluster.OpenBucket(bucketName, "")
	if err != nil {
		log.Panicf("Could not open bucket: %v.  Err: %v", bucket, err)
	}

	return &GoCBStorageEngine{
		Bucket: bucket,
	}
}
Пример #9
0
func newGoCbClient(serverURL string, bucketName string) *goCbClient {
	cluster, err := gocb.Connect(serverURL)
	if err != nil {
		log.Fatalf("Could not connect to %v.  Err: %v", serverURL, err)
	}
	bucket, err := cluster.OpenBucket(bucketName, "")
	if err != nil {
		log.Fatalf("Could not open bucket: %v.  Err: %v", bucketName, err)
	}

	return &goCbClient{
		bucket: bucket,
	}
}
Пример #10
0
// userlist queries the playerlist view from the gamesim bucket and returns
// a JSON response containing all documents returned by the view
func userList(c *gin.Context) {
	myCluster, _ := gocb.Connect("couchbase://localhost")
	myBucket, _ := myCluster.OpenBucket("gamesim-sample", "")
	myQuery := gocb.NewViewQuery("players", "playerlist")
	rows := myBucket.ExecuteViewQuery(myQuery)

	var results = make([]map[string]interface{}, 0)
	var row interface{}
	for rows.Next(&row) {
		results = append(results, row.(map[string]interface{}))
	}
	c.JSON(http.StatusOK, &results)

}
Пример #11
0
func main() {
	// Cluster connection and bucket for couchbase
	cluster, _ := gocb.Connect("couchbase://127.0.0.1")
	bucket, _ = cluster.OpenBucket("travel-sample", "")

	// Http Routing
	http.Handle("/", http.FileServer(http.Dir("./static")))
	http.HandleFunc("/api/airport/findAll", airportHandler)
	http.HandleFunc("/api/flightPath/findAll", flightPathHandler)
	http.HandleFunc("/api/user/login", loginHandler)
	http.HandleFunc("/api/user/flights", userFlightsHandler)
	fmt.Printf("Starting server on :3000\n")
	http.ListenAndServe(":3000", nil)
}
Пример #12
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Create a document
	key := "goDevguideExampleUpdate"
	val := "Retrieve Test Value"
	_, err = bucket.Upsert(key, &val, 0)
	if err != nil {
		fmt.Println("ERRROR CREATING DOCUMENT:", err)
	}

	// Retrieve Document
	var retValue interface{}
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Add something to the value
	retValue = retValue.(string) + " with Additional Test Value"

	// Replace the existing document
	_, err = bucket.Replace(key, &retValue, 0, 0)
	if err != nil {
		fmt.Println("ERRROR REPLACING DOCUMENT:", err)
	}

	// Retrieve updated document
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Exiting
	fmt.Println("Example Successful  - Exiting")
}
Пример #13
0
func deleteAllBuckets(host string, t *testing.T) {
	connection, err := gocb.Connect(host)
	if err != nil {
		t.Fatal("Unable to connect to cluster: " + err.Error())
	}

	manager := connection.Manager("Administrator", "password")
	buckets, err := manager.GetBuckets()
	if err != nil {
		t.Fatal("Unable to get all buckets: " + err.Error())
	}

	for _, bucket := range buckets {
		if err := manager.RemoveBucket(bucket.Name); err != nil {
			t.Fatalf("Error deleting bucket %s", bucket.Name)
		}
	}
}
Пример #14
0
func IndexHandler(w http.ResponseWriter, r *http.Request) {
	cluster, _ := gocb.Connect("couchbase://localhost")
	bucket, _ := cluster.OpenBucket("default", "")

	value := "test"
	cas, _ := bucket.Insert("snake", &value, 0)
	fmt.Printf("Inserted document CAS is `%08x`\n", cas)

	snake := lib.Snake{
		Points: lib.Points{
			lib.Point{
				X: 1,
				Y: 2,
			},
		},
	}

	json.NewEncoder(w).Encode(snake)
}
Пример #15
0
// Creates a Bucket that talks to a real live Couchbase server.
func GetCouchbaseBucketGoCB(spec BucketSpec) (bucket Bucket, err error) {

	// Only wrap the gocb logging when the log key is set, to avoid the overhead of a log keys
	// map lookup for every gocb log call

	logKeys := GetLogKeys()
	if logKeys["gocb"] {
		EnableGoCBLogging()
	}

	cluster, err := gocb.Connect(spec.Server)
	if err != nil {
		return nil, err
	}

	password := ""
	if spec.Auth != nil {
		_, password, _ = spec.Auth.GetCredentials()
	}

	goCBBucket, err := cluster.OpenBucket(spec.BucketName, password)

	if err != nil {
		return nil, err
	}

	spec.MaxNumRetries = 10
	spec.InitialRetrySleepTimeMS = 5

	// Define channels to limit the number of concurrent single and bulk operations,
	// to avoid gocb queue overflow issues
	bucket = CouchbaseBucketGoCB{
		goCBBucket,
		spec,
		make(chan struct{}, MaxConcurrentSingleOps),
		make(chan struct{}, MaxConcurrentBulkOps),
	}

	return bucket, err

}
Пример #16
0
func BenchmarkCouchBaseInsertgocb(b *testing.B) {
	var err error
	insertUsers := make([]*User, len(couchBaseTestUsers))
	copy(insertUsers, couchBaseTestUsers)
	db_url, err := url.Parse(*couchBaseUrl)
	mf(err, "parse", b)

	cluster, err := gocb.Connect(db_url.String())
	mf(err, "connect - "+db_url.String(), b)

	bucket, err := cluster.OpenBucket(*couchBaseBucket, "")
	mf(err, "bucket", b)

	b.ResetTimer()

	for i := 0; i < b.N; i++ {
		user := getUser(i, uuid.NewV4().String(), &couchBaseTestUsers)
		_, err := bucket.Insert(user.Id, user, 0)
		mf(err, "Insert", b)
	}
}
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Inbound http handlers
	http.Handle("/", http.FileServer(http.Dir("./public")))
	http.HandleFunc("/api/query/untyped", queryUntyped)
	http.HandleFunc("/api/query/typed", queryTyped)
	http.HandleFunc("/api/query/one", queryOne)
	fmt.Printf("Starting server on :3000\n")
	http.ListenAndServe(":3000", nil)

}
Пример #18
0
func BenchmarkCouchBaseGetgocb(b *testing.B) {
	var err error

	db_url, err := url.Parse(*couchBaseUrl)
	mf(err, "parse", b)

	cluster, err := gocb.Connect(db_url.String())
	mf(err, "connect - "+db_url.String(), b)

	bucket, err := cluster.OpenBucket(*couchBaseBucket, "")
	mf(err, "bucket", b)

	b.ResetTimer()

	max := len(couchBaseTestUsers)
	for i := 0; i < b.N; i++ {
		user := &User{}
		_, err := bucket.Get(couchBaseTestUsers[rand.Intn(max)].Id, *user)
		mf(err, "Get", b)
	}
}
Пример #19
0
func loadData(host string, bucket string, password string, items int,
	prefix string, t *testing.T) {
	connection, err := gocb.Connect(host)
	if err != nil {
		t.Fatal("Test data loader cannot connect to the cluster: " + err.Error())
	}
	b, err := connection.OpenBucket(bucket, password)
	if err != nil {
		t.Fatal("Test data loader cannot connect to the bucket: " + err.Error())
	}

	for i := 0; i < items; i++ {
		key := prefix + strconv.Itoa(i)
		_, err := b.Insert(key, map[string]interface{}{"x": i}, 0)
		if err != nil {
			t.Fatal("Error setting `" + key + "`, " + err.Error())
		}
	}

	b.Close()
}
Пример #20
0
func main() {

	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER")
		panic(err)
	}

	// Open Bucket
	bucket, err = cluster.OpenBucket("default", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET")
		panic(err)
	}

	fmt.Println("==> Enter keyword(s):")

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {

		word := strings.Replace(scanner.Text(), " ", "+", -1)

		meaning, err := lib.GetURL(lib.URL + word)

		if err != nil {
			fmt.Println("Error fetching from urbandictionary.com")
			panic(err)
		}

		cas, err2 := lib.Upsert(bucket, word, meaning)
		if err2 != nil {
			fmt.Println(err2)
		}
		fmt.Printf("Inserted document CAS is `%08x`\n", cas)

		fmt.Println("==> Enter keyword(s):")
	}
}
Пример #21
0
func createBucket(host string, settings *gocb.BucketSettings, t *testing.T) {

	connection, err := gocb.Connect(host)
	if err != nil {
		t.Fatal("Unable to connect to cluster: " + err.Error())
	}

	manager := connection.Manager("Administrator", "password")
	err = manager.InsertBucket(settings)
	if err != nil {
		t.Fatal("Bucket creation failed: " + err.Error())
	}

	for i := 0; i < 30; i++ {
		if isBucketReady(host, settings.Name, t) {
			return
		}
		time.Sleep(1 * time.Second)
	}

	t.Fatal("Bucket creation timed out")
}
Пример #22
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER")
		panic(err)
	}

	// Open Bucket
	bucket, err = cluster.OpenBucket("default", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET")
		panic(err)
	}

	fmt.Println("==> Enter a JSON document:")

	scanner := bufio.NewScanner(os.Stdin)

	for scanner.Scan() {

		document := scanner.Text()

		key, _ := uuid()

		cas, err := bucket.Upsert(key, &document, 0)

		if err != nil {
			fmt.Println("ERROR:", err)
		} else {
			fmt.Printf("Inserted document CAS is `%08x`\n", cas)
		}

		fmt.Println("==> Enter a JSON document:")
	}
}
Пример #23
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Create a document and assign it to 10 - counter works atomically by
	//   first creating a document if it doesn't exist.   If it exists, the
	//   same method will increment/decrement per the "delta" parameter
	key := "goDevguideExampleCounter"
	curKeyValue, _, err := bucket.Counter(key, 2, 10, 0)
	if err != nil {
		fmt.Println("ERRROR CREATING KEY:", err)
	}

	// Should Print 10
	fmt.Println("Initialized Counter:", curKeyValue)

	// Issue same operation, increment value by 2, to 12
	curKeyValue, _, err = bucket.Counter(key, 2, 10, 0)
	if err != nil {
		fmt.Println("ERRROR CREATING KEY:", err)
	}

	// Should Print 12
	fmt.Println("Incremented Counter:", curKeyValue)

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
Пример #24
0
func main() {
	// Create a JSON document struct
	type Doc struct {
		FirstItem  string `json:"firstItem"`
		SecondItem string `json:"secondItem"`
		ThirdItem  string `json:"thirdItem"`
	}

	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}
	// Create a document
	key := "goDevguideExampleSubdoc"
	val := Doc{"Some Test Field Data for firstItem", "Some Test Field Data for secondItem", "Some Test Field Data for thirdItem"}
	_, err = bucket.Upsert(key, &val, 0)
	if err != nil {
		fmt.Println("ERRROR CREATING DOCUMENT:", err)
	}
	// Retrieve Full Document, Get operation
	var retValue interface{}
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Subdoc Operation: Retrieve Document Fragment for two fields, using LookupIn
	frag, err := bucket.LookupIn("goDevguideExampleSubdoc").Get("secondItem").Get("thirdItem").Execute()
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT FRAGMENT:", err)
	}
	// Print results
	frag.Content("firstItem", &retValue)
	fmt.Println("Document Fragment Retrieved (secondItem):", retValue)
	frag.Content("thirdItem", &retValue)
	fmt.Println("Document Fragment Retrieved (thirdItem):", retValue)

	// Subdoc Operation: Add a fourth item that is an array
	frag, err = bucket.MutateIn("goDevguideExampleSubdoc", 0, 0).
		Upsert("fourthItem", []string{"250 GT SWB", "250 GTO", "250 LM", "275 GTB"}, true).
		Execute()
	if err != nil {
		fmt.Println("ERRROR ADDING DOCUMENT FRAGMENT:", err)
	}
	// LookupIn to retrieve the changes
	frag, err = bucket.LookupIn("goDevguideExampleSubdoc").Get("fourthItem").Execute()
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT FRAGMENT:", err)
	}
	frag.Content("fourthItem", &retValue)
	fmt.Println("Document Fragment Retrieved (fourthItem):", retValue)

	// Subdoc Operation: Add a value to a specific position, to the "front" of the fourthItem Array,
	// to the "back" of the fourthItem Array, and another unique value to the back of the Array in one operation
	frag, err = bucket.MutateIn("goDevguideExampleSubdoc", 0, 0).
		ArrayInsert("fourthItem[2]", "250 GTO Series II").
		PushFront("fourthItem", "250 GT Lusso", false).
		PushBack("fourthItem", "275 GTB/4", false).
		AddUnique("fourthItem", "288 GTO", false).
		Execute()
	if err != nil {
		fmt.Println("ERRROR ADDING DOCUMENT FRAGMENT:", err)
	}
	// LookupIn to retrieve the changes
	frag, err = bucket.LookupIn("goDevguideExampleSubdoc").Get("fourthItem").Execute()
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT FRAGMENT:", err)
	}
	frag.Content("fourthItem", &retValue)
	fmt.Println("Document Fragment Retrieved (fourthItem):", retValue)

	// Subdoc Operation: Remove a value from the fourthItem Array
	frag, err = bucket.MutateIn("goDevguideExampleSubdoc", 0, 0).Remove("fourthItem[3]").Execute()
	if err != nil {
		fmt.Println("ERRROR ADDING DOCUMENT FRAGMENT:", err)
	}
	// LookupIn to retrieve the changes
	frag, err = bucket.LookupIn("goDevguideExampleSubdoc").Get("fourthItem").Execute()
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT FRAGMENT:", err)
	}
	frag.Content("fourthItem", &retValue)
	fmt.Println("Document Fragment Retrieved (fourthItem):", retValue)

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
Пример #25
0
func listenCouchbase(params Params, state *State, handler func()) {
	cluster, err := gocb.Connect(clusterURI)
	if err != nil {
		log.Printf("failed to connect to cluster: %v", err)
		return
	}
	var connectionName string
	if len(params.partitions) > 20 {
		connectionName = fmt.Sprintf("DCPL[%d/%s]", rand.Int(), params.partitions[0:20])
	} else {
		connectionName = fmt.Sprintf("DCPL[%d/%s]", rand.Int(), params.partitions)
	}
	bucket, err := cluster.OpenStreamingBucket(connectionName, params.bucket, params.password)
	if err != nil {
		log.Printf("failed to open the bucket %q: %v", params.bucket, err)
		return
	}
	state.bucket = bucket
	type partitionState struct {
		vbUuid         gocbcore.VbUuid
		startSeqNo     gocbcore.SeqNo
		endSeqNo       gocbcore.SeqNo
		snapStartSeqNo gocbcore.SeqNo
		snapEndSeqNo   gocbcore.SeqNo
	}
	direction := parseDirection(params.direction)

	agent := bucket.IoRouter()
	numServers := agent.NumServers()
	partitionsRange := parseRange(params.partitions, bucket)
	partitionsState := make([]partitionState, agent.NumVbuckets())
	for i := 0; i < numServers; i++ {
		_, err = agent.GetVbucketSeqnos(i, func(vbId uint16, lastSeqno gocbcore.SeqNo, err error) {
			if err != nil {
				log.Printf("failed to get last checkpoint for server %d: %v", i, err)
				return
			}
			switch direction {
			case DIRECTION_FROM_CURRENT:
				partitionsState[vbId] = partitionState{
					startSeqNo:     lastSeqno,
					endSeqNo:       0xffffffff,
					snapStartSeqNo: lastSeqno,
					snapEndSeqNo:   0xffffffff,
				}
			case DIRECTION_TO_CURRENT:
				partitionsState[vbId] = partitionState{
					startSeqNo:     0,
					endSeqNo:       lastSeqno,
					snapStartSeqNo: 0,
					snapEndSeqNo:   lastSeqno,
				}
			}
		})
	}
	for _, p := range partitionsRange {
		pid := p
		stream := state.NewStream(p)
		ps := partitionState{
			vbUuid:         0,
			startSeqNo:     0,
			endSeqNo:       0xffffffff,
			snapStartSeqNo: 0,
			snapEndSeqNo:   0,
		}
		if direction != DIRECTION_EVERYTHING {
			ps = partitionsState[pid]
			agent := bucket.IoRouter()
			_, err = agent.GetFailoverLog(pid, func(flog []gocbcore.FailoverEntry, err error) {
				if err != nil {
					log.Printf("failed to get failover log for vbucket %d: %v", pid, err)
					return
				}
				ps.vbUuid = flog[0].VbUuid
			})
			if err != nil {
				log.Printf("failed to schedule get failover log for vbucket %d: %v", pid, err)
				return
			}
		}
		_, err = agent.OpenStream(pid,
			ps.vbUuid,
			ps.startSeqNo,
			ps.endSeqNo,
			ps.snapStartSeqNo,
			ps.snapEndSeqNo,
			stream, func(slog []gocbcore.FailoverEntry, err error) {
				if err != nil {
					log.Printf("failed to open DCP stream for vbucket %d: %v", pid, err)
					// FIXME: close opened streams or just skip failures
					return
				}
			})
		if err != nil {
			log.Printf("failed to schedule open DCP stream for vbucket %d: %v", pid, err)
			// FIXME: close opened streams or just skip failures
			return
		}
	}

	handler()
}
Пример #26
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}

	// Create a document and assign an expiration of 2 seconds
	key := "goDevguideExampleExpiration"
	val := "Expiration Test Value"
	_, err = bucket.Upsert(key, &val, 2)
	if err != nil {
		fmt.Println("ERRROR CREATING DOCUMENT:", err)
	}

	// Retrieve Value Right Away
	var retValue interface{}
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Not Yet Expired:", retValue)

	// Sleep for 4 seconds
	time.Sleep(4 * time.Second)

	// Try to retrieve document when Expired
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("Document Expired:", err)
	}

	// Create a document with no expiration, and add an expiry using
	//  touch after the document is created of 2 seconds
	_, err = bucket.Upsert(key, &val, 2)
	if err != nil {
		fmt.Println("ERRROR CREATING DOCUMENT:", err)
	}

	// Add an expiry
	_, err = bucket.Touch(key, 0, 2)
	if err != nil {
		fmt.Println("ERRROR TOUCHING DOCUMENT:", err)
	}

	// Retrieve Document
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Not Yet Expired:", retValue)

	// Sleep for 4 seconds
	time.Sleep(4 * time.Second)

	// Try to retrieve document when Expired
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("Document Expired:", err)
	}

	// Exiting
	fmt.Println("Example Successful - Exiting")
}
Пример #27
0
func testGet() {
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//
	//		for i := 0; i < N; i++ {
	//			var curUser User
	//			if _, err := bucket1.Get("*****@*****.**", &curUser); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//				//			fmt.Printf("%+v", curUser)
	//			}
	//
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()

	cluster, e := gocb.Connect("couchbase://127.0.0.1")
	//	cluster, e := gocb.Connect("couchbase://42.62.101.136")
	log.Debugf("connect e=%+v", e)
	if e != nil {
		fmt.Errorf("%+v", e.Error())
	}
	bucket, e = cluster.OpenBucket("travel-sample", "")
	if e != nil {
		fmt.Errorf("OpenBucket ERROR=%+v", e.Error())
	}
	//
	//	myQuery := gocb.NewN1qlQuery("select * from `travel-sample`")
	myQuery := gocb.NewN1qlQuery("select * from `travel-sample` where airportname IS NOT NULL limit 1")
	N := 100
	t1 := time.Now()

	//	for i := 0; i < N; i++ {

	rows, err := bucket.ExecuteN1qlQuery(myQuery, nil)
	if err != nil {
		fmt.Println("ERROR EXECUTING N1QL QUERY:", err)
	}
	var airports []AirportIntermediary
	var row AirportIntermediary // 这里的查询结果要严格对应定义的格式,否则转出来的struct的内部值都是空值
	log.Infof("rows=%#v", rows)
	for rows.Next(&row) {
		airports = append(airports, row)
		log.Debugf("row=%+v", row)
	}
	log.Infof("airport = %+v", airports)

	if err := rows.Close(); err != nil {
		fmt.Printf("N1QL query error: %s\n", err)
	}

	//	}
	t2 := time.Now()
	d := t2.Sub(t1)

	log.Infof("BenchmarkGo, %+v times in %+v", N, d)
}
Пример #28
0
func testSet() {
	buckValue := `this is the value`

	// 同时写会出问题..

	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			if _, err := bucket1.Upsert(string("*****@*****.**"), buckValue, 0); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			if _, err := bucket1.Upsert(string("*****@*****.**"), buckValue, 0); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			if _, err := bucket1.Upsert(string("*****@*****.**"), buckValue, 0); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()
	//
	//	go func() {
	//		cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	//		log.Debugf("connect e=%+v", e)
	//		if e != nil {
	//			fmt.Errorf("%+v", e.Error())
	//		}
	//		bucket1, e := cluster1.OpenBucket("travel-sample", "")
	//
	//
	//		N := 50000
	//		t1 := time.Now()
	//
	//		for i := 0; i < N; i++ {
	//			if _, err := bucket1.Upsert(string("*****@*****.**"), buckValue, 0); err != nil {
	//				fmt.Print("bucket get err=", err.Error())
	//				return
	//			}else {
	//			}
	//		}
	//		t2 := time.Now()
	//		d := t2.Sub(t1)
	//
	//		log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
	//
	//	}()

	cluster1, e := gocb.Connect("couchbase://127.0.0.1")
	log.Debugf("connect e=%+v", e)
	if e != nil {
		fmt.Errorf("%+v", e.Error())
	}
	bucket1, e := cluster1.OpenBucket("travel-sample", "")

	N := 1
	t1 := time.Now()

	for i := 0; i < N; i++ {
		if _, err := bucket1.Upsert(string("*****@*****.**"), buckValue, 0); err != nil {
			fmt.Print("bucket get err=", err.Error())
			return
		} else {
		}
	}
	t2 := time.Now()
	d := t2.Sub(t1)

	log.Infof("BenchmarkGo go, %+v times in %+v", N, d)
}
Пример #29
0
func main() {
	// Connect to Cluster
	cluster, err := gocb.Connect("couchbase://127.0.0.1")
	if err != nil {
		fmt.Println("ERRROR CONNECTING TO CLUSTER:", err)
	}
	// Open Bucket
	bucket, err = cluster.OpenBucket("travel-sample", "")
	if err != nil {
		fmt.Println("ERRROR OPENING BUCKET:", err)
	}
	// Set Durability Timeout to 3 seconds, default is 40 seconds
	bucket.SetDurabilityTimeout(3 * time.Second)

	// Create a document and assign a "Persist To" value of 1 node.
	// Should Always Succeed, even on single node cluster.
	//   NOTICE the function signature:
	//      Bucket.UpsertDura(key, value, expiry, replicateTo, _PERSIST_TO_)
	key := "goDevguideExamplePersistTo"
	val := "Durabilty PersistTo Test Value"
	_, err = bucket.UpsertDura(key, &val, 0, 0, 1)
	if err != nil {
		fmt.Println("ERROR, DURABILITY Persist Example:", err)
	}

	// Retrieve Value Persist To
	var retValue interface{}
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Create a document and assign a "Replicate To" value of 1 node.
	// Should Fail on a single node cluster, succeed on a multi node
	// cluster of 3 or more nodes with at least one replica enabled.
	//   NOTICE the function signature:
	//      Bucket.UpsertDura(key, value, expiry, _REPLICATE_TO_, persistTo)

	key = "goDevguideExampleReplicateTo"
	val = "Durabilty ReplicateTo Test Value"
	_, err = bucket.UpsertDura(key, &val, 0, 1, 0)
	if err != nil {
		fmt.Println("ERROR, DURABILITY Replicate Example:", err)
	}

	// Retrieve Value Replicate To
	// Should succeed even if durability fails, as the document was
	// still written.
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Create a document and assign a "Replicate To" and a "Persist TO"
	// value of 1 node. Should Fail on a single node cluster, succeed on
	// a multi node cluster of 3 or more nodes with at least one replica
	// enabled.
	//   NOTICE the function signature:
	//      Bucket.UpsertDura(key, value, expiry, _REPLICATE_TO_, _PERSIST_TO_)

	key = "goDevguideExampleReplicateToAndPersistTo"
	val = "Durabilty ReplicateTo and PersistTo Test Value"
	_, err = bucket.UpsertDura(key, &val, 0, 1, 1)
	if err != nil {
		fmt.Println("ERROR, DURABILITY Replicate and Persist Example:", err)
	}

	// Retrieve Value Replicate To and Persist To
	// Should succeed even if durability fails, as the document was
	// still written.
	_, err = bucket.Get(key, &retValue)
	if err != nil {
		fmt.Println("ERRROR RETURNING DOCUMENT:", err)
	}
	fmt.Println("Document Retrieved:", retValue)

	// Exiting
	fmt.Println("Example Complete - Exiting")
}