func (self *TDynamoDBStore) UpdateItem(item *Item) (bool, error) { glog.Infof("Updating item: %v", item) ok, err := self.table.UpdateAttributes(item.MakePrimaryKey(), item.Attrs) if ok { glog.Infof("Succeed update item: %v", item) } return ok, err }
func (self *TDynamoDBStore) DeleteItem(item *Item) (bool, error) { glog.Infof("Deleting item: %v", item) ok, err := self.table.DeleteItem(item.MakePrimaryKey()) if ok { glog.Infof("Succeed delete item: %v", item) } return ok, err }
func (self *TDynamoDBStore) PutItem(item *Item) (bool, error) { glog.Infof("Inserting item: %v", item) ok, err := self.table.PutItem(item.PrimaryKey, item.RangeKey, item.Attrs) if ok { glog.Infof("Succeed insert item: %v", item) } return ok, err }
func (self *TDynamoDBStore) findTableByName(name string) bool { glog.Infof("Searching for table %s in table list", name) tables, err := self.dynamoServer.ListTables() glog.Infof("Got table list: %v", tables) contract.RequireNoError(err) for _, t := range tables { if t == name { glog.Infof("Found table %s", name) return true } } glog.Infof("Table %s wasnt found", name) return false }
func (self *TDynamoDBStore) GetItem(pk string) (*Item, error) { glog.Infof("Getting item with pk: %s", pk) attrMap, err := self.table.GetItem(&dynamodb.Key{HashKey: pk}) if err == nil { glog.Infof("Succeed item %s fetch, got: %v", pk, attrMap) } else { return nil, err } attrSlice := []dynamodb.Attribute{} for _, v := range attrMap { attrSlice = append(attrSlice, *v) } item := MakeItem(pk, attrSlice...) return item, err }
func (self *TDynamoDBStore) DestroyTable() { glog.Info("Destroying tables") newTableDesc := DynamoDBDemoTableDescription() tableExists := self.findTableByName(newTableDesc.TableName) if !tableExists { glog.Infof("Table %s doesn't exists, skipping deletion", newTableDesc.TableName) return } else { _, err := self.dynamoServer.DeleteTable(newTableDesc) if err != nil { glog.Fatal(err) } glog.Infof("Table %s deleted successfully", newTableDesc.TableName) } }
func (self *TDynamoDBStore) InitTable() { glog.Info("Initializing tables") newTableDesc := DynamoDBDemoTableDescription() tableExists := self.findTableByName(newTableDesc.TableName) if tableExists { glog.Infof("Table %s exists, skipping init", newTableDesc.TableName) glog.Infof("Waiting until table %s becomes active", newTableDesc.TableName) self.waitUntilTableIsActive(newTableDesc.TableName) glog.Infof("Table %s is active", newTableDesc.TableName) return } else { glog.Infof("Creating table %s", newTableDesc.TableName) status, err := self.dynamoServer.CreateTable(newTableDesc) contract.RequireNoError(err) if status == TableStatusCreating { glog.Infof("Waiting until table %s becomes active", newTableDesc.TableName) self.waitUntilTableIsActive(newTableDesc.TableName) glog.Infof("Table %s is active", newTableDesc.TableName) return } if status == TableStatusActive { glog.Infof("Table %s is active", newTableDesc.TableName) return } glog.Fatal("Unexpected status:", status) } }
func main() { var cfg Config err := gcfg.ReadFileInto(&cfg, "dynamodb.gcfg.local") if err != nil { glog.Fatal(err) } store := MakeDynamoDBStore(cfg.DynamoDemos_AWS.Key, cfg.DynamoDemos_AWS.Secret) store.InitTable() item := MakeItem("some-unique-id", StringAttr("color", "red")) // insert row if ok, err := store.PutItem(item); !ok { glog.Fatalf("Failed to save item %v because of: %v", item, err) } glog.Infof("Item %v saved ", item) // find row savedItem, err := store.GetItem(item.PrimaryKey) if err != nil { glog.Fatalf("Failed to get saved item with pk %s", item.PrimaryKey) } glog.Infof("Got item %#v", savedItem) // find non-existing row _, err = store.GetItem("unknown") if err == nil { glog.Fatalf("Shouldnt get an item with pk %s", "unknown") } else if err.Error() == "Item not found" { glog.Infof("Failed to get non-existent item with err %s", err.Error()) } else { glog.Fatalf("Failed to get non-existent item with unexpected err %s", err.Error()) } // update row updItem := MakeItem("some-unique-id", StringAttr("color", "violet")) if ok, err := store.UpdateItem(updItem); !ok { glog.Fatalf("Failed to update item with unexpected err: %v", err) } // delete row if ok, err := store.DeleteItem(item); !ok { glog.Fatalf("Failed to delete item with unexpected err: %v", err) } store.DestroyTable() }