Пример #1
0
// Insert a new document, and assign it a UID.
func (col *Col) InsertWithUID(doc interface{}) (newID uint64, newUID string, err error) {
	newUID = uid.NextUID()
	if docMap, ok := doc.(map[string]interface{}); !ok {
		err = errors.New("Only JSON object document may have UID")
		return
	} else {
		docMap["_uid"] = newUID
		newID, err = col.Insert(doc)
		return
	}
}
Пример #2
0
// Give a document (identified by ID) a new UID.
func (col *Col) ReassignUID(id uint64) (newID uint64, newUID string, err error) {
	newUID = uid.NextUID()
	var originalDoc interface{}
	if err = col.Read(id, &originalDoc); err != nil {
		return
	}
	if docWithUID, ok := originalDoc.(map[string]interface{}); !ok {
		err = errors.New("Only JSON object document may have UID")
		return
	} else {
		docWithUID["_uid"] = newUID
		newID, err = col.Update(id, docWithUID)
		return
	}
}
Пример #3
0
// Insert a document, return its unique ID.
func (col *Col) Insert(doc map[string]interface{}) (id uint64, err error) {
	// Allocate an ID to the document
	id = uid.NextUID()
	doc[uid.PK_NAME] = strconv.FormatUint(id, 10)
	num := id % col.NumChunksI64
	// Lock the chunk while inserting the document
	lock := col.ChunkMutexes[num]
	lock.Lock()
	if _, err = col.Chunks[num].Insert(doc); err != nil {
		lock.Unlock()
		return
	}
	col.indexDoc(id, doc)
	lock.Unlock()
	return
}