Exemplo n.º 1
0
func (s *ApiServer) ForwardFetchFeed(ctx context.Context, req *pb.FeedRequest) (*pb.Feed, error) {
	if req.PageSize <= 0 || req.PageSize >= 100 {
		req.PageSize = 50
	}

	profile, err := store.GetProfile(s.mdb, req.Id)
	if err != nil {
		return nil, err
	}

	uuid1, _ := uuid.FromString(profile.Uuid)
	preKey := store.NewUUIDKey(store.TableReverseEntryIndex, uuid1)
	log.Println("forward seeking:", preKey.String())

	start := req.Start
	var entries []*pb.Entry
	_, err = store.ForwardTableScan(s.rdb, preKey, func(i int, k, v []byte) error {
		if start > 0 {
			start--
			return nil // continue
		}

		entry := new(pb.Entry)
		rawdata, err := s.rdb.Get(v) // index value point to entry key
		if err != nil || len(rawdata) == 0 {
			return fmt.Errorf("entry data missing")
		}
		if err := proto.Unmarshal(rawdata, entry); err != nil {
			return err
		}
		if err = FormatFeedEntry(s.mdb, req, entry); err != nil {
			return err
		}

		entries = append(entries, entry)
		if i > int(req.PageSize+req.Start) {
			return &store.Error{"ok", store.StopIteration}
		}
		return nil
	})

	if err != nil {
		return nil, err
	}

	feed := &pb.Feed{
		Uuid:        profile.Uuid,
		Id:          profile.Id,
		Name:        profile.Name,
		Picture:     profile.Picture,
		Type:        profile.Type,
		Private:     profile.Private,
		SupId:       profile.SupId,
		Description: profile.Description,
		Entries:     entries[:],
	}
	return feed, nil
}
Exemplo n.º 2
0
func (s *ApiServer) cachedFeed(req *pb.FeedRequest) (*pb.Feed, error) {
	if req.PageSize <= 0 || req.PageSize >= 100 {
		req.PageSize = 50
	}

	start := req.Start
	index := s.cached[req.Id]

	var entries []*pb.Entry
	found := 0
	for i := 0; i < len(index.bufq); i++ {
		if start > 0 {
			start--
			continue
		}

		key := index.bufq[i]
		if key == "" {
			break
		}

		kb, _ := hex.DecodeString(key)
		entry := new(pb.Entry)
		rawdata, err := s.rdb.Get(kb)
		if err != nil || len(rawdata) == 0 {
			return nil, fmt.Errorf("entry data missing")
		}
		if err := proto.Unmarshal(rawdata, entry); err != nil {
			return nil, err
		}
		FormatFeedEntry(s.mdb, req, entry)
		entries = append(entries, entry)
		found++
		if found > int(req.PageSize) {
			break
		}
	}

	feed := &pb.Feed{
		Uuid:    "Public",
		Id:      "Public",
		Name:    "Everyone's feed",
		Type:    "group",
		Private: false,
		SupId:   "0000-00",
		Entries: entries[:],
	}
	return feed, nil
}