func (rpcMethod *RPCMethod) listIDs(response *ResponseParameters) error {

	//open a read transaction
	rpcMethod.rpcServer.boltDB.View(func(tx *bolt.Tx) error {
		var cursor *bolt.Cursor
		cursor = tx.Cursor()

		var bucket *bolt.Bucket
		response.Result = make([]interface{}, 0, 10)

		//traverse through all keys
		for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() {
			bucket = tx.Bucket(k)

			//traverse through all relation and value pairs
			bucket.ForEach(func(relation, value []byte) error {
				tuple := make([]string, 2)
				rpcMethod.rpcServer.logger.Println(string(k), string(relation), string(value))
				//make an array of 2 strings [key,relation]
				tuple[0] = string(k)
				tuple[1] = string(relation)
				response.Result = append(response.Result, tuple)
				return nil
			})
		}
		return nil
	})

	response.Error = nil
	return nil

}
/*
Called in Shutdown - keys are transferred to successor
*/
func (rpcServer *RPCServer) makeInsertsToSuccessor() {
	//open a read transaction
	rpcServer.boltDB.View(func(tx *bolt.Tx) error {
		var cursor *bolt.Cursor
		cursor = tx.Cursor()

		var bucket *bolt.Bucket

		//traverse through all keys
		for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() {
			bucket = tx.Bucket(k)

			//traverse through all relation and value pairs
			bucket.ForEach(func(relation, value []byte) error {
				//create paramter - successor

				//add to array of interface

				parameterArray := make([]interface{}, 3)
				parameterArray[0] = string(k)
				parameterArray[1] = string(relation)
				parameterArray[2] = string(value)

				//create json message
				jsonMessage := rpcclient.RequestParameters{}
				jsonMessage.Method = "Insert"
				jsonMessage.Params = parameterArray

				jsonBytes, err := json.Marshal(jsonMessage)
				if err != nil {
					rpcServer.logger.Println(err)
					return err
				}

				rpcServer.logger.Println(string(jsonBytes))

				clientServerInfo, err := rpcServer.chordNode.PrepareClientServerInfo(rpcServer.chordNode.FingerTable[1])
				if err != nil {

					rpcServer.logger.Println(err)
					return nil

				}
				client := &rpcclient.RPCClient{}
				err, _ = client.RpcCall(clientServerInfo, string(jsonBytes))

				if err != nil {
					rpcServer.logger.Println(err)
					return nil
				}

				return nil
			})
		}
		return nil
	})

}
Ejemplo n.º 3
0
// TODO(fluffle): Dedupe this with Prefix when less hungover.
func (bucket *boltBucket) All(key Key, value interface{}) error {
	// This entirely stolen from mgo's Iter.All() \o/
	vv := reflect.ValueOf(value)
	if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Slice {
		panic("All() requires a pointer-to-slice.")
	}
	sv := vv.Elem()
	sv = sv.Slice(0, sv.Cap())
	et := sv.Type().Elem()

	return bucket.db.View(func(tx *bolt.Tx) error {
		b, last, err := bucketFor(key, tx.Bucket(bucket.name))
		if err != nil {
			return err
		}
		// All implies that the last key elem is also a bucket,
		// but we support a zero-length key to perform a scan
		// over the root bucket.
		cs := []*bolt.Cursor{b.Cursor()}
		if len(last) > 0 {
			if b = b.Bucket(last); b == nil {
				return bolt.ErrBucketNotFound
			}
			cs[0] = b.Cursor()
		}
		var i int
		var c *bolt.Cursor
		for len(cs) > 0 {
			c, cs = cs[0], cs[1:]
			for k, v := c.First(); k != nil; k, v = c.Next() {
				if v == nil {
					// All flattens the nested buckets under key.
					if nest := b.Bucket(k); nest != nil {
						cs = append(cs, nest.Cursor())
					}
					continue
				}
				if sv.Len() == i {
					ev := reflect.New(et)
					if err := bson.Unmarshal(v, ev.Interface()); err != nil {
						return err
					}
					sv = reflect.Append(sv, ev.Elem())
					sv = sv.Slice(0, sv.Cap())
				} else {
					if err := bson.Unmarshal(v, sv.Index(i).Addr().Interface()); err != nil {
						return err
					}
				}
				i++
			}
		}
		vv.Elem().Set(sv.Slice(0, i))
		return nil
	})
}
//its the same as list buckets
func (rpcMethod *RPCMethod) listKeys(response *ResponseParameters) error {

	//open a read transaction
	rpcMethod.rpcServer.boltDB.View(func(tx *bolt.Tx) error {
		var cursor *bolt.Cursor
		cursor = tx.Cursor()

		//append to reselt the list of buckets
		response.Result = make([]interface{}, 0, 10)
		for k, _ := cursor.First(); k != nil; k, _ = cursor.Next() {
			rpcMethod.rpcServer.logger.Println("BUCKET ", string(k))
			response.Result = append(response.Result, string(k))
		}

		return nil
	})

	response.Error = nil

	return nil

}