當前位置: 首頁>>代碼示例>>Golang>>正文


Golang cqlc.NewContext函數代碼示例

本文整理匯總了Golang中github.com/relops/cqlc/cqlc.NewContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewContext函數的具體用法?Golang NewContext怎麽用?Golang NewContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: main

func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, REVERSE_TIMESERIES)

	result := "FAILED"

	ctx := cqlc.NewContext()
	batch := gocql.NewBatch(gocql.LoggedBatch)

	events := 100

	for i := 0; i < events; i++ {

		unix := time.Now().Unix()
		t := time.Unix(unix+int64(i), 0)

		ctx.Upsert(REVERSE_TIMESERIES).
			SetString(REVERSE_TIMESERIES.EVENT_TYPE, "x").
			SetTimestamp(REVERSE_TIMESERIES.INSERTION_TIME, t).
			SetBytes(REVERSE_TIMESERIES.EVENT, []byte("neb")).
			Batch(batch)
	}

	if err := session.ExecuteBatch(batch); err != nil {
		log.Fatalf("Could not execute batch: %v", err)
		os.Exit(1)
	}

	iter, err := ctx.Select().From(REVERSE_TIMESERIES).Fetch(session)
	if err != nil {
		log.Fatalf("Could not read REVERSE_TIMESERIES: %v", err)
		os.Exit(1)
	}

	var previous time.Time

	err = MapReverseTimeseries(iter, func(e ReverseTimeseries) (bool, error) {

		current := e.InsertionTime

		if !previous.IsZero() {
			if current.After(previous) {
				return false, fmt.Errorf("Wrong ordering (DESC): previous was %v but current is %v", previous, current)
			}
		}

		previous = current
		return true, nil
	})

	if err != nil {
		log.Fatalf("Unexpected order of REVERSE_TIMESERIES: %v", err)
		os.Exit(1)
	}

	result = "PASSED"

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:60,代碼來源:reverse.go

示例2: main

func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, REALLY_BASIC)

	result := "FAILED"

	ctx := cqlc.NewContext()

	basic := ReallyBasic{
		Id:          "y",
		Int32Column: 2001,
	}

	err := ctx.Store(REALLY_BASIC.Bind(basic)).Exec(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	fetched := fetchFirstReallyBasic(ctx, session, "y")

	if reflect.DeepEqual(fetched, basic) {
		result = "PASSED"
	} else {
		result = fmt.Sprintf("[%+v] [%+v]", fetched, basic)
	}

	os.Stdout.WriteString(result)

}
開發者ID:EverythingMe,項目名稱:cqlc,代碼行數:32,代碼來源:bind.go

示例3: main

