Example #1
0
func (bd *BSONDump) Debug() error {
	stream, err := bd.init()
	if err != nil {
		return err
	}

	defer stream.Close()

	reusableBuf := make([]byte, db.MaxBSONSize)
	var result bson.Raw
	for {
		hasDoc, docSize := stream.LoadNextInto(reusableBuf)
		if !hasDoc {
			break
		}
		result.Kind = reusableBuf[0]
		result.Data = reusableBuf[0:docSize]
		err = DebugBSON(result, 0, os.Stdout)
		if err != nil {
			return err
		}
	}
	if err := stream.Err(); err != nil {
		return err
	}
	return nil
}
Example #2
0
// Debug iterates through the BSON file and for each document it finds,
// recursively descends into objects and arrays and prints a human readable
// BSON representation containing the type and size of each field.
// It returns the number of documents processed and a non-nil error if one is
// encountered before the end of the file is reached.
func (bd *BSONDump) Debug() (int, error) {
	numFound := 0

	if bd.bsonSource == nil {
		panic("Tried to call Debug() before opening file")
	}

	defer bd.bsonSource.Close()

	reusableBuf := make([]byte, db.MaxBSONSize)
	var result bson.Raw
	for {
		hasDoc, docSize := bd.bsonSource.LoadNextInto(reusableBuf)
		if !hasDoc {
			break
		}
		result.Data = reusableBuf[0:docSize]

		if bd.BSONDumpOptions.ObjCheck {
			validated := bson.M{}
			err := bson.Unmarshal(result.Data, &validated)
			if err != nil {
				// ObjCheck is turned on and we hit an error, so short-circuit now.
				return numFound, fmt.Errorf("failed to validate bson during objcheck: %v", err)
			}
		}
		err := printBSON(result, 0, bd.Out)
		if err != nil {
			log.Logf(log.Always, "encountered error debugging BSON data: %v", err)
		}
		numFound++
	}

	if err := bd.bsonSource.Err(); err != nil {
		// This error indicates the BSON document header is corrupted;
		// either the 4-byte header couldn't be read in full, or
		// the size in the header would require reading more bytes
		// than the file has left
		return numFound, err
	}
	return numFound, nil
}