Example #1
0
func (ds DataStore) Post(key string, post data.Post) error {
	file, err := os.OpenFile(filepath.Join(string(ds), key), os.O_RDWR|os.O_CREATE, os.ModePerm)
	if err != nil {
		return err
	}
	defer file.Close()
	posts, err := data.ReadPosts(file)
	if err != nil && err != io.EOF {
		return err
	}
	if _, err = file.Seek(0, os.SEEK_SET); err != nil {
		return err
	}
	if err = file.Truncate(0); err != nil {
		return err
	}
	return data.AddPost(post, posts, file, MAX_POSTS_PER_KEY)
}
Example #2
0
func (s Storage) Post(key string, post data.Post) error {
	var status error
	if err := datastore.RunInTransaction(s.context, func(context appengine.Context) error {
		k := makeKey(context, key)
		value, err := get(context, k)
		if err != nil {
			return err
		}
		posts, status := data.ReadPosts(bytes.NewBuffer(value.Bytes))
		if status != nil && status != io.EOF {
			return nil
		}
		buffer := bytes.NewBuffer(nil)
		status = data.AddPost(post, posts, buffer, MAX_POSTS_PER_KEY)
		if status != nil {
			return nil
		}
		_, err = datastore.Put(context, k, &record{Bytes: buffer.Bytes()})
		return err
	}, nil); err != nil {
		return err
	}
	return status
}