Esempio n. 1
0
func Download(t btesting.T, bucketId typing.BucketId, hash []byte,
	writer io.Writer) (entireLength uint64) {

	var skip int = 0
	var limit int = readerBlockSize
	var entireLengthProcessed bool

	for {
		var err error
		skipEncoded := encoding.UIntToUVarInt(uint64(skip))
		limitEncoded := encoding.UIntToUVarInt(uint64(limit))
		//hash/[HASH]/content/VUI(skip_optional)/VUI(limit_optional)
		key := typing.Key{[]byte("hash"), hash, []byte("content"), skipEncoded, limitEncoded}
		value := operations.Get(t, bucketId, key, true)
		// value = [data, CBOR(entire_length)]
		if len(value) != 2 {
			t.Errorf("Got invalid get from bucket / expecting 2 elements in value. Have %v",
				len(value))
			return
		}
		data := value[0]

		// Set entire length
		if !entireLengthProcessed {
			entireLengthEncoded := value[1]
			err = encoding.Cbor().Decode(entireLengthEncoded, &entireLength)
			entireLengthProcessed = true
			if err != nil {
				t.Errorf("Error decoding entire length %v", err)
				return
			}
		}

		_, err = writer.Write(data)
		if err != nil {
			t.Errorf("Unable to write to writer: %v", err)
			return
		}

		skip += readerBlockSize

		// Next one? End if we got less than requested or would exceed entire length
		if uint64(len(data)) < readerBlockSize || uint64(skip) >= entireLength {
			// No, end here
			return
		}
	}
	return
}
Esempio n. 2
0
func Upload(t btesting.T, bucketId typing.BucketId,
	reader io.Reader) (hash []byte) {

	demoId := []byte("demoId")

	for {
		buf := make([]byte, chunkSize)
		numRead, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			t.Errorf("Error reading: %v", err)
			return
		}
		if numRead > 0 {
			buf = buf[:numRead]
			//##PUT: keys = incubation/[ID]/append, values = [data]
			operations.Put(t, bucketId, typing.Key{[]byte("incubation"), demoId, []byte("append")},
				typing.Value{buf})
			if t.Failed() {
				return
			}
		}
		if numRead != chunkSize {
			// End
			break
		}
	}

	// Get the hash
	// ##GET: keys = incubation/[ID]/sum, values = [CBOR(hash), CBOR(length)]
	value := operations.Get(t, bucketId, typing.Key{[]byte("incubation"),
		demoId, []byte("sum")}, true)
	if t.Failed() {
		return
	}
	if len(value) != 2 {
		t.Errorf("Expecting 2 value entries (cbor(hash) and cbor(length))")
		return
	}
	err := encoding.Cbor().Decode(value[0], &hash)
	if err != nil {
		t.Errorf("Error getting the hash: %v", err)
		return
	}
	var length uint64
	err = encoding.Cbor().Decode(value[1], &length)
	if err != nil {
		t.Errorf("Error getting the length: %v", err)
		return
	}

	// Ok, commit
	//##PUT: keys = incubation/[ID]/finish, values = []

	operations.Put(t, bucketId, typing.Key{[]byte("incubation"), demoId, []byte("finish")},
		typing.Value{})
	if t.Failed() {
		return
	}

	return
}
Esempio n. 3
0
func uploadDirectoryAndGet(t btesting.T) {
	// First create the blob bucket (that's where the directories are stored, and the data too)
	var blobBucketId typing.BucketId
	operations.CreateBucket(t, typing.TypeId_BlobStore, &blobBucketId)
	if t.Failed() {
		return
	}

	// Now create the directory bucket and connect that to the blob bucket
	var bucketId typing.BucketId
	var blobBucketIdCBor []byte
	blobBucketIdCBor, err := encoding.Cbor().Encode([]byte(blobBucketId))
	if err != nil {
		t.Errorf("error cbor encoding: %v", err)
		return
	}
	operations.CreateBucketWithMetadata(t, typing.TypeId_Directory, map[string][]byte{
		"const.forwarder.blob": blobBucketIdCBor,
	}, &bucketId)
	if t.Failed() {
		return
	}

	// Both buckets now exist and are connected
	dir, err := createDemoDirectory()
	if err != nil {
		t.Errorf("Error creating directory: %v\n", err)
	}

	// Now upload the directory to the blob storage
	dirReader := bytes.NewReader(dir)
	hash := blob.Upload(t, blobBucketId, dirReader)
	if t.Failed() {
		return
	}

	// The directory is now in the blob storage - the directory bucket can now index it
	cborHash, err := encoding.Cbor().Encode(hash)
	operations.Put(t, bucketId, typing.Key{[]byte("index")}, typing.Value{cborHash})
	if t.Failed() {
		return
	}

	// Get some files
	operations.Get(t, bucketId, typing.Key{hash, []byte("file_1.txt")}, true)
	if t.Failed() {
		return
	}
	operations.Get(t, bucketId, typing.Key{hash, []byte("another_file.jpeg")}, true)
	if t.Failed() {
		return
	}

	// Now some files that do not exist
	operations.Get(t, bucketId, typing.Key{hash, []byte("<UNKNOWN_FILE>.exe")}, false)
	if t.Failed() {
		return
	}
	operations.Get(t, bucketId, typing.Key{hash, []byte("no_is_not_in_directory.com")}, false)
	if t.Failed() {
		return
	}
}