func transformPartialLog()

in executor/executor.go [279:373]


func transformPartialLog(partialLog *oplog.PartialLog, nsTrans *transform.NamespaceTransform, transformRef bool) *oplog.PartialLog {
	db := strings.SplitN(partialLog.Namespace, ".", 2)[0]
	if partialLog.Operation != "c" {
		// {"op" : "i", "ns" : "my.system.indexes", "o" : { "v" : 2, "key" : { "date" : 1 }, "name" : "date_1", "ns" : "my.tbl", "expireAfterSeconds" : 3600 }
		if strings.HasSuffix(partialLog.Namespace, "system.indexes") {
			value := oplog.GetKey(partialLog.Object, "ns")
			oplog.SetFiled(partialLog.Object, "ns", nsTrans.Transform(value.(string)))
		}
		partialLog.Namespace = nsTrans.Transform(partialLog.Namespace)
		if transformRef {
			partialLog.Object = transform.TransformDBRef(partialLog.Object, db, nsTrans)
		}
	} else {
		operation, found := oplog.ExtraCommandName(partialLog.Object)
		if !found {
			LOG.Warn("extraCommandName meets type[%s] which is not implemented, ignore!", operation)
			return nil
		}
		switch operation {
		case "create":
			// { "create" : "my", "idIndex" : { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "my.my" }
			if idIndex := oplog.GetKey(partialLog.Object, "idIndex"); idIndex != nil {
				if ns := oplog.GetKey(idIndex.(bson.D), "ns"); ns != nil {
					oplog.SetFiled(idIndex.(bson.D), "ns", nsTrans.Transform(ns.(string)))
				}
			} else {
				LOG.Warn("transformLogs meet unknown create command: %v", partialLog.Object)
			}
			fallthrough
		case "createIndexes":
			fallthrough
		case "commitIndexBuild":
			fallthrough
		case "collMod":
			fallthrough
		case "drop":
			fallthrough
		case "deleteIndex":
			fallthrough
		case "deleteIndexes":
			fallthrough
		case "dropIndex":
			fallthrough
		case "dropIndexes":
			fallthrough
		case "convertToCapped":
			fallthrough
		case "emptycapped":
			col, ok := oplog.GetKey(partialLog.Object, operation).(string)
			if !ok {
				LOG.Warn("extraCommandName meets illegal %v oplog %v, ignore!", operation, partialLog.Object)
				return nil
			}
			partialLog.Namespace = nsTrans.Transform(fmt.Sprintf("%s.%s", db, col))
			oplog.SetFiled(partialLog.Object, operation, strings.SplitN(partialLog.Namespace, ".", 2)[1])
		case "renameCollection":
			// { "renameCollection" : "my.tbl", "to" : "my.my", "stayTemp" : false, "dropTarget" : false }
			fromNs, ok := oplog.GetKey(partialLog.Object, operation).(string)
			if !ok {
				LOG.Warn("extraCommandName meets illegal %v oplog %v, ignore!", operation, partialLog.Object)
				return nil
			}
			toNs, ok := oplog.GetKey(partialLog.Object, "to").(string)
			if !ok {
				LOG.Warn("extraCommandName meets illegal %v oplog %v, ignore!", operation, partialLog.Object)
				return nil
			}
			partialLog.Namespace = nsTrans.Transform(fromNs)
			oplog.SetFiled(partialLog.Object, operation, partialLog.Namespace)
			oplog.SetFiled(partialLog.Object, "to", nsTrans.Transform(toNs))
		case "applyOps":
			if ops := oplog.GetKey(partialLog.Object, "applyOps").([]bson.D); ops != nil {
				// except field 'o'
				except := map[string]struct{}{
					"o": {},
				}
				for i, ele := range ops {
					// m, keys := oplog.ConvertBsonD2M(ele)
					m, keys := oplog.ConvertBsonD2MExcept(ele, except)
					subLog := oplog.NewPartialLog(m)
					transSubLog := transformPartialLog(subLog, nsTrans, transformRef)
					if transSubLog == nil {
						LOG.Warn("transformPartialLog sublog %v return nil, ignore!", subLog)
						return nil
					}
					ops[i] = transSubLog.Dump(keys, false)
				}
			}
		default:
			// such as: dropDatabase
			partialLog.Namespace = nsTrans.Transform(partialLog.Namespace)
		}
	}
	return partialLog
}