Beispiel #1
0
func (db *DB) ObjectDocSchemaSync(fold string, schema skv.ObjectDocSchema) *skv.Reply {

	_obj_doc_global_locker.Lock()
	defer _obj_doc_global_locker.Unlock()

	var (
		rpl  = skv.NewReply("")
		key  = skv.ObjectDocFoldKey(fold)
		prev skv.ObjectDocSchema
	)

	if len(schema.Indexes) > skv.ObjectDocSchemaMaxIndex {
		rpl.Status = skv.ReplyBadArgument
		return rpl
	}

	if rs := db.RawGet(skv.ObjectDocSchemaKey(key)); rs.Status == skv.ReplyOK {
		rs.JsonDecode(&prev)
	}

	for i, ei := range schema.Indexes {
		ei.Column = skv.ObjectDocIndexStringFilter(ei.Column)
		schema.Indexes[i] = ei
	}

	for _, pi := range prev.Indexes {

		removed, changed := true, false

		for j, ei := range schema.Indexes {

			if pi.Column == ei.Column {

				removed = false

				if pi.Type != ei.Type ||
					pi.Length != ei.Length ||
					pi.Unique != ei.Unique ||
					pi.AutoIncr != ei.AutoIncr {

					changed = true
				}

				ei.Seq = pi.Seq

				schema.Indexes[j] = ei

				break
			}
		}

		if removed || changed {

			objIdxKeyPrefix, limit := skv.ObjectDocIndexFieldPrefix(key, pi.Seq), 1000

			// fmt.Println("WARN CLEAN prev INDEXES", pi.Column)
			for {

				rs := db.RawScan(objIdxKeyPrefix, objIdxKeyPrefix, uint32(limit)).Hash()

				if len(rs) > 0 {
					batch := new(leveldb.Batch)

					for _, entry := range rs {
						batch.Delete(entry.Key)
					}

					db.ldb.Write(batch, nil)
				}

				if len(rs) < limit {
					break
				}
			}
		}
	}

	for i, ei := range schema.Indexes {

		reidx := true

		for _, pi := range prev.Indexes {

			if pi.Column == ei.Column {

				ei.Seq = pi.Seq

				schema.Indexes[i] = ei

				if pi.Type == ei.Type &&
					pi.Length == ei.Length &&
					pi.Unique == ei.Unique &&
					pi.AutoIncr == ei.AutoIncr &&
					pi.Seq > 0 {

					reidx = false
				}

				break
			}
		}

		//
		for _, ci := range schema.Indexes {

			if ei.Seq == ci.Seq && ei.Column != ci.Column {
				ei.Seq = 0
				schema.Indexes[i] = ei
				break
			}
		}

		if ei.Seq == 0 {

			reidx = true

			for j := uint8(1); j <= 255; j++ {

				dup := false

				for _, ci := range schema.Indexes {

					if j == ci.Seq && ei.Column != ci.Column {
						dup = true
						break
					}
				}

				if !dup {
					ei.Seq = j
					schema.Indexes[i] = ei
					break
				}
			}
		}

		if reidx && ei.Seq > 0 {

			// fmt.Println("WARN NEW INDEXES", ei.Column, ei.Seq)

			offset, limit := "", 1000

			for {

				rs := db.ObjectScan(fold, offset, "", uint32(limit)).ObjectList()

				batch := new(leveldb.Batch)

				for _, entry := range rs {

					var obj map[string]interface{}
					if err := entry.Data.JsonDecode(&obj); err == nil {

						for mk, mv := range obj {

							mk = skv.ObjectDocIndexStringFilter(mk)

							if mk != ei.Column {
								continue
							}

							if bs, ok := skv.ObjectDocIndexValue(&ei, reflect.ValueOf(mv)); ok {
								batch.Put(dbutil.BytesConcat(skv.ObjectDocIndexFieldPrefix(key, ei.Seq), bs, entry.Key), []byte{})
							}

							break
						}

						offset = dbutil.BytesToHexString(entry.Key)
					}
				}

				db.ldb.Write(batch, nil)

				if len(rs) < limit {
					break
				}
			}
		}
	}

	skey := string(key)

	rpl = db._raw_put_json(skv.ObjectDocSchemaKey(key), schema, 0)
	if rpl.Status == skv.ReplyOK {
		_obj_doc_indexes[skey] = schema
	}

	return rpl
}
Beispiel #2
0
func (db *DB) ObjectDocPut(fold, key string, obj interface{}, opts *skv.ObjectWriteOptions) *skv.Reply {

	_obj_doc_global_locker.Lock()
	_obj_doc_global_locker.Unlock()

	var (
		opath = skv.NewObjectPathKey(fold, key)
		rpl   = skv.NewReply("")
	)

	if len(opath.Fold) > skv.ObjectDocKeyLenMax ||
		len(opath.Field) > skv.ObjectDocPriLenMax ||
		obj == nil {
		rpl.Status = skv.ReplyBadArgument
		return rpl
	}

	var (
		bkey    = opath.EntryIndex()
		objt    = reflect.TypeOf(obj)
		objv    = reflect.ValueOf(obj)
		prev    = map[string]interface{}{}
		previdx = map[uint8]skv.ObjectDocSchemaIndexEntryBytes{}
		set     = map[string]interface{}{}
	)

	if opts == nil {
		opts = _obj_options_def
	}

	prevobj := db.RawGet(bkey).Object()
	if prevobj.Status == skv.ReplyOK {

		if err := prevobj.Data.JsonDecode(&prev); err == nil {
			previdx = skv.ObjectDocIndexDataExport(_obj_doc_indexes, opath.Fold, prev)
		}
	}

	if objt.Kind() == reflect.Struct {

		for i := 0; i < objt.NumField(); i++ {
			set[skv.ObjectDocIndexStringFilter(objt.Field(i).Name)] = objv.Field(i).Interface()
		}

	} else if objt.Kind() == reflect.Map {

		mks := objv.MapKeys()

		for _, mkv := range mks {

			if mkv.Kind() == reflect.String {
				set[skv.ObjectDocIndexStringFilter(mkv.String())] = objv.MapIndex(mkv).Interface()
			}
		}

	} else {

		rpl.Status = skv.ReplyBadArgument
		return rpl
	}

	setidx, idxnew, idxdup := skv.ObjectDocIndexDataExport(_obj_doc_indexes, opath.Fold, set), [][]byte{}, [][]byte{}

	// fmt.Println("\tsetidx", setidx)
	// fmt.Println("\tprevidx", previdx)

	for siKey, siEntry := range setidx {

		var incr_set, incr_prev uint64

		if siEntry.AutoIncr {
			incr_set = dbutil.BytesToUint64(siEntry.Data)
		}

		//
		if piEntry, ok := previdx[siKey]; ok {

			if siEntry.AutoIncr && incr_set == 0 {

				if incr_prev = dbutil.BytesToUint64(piEntry.Data); incr_prev > 0 {

					siEntry.Data, incr_set = piEntry.Data, incr_prev

					set[siEntry.FieldName] = incr_set

					continue
				}

			} else if bytes.Compare(piEntry.Data, siEntry.Data) == 0 {
				continue
			}

			idxdup = append(idxdup, append(append(skv.ObjectDocIndexFieldPrefix(opath.Fold, siKey), piEntry.Data...), opath.Field...))
		}

		//
		if siEntry.AutoIncr {

			if incr_set == 0 {

				incr_set = db.RawIncrby(skv.ObjectDocIndexIncrKey(opath.Fold, siEntry.Seq), 1).Uint64()

				ibs := make([]byte, 8)
				binary.BigEndian.PutUint64(ibs, incr_set)

				siEntry.Data = ibs[(8 - len(siEntry.Data)):]

				set[siEntry.FieldName] = incr_set

			} else if incr_set > 0 && incr_set > incr_prev {

				if db.RawGet(skv.ObjectDocIndexIncrKey(opath.Fold, siEntry.Seq)).Uint64() < incr_set {
					db.RawPut(skv.ObjectDocIndexIncrKey(opath.Fold, siEntry.Seq), []byte(strconv.FormatUint(incr_set, 10)), 0)
				}
			}
		}

		if siEntry.Unique || siEntry.AutoIncr {

			objIdxKeyPrefix := append(skv.ObjectDocIndexFieldPrefix(opath.Fold, siKey), siEntry.Data...)

			if rs := db.RawScan(objIdxKeyPrefix, []byte{}, 1).Hash(); len(rs) > 0 {
				rpl.Status = skv.ReplyBadArgument
				return rpl
			}
		}

		idxnew = append(idxnew, append(append(skv.ObjectDocIndexFieldPrefix(opath.Fold, siKey), siEntry.Data...), opath.Field...))
	}

	//

	//
	batch := new(leveldb.Batch)

	for _, idxkey := range idxdup {
		batch.Delete(idxkey)
	}

	for _, idxkey := range idxnew {
		batch.Put(idxkey, []byte{})
	}

	bvalue, _ := dbutil.JsonEncode(set)
	sum := crc32.ChecksumIEEE(bvalue)

	if prevobj.Meta.Sum == sum {
		return skv.NewReply(skv.ReplyOK)
	}

	db._obj_meta_sync(skv.ObjectTypeDocument, &prevobj.Meta, opath, int64(len(bvalue)), sum, _obj_options_def)

	batch.Put(bkey, append(prevobj.Meta.Export(), bvalue...))

	if err := db.ldb.Write(batch, nil); err != nil {
		rpl.Status = err.Error()
	}

	return rpl
}