indexpack

package
v0.0.28 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2018 License: Apache-2.0, NCSA Imports: 21 Imported by: 0

Documentation

Overview

Package indexpack provides an interface to a collection of compilation units stored in an "index pack" directory structure. The index pack format is defined in kythe-index-pack.txt.

Example usage, writing:

pack, err := indexpack.Create(ctx, "path/to/some/directory")
if err != nil {
  log.Exit(err)
}
for _, cu := range fetchUnits() {
  if _, err := pack.WriteUnit(ctx, "kythe", cu); err != nil {
    log.Error(err)
    continue
  }
  for _, input := range cu.RequiredInput {
    data, err := readFile(input)
    if err != nil {
      log.Error(err)
      continue
    }
    if err := pack.WriteFile(ctx, data); err != nil {
      log.Error(err)
    }
  }
}

Example usage, reading:

pack, err := indexpack.Open(ctx, "some/dir/path", indexpack.UnitType((*cpb.CompilationUnit)(nil)))
if err != nil {
  log.Exit(err)
}

// The value passed to the callback will have the concrete type of
// the third parameter to Open (or Create).
err := pack.ReadUnits(ctx, "kythe", func(digest string, cu interface{}) error {
  for _, input := range cu.(*cpb.CompilationUnit).RequiredInput {
    bits, err := pack.ReadFile(ctx, input.Digest)
    if err != nil {
      return err
    }
    processData(bits)
  }
  return doSomethingUseful(cu)
})
if err != nil {
  log.Exit(err)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Archive

type Archive struct {
	// contains filtered or unexported fields
}

Archive represents an index pack directory.

func Create

func Create(ctx context.Context, path string, opts ...Option) (*Archive, error)

Create creates a new empty index pack at the specified path. It is an error if the path already exists.

func CreateOrOpen

func CreateOrOpen(ctx context.Context, path string, opts ...Option) (*Archive, error)

CreateOrOpen opens an existing index pack with the given parameters, if one exists; or if not, then attempts to create one.

func Open

func Open(ctx context.Context, path string, opts ...Option) (*Archive, error)

Open returns a handle for an existing valid index pack at the specified path. It is an error if path does not exist, or does not have the correct format.

func OpenZip

func OpenZip(ctx context.Context, r io.ReaderAt, size int64, opts ...Option) (*Archive, error)

OpenZip returns a read-only *Archive tied to the ZIP file at r, whose size in bytes is given. The ZIP file is expected to contain the recursive contents of an indexpack directory and its subdirectories. Operations that write to the pack will return errors.

func (*Archive) Fetcher

func (a *Archive) Fetcher(ctx context.Context) analysis.Fetcher

Fetcher returns an analysis.Fetcher that reads file contents from a.

func (*Archive) FileExists

func (a *Archive) FileExists(ctx context.Context, digest string) (bool, error)

FileExists determines whether a file with the given digest exists.

func (*Archive) ReadFile

func (a *Archive) ReadFile(ctx context.Context, digest string) ([]byte, error)

ReadFile reads and returns the file contents corresponding to the given hex-encoded SHA-256 digest.

func (*Archive) ReadUnit

func (a *Archive) ReadUnit(ctx context.Context, formatKey, digest string, f func(interface{}) error) error

ReadUnit calls f with the compilation unit stored in the units subdirectory with the given formatKey and digest. The concrete type of the value passed to f will be the same as the concrete type of the UnitType option that was passed to Open or Create. If no UnitType was specified, the value is a json.RawMessage.

If f returns a non-nil error, it is returned.

func (*Archive) ReadUnits

func (a *Archive) ReadUnits(ctx context.Context, formatKey string, f func(string, interface{}) error) error

ReadUnits calls f with the digest and content of each compilation unit stored in the units subdirectory of the index pack whose format key equals formatKey. The concrete type of the value passed to f will be the same as the concrete type of the UnitType option that was passed to Open or Create. If no UnitType was specified, the value is a json.RawMessage.

If f returns a non-nil error, no further compilations are read and the error is propagated back to the caller of ReadUnits.

func (*Archive) Root

func (a *Archive) Root() string

Root returns the root path of the archive.

func (*Archive) WriteFile

func (a *Archive) WriteFile(ctx context.Context, data []byte) (string, error)

WriteFile writes the specified file contents to the files/ subdirectory of the index pack. Returns the resulting filename, whether or not there is an error in writing the file.

func (*Archive) WriteUnit

func (a *Archive) WriteUnit(ctx context.Context, formatKey string, cu interface{}) (string, error)

WriteUnit writes the specified compilation unit to the units/ subdirectory of the index pack, using the specified format key. Returns the resulting filename, whether or not there is an error in writing the file, as long as marshaling succeeded.

type Option

type Option func(*Archive) error

An Option is a configurable setting for an Archive.

func FS

func FS(fs vfs.Interface) Option

FS returns an Option that sets the filesystem interface used to implement the index pack.

func FSReader added in v0.0.3

func FSReader(fs vfs.Reader) Option

FSReader returns an Option that sets the filesystem interface used to implement the index pack. The resulting index pack will be read-only; methods such as WriteUnit and WriteFile will return with an error.

func UnitType

func UnitType(t interface{}) Option

UnitType returns an Option that sets the concrete type used to unmarshal compilation units in the ReadUnits method. If t != nil, the interface value passed to the callback will be a pointer to the type of t.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL