Skip to content

Commit

Permalink
Create a new column family to record a time series of the product count.
Browse files Browse the repository at this point in the history
Keep it a bit more open so we can have additional statistics later.
  • Loading branch information
caoimhechaos committed Jan 5, 2014
1 parent ca4b280 commit 1146600
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions cassandra-schema
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
create keyspace starstock;
use starstock;
create column family products with key_validation_class = 'LexicalUUIDType' and comparator = 'AsciiType' and column_metadata = [{column_name: name, validation_class: UTF8Type, index_type: KEYS}, {column_name: price, validation_class: DoubleType, index_type: KEYS}, {column_name: vendor, validation_class: LexicalUUIDType, index_type: KEYS}, {column_name: barcodes, validation_class: BytesType, index_type: 0}, {column_name: stock, validation_class: LongType, index_type: KEYS}];
create column family product_tsdata with key_validation_class = 'BytesType' and comparator = 'AsciiType' and default_validation_class = 'LongType';
create column family vendors with key_validation_class = 'LexicalUUIDType' and comparator = 'AsciiType' and column_metadata = [{column_name: name, validation_class: UTF8Type, index_type: KEYS}, {column_name: address, validation_class: UTF8Type, index_type: 0}, {column_name: comments, validation_class: UTF8Type, index_type: 0}];
create column family products_byname with key_validation_class = 'UTF8Type' and comparator = 'AsciiType' and column_metadata = [{column_name: product, validation_class: LexicalUUIDType, index_type: KEYS}];
create column family products_bybarcode with key_validation_class = 'AsciiType' and comparator = 'AsciiType' and column_metadata = [{column_name: product, validation_class: LexicalUUIDType, index_type: KEYS}];
36 changes: 35 additions & 1 deletion productedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ func (self *ProductEditAPI) ServeHTTP(w http.ResponseWriter, req *http.Request)
var uuid UUID
var codes *Barcodes = new(Barcodes)
var mmap map[string]map[string][]*cassandra.Mutation
var tsprefix []byte
var prod Product
var mutations []*cassandra.Mutation
var mutation *cassandra.Mutation
Expand Down Expand Up @@ -353,6 +354,7 @@ func (self *ProductEditAPI) ServeHTTP(w http.ResponseWriter, req *http.Request)
mmap[string(uuid)] = make(map[string][]*cassandra.Mutation)
mmap[string(uuid)]["products"] = mutations

// Create the entry in the products_byname index.
mutations = make([]*cassandra.Mutation, 0)
col = cassandra.NewColumn()
col.Name = []byte("product")
Expand All @@ -365,12 +367,44 @@ func (self *ProductEditAPI) ServeHTTP(w http.ResponseWriter, req *http.Request)
mmap[prod.Name] = make(map[string][]*cassandra.Mutation)
mmap[prod.Name]["products_byname"] = mutations

// If a barcode has been given, specify it in the products_bybarcode
// column.
if len(barcode) > 0 {
mmap[barcode] = make(map[string][]*cassandra.Mutation)
mmap[barcode]["products_bybarcode"] = mutations
}

ire, ue, te, err = self.client.BatchMutate(mmap,
// Log a timeseries entry stating that the product has been present
// at the time.
mutations = make([]*cassandra.Mutation, 0)
col = cassandra.NewColumn()
col.Name = []byte("product-count")
col.Value = buf.Bytes() // still the product count from above.
col.Timestamp = now.Unix()
mutation = cassandra.NewMutation()
mutation.ColumnOrSupercolumn = cassandra.NewColumnOrSuperColumn()
mutation.ColumnOrSupercolumn.Column = col
mutations = append(mutations, mutation)

// Create the TS prefix: first the UUID, then the timestamp.
buf = new(bytes.Buffer)
err = binary.Write(buf, binary.BigEndian, now.UnixNano())
if err != nil {
productEditErrors.Add(err.Error(), 1)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tsprefix = make([]byte, len(uuid)+buf.Len()+1)
copy(tsprefix, uuid)
tsprefix[len(uuid)] = ':'
for i, v := range buf.Bytes() {
tsprefix[len(uuid)+i+1] = v
}
mmap[string(tsprefix)] = make(map[string][]*cassandra.Mutation)
mmap[string(tsprefix)]["product_tsdata"] = mutations

// Now, write the mutations to the database.
ire, ue, te, err = self.client.AtomicBatchMutate(mmap,
cassandra.ConsistencyLevel_QUORUM)
if ire != nil {
log.Println("Invalid request: ", ire.Why)
Expand Down

0 comments on commit 1146600

Please sign in to comment.