Beispiel #1
0
func writeDecorations(ctx context.Context, t table.Proto, entries <-chan *spb.Entry) error {
	fragments, err := createFragmentsTable(ctx, entries)
	if err != nil {
		return fmt.Errorf("error creating fragments table: %v", err)
	}
	defer fragments.Close(ctx)

	it, err := fragments.ScanPrefix(nil, &keyvalue.Options{LargeRead: true})
	if err != nil {
		return err
	}
	defer it.Close()

	log.Println("Writing Decorations")

	var curFile string
	var decor *srvpb.FileDecorations
	for {
		k, v, err := it.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			return fmt.Errorf("error scanning temporary table: %v", err)
		}

		ss := strings.Split(string(k), tempTableKeySep)
		if len(ss) != 2 {
			return fmt.Errorf("invalid temporary table key: %q", string(k))
		}
		fileTicket := ss[0]

		if decor != nil && curFile != fileTicket {
			if decor.FileTicket != "" {
				if err := writeDecor(ctx, t, decor); err != nil {
					return err
				}
			}
			decor = nil
		}
		curFile = fileTicket
		if decor == nil {
			decor = &srvpb.FileDecorations{}
		}

		var fragment srvpb.FileDecorations
		if err := proto.Unmarshal(v, &fragment); err != nil {
			return fmt.Errorf("invalid temporary table value: %v", err)
		}

		if fragment.FileTicket == "" {
			decor.Decoration = append(decor.Decoration, fragment.Decoration...)
		} else {
			decor.FileTicket = fragment.FileTicket
			decor.SourceText = fragment.SourceText
			decor.Encoding = fragment.Encoding
		}
	}

	if decor != nil && decor.FileTicket != "" {
		if err := writeDecor(ctx, t, decor); err != nil {
			return err
		}
	}

	return nil
}
Beispiel #2
0
func writeFileDecorations(ctx context.Context, edges <-chan *srvpb.Edge, out *servingOutput) error {
	temp, err := tempTable("decor.fragments")
	if err != nil {
		return fmt.Errorf("failed to create temporary table: %v", err)
	}
	fragments := &table.KVProto{temp}
	defer func() {
		if err := fragments.Close(ctx); err != nil {
			log.Printf("Error closing fragments table: %v", err)
		}
	}()

	log.Println("Writing decoration fragments")
	if err := createDecorationFragments(ctx, edges, fragments); err != nil {
		return err
	}

	log.Println("Writing completed FileDecorations")

	it, err := fragments.ScanPrefix(nil, &keyvalue.Options{LargeRead: true})
	if err != nil {
		return err
	}
	defer it.Close()

	log.Println("Writing Decorations")

	var curFile string
	var decor *srvpb.FileDecorations
	var fragment srvpb.FileDecorations
	for {
		k, v, err := it.Next()
		if err == io.EOF {
			break
		} else if err != nil {
			return fmt.Errorf("error scanning temporary table: %v", err)
		}

		ss := strings.Split(string(k), tempTableKeySep)
		if len(ss) != 2 {
			return fmt.Errorf("invalid temporary table key: %q", string(k))
		}
		fileTicket := ss[0]

		if decor != nil && curFile != fileTicket {
			if decor.FileTicket != "" {
				if err := writeDecor(ctx, out.xs, decor); err != nil {
					return err
				}
			}
			decor = nil
		}
		curFile = fileTicket
		if decor == nil {
			decor = &srvpb.FileDecorations{}
		}

		if err := proto.Unmarshal(v, &fragment); err != nil {
			return fmt.Errorf("invalid temporary table value: %v", err)
		}

		if fragment.FileTicket == "" {
			decor.Decoration = append(decor.Decoration, fragment.Decoration...)
		} else {
			decor.FileTicket = fragment.FileTicket
			decor.SourceText = fragment.SourceText
			decor.Encoding = fragment.Encoding
		}
	}

	if decor != nil && decor.FileTicket != "" {
		if err := writeDecor(ctx, out.xs, decor); err != nil {
			return err
		}
	}

	return nil
}