func update_file(session *gocql.Session, dc string, w http.ResponseWriter, r *http.Request) { /* Updates a new file in Cassandra via a POST. */ // Instantiate the struct. file := Asset{} // Decodes the json POST. decoder := json.NewDecoder(r.Body) err := decoder.Decode(&file) if err != nil { log.Printf("Failed to decode: %s\n", err) } log.Printf("%s toggled file %s as true in %s", strings.Split(r.RemoteAddr, ":")[0], file.Fid, strings.ToUpper(dc)) // Compose the CQL query. query := fmt.Sprintf(` UPDATE file_ledger SET %s = true WHERE fid = '%s'`, dc, file.Fid) // Execute the insert, at ConsistancyLevel:ALL. if err := session.Query(query).Consistency(gocql.All).Exec(); err != nil { log.Printf("Failed to update: %s\n", err) fail_resp := []byte(`{"update":"fail"}`) w.Write(fail_resp) } else { ok_resp := []byte(`{"update":"success"}`) w.Write(ok_resp) } }
func add_new_file(session *gocql.Session, w http.ResponseWriter, r *http.Request) { /* Adds a new file to Cassandra via a POST. */ // Instantiate the struct. file := Asset{} // Decodes the json POST. decoder := json.NewDecoder(r.Body) err := decoder.Decode(&file) if err != nil { log.Printf("Failed to decode: %s\n", err) } // We create the creation time instead of the client. created := time.Now() log.Printf("%s added new file: %s, Originally created at %s in %s. Exists in MA01: %t. Exists in TX01: %t.", strings.Split(r.RemoteAddr, ":")[0], file.Fid, file.Created, strings.ToUpper(file.Origin), file.Ma01, file.Tx01) // Execute the insert, at CL:ALL. if err := session.Query(` INSERT INTO file_ledger (fid, created, origin, ma01, tx01) VALUES (?, ?, ?, ?, ?) IF NOT EXISTS`, &file.Fid, created, &file.Origin, &file.Ma01, &file.Tx01).Consistency(gocql.All).Exec(); err != nil { log.Printf("Failed to insert: %s\n", err) fail_resp := []byte(`{"insert":"fail"}`) w.Write(fail_resp) } else { ok_resp := []byte(`{"insert":"success"}`) w.Write(ok_resp) } }
// Function handle new use creating func createUser(body *[]byte, session *gocql.Session) (int, error) { var request newUserRequest var count int err := json.Unmarshal(*body, &request) if err != nil { return http.StatusBadRequest, err } // Here should be call of function to extended validation, but nothing was in requirements if request.Password == "" || request.Username == "" { return http.StatusBadRequest, errors.New("User or password is empty") } // Check if such user already existing err = session.Query("SELECT COUNT(*) from users where username = '******'").Scan(&count) if err != nil { return http.StatusInternalServerError, err } if count > 0 { return http.StatusConflict, errors.New("Such user alredy existing") } // Prepare password hash to write it to DB hash := sha256.New() hash.Write([]byte(request.Password)) err = session.Query("INSERT INTO users (username,password) VALUES (?,?)", request.Username, hex.EncodeToString(hash.Sum(nil))).Exec() if err != nil { return http.StatusInternalServerError, err } return http.StatusCreated, nil }
// UserQ ... func UserQ(session *gocql.Session, query string) map[string]interface{} { user := map[string]interface{}{"id": 0, "name": ""} session.Query(query).MapScan(user) return user }
func (asset *Asset) Find(session *gocql.Session, assetId string) (*Asset, error) { var id gocql.UUID var name string var path string var contentType string var createdAt time.Time var binary []byte // Check if the assetId is an valid UUID idCheck, err := gocql.ParseUUID(assetId) if err != nil { return nil, err } if idCheck.Timestamp() == 0 { return nil, errors.New("Invalid UUID") } if err := session.Query(`SELECT id, name, path, contenttype, createdat, binary FROM assets WHERE id = ? LIMIT 1`, assetId).Consistency(gocql.One).Scan(&id, &name, &path, &contentType, &createdAt, &binary); err != nil { return nil, err } return &Asset{id, name, strings.Split(path, ","), contentType, createdAt, binary}, nil }
func worker(session *gocql.Session, ch chan *sample) { for s := range ch { if err := session.Query(`INSERT INTO samples (metric, timestamp, value) VALUES (?, ?, ?)`, s.name, s.timestamp, s.value).Exec(); err != nil { log.Fatalf("Query error: %s", err) } } }
func Truncate(session *gocql.Session, table cqlc.Table) { stmt := fmt.Sprintf("truncate %s", table.TableName()) err := session.Query(stmt).Exec() if err != nil { log.Fatalf("Could not %s: %v", stmt, err) os.Exit(1) } }
func tearDown(t *testing.T, session *gocql.Session) { defer session.Close() // if err := session.Query(`Drop Table message`).Exec(); err != nil { // t.Log("can not drop Table testdata") // t.Fatal(err.Error()) // } }
// CreateTweet creates a new tweet func CreateTweet(s *gocql.Session, tweet *Tweet) error { q1 := `INSERT INTO tweets_by_id (id, tweet) VALUES (?,?);` q2 := `UPDATE users_by_name SET tweets = tweets + [?] WHERE name = ?;` err := s.Query(q1, tweet.ID, tweet).Exec() if err != nil { return err } return s.Query(q2, tweet, tweet.Username).Exec() }
func getDefinition(session *gocql.Session, words []string) string { var defn string thingtodefine := strings.ToLower(strings.Join(words, " ")) iter := session.Query("select defn from words where word = ?", thingtodefine).Consistency(gocql.One).Iter() for iter.Scan(&defn) { return fmt.Sprintf("'%s': %s", thingtodefine, defn) } return fmt.Sprintf("Sorry I don't know about '%s'", thingtodefine) }
func insertSerieIds(session *gocql.Session, serieIdsToInsert []gocql.UUID) error { insertSerieQuery := session.Query("INSERT INTO \"SerieId\" (\"SerieId\") VALUES (?);") for _, id := range serieIdsToInsert { if err := insertSerieQuery.Bind(id).Exec(); err != nil { return err } } return nil }
func (asset Asset) FindByPath(session *gocql.Session, path string) ([]Asset, error) { var id gocql.UUID var name string var assets = make([]Asset, 0) iter := session.Query(`SELECT id, name FROM assetbypaths WHERE path = ?`, path).Iter() for iter.Scan(&id, &name) { assets = append(assets, Asset{Id: id, Name: name, Path: strings.Split(path, ",")}) } return assets, nil }
func injectEvent(session *gocql.Session, resId string) error { t := time.Now().UTC() err := session.Query(`INSERT INTO resources_simple(event_time, resource_id, event) VALUES (?, ?, ?)`, t, resId, fmt.Sprintf(`{"message":"Hello World", "ts":"%s"}`, t.Format(time.RFC3339Nano))).Exec() if err != nil { log.Printf("Error with insert for id: %s: %s", resId, err) return fmt.Errorf("insert error:", err) } return nil }
func Migrate_0_9_1_to_15_04_03(session *gocql.Session) error { // Perform all migration queries. for _, query := range migrationQueries_0_9_1_to_15_04_03 { canolog.Info(query) if err := session.Query(query).Exec(); err != nil { // Ignore errors (just print them). canolog.Warn(query, ": ", err) } } return nil }
func cleanup(session *gocql.Session) error { if err := session.Query("TRUNCATE \"SerieId\";").Exec(); err != nil { return err } if err := session.Query("TRUNCATE \"Timeserie\"").Exec(); err != nil { return err } return nil }
//EnsureTableExists makes sure the schema version table is present func EnsureTableExists(session *gocql.Session) error { return session.Query(` CREATE TABLE IF NOT EXISTS Schema_Version( appliedOn timestamp, versionId bigint, description text, PRIMARY KEY ( versionId, appliedOn ) ) WITH CLUSTERING ORDER BY (appliedOn DESC) `).Exec() }
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) }
func getCandidates(session *gocql.Session, words []string) [][]string { var first, rest, defn string rval := make([][]string, 0) iter := session.Query(`select first, rest, defn from defs where first in ?`, words).Consistency(gocql.One).Iter() for iter.Scan(&first, &rest, &defn) { rval = append(rval, []string{first, rest, defn}) } if err := iter.Close(); err != nil { log.Fatal(err) } return rval }
// NewCassandraLockClient creates a new client from options func NewCassandraLockClient(opts CassandraOptions) (*CassandraClient, error) { if opts.ReplicationFactor <= 0 { opts.ReplicationFactor = 1 } consistency := gocql.Quorum if opts.ReplicationFactor == 0 { consistency = gocql.One } var session *gocql.Session var err error var c CassandraClient for proto := 4; proto > 1; proto-- { c = CassandraClient{nil, opts.Hosts, "", "", "", nil, proto, consistency} c.cluster = gocql.NewCluster(opts.Hosts...) c.cluster.ProtoVersion = proto session, err = c.cluster.CreateSession() if err == nil { break } } if err != nil { return nil, err } err = session.Query( fmt.Sprintf(createKs, opts.KeySpace, opts.ReplicationFactor), ).Exec() if err != nil { return nil, err } err = session.Query( fmt.Sprintf(createTable, opts.KeySpace, opts.TableName), ).Exec() if err != nil { return nil, err } id, err := gocql.RandomUUID() if err != nil { return nil, err } c.hosts = opts.Hosts c.keyspace = opts.KeySpace c.table = opts.TableName c.clientID = id.String() c.Reconnect() return &c, nil }
func (c *Context) Exec(s *gocql.Session) error { stmt, placeHolders, err := BuildStatement(c) if err != nil { return err } if c.Debug { debugStmt(stmt, placeHolders) } return s.Query(stmt, placeHolders...).Exec() }
func getRandom(session *gocql.Session) [][]string { var first, rest, defn string rval := make([][]string, 0) random, _ := gocql.RandomUUID() iter := session.Query(`select first, rest, defn from defs where TOKEN(first) > TOKEN(?) limit 1`, random.String()).Consistency(gocql.One).Iter() for iter.Scan(&first, &rest, &defn) { rval = append(rval, []string{first, rest, defn}) } if err := iter.Close(); err != nil { log.Fatal(err) } return rval }
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) }
func (c *Context) Exec(s *gocql.Session) error { stmt, placeHolders, err := BuildStatement(c) if err != nil { return err } if c.Debug { fmt.Printf("%+v bound to stmt: %s\n", placeHolders, stmt) } return s.Query(stmt, placeHolders...).Exec() }
func getSorted(session *gocql.Session) ([]versionItem, error) { var versions []versionItem var version int var on time.Time iter := session.Query("select versionId, appliedOn from Schema_Version").Iter() for iter.Scan(&version, &on) { versions = append(versions, versionItem{ID: version, AppliedOn: on}) } sort.Sort(byTime(versions)) return versions, iter.Close() }
func (asset *Asset) Save(session *gocql.Session) error { if asset.Id.Timestamp() == 0 { asset.Id = gocql.TimeUUID() if err := session.Query(`INSERT INTO assets (id, name, path, contenttype, createdat, binary) VALUES (?, ?, ?, ?, ?, ?)`, asset.Id, asset.Name, strings.Join(asset.Path, ","), asset.ContentType, asset.CreatedAt, asset.Binary).Exec(); err != nil { glog.Fatal(err) return err } if err := session.Query(`INSERT INTO assetbypaths (path, id, name) VALUES (?, ?, ?)`, strings.Join(asset.Path, ","), asset.Id, asset.Name).Exec(); err != nil { glog.Fatal(err) return err } return nil } else { if err := session.Query(`UPDATE assets SET name = ?, path = ?, contenttype = ? WHERE id = ?`, asset.Name, strings.Join(asset.Path, ","), asset.ContentType, asset.Id).Exec(); err != nil { glog.Fatal(err) return err } if err := session.Query(`UPDATE assetbypaths SET name = ?, path = ? WHERE id = ?`, asset.Name, strings.Join(asset.Path, ","), asset.Id).Exec(); err != nil { glog.Fatal(err) return err } return nil } }
func initializeCassandra(session *gocql.Session) error { // projects table q := fmt.Sprintf("create table if not exists projects (name text PRIMARY KEY, oids SET<text>);") err := session.Query(q).Exec() if err != nil { return err } // create an index so we can search on oids q = fmt.Sprintf("create index if not exists on projects(oids);") err = session.Query(q).Exec() if err != nil { return err } // Oids table q = fmt.Sprintf("create table if not exists oids(oid text primary key, size bigint);") session.Query(q).Exec() if err != nil { return err } // user management q = fmt.Sprintf("create table if not exists users(username text primary key, password text);") return session.Query(q).Exec() }
func Root(session *gocql.Session, r render.Render) { var first_name, last_name string var id gocql.UUID iter := session.Query("select user_id, first_name, last_name from user limit 1000").Iter() response := make([]result, 0, 1000) for iter.Scan(&id, &first_name, &last_name) { response = append(response, result{FirstName: first_name, LastName: last_name}) } r.JSON(200, response) }
func deleteSession(session *gocql.Session, session_id string) (int, error) { // fast path to don't use DB when session_id cookie not indicated at all if session_id == "" { return http.StatusUnauthorized, nil } // removing session for DB err := session.Query("DELETE FROM sessions WHERE session_id = '" + session_id + "'").Exec() if err != nil { return http.StatusInternalServerError, err } return http.StatusOK, nil }
func insert(session *gocql.Session, config benchConfig) (int64, error) { pointsPerSerie := 18000 // Average number of points per day taken from real data serieIdsToInsert := generateSerieIds(config.seriesToInsert) if err := insertSerieIds(session, serieIdsToInsert); err != nil { return 0, err } pendingInsertsChannel := make(chan dataToInsert, 100000) var allWorkersAreDoneWaitGroup sync.WaitGroup // Data generation routine go func() { random := rand.New(rand.NewSource(123)) timestamp := time.Now().UTC().Truncate(time.Hour * 24) for i := 0; i < pointsPerSerie; i++ { timestamp = timestamp.Add(time.Second) for _, serieID := range serieIdsToInsert { pendingInsertsChannel <- dataToInsert{dateTime: timestamp, serieID: serieID, value: random.Float64()} } } close(pendingInsertsChannel) }() var insertedRowCount int64 // Data insertion routines for i := 0; i < config.writeParallelStatementCount; i++ { allWorkersAreDoneWaitGroup.Add(1) go func() { defer allWorkersAreDoneWaitGroup.Done() query := session.Query("INSERT INTO \"Timeserie\" (\"SerieId\", \"Day\", \"UtcDate\", \"Value\") VALUES (?, ?, ?, ?) USING TTL ?;") for dataToInsert := range pendingInsertsChannel { if err := query.Bind(dataToInsert.serieID, dataToInsert.dateTime.Truncate(time.Hour*24), dataToInsert.dateTime, dataToInsert.value, ttl).Exec(); err != nil { fmt.Println(err.Error()) } atomic.AddInt64(&insertedRowCount, 1) } }() } allWorkersAreDoneWaitGroup.Wait() return insertedRowCount, nil }
// Returns true if the CAS operation was applied, false otherwise. // If the operation was applied, then the result bindings will not be popluated. func (c *Context) Swap(s *gocql.Session) (bool, error) { if len(c.CASBindings) == 0 { return false, ErrCASBindings } casPlaceHolders := make([]interface{}, len(c.CASBindings)) for i, binding := range c.CASBindings { casPlaceHolders[i] = binding.Value } stmt, queryPlaceholders, err := BuildStatement(c) if err != nil { return false, err } return s.Query(stmt, queryPlaceholders...).ScanCAS(casPlaceHolders...) }