Exemplo n.º 1
0
func (e *Engine) SaveMineField(mat [][]int, userID string) (*MineField, error) {
	mf := MineField{
		Matrix:    mat,
		CreatedAt: time.Now().UTC(),
		CreatedBy: userID,
	}

	insert, insertErr := e.term.Table(MineTB).
		Insert(&mf, r.InsertOpts{ReturnChanges: true, Durability: "hard"}).
		RunWrite(e.session)
	if insertErr != nil {
		e.log.Println(insertErr)
		return nil, ErrQueryFailed
	}
	if insert.Inserted != 1 {
		e.log.Println(insert.FirstError)
		return nil, ErrQueryFailed
	}
	if scanErr := re.Decode(&mf, insert.Changes[0].NewValue); scanErr != nil {
		e.log.Println(scanErr)
		return nil, ErrQueryFailed
	}

	return &mf, nil
}
Exemplo n.º 2
0
func (c *Cursor) loadNext(dest interface{}) (bool, error) {
	for c.lastErr == nil {
		// Check if response is closed/finished
		if c.buffer.Len() == 0 && c.responses.Len() == 0 && c.closed != 0 {
			return false, errCursorClosed
		}

		if c.buffer.Len() == 0 && c.responses.Len() == 0 && !c.finished {
			err := c.fetchMore()
			if err != nil {
				return false, err
			}
		}

		if c.buffer.Len() == 0 && c.responses.Len() == 0 && c.finished {
			return false, nil
		}

		if c.buffer.Len() == 0 && c.responses.Len() > 0 {
			if response, ok := c.responses.Pop().(json.RawMessage); ok {
				var value interface{}
				err := json.Unmarshal(response, &value)
				if err != nil {
					return false, err
				}

				value, err = recursivelyConvertPseudotype(value, c.opts)
				if err != nil {
					return false, err
				}

				// If response is an ATOM then try and convert to an array
				if data, ok := value.([]interface{}); ok && c.isAtom {
					for _, v := range data {
						c.buffer.Push(v)
					}
				} else if value == nil {
					c.buffer.Push(nil)
				} else {
					c.buffer.Push(value)
				}
			}
		}

		if c.buffer.Len() > 0 {
			data := c.buffer.Pop()

			err := encoding.Decode(dest, data)
			if err != nil {
				return false, err
			}

			return true, nil
		}
	}

	return false, c.lastErr
}
Exemplo n.º 3
0
// Scan copies the result in the current row into the value pointed at by dest.
//
// If an argument is of type *interface{}, Scan copies the value provided by the
// database without conversion.
//
// If the value is a struct then Scan traverses
// the result recursively and attempts to match the keys returned by the database
// to the name used by the structs field (either the struct field name or its
// key).
func (r *ResultRows) Scan(dest interface{}) error {
	if r.err != nil {
		return r.err
	}
	if r.current == nil {
		return RqlDriverError{"Scan called without calling Next"}
	}

	data, err := deconstructDatum(r.current, r.opts)
	if err != nil {
		return err
	}

	err = encoding.Decode(dest, data)
	if err != nil {
		return err
	}

	return nil
}
Exemplo n.º 4
0
func (r repository) Get(id string, typeRegistry cqrs.TypeRegistry) ([]cqrs.VersionedEvent, error) {
	cursor, error := rethinkdb.Table(r.eventsTableName).GetAllByIndex("sourceID", id).OrderBy("version").Run(r.session)
	if error != nil {
		return nil, error
	}

	var events []cqrs.VersionedEvent
	var rawEvents []rethinkVersionedEvent
	error = cursor.All(&rawEvents)
	if error != nil {
		return nil, error
	}

	for _, raw := range rawEvents {
		eventType, ok := typeRegistry.GetEventType(raw.EventType)
		if !ok {
			log.Println("Cannot find event type", raw.EventType)
			return nil, errors.New("Cannot find event type " + raw.EventType)
		}

		eventValue := reflect.New(eventType)
		event := eventValue.Interface()
		if err := encoding.Decode(event, raw.Event); err != nil {
			log.Println("Error deserializing event ", raw.Event)
			return nil, err
		}

		versionedEvent := cqrs.VersionedEvent{
			ID:        raw.ID,
			SourceID:  raw.SourceID,
			Version:   raw.Version,
			EventType: raw.EventType,
			Created:   raw.Created,
			Event:     reflect.Indirect(eventValue).Interface()}

		events = append(events, versionedEvent)
	}

	return events, nil
}
Exemplo n.º 5
0
func (c *Cursor) loadNextLocked(dest interface{}) (bool, error) {
	for {
		if c.lastErr != nil {
			return false, c.lastErr
		}

		// Check if response is closed/finished
		if c.buffer.Len() == 0 && c.responses.Len() == 0 && c.closed {
			return false, errCursorClosed
		}

		if c.buffer.Len() == 0 && c.responses.Len() == 0 && !c.finished {
			c.mu.Unlock()
			err := c.fetchMore()
			c.mu.Lock()
			if err != nil {
				return false, err
			}
		}

		if c.buffer.Len() == 0 && c.responses.Len() == 0 && c.finished {
			return false, nil
		}

		if c.buffer.Len() == 0 && c.responses.Len() > 0 {
			if response, ok := c.responses.Pop().(json.RawMessage); ok {
				var value interface{}
				decoder := json.NewDecoder(bytes.NewBuffer(response))
				if c.conn.opts.UseJSONNumber {
					decoder.UseNumber()
				}
				err := decoder.Decode(&value)
				if err != nil {
					return false, err
				}

				value, err = recursivelyConvertPseudotype(value, c.opts)
				if err != nil {
					return false, err
				}

				// If response is an ATOM then try and convert to an array
				if data, ok := value.([]interface{}); ok && c.isAtom {
					for _, v := range data {
						c.buffer.Push(v)
					}
				} else if value == nil {
					c.buffer.Push(nil)
				} else {
					c.buffer.Push(value)
				}
			}
		}

		if c.buffer.Len() > 0 {
			data := c.buffer.Pop()

			err := encoding.Decode(dest, data)
			if err != nil {
				return false, err
			}

			return true, nil
		}
	}
}
Exemplo n.º 6
0
// Next retrieves the next document from the result set, blocking if necessary.
// This method will also automatically retrieve another batch of documents from
// the server when the current one is exhausted, or before that in background
// if possible.
//
// Next returns true if a document was successfully unmarshalled onto result,
// and false at the end of the result set or if an error happened.
// When Next returns false, the Err method should be called to verify if
// there was an error during iteration.
func (c *Cursor) Next(result interface{}) bool {
	c.mu.Lock()

	// Load more data if needed
	for c.err == nil {
		// Check if response is closed/finished
		if len(c.buffer) == 0 && len(c.responses) == 0 && c.closed {
			c.err = errors.New("connection closed, cannot read cursor")
			c.mu.Unlock()
			return false
		}
		if len(c.buffer) == 0 && len(c.responses) == 0 && c.finished {
			c.mu.Unlock()
			return false
		}

		// Start precomputing next batch
		if len(c.responses) == 1 && !c.finished {
			c.mu.Unlock()
			if err := c.session.asyncContinueQuery(c); err != nil {
				c.err = err
				return false
			}
			c.mu.Lock()
		}

		// If the buffer is empty fetch more results
		if len(c.buffer) == 0 {
			if len(c.responses) == 0 && !c.finished {
				c.mu.Unlock()
				if err := c.session.continueQuery(c); err != nil {
					c.err = err
					return false
				}
				c.mu.Lock()
			}

			// Load the new response into the buffer
			if len(c.responses) > 0 {
				var err error
				c.buffer = c.responses[0].Responses
				if err != nil {
					c.err = err
					c.mu.Unlock()
					return false
				}
				c.responses = c.responses[1:]
			}
		}

		// If the buffer is no longer empty then move on otherwise
		// try again
		if len(c.buffer) > 0 {
			break
		}
	}

	if c.err != nil {
		c.mu.Unlock()
		return false
	}

	var data interface{}
	data, c.buffer = c.buffer[0], c.buffer[1:]

	data, err := recursivelyConvertPseudotype(data, c.opts)
	if err != nil {
		c.err = err
		c.mu.Unlock()
		return false
	}

	c.mu.Unlock()
	err = encoding.Decode(result, data)
	if err != nil {
		c.mu.Lock()
		if c.err == nil {
			c.err = err
		}
		c.mu.Unlock()

		return false
	}

	return true
}
Exemplo n.º 7
0
func (c *Cursor) loadNextLocked(dest interface{}) (bool, error) {
	for {
		if c.lastErr != nil {
			return false, c.lastErr
		}

		// Check if response is closed/finished
		if len(c.buffer) == 0 && len(c.responses) == 0 && c.closed {
			return false, errCursorClosed
		}

		if len(c.buffer) == 0 && len(c.responses) == 0 && !c.finished {
			c.mu.Unlock()
			err := c.fetchMore()
			c.mu.Lock()
			if err != nil {
				return false, err
			}
			// Check if cursor was closed while fetching results
			if c.closed {
				return false, nil
			}
		}

		if len(c.buffer) == 0 && len(c.responses) == 0 && c.finished {
			return false, nil
		}

		if len(c.buffer) == 0 && len(c.responses) > 0 {
			var response json.RawMessage
			response, c.responses = c.responses[0], c.responses[1:]

			var value interface{}
			decoder := json.NewDecoder(bytes.NewBuffer(response))
			if c.conn.opts.UseJSONNumber {
				decoder.UseNumber()
			}
			err := decoder.Decode(&value)
			if err != nil {
				return false, err
			}

			value, err = recursivelyConvertPseudotype(value, c.opts)
			if err != nil {
				return false, err
			}

			// If response is an ATOM then try and convert to an array
			if data, ok := value.([]interface{}); ok && c.isAtom {
				for _, v := range data {
					c.buffer = append(c.buffer, v)
				}
			} else if value == nil {
				c.buffer = append(c.buffer, nil)
			} else {
				c.buffer = append(c.buffer, value)
			}
		}

		if len(c.buffer) > 0 {
			var data interface{}
			data, c.buffer = c.buffer[0], c.buffer[1:]

			err := encoding.Decode(dest, data)
			if err != nil {
				return false, err
			}

			return true, nil
		}
	}
}