コード例 #1
0
ファイル: worker.go プロジェクト: SpiderOak/gonimbusio
func worker(id int, filePath string, requester nimbusapi.Requester,
	work <-chan workUnit, results chan<- workResult) {
	result := workResult{}
	result.workerID = id

	file, err := os.Open(filePath)
	if err != nil {
		result.err = err
		result.action = "Open"
		results <- result
		return
	}
	defer file.Close()

	for workUnit := range work {

		_, err = file.Seek(workUnit.offset, 0)
		if err != nil {
			result.err = err
			result.action = "Seek"
			results <- result
			return
		}

		conjoinedParams := nimbusapi.ConjoinedParams{
			workUnit.conjoinedIdentifier, workUnit.conjoinedPart}

		_, err := nimbusapi.Archive(requester, workUnit.collection,
			workUnit.key, &conjoinedParams, workUnit.size,
			io.LimitReader(file, workUnit.size))

		if err != nil {
			result.err = err
			result.action = "Archive"
			results <- result
			return
		}

		result.conjoinedPart = workUnit.conjoinedPart
		result.size = workUnit.size
		results <- result
	}
}
コード例 #2
0
func main() {
	fmt.Println("start")
	var credentials *nimbusapi.Credentials
	var err error

	sp := flag.String("credentials", "", "path to credentials file")
	flag.Parse()
	if *sp == "" {
		credentials, err = nimbusapi.LoadCredentialsFromDefault()
	} else {
		credentials, err = nimbusapi.LoadCredentialsFromPath(*sp)
	}
	if err != nil {
		log.Fatalf("Error loading credentials %s\n", err)
	}

	requester, err := nimbusapi.NewRequester(credentials)
	if err != nil {
		log.Fatalf("NewRequester failed %s\n", err)
	}

	collectionList, err := nimbusapi.ListCollections(requester,
		credentials.Name)
	if err != nil {
		log.Fatalf("Request failed %s\n", err)
	}
	fmt.Printf("starting collection list = %v\n", collectionList)

	collectionName := nimbusapi.ReservedCollectionName(credentials.Name,
		fmt.Sprintf("test-%05d", len(collectionList)))
	collection, err := nimbusapi.CreateCollection(requester,
		credentials.Name, collectionName)
	if err != nil {
		log.Fatalf("CreateCollection failed %s\n", err)
	}
	fmt.Printf("created collection = %v\n", collection)

	archiveBody := strings.NewReader(testBody)
	versionIdentifier, err := nimbusapi.Archive(requester, collectionName,
		testKey, nil, archiveBody)
	if err != nil {
		log.Fatalf("Archive failed %s\n", err)
	}
	fmt.Printf("archived key '%s' to version %v\n", testKey, versionIdentifier)

	retrieveBody, err := nimbusapi.Retrieve(requester, collectionName,
		testKey, nimbusapi.RetrieveParams{})
	if err != nil {
		log.Fatalf("Retrieve failed %s\n", err)
	}

	retrieveResult, err := ioutil.ReadAll(retrieveBody)
	retrieveBody.Close()
	if err != nil {
		log.Fatalf("read failed %s\n", err)
	}
	fmt.Printf("retrieved key '%s'; matches testBody = %v\n", testKey,
		string(retrieveResult) == testBody)

	keySlice, _, err := nimbusapi.ListKeysInCollection(requester,
		collectionName)
	if err != nil {
		log.Fatalf("ListKeysInCollection failed %s\n", err)
	}
	for _, keyEntry := range keySlice {
		fmt.Printf("deleting key %v\n", keyEntry)
		err = nimbusapi.DeleteKey(requester, collectionName, keyEntry.Name)
		if err != nil {
			log.Fatalf("DeleteKey %v failed %s\n", keyEntry, err)
		}
	}

	success, err := nimbusapi.DeleteCollection(requester, credentials.Name,
		collectionName)
	if err != nil {
		log.Fatalf("DeleteCollection failed %s\n", err)
	}
	fmt.Printf("deleted collection = %s %v\n", collectionName, success)

	fmt.Println("end")
}