示例#1
0
func (tbl *TableCached) DeleteParam(ent EntryCached, rwp *RWParams) bool {
	refval := reflect.Indirect(reflect.ValueOf(ent))
	id := refval.Field(tbl._idxid).Interface()
	if tbl.Cache != nil {
		tbl.Cache.Delete(id)
	}
	tx := sqlx.Ext(nil)
	if rwp != nil && rwp.Tx != nil {
		tx = &rwp.Tx.Tx
	}
	res, _ := tbl.Table.DeletePrimTx(tx, ent)
	return res > 0
}
示例#2
0
func (tbl *TableCached) WriteParam(ent EntryCached, rwp *RWParams) bool {
	refval := reflect.Indirect(reflect.ValueOf(ent))
	now := time.Now().UnixNano()

	if tbl._idxtimeaccess != -1 {
		refval.Field(tbl._idxtimeaccess).SetInt(now)
	}
	if tbl._idxtimemodify != -1 {
		refval.Field(tbl._idxtimemodify).SetInt(now)
	}
	if rwp != nil && rwp.MarkOnline && tbl._idxtimeonline != -1 {
		refval.Field(tbl._idxtimeonline).SetInt(now)
	}

	id := refval.Field(tbl._idxid).Interface()
	ver := int64(-1)
	if tbl._idxversion != -1 {
		ver = refval.Field(tbl._idxversion).Int()
	}

	tx := sqlx.Ext(nil)
	if rwp != nil && rwp.Tx != nil {
		tx = &rwp.Tx.Tx
	}

	autoCommit := false
	if tx == nil {
		if tbl.WriteChildrenParams(ent, rwp, false) > 0 {
			rwp.Tx, _ = tbl.TXBegin()
			if rwp.Tx == nil {
				return false
			}
			tx = rwp.Tx
			autoCommit = true
		}
	}
	if tx != nil {
		if tbl.WriteChildrenParams(ent, rwp, true) < 0 {
			if autoCommit {
				rwp.Tx.Rollback()
			}
			return false
		}
	}

	diff := ^uint64(0)

	if tbl.Cache != nil {
		v, _ := tbl.Cache.Get(id)
		if v != nil {
			entCached, _ := v.(EntryCached)
			diff = tbl.Diff(ent, entCached)
			log.Println("ChangeDiffed:", strconv.FormatInt(int64(diff), 2))
		}
	}

	res, _ := tbl.Table.UpdateMultiMaskTx(tx, ent, diff, ver != -1)

	if res <= 0 {
		//fail, delete from cache
		if tbl.Cache != nil {
			tbl.Cache.Delete(id)
		}
		log.Println("WriteCached failed:", tbl.Table.Name, id, ver)
		if rwp != nil && rwp.ReadOnWriteConflict {
			tbl.ReadParam(ent, rwp)
		}
		if autoCommit {
			rwp.Tx.Rollback()
		}
		return false
	}

	if rwp != nil && autoCommit {
		defer func() { rwp.Tx.Commit() }()
	}

	if tbl.Cache != nil {
		tbl.Cache.Lock()
		defer tbl.Cache.Unlock()
		v, _ := tbl.Cache.GetUnsafe(id)
		if v == nil {
			//alredy deleted
			return true
		}
		entCached := v.(EntryCached)
		refval := reflect.Indirect(reflect.ValueOf(entCached))
		if tbl._idxversion != -1 {
			if refval.Field(tbl._idxversion).Int() != ver {
				//cache mismatch invalidate
				tbl.Cache.Delete(id)
				return true
			}
		}

		//update version
		entCached.CopyFrom(ent)
		if tbl._idxversion != -1 {
			refval.Field(tbl._idxversion).SetInt(ver + 1)
		}
		if tbl._idxdbcache != -1 {
			refval.Field(tbl._idxdbcache).Set(dbCacheTypeZero)
		}
	}
	return true
}