Ejemplo n.º 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
	}
}
Ejemplo n.º 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
	}
}
Ejemplo n.º 3
0
Archivo: col.go Proyecto: jbenet/tiedot
// 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
}