Пример #1
0
func (self *blobstorePrototype) createInstance(state *minos.OperationState) (
	instance *blobstoreStruct,
	ret retcode.Status) {

	var err error
	backendId, exists, err := state.MetadataGetterSetter.Forwarder(
		true, metadataKey_backend)
	if err != nil {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Could not get forwarder metadata (backend)"))
	}
	if !exists {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Backend bucket id not found in metadata"))
	}

	ni := new(blobstoreStruct)
	ni.incubator = new(incubator)
	ni.incubator.bucketId = backendId
	ni.public = new(public)
	ni.public.bucketId = backendId
	ni.public.incubator = ni.incubator

	return ni, retcode.NewStatusOk()
}
Пример #2
0
func (self *notifier) createInstance(state *minos.OperationState) (
	instance *notifierInstance,
	ret retcode.Status) {

	var err error
	keysBucketId, exists, err := state.MetadataGetterSetter.Forwarder(
		true, metadataKey_keys)
	if err != nil || !exists {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Could not get forwarder metadata (keys)"))
	}
	registrationsBucketId, exists, err := state.MetadataGetterSetter.Forwarder(
		true, metadataKey_registrations)
	if err != nil || !exists {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Could not get forwarder metadata (keys)"))
	}

	ni := new(notifierInstance)
	ni.base = self
	ni.keysBucketId = keysBucketId
	ni.registrationsBucketId = registrationsBucketId

	return ni, retcode.NewStatusOk()
}
Пример #3
0
func (self *Store) Op_hash(operation *operations.Hash) (ret bucket.BucketReturn) {

	opGet := operations.Get{
		BucketId: operation.BucketId,
		Key:      operation.Key,
	}
	ret = self.Op_get(&opGet)
	if !ret.GetCode().IsOk() {
		// Error
		return
	}
	if ret.GetCode() == retcode.OkNotFound {
		// Not found, so we cannot hash it
		return
	}

	// Ok, something we can hash
	returnGet := ret.(*operations.GetReturn)
	value := returnGet.Value

	hasher := hasher.NewHasher(value)

	hashreturn := new(operations.HashReturn)
	hashreturn.FirstElementSha256Hash = hasher.FirstElement()
	hashreturn.AllElementsSha256Hash = hasher.AllElements()
	hashreturn.Status = retcode.NewStatusOk()

	return hashreturn
}
Пример #4
0
func (self *notifierInstance) op_subscribe(operation *operations.Subscribe,
	state *minos.OperationState) (ret bucket.BucketReturn) {

	// Generate the subscription ID and register that ID
	subscriptionId, retStatus := self.putToRegistrationBucket(state, operation)
	if !retStatus.GetCode().IsOk() {
		return &bucket.ReturnGeneric{retStatus.GetStatus()}
	}

	keysForLookupBucket, retStatus := createKeyForLookupBucket(
		operation.MatchChildKeys,
		subscriptionId, operation.Key, state)
	if !retStatus.GetCode().IsOk() {
		return &bucket.ReturnGeneric{retStatus.GetStatus()}
	}

	// Ok, now add to the lookup bucket
	opIterate, retState := self.createPutOperationForLookupBucket(
		keysForLookupBucket, &operation.ReturnConfig)
	if !retState.GetCode().IsOk() {
		return &bucket.ReturnGeneric{retState}
	}
	registerOpRet := state.Dispatcher.Perform(state.Context, opIterate)
	if !registerOpRet.GetCode().IsOk() {
		return &bucket.ReturnGeneric{registerOpRet.GetStatus()}
	}

	return &operations.SubscribeReturn{
		Status: retcode.NewStatusOk(),
		Id:     subscriptionId,
	}
}
Пример #5
0
func (self *directoryBucket) createInstance(state *minos.OperationState) (
	instance *directoryBucketInstance,
	ret retcode.Status) {

	var err error
	backendId, exists, err := state.MetadataGetterSetter.Forwarder(
		true, forwarder_name)
	if err != nil {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Could not get forwarder metadata (backend)"))
	}
	if !exists {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Backend bucket id not found in metadata"))
	}

	cstore := &commonstore.Store{
		DbTypeName:       levelDbTypeName,
		DatabaseProvider: self.database,
		State:            state,
		DoNotPutForward:  true,
	}

	ni := new(directoryBucketInstance)
	ni.base = self
	ni.cstore = cstore
	ni.backendId = backendId

	return ni, retcode.NewStatusOk()
}
Пример #6
0
func (self *Store) Op_put(operation *operations.Put) (ret bucket.BucketReturn) {
	leveldb, err := self.Database()
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	keyBytes := self.KeyToBytesAndPrefix(operation.Key)

	// Success, now forward that thing (forwarding never fails)
	if self.State.PutForwarder != nil && !self.DoNotPutForward {
		self.State.PutForwarder.ForwardIfOk(operation)
	}

	var returnValue *operations.PutReturn
	if operation.Value != nil {
		valueBytes := operation.Value.Serialize()
		//fmt.Printf("PUT: %v\n", keyBytes)

		err = leveldb.Put(keyBytes, valueBytes, nil /*TODO*/)
		if err != nil {
			return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
		}
		returnValue = &operations.PutReturn{retcode.NewStatusOk()}
	} else {
		// Remove operation
		err = leveldb.Delete(keyBytes, nil)
		if err != nil {
			return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
		}
		returnValue = &operations.PutReturn{retcode.NewStatus(retcode.OkRemove)}
	}

	return returnValue
}
Пример #7
0
func (self *incubator) cleanIncubation(state *minos.OperationState,
	id string) (ret retcode.Status) {

	filePath, ret := self.incubationFilePath(state, id)
	if !ret.IsOk() {
		return
	}

	// Ignore errors (since it's ok to be missing if moved to public)
	_ = os.Remove(filePath)

	key := self.createInformationKey(state, id)

	apiOperation := &operations.Put{
		BucketId: self.bucketId,
		Key:      key,
	}
	opRet := state.Dispatcher.Perform(state.Context, apiOperation)
	if !opRet.GetCode().IsOk() {
		// Unable to remove information
		ret = opRet.GetStatus()
		return
	}
	ret = retcode.NewStatusOk()
	return
}
Пример #8
0
func (self *restContext) ReadBucketId(value *httpserver.Values, key string, optional bool) (
	bucketId bucket.Id,
	bucketIdAsString string,
	status retcode.Status) {

	if optional {
		bucketIdAsString = value.OptionalString(key)
		if bucketIdAsString == "" {
			return
		}
	} else {
		bucketIdAsString, status = value.NonEmptyString(key)
		if !status.IsOk() {
			return
		}
	}

	bucketIdBytes, err := encoding.Base32Decode(bucketIdAsString)
	if err != nil {
		status = retcode.NewStatusFmt(retcode.ErrorClient, "Invalid bucket id given. The id "+
			"has to be base32 without padding. Err: %v", err)
		return
	}
	bucketId = bucket.Id(bucketIdBytes)
	status = retcode.NewStatusOk()
	return
}
Пример #9
0
func (self *metadataStruct) createInstance(state *minos.OperationState) (
	instance *metadataInstance,
	ret retcode.Status) {

	ni := new(metadataInstance)
	ni.base = self

	return ni, retcode.NewStatusOk()
}
Пример #10
0
func (self *notifierInstance) unsubscribeSingle(
	subscriptionId operations.SubscriptionId,
	state *minos.OperationState) (status retcode.Status) {

	// Ask the registration bucket
	key := types.Key{[]byte(state.Context.ClientId),
		[]byte(subscriptionId)}
	opGet := operations.Get{
		BucketId: self.registrationsBucketId,
		Key:      key,
	}
	opGetApiRet := state.Dispatcher.Perform(state.Context, &opGet)
	if opGetApiRet.GetCode() == retcode.OkNotFound {
		// No, no such registration
		return opGetApiRet.GetStatus()
	}

	opGetReturn := opGetApiRet.(*operations.GetReturn)
	valueInRegistrationBucketAsKey := opGetReturn.Value.ToKey()
	subscriptionKey := valueInRegistrationBucketAsKey[1:]
	matchChildKeys := deserializeMatchChildKey(valueInRegistrationBucketAsKey[0][0])

	keyInLookupBucket, status := createKeyForLookupBucket(
		matchChildKeys,
		subscriptionId, subscriptionKey, state)
	if !status.Code.IsOk() {
		return
	}

	// Now remove from lookup bucket
	removeOp := operations.Put{
		BucketId: self.keysBucketId,
		Key:      keyInLookupBucket,
		Value:    nil,
	}
	removeOpApiReturn := state.Dispatcher.Perform(state.Context, &removeOp)
	if !removeOpApiReturn.GetCode().IsOk() {
		// Error removing
		status = removeOpApiReturn.GetStatus()
		return
	}

	// Ok, removed, now remove from registration bucket
	removeFromRegistrationBucketOp := operations.Put{
		BucketId: self.registrationsBucketId,
		Key:      key,
		Value:    nil,
	}
	removeFromRegistrationReturn := state.Dispatcher.Perform(state.Context,
		&removeFromRegistrationBucketOp)
	if removeFromRegistrationReturn.GetCode().IsOk() {
		status = retcode.NewStatusOk()
	} else {
		status = removeFromRegistrationReturn.GetStatus()
	}
	return
}
Пример #11
0
func (self *Values) NonEmptyString(key string) (value string, status retcode.Status) {
	value = self.valueReader(key)
	if value == "" {
		status = retcode.NewStatusFmt(retcode.ErrorInputFormat, "%v: "+
			"Expecting %v to have a non-empty string - but string is empty.", key)
		return
	}
	status = retcode.NewStatusOk()
	return
}
Пример #12
0
func (self *Store) Op_iterate(operation *operations.Iterate) (
	ret bucket.BucketReturn) {

	database, err := self.Database()
	if err != nil {
		ret = &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
		return
	}

	r, err := self.createScanRangeForIterate(operation, self.State.BucketId)
	if err != nil {
		ret = &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorClient, err)}
		return
	}
	iterator := database.NewIterator(r, nil)
	defer iterator.Release()

	var exhausted bool
	exhausted = !iterator.First()
	resultNumber := 0
	var numberOfErrors uint32
	numberOfErrors = 0
	for !exhausted {
		key, err := typescommon.PrefixedKeyBytesToKey(
			self.State.BucketId, iterator.Key())
		if err != nil {
			numberOfErrors++
		} else {
			value, err := types.NewArrayFromBytes(iterator.Value())
			if err != nil {
				numberOfErrors++
			} else {
				// Ok
				iterateState := new(operations.IterateState)
				iterateState.ResultNumber = uint32(resultNumber)
				iterateState.Key = key
				iterateState.Value = value

				end := operation.Function(iterateState)
				if end {
					break
				}
			}
		}
		resultNumber++
		exhausted = iterator.Next()
		exhausted = !iterator.Valid() //TODO: Why do we need this too?
	}

	return &operations.IterateReturn{
		Status:         retcode.NewStatusOk(),
		NumberOfErrors: numberOfErrors,
	}
}
Пример #13
0
func Globals(ctx *caprica.Context) {
	globalsInfoMap := make(map[string]interface{})
	globalsInfoMap[Globals_Key_Blob] =
		ctx.Converter.WriteBucketId(ctx.Caprica.GlobalBlobStoreId())
	globalsInfoMap[Globals_Key_Directory] =
		ctx.Converter.WriteBucketId(ctx.Caprica.GlobalDirectoryId())

	ouput := GlobalsOut{
		Globals: globalsInfoMap,
	}
	ctx.Output.Write(retcode.NewStatusOk(), ouput)
}
Пример #14
0
func (self *incubator) incubationFilePath(state *minos.OperationState,
	id string) (fname string, ret retcode.Status) {

	clientIdAsBase32 := encoding.Base32Encode([]byte(state.Context.ClientId))
	fname, err := state.Files.Filename(self.bucketId, "_"+clientIdAsBase32, id)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}
	ret = retcode.NewStatusOk()
	return
}
Пример #15
0
func (self *dispatcherimpl) perform_OpCreateBucket(context *ares.Context,
	operation *operations.CreateBucket) (
	ret ares.Return) {

	if context.UserId.IsPublicUser() {
		return &operations.GenericReturn{retcode.NewStatusError(
			retcode.ErrorNotAllowedForPublicUser,
			errors.New("Need to be authenticated"))}
	}

	/* Validate and create */
	bucketType := self.typesRegistry.BucketType(operation.TypeId)
	if bucketType == nil {
		return &operations.GenericReturn{retcode.NewStatusError(retcode.ErrorClient,
			errors.New(fmt.Sprintf("BucketType %v not found", operation.TypeId)))}
	}
	if operation.TypeId.IsMetadata() {
		return &operations.GenericReturn{retcode.NewStatusError(retcode.ErrorClient,
			errors.New("You tried to create a metadata bucket (bucket type). That's "+
				"not possible, since metadata buckets are only 'virtual' and cannot "+
				"be created (convert a bucket ID to a metadata bucket ID to access its "+
				"metadata)."))}
	}

	createdMetadata := operation.Metadata
	if createdMetadata == nil {
		createdMetadata = make(map[string]([]byte))
	}

	createState := new(minos.CreateState)
	createState.TypeId = operation.TypeId
	createState.Metadata = self.metadata.NewMemoryTypedReaderWriter(createdMetadata)
	createState.Context = context
	createState.Dispatcher = self
	createState.Files = self.files

	status := bucketType.Create(createState)
	if !status.GetCode().IsOk() {
		return &operations.GenericReturn{status}
	}

	/* Now create the bucket */
	bucketId, err := self.metadata.Create(operation.TypeId, createdMetadata,
		context.UserId)
	if err != nil {
		// TODO: Rollback what 'bucketType.Create' did
		return &operations.GenericReturn{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	// Done
	return &operations.CreateBucketReturn{bucketId, retcode.NewStatusOk()}
}
Пример #16
0
func directoryGet(ctx *restContext, dirHash []byte, filename string) (
	entry *directoryEntry,
	found bool,
	status retcode.Status) {

	getEntry := &operations.BucketGetIn{
		BucketOperation: operations.BucketOperation{
			BucketId: ctx.caprica.GlobalDirectoryId(),
		},
		Key: [][]byte{dirHash, []byte(filename)},
	}
	var out interface{}
	out, status = ctx.execute(getEntry, operations.BucketGet)

	if !status.IsOk() {
		return
	}
	if status.Code == retcode.OkNotFound {
		return
	}
	found = true

	getEntryReturn := out.(operations.BucketGetOut)
	value := getEntryReturn.Value.([][]byte)

	var targetHash []byte
	err := encoding.Cbor().Decode(value[0], &targetHash)
	if err != nil {
		status = retcode.NewStatusFmt(retcode.ErrorServer, "Unable to decode hash")
		return
	}
	entry = new(directoryEntry)
	entry.targetHash = targetHash
	if len(value) == 1 {
		// Directory
		entry.isDirectory = true
	} else {
		entry.isDirectory = false
		// Mime
		var mime string
		err := encoding.Cbor().Decode(value[1], &mime)
		if err != nil {
			status = retcode.NewStatusFmt(retcode.ErrorServer, "Unable to decode mime")
			return
		}
		entry.mimeType = mime
	}

	status = retcode.NewStatusOk()
	return
}
Пример #17
0
func (self *public) publicFilePath(state *minos.OperationState,
	hash []byte) (fname string, ret retcode.Status) {

	hashAsBase32 := encoding.Base32Encode(hash)
	firstBytesAsDirectory := hashAsBase32[:6]
	fname, err := state.Files.Filename(self.bucketId,
		firstBytesAsDirectory, hashAsBase32)
	if err != nil {
		ret = retcode.NewStatusError(retcode.ErrorServer, err)
		return
	}
	ret = retcode.NewStatusOk()
	return
}
Пример #18
0
func (self *notifierInstance) createPutOperationForLookupBucket(
	keyForKeysRegistration types.Key,
	returnConfiguration *operations.SubscribeReturnConfig) (
	operation *operations.Put, ret retcode.Status) {

	serializedReturnConfig := serializeConfiguration(returnConfiguration)
	value := types.Array{serializedReturnConfig}

	opPut := operations.Put{
		BucketId: self.keysBucketId,
		Key:      keyForKeysRegistration,
		Value:    value,
	}
	return &opPut, retcode.NewStatusOk()
}
Пример #19
0
func (self *incubator) new(state *minos.OperationState,
	id string,
	removeIfAlreadyExisting bool) (ret retcode.Status) {

	// See if it already exists (and maybe remove it)
	_, ret = self.information(state, id)
	if !ret.IsOk() {
		return
	}
	alreadyFound := ret.Code != retcode.OkNotFound
	if alreadyFound && removeIfAlreadyExisting {
		// Ok, remove current
		ret = self.cleanIncubation(state, id)
		if !ret.IsOk() {
			return
		}
	}

	if !alreadyFound {
		// Create it
		hash := serializablehash.NewSha256()
		newState := new(incubationState)
		newState.length = 0
		newState.hashState = hash.Serialize()
		self.writeInformation(state, id, newState)

		// Create empty file
		filePath, retLocal := self.incubationFilePath(state, id)
		if !retLocal.IsOk() {
			ret = retLocal
			return
		}
		file, err := os.Create(filePath)
		if err != nil {
			err = errors.New(fmt.Sprintf("Unable to create incubation file: %v", err))
			ret = retcode.NewStatusError(retcode.ErrorServer, err)
			return
		}
		err = file.Close()
		if err != nil {
			err = errors.New(fmt.Sprintf("To close incubation file: %v", err))
			ret = retcode.NewStatusError(retcode.ErrorServer, err)
			return
		}
	}
	ret = retcode.NewStatusOk()
	return
}
Пример #20
0
func (self *notifierInstance) triggerSubscribers(operation *operations.Put,
	keysToMatch types.Key,
	matchChildKeys bool,
	state *minos.OperationState) (ret bucket.BucketReturn) {

	numberOfKeyElements := len(keysToMatch)

	matchChildKeysByte := serializeMatchChildKey(matchChildKeys)

	lookupKeyBegin := make(types.Key, numberOfKeyElements+3)
	lookupKeyBegin[0] = []byte{matchChildKeysByte}
	lookupKeyBegin[1] = []byte{byte(numberOfKeyElements)}
	copy(lookupKeyBegin[2:], keysToMatch)
	lookupKeyBegin[len(lookupKeyBegin)-1] = []byte{}

	lookupKeyEnd := make(types.Key, numberOfKeyElements+3)
	lookupKeyEnd[0] = []byte{matchChildKeysByte}
	lookupKeyEnd[1] = []byte{byte(numberOfKeyElements)}
	copy(lookupKeyEnd[2:], keysToMatch)
	lookupKeyEnd[len(lookupKeyEnd)-1] = []byte{255}

	is := new(iterateState)
	is.opPut = operation
	is.notificationReceiver = state.NotificationReceiver
	is.id = state.BucketId

	opIterate := &operations.Iterate{
		BucketId: self.keysBucketId,
		From: operations.Bound{
			Key: lookupKeyBegin,
		},
		To: &operations.Bound{
			Key: lookupKeyEnd,
		},
		Function: is.iterateFunction,
	}
	apiReturn := state.Dispatcher.Perform(state.Context, opIterate)
	if !apiReturn.GetCode().IsOk() {
		return &bucket.ReturnGeneric{apiReturn.GetStatus()}
	}

	if operation.Value != nil {
		return &operations.PutReturn{retcode.NewStatusOk()}
	} else {
		return &operations.PutReturn{retcode.NewStatus(retcode.OkRemove)}
	}
}
Пример #21
0
func (self *directoryBucketInstance) opPut(operation *operations.Put,
	state *minos.OperationState) (ret bucket.BucketReturn) {

	directory, hash, ret := self.readDirectory(operation, state)
	if ret != nil {
		return
	}

	ret = self.indexDirectory(operation, state, hash, directory)
	if ret != nil {
		return
	}

	// Everything ok
	ret = &operations.PutReturn{retcode.NewStatusOk()}
	return
}
Пример #22
0
func (self *notifierInstance) putToRegistrationBucket(state *minos.OperationState,
	operation *operations.Subscribe) (subscriptionId operations.SubscriptionId,
	status retcode.Status) {

	// Locks, so we do not generate duplicate IDs
	state.Locker.BucketIdWriteLock(self.registrationsBucketId)
	defer state.Locker.BucketIdWriteUnlock(self.registrationsBucketId)

	var err error
	for index := 0; index < numberOfSubscriptionIdGenRetries; index++ {
		subscriptionId, err = createSubscriptionIdCandidate()
		if err == nil {
			key := types.Key{[]byte(state.Context.ClientId),
				[]byte(subscriptionId)}
			opGet := operations.Get{
				BucketId: self.registrationsBucketId,
				Key:      key,
			}
			opGetApiRet := state.Dispatcher.Perform(state.Context, &opGet)
			if opGetApiRet.GetCode() == retcode.OkNotFound {
				// Great, entry not found, so add it
				keyInOperationAsArray := operation.Key.ToArray()
				value := make([][]byte, len(keyInOperationAsArray)+1)
				value[0] = []byte{serializeMatchChildKey(operation.MatchChildKeys)}
				copy(value[1:], keyInOperationAsArray)

				opPut := operations.Put{
					BucketId: self.registrationsBucketId,
					Key:      key,
					Value:    value,
				}
				opPutApiRet := state.Dispatcher.Perform(state.Context, &opPut)
				if opPutApiRet.GetCode().IsOk() {
					// Great, registration is OK
					status = retcode.NewStatusOk()
					return
				}
			}
		}
	}
	return nil, retcode.NewStatusError(retcode.ErrorServer,
		errors.New("No free subscription ID found"))
}
Пример #23
0
func (self *Store) Op_exists(operation *operations.Exists) (ret bucket.BucketReturn) {
	database, err := self.Database()
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	keyBytes := self.KeyToBytesAndPrefix(operation.Key)

	_, err = database.Get(keyBytes, nil)
	if err == leveldb.ErrNotFound {
		return &operations.ExistsReturn{retcode.NewStatus(retcode.OkNotFound)}
	}
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	return &operations.ExistsReturn{
		Status: retcode.NewStatusOk(),
	}
}
Пример #24
0
func (self *directoryBucket) Create(state *minos.CreateState) (status retcode.Status) {

	suppliedForwarder, suppliedForwarderExists, err :=
		state.Metadata.Forwarder(true, forwarder_name)
	if err != nil {
		return retcode.NewStatusError(
			retcode.ErrorClient, errors.New(fmt.Sprintf(
				"Error getting the supplied forwarder: %v\n", err)))
	}
	if suppliedForwarderExists {
		// No need to create one, already have one. Is the type correct?
		if suppliedForwarder.TypeId() != bucket.TypeId_BlobStore {
			return retcode.NewStatusError(
				retcode.ErrorClient, errors.New(fmt.Sprintf(
					"The supplied forwarder has to be of type blobstore, but it's: %v\n",
					suppliedForwarder.TypeId())))
		}
	} else {
		// We have to create the backend bucket
		createBucket := operations.CreateBucket{
			TypeId: bucket.TypeId_BlobStore,
		}
		retBackendBucket := state.Dispatcher.Perform(state.Context, &createBucket)
		if !retBackendBucket.GetCode().IsOk() {
			return retBackendBucket.GetStatus()
		}

		backendBucket := retBackendBucket.(*operations.CreateBucketReturn)

		var err error
		err = state.Metadata.SetForwarder(true, forwarder_name, backendBucket.Id)
		if err != nil {
			return retcode.NewStatusError(
				retcode.ErrorServer, errors.New(fmt.Sprintf(
					"Could not modify metadata: %v\n", err)))
		}
	}

	// Nothing to do here
	return retcode.NewStatusOk()
}
Пример #25
0
func (self *hasherStruct) createInstance(state *minos.OperationState) (
	instance *hasherInstance,
	ret retcode.Status) {

	var err error
	backendId, exists, err := state.MetadataGetterSetter.Forwarder(
		true, metadataKey_backend)
	if err != nil {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Could not get forwarder metadata (backend)"))
	}
	if !exists {
		return nil, retcode.NewStatusError(
			retcode.ErrorServer, errors.New("Backend bucket id not found in metadata"))
	}

	ni := new(hasherInstance)
	ni.base = self
	ni.backendId = backendId

	return ni, retcode.NewStatusOk()
}
Пример #26
0
func createKeyForLookupBucket(
	matchChildKeys bool,
	subscriptionId operations.SubscriptionId,
	subscriptionKey types.Key,
	state *minos.OperationState) (key types.Key, ret retcode.Status) {

	numberOfKeyElements := len(subscriptionKey)
	if numberOfKeyElements > 255 {
		ret = retcode.NewStatusError(retcode.ErrorClient,
			errors.New("Too many key elements"))
		return
	}

	matchChildKeysByte := serializeMatchChildKey(matchChildKeys)

	newKey := make(types.Key, numberOfKeyElements+4)
	newKey[0] = []byte{matchChildKeysByte}
	newKey[1] = []byte{byte(numberOfKeyElements)}
	copy(newKey[2:], subscriptionKey)
	newKey[2+numberOfKeyElements] = []byte(state.Context.ClientId)
	newKey[3+numberOfKeyElements] = []byte(subscriptionId)
	return newKey, retcode.NewStatusOk()
}
Пример #27
0
func (self *counterInstance) op_get(operation *operations.Get) (
	ret bucket.BucketReturn) {

	get_return := self.cstore.Op_get(operation)
	if get_return.GetCode() == retcode.OkNotFound {
		// This is a special case, if non existent, return 0
		zeroValue := 0
		valueBytes, err := encoding.Cbor().Encode(zeroValue)
		if err != nil {
			return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer,
				errors.New(fmt.Sprintf("Could not encode cbor", err)))}
		}

		value := types.Array{valueBytes}
		get_return := operations.GetReturn{
			Status: retcode.NewStatusOk(),
			Value:  value,
		}
		return &get_return
	} else {
		// Everything else is returned 1:1
		return get_return
	}
}
Пример #28
0
func (self *blobstorePrototype) Create(state *minos.CreateState) (status retcode.Status) {

	// We have to create the backend bucket
	createBucket := operations.CreateBucket{
		TypeId: bucket.TypeId_Store,
	}
	retBackendBucket := state.Dispatcher.Perform(state.Context, &createBucket)
	if !retBackendBucket.GetCode().IsOk() {
		return retBackendBucket.GetStatus()
	}

	backendBucket := retBackendBucket.(*operations.CreateBucketReturn)

	var err error
	err = state.Metadata.SetForwarder(true, metadataKey_backend, backendBucket.Id)
	if err != nil {
		return retcode.NewStatusError(
			retcode.ErrorServer, errors.New(fmt.Sprintf(
				"Could not modify metadata: %v\n", err)))
	}

	// Nothing to do here
	return retcode.NewStatusOk()
}
Пример #29
0
func (self *Store) Op_get(operation *operations.Get) (ret bucket.BucketReturn) {
	database, err := self.Database()
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	keyBytes := self.KeyToBytesAndPrefix(operation.Key)
	value, err := database.Get(keyBytes, nil)
	if err == leveldb.ErrNotFound {
		return &bucket.ReturnGeneric{retcode.NewStatus(retcode.OkNotFound)}
	}
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}
	keyConstructed, err := types.NewArrayFromBytes(value)
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	return &operations.GetReturn{
		Status: retcode.NewStatusOk(),
		Value:  keyConstructed,
	}
}
Пример #30
0
func (self *notifier) Create(state *minos.CreateState) (
	status retcode.Status) {

	// We have to create two buckets here
	createBucket := operations.CreateBucket{
		TypeId: bucket.TypeId_Store,
	}
	retRegistrationsBucket := state.Dispatcher.Perform(state.Context, &createBucket)
	if !retRegistrationsBucket.GetCode().IsOk() {
		return retRegistrationsBucket.GetStatus()
	}
	retKeysBucket := state.Dispatcher.Perform(state.Context, &createBucket)
	if !retKeysBucket.GetCode().IsOk() {
		return retKeysBucket.GetStatus()
	}

	registrationsBucket := retRegistrationsBucket.(*operations.CreateBucketReturn)
	keysBucket := retKeysBucket.(*operations.CreateBucketReturn)

	var err error
	err = state.Metadata.SetForwarder(true, metadataKey_registrations, registrationsBucket.Id)
	if err != nil {
		return retcode.NewStatusError(
			retcode.ErrorServer, errors.New(fmt.Sprintf(
				"Could not modify metadata: %v\n", err)))
	}
	err = state.Metadata.SetForwarder(true, metadataKey_keys, keysBucket.Id)
	if err != nil {
		return retcode.NewStatusError(
			retcode.ErrorServer, errors.New(fmt.Sprintf(
				"Could not modify metadata: %v\n", err)))
	}

	// Nothing to do here
	return retcode.NewStatusOk()
}