func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	session.SetPageSize(1000)
	integration.Truncate(session, EVENTS)

	result := "FAILED"

	ctx := cqlc.NewContext()
	batch := gocql.NewBatch(gocql.LoggedBatch)

	rounds := 10
	distinct := 10

	for i := 0; i < rounds; i++ {
		for j := 0; j < distinct; j++ {
			ctx.Upsert(EVENTS).
				SetInt64(EVENTS.SENSOR, int64(j)).
				SetTimeUUID(EVENTS.TIMESTAMP, gocql.TimeUUID()).
				SetFloat32(EVENTS.TEMPERATURE, 19.8).
				SetInt32(EVENTS.PRESSURE, 357).
				Batch(batch)
		}
	}

	err := session.ExecuteBatch(batch)

	if err != nil {
		log.Fatalf("Could not execute batch: %v", err)
		os.Exit(1)
	}

	iter, err := ctx.SelectDistinct(EVENTS.SENSOR).From(EVENTS).Fetch(session)
	if err != nil {
		log.Fatalf("Could not prepare query: %v", err)
		os.Exit(1)
	}

	count := 0

	MapEvents(iter, func(e Events) (bool, error) {
		count++
		return true, nil
	})

	if err := iter.Close(); err != nil {
		log.Fatalf("Could not close iterator: %v", err)
		os.Exit(1)
	}

	if count == distinct {
		result = "PASSED"
	} else {
		result = fmt.Sprintf("Expected %d distinct rows; got %d", distinct, count)
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:58,代碼來源:distinct.go

示例4: runWithStaticKeyspace

func runWithStaticKeyspace(s *gocql.Session) string {
	truncate := fmt.Sprintf("TRUNCATE %s.%s", SHARED.Keyspace(), SHARED.TableName())
	if err := s.Query(truncate).Exec(); err != nil {
		log.Fatalf("Could not connect to cassandra: %v", err)
		os.Exit(1)
	}

	ctx := cqlc.NewContext()
	ctx.StaticKeyspace = true

	return runWithContext(s, ctx)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:12,代碼來源:keyspace.go

示例5: main

func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, BASIC_CLUSTERED)

	result := "FAILED"

	rows := 10

	ctx := cqlc.NewContext()
	batch := gocql.NewBatch(gocql.LoggedBatch)

	for i := 0; i < rows; i++ {
		err := ctx.Upsert(BASIC_CLUSTERED).
			SetInt64(BASIC_CLUSTERED.ID, int64(0)).
			SetInt32(BASIC_CLUSTERED.INT32_CLUSTER, int32(i)).
			SetInt32(BASIC_CLUSTERED.INT32_COLUMN, int32(i)).
			Batch(batch)

		if err != nil {
			log.Fatalf("Could not batch upsert: %v", err)
			os.Exit(1)
		}
	}

	err := session.ExecuteBatch(batch)

	if err != nil {
		log.Fatalf("Could not execute batch: %v", err)
		os.Exit(1)
	}

	iter, err := ctx.Select().From(BASIC_CLUSTERED).Where(BASIC_CLUSTERED.ID.Eq(0)).Fetch(session)

	basics, err := BindBasicClustered(iter)
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	err = iter.Close()
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	if len(basics) == rows {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:52,代碼來源:batch.go

示例6: checkOrderedLimit

func checkOrderedLimit(session *gocql.Session, col ...cqlc.ClusteredColumn) (gocql.UUID, error) {
	var u gocql.UUID
	ctx := cqlc.NewContext()
	_, err := ctx.Select().
		From(SIGNIFICANT_EVENTS).
		Where(SIGNIFICANT_EVENTS.SENSOR.Eq(100)).
		OrderBy(col...).
		Limit(1).
		Bind(SIGNIFICANT_EVENTS.TIMESTAMP.To(&u)).
		FetchOne(session)

	return u, err
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:13,代碼來源:order.go

示例7: runWithKeyspace

func runWithKeyspace(s *gocql.Session, keyspace string) string {

	truncate := fmt.Sprintf("TRUNCATE %s.shared", keyspace)
	if err := s.Query(truncate).Exec(); err != nil {
		log.Fatalf("Could not connect to cassandra: %v", err)
		os.Exit(1)
	}

	ctx := cqlc.NewContext()
	ctx.Keyspace = keyspace

	return runWithContext(s, ctx)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:13,代碼來源:keyspace.go

示例8: main

func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, EVENTS)

	result := "FAILED"

	ctx := cqlc.NewContext()

	var sensorId int64 = 100

	ctx.Upsert(EVENTS).
		SetInt64(EVENTS.SENSOR, sensorId).
		SetTimeUUID(EVENTS.TIMESTAMP, gocql.TimeUUID()).
		SetFloat32(EVENTS.TEMPERATURE, 19.8).
		SetInt32(EVENTS.PRESSURE, 357).
		Exec(session)

	iter, err := ctx.Select().
		From(EVENTS).
		Where(
			EVENTS.SENSOR.Eq(sensorId),
			EVENTS.TIMESTAMP.Lt(gocql.TimeUUID())).
		Fetch(session)

	if err != nil {
		log.Fatalf("Could not execute query: %v", err)
		return
	}

	events, err := BindEvents(iter)
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		return
	}

	err = iter.Close()
	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		return
	}

	if len(events) == 1 {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:48,代碼來源:sensor.go

示例9: main

func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, REALLY_BASIC)

	result := "FAILED"

	ctx := cqlc.NewContext()

	err := ctx.Upsert(REALLY_BASIC).
		SetString(REALLY_BASIC.ID, "x").
		SetInt32(REALLY_BASIC.INT32_COLUMN, 222).
		Exec(session)

	if err != nil {
		log.Fatalf("Could not execute upsert: %v", err)
		os.Exit(1)
	}

	err = ctx.Delete(REALLY_BASIC.INT32_COLUMN).From(REALLY_BASIC).Where(REALLY_BASIC.ID.Eq("x")).Exec(session)

	if err != nil {
		log.Fatalf("Could not execute delete: %v", err)
		os.Exit(1)
	}

	basic := fetchFirstBasic(ctx, session, "x")

	if basic.Int32Column != 0 {
		log.Fatalf("Got bogus basic: %v", basic)
		os.Exit(1)
	}

	err = ctx.Delete().From(REALLY_BASIC).Where(REALLY_BASIC.ID.Eq("x")).Exec(session)

	if err != nil {
		log.Fatalf("Could not execute delete: %v", err)
		os.Exit(1)
	}

	basic = fetchFirstBasic(ctx, session, "x")

	if basic == nil {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:47,代碼來源:delete.go

示例10: main

func main() {

	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, ORIGINAL)

	result := "FAILED"

	ctx := cqlc.NewContext()

	o := Original{
		Id:      "x",
		Date:    time.Now().UTC().Truncate(time.Millisecond),
		Comment: "foo",
	}

	if err := ctx.Store(ORIGINAL.Bind(o)).Exec(session); err != nil {
		log.Fatalf("Could not upsert ORIGINAL: %v", err)
		os.Exit(1)
	}

	iter, err := ctx.Select().From(ORIGINAL).Fetch(session)

	if err != nil {
		log.Fatalf("Could not read ORIGINAL: %v", err)
		os.Exit(1)
	}

	var c Clone

	err = MapOriginal(iter, func(o Original) (bool, error) {
		c = duplicate(&o)
		return false, nil
	})

	if err != nil {
		log.Fatalf("Could not map ORIGINAL: %v", err)
		os.Exit(1)
	}

	if o.Id == c.Id && o.Date == c.Date {
		result = "PASSED"
	} else {
		result = fmt.Sprintf("Original: %+v; clone: %+v", o, c)
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:47,代碼來源:like.go

示例11: main

func main() {

	host := "127.0.0.1"
	keyspace := "twitter_example"

	cluster := gocql.NewCluster(host)
	cluster.Keyspace = keyspace
	session, err := cluster.CreateSession()

	if err != nil {
		log.Fatalf("Could not create CQL session: %s", err)
	}

	ctx := cqlc.NewContext()

	err = ctx.Upsert(TWEETS).
		SetString(TWEETS.NAME, "tweeter").
		Where(TWEETS.ID.Eq(1)).
		Exec(session)

	if err != nil {
		log.Fatalf("Could not execute CQL upsert: %s", err)
	}

	iter, err := ctx.Select().
		From(TWEETS).
		Where(TWEETS.ID.Eq(1)).
		Fetch(session)

	if err != nil {
		log.Fatalf("Could not execute CQL select: %s", err)
	}

	tweets, err := twitter.BindTweets(iter)
	if err != nil {
		log.Fatalf("Could not bind tweets: %s", err)
	}

	err = iter.Close()
	if err != nil {
		log.Fatalf("Could not bind tweets: %s", err)
	}

	log.Printf("Got tweets: %+v\n", tweets)
}
開發者ID:relops,項目名稱:cqlc-example,代碼行數:45,代碼來源:main.go

示例12: main

func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, CLUSTER_BY_STRING_AND_INT)

	result := "FAILED"

	ctx := cqlc.NewContext()

	err := ctx.Upsert(CLUSTER_BY_STRING_AND_INT).
		SetString(CLUSTER_BY_STRING_AND_INT.ID, "a").
		SetString(CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER, "y").
		SetInt64(CLUSTER_BY_STRING_AND_INT.INT64_CLUSTER, 4).
		SetInt64(CLUSTER_BY_STRING_AND_INT.LAST_CLUSTER_ELEMENT, 40).
		SetInt32(CLUSTER_BY_STRING_AND_INT.INT32_COLUMN, 100).
		Exec(session)

	if err != nil {
		log.Fatalf("Could not execute upsert: %v", err)
		os.Exit(1)
	}

	id := CLUSTER_BY_STRING_AND_INT.ID.Eq("a")

	passed := expectComparisonResult(ctx, session, expect(1), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Gt("x"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Gt("y"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Ge("z"))
	passed = expectComparisonResult(ctx, session, expect(1), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Lt("z"))
	passed = expectComparisonResult(ctx, session, expect(1), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Le("y"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Lt("y"))
	passed = expectComparisonResult(ctx, session, expect(0), id, CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Lt("y"))

	stringCluster := CLUSTER_BY_STRING_AND_INT.STRING_CLUSTER.Eq("y")
	int64Cluster := CLUSTER_BY_STRING_AND_INT.INT64_CLUSTER.Eq(4)
	lastClusterElement := CLUSTER_BY_STRING_AND_INT.LAST_CLUSTER_ELEMENT.In(39, 40, 41)

	passed = expectComparisonResult(ctx, session, expect(1), id, stringCluster, int64Cluster, lastClusterElement)

	passed = expectComparisonResult(ctx, session, expect(1), CLUSTER_BY_STRING_AND_INT.ID.In("a"))

	if passed {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:45,代碼來源:comparison.go

示例13: readCounter

func readCounter(session *gocql.Session, key string) (bool, int64) {

	var counter int64

	ctx := cqlc.NewContext()
	found, err := ctx.Select(COUNTER.COUNTER_COLUMN).
		From(COUNTER).
		Where(COUNTER.ID.Eq(key)).
		Bind(COUNTER.COUNTER_COLUMN.To(&counter)).
		FetchOne(session)

	if err != nil {
		log.Fatalf("Could not bind data: %v", err)
		os.Exit(1)
	}

	return found, counter
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:18,代碼來源:counter.go

示例14: main

func main() {
	session := integration.TestSession("127.0.0.1", "cqlc")
	integration.Truncate(session, SIMPLE_INDEXED_COMPOSITE)

	result := "FAILED"

	ctx := cqlc.NewContext()

	s := SimpleIndexedComposite{
		X: 1,
		Y: 2,
		Z: 3,
	}

	if err := ctx.Store(SIMPLE_INDEXED_COMPOSITE.Bind(s)).Exec(session); err != nil {
		log.Fatalf("Could not upsert ORIGINAL: %v", err)
		os.Exit(1)
	}

	var z int32

	found, err := ctx.Select(SIMPLE_INDEXED_COMPOSITE.Z).
		From(SIMPLE_INDEXED_COMPOSITE).
		Where(SIMPLE_INDEXED_COMPOSITE.X.Eq(s.X), SIMPLE_INDEXED_COMPOSITE.Y.Eq(s.Y)).
		Bind(SIMPLE_INDEXED_COMPOSITE.Z.To(&z)).
		FetchOne(session)

	if !found {
		log.Fatalf("Could not find SIMPLE_INDEXED_COMPOSITE with key %v", 1)
		os.Exit(1)
	}

	if err != nil {
		log.Fatalf("Could not retrieve SIMPLE_INDEXED_COMPOSITE: %v", err)
		os.Exit(1)
	}

	if z == s.Z {
		result = "PASSED"
	}

	os.Stdout.WriteString(result)
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:43,代碼來源:composite_index.go

示例15: compareAndSwap

func compareAndSwap(session *gocql.Session, id string, int32column int32) (bool, string, int32) {

	var casId string
	var casInt32column int32

	ctx := cqlc.NewContext()

	applied, err := ctx.Upsert(REALLY_BASIC).
		SetString(REALLY_BASIC.ID, id).
		SetInt32(REALLY_BASIC.INT32_COLUMN, int32column).
		IfExists(REALLY_BASIC.ID.To(&casId), REALLY_BASIC.INT32_COLUMN.To(&casInt32column)).
		Swap(session)

	if err != nil {
		log.Fatalf("Could not execute CAS statement: %v", err)
		os.Exit(1)
	}

	return applied, casId, casInt32column
}
開發者ID:arkxu,項目名稱:cqlc,代碼行數:20,代碼來源:cas.go


注:本文中的github.com/relops/cqlc/cqlc.NewContext函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。