|
|
@ -52,6 +52,27 @@ type Elem struct {
|
|
|
|
LastUpdate time.Time `bson:"last_update"`
|
|
|
|
LastUpdate time.Time `bson:"last_update"`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type tableWriteLog struct {
|
|
|
|
|
|
|
|
ID primitive.ObjectID `bson:"_id"`
|
|
|
|
|
|
|
|
DID string `bson:"d_id"`
|
|
|
|
|
|
|
|
Logs []Elem `bson:"logs"`
|
|
|
|
|
|
|
|
Version uint `bson:"version"`
|
|
|
|
|
|
|
|
Deleted uint `bson:"deleted"`
|
|
|
|
|
|
|
|
LastUpdate time.Time `bson:"last_update"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *tableWriteLog) WriteLog() *WriteLog {
|
|
|
|
|
|
|
|
return &WriteLog{
|
|
|
|
|
|
|
|
ID: t.ID,
|
|
|
|
|
|
|
|
DID: t.DID,
|
|
|
|
|
|
|
|
Logs: t.Logs,
|
|
|
|
|
|
|
|
Version: t.Version,
|
|
|
|
|
|
|
|
Deleted: t.Deleted,
|
|
|
|
|
|
|
|
LastUpdate: t.LastUpdate,
|
|
|
|
|
|
|
|
LogLen: 0,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type DataLog interface {
|
|
|
|
type DataLog interface {
|
|
|
|
WriteLog(ctx context.Context, dId string, eIds []string, deleted bool) error
|
|
|
|
WriteLog(ctx context.Context, dId string, eIds []string, deleted bool) error
|
|
|
|
FindChangeLog(ctx context.Context, dId string, version uint, limit int) (*WriteLog, error)
|
|
|
|
FindChangeLog(ctx context.Context, dId string, version uint, limit int) (*WriteLog, error)
|
|
|
@ -94,7 +115,7 @@ func (l *logModel) WriteLog(ctx context.Context, dId string, eIds []string, dele
|
|
|
|
if res.MatchedCount > 0 {
|
|
|
|
if res.MatchedCount > 0 {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := l.initDoc(ctx, dId, eIds, deleted, now); err == nil {
|
|
|
|
if _, err := l.initDoc(ctx, dId, eIds, deleted, now); err == nil {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
} else if !mongo.IsDuplicateKeyError(err) {
|
|
|
|
} else if !mongo.IsDuplicateKeyError(err) {
|
|
|
|
return err
|
|
|
|
return err
|
|
|
@ -107,15 +128,9 @@ func (l *logModel) WriteLog(ctx context.Context, dId string, eIds []string, dele
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *logModel) initDoc(ctx context.Context, dId string, eIds []string, deleted bool, now time.Time) error {
|
|
|
|
func (l *logModel) initDoc(ctx context.Context, dId string, eIds []string, deleted bool, now time.Time) (*tableWriteLog, error) {
|
|
|
|
type tableWriteLog struct {
|
|
|
|
|
|
|
|
DID string `bson:"d_id"`
|
|
|
|
|
|
|
|
Logs []Elem `bson:"logs"`
|
|
|
|
|
|
|
|
Version uint `bson:"version"`
|
|
|
|
|
|
|
|
Deleted uint `bson:"deleted"`
|
|
|
|
|
|
|
|
LastUpdate time.Time `bson:"last_update"`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
wl := tableWriteLog{
|
|
|
|
wl := tableWriteLog{
|
|
|
|
|
|
|
|
ID: primitive.NewObjectID(),
|
|
|
|
DID: dId,
|
|
|
|
DID: dId,
|
|
|
|
Logs: make([]Elem, 0, len(eIds)),
|
|
|
|
Logs: make([]Elem, 0, len(eIds)),
|
|
|
|
Version: FirstVersion,
|
|
|
|
Version: FirstVersion,
|
|
|
@ -131,12 +146,12 @@ func (l *logModel) initDoc(ctx context.Context, dId string, eIds []string, delet
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err := l.coll.InsertOne(ctx, &wl)
|
|
|
|
_, err := l.coll.InsertOne(ctx, &wl)
|
|
|
|
return err
|
|
|
|
return &wl, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *logModel) writeLogBatch(ctx context.Context, dId string, eIds []string, deleted bool, now time.Time) (*mongo.UpdateResult, error) {
|
|
|
|
func (l *logModel) writeLogBatch(ctx context.Context, dId string, eIds []string, deleted bool, now time.Time) (*mongo.UpdateResult, error) {
|
|
|
|
if len(eIds) == 0 {
|
|
|
|
if eIds == nil {
|
|
|
|
return nil, errs.ErrArgs.WrapMsg("elem id is empty", "dId", dId)
|
|
|
|
eIds = []string{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
filter := bson.M{
|
|
|
|
filter := bson.M{
|
|
|
|
"d_id": dId,
|
|
|
|
"d_id": dId,
|
|
|
@ -195,17 +210,25 @@ func (l *logModel) writeLogBatch(ctx context.Context, dId string, eIds []string,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *logModel) findDoc(ctx context.Context, dId string) (*WriteLog, error) {
|
|
|
|
func (l *logModel) findDoc(ctx context.Context, dId string) (*WriteLog, error) {
|
|
|
|
res, err := mongoutil.FindOne[*WriteLog](ctx, l.coll, bson.M{"d_id": dId}, options.FindOne().SetProjection(bson.M{"logs": 0}))
|
|
|
|
return mongoutil.FindOne[*WriteLog](ctx, l.coll, bson.M{"d_id": dId}, options.FindOne().SetProjection(bson.M{"logs": 0}))
|
|
|
|
if err == nil {
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
|
|
|
|
} else if errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
|
func (l *logModel) FindChangeLog(ctx context.Context, dId string, version uint, limit int) (*WriteLog, error) {
|
|
|
|
return &WriteLog{}, nil
|
|
|
|
if wl, err := l.findChangeLog(ctx, dId, version, limit); err == nil {
|
|
|
|
|
|
|
|
return wl, nil
|
|
|
|
|
|
|
|
} else if !errors.Is(err, mongo.ErrNoDocuments) {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if res, err := l.initDoc(ctx, dId, nil, false, time.Now()); err == nil {
|
|
|
|
|
|
|
|
return res.WriteLog(), nil
|
|
|
|
|
|
|
|
} else if mongo.IsDuplicateKeyError(err) {
|
|
|
|
|
|
|
|
return l.findChangeLog(ctx, dId, version, limit)
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *logModel) FindChangeLog(ctx context.Context, dId string, version uint, limit int) (*WriteLog, error) {
|
|
|
|
func (l *logModel) findChangeLog(ctx context.Context, dId string, version uint, limit int) (*WriteLog, error) {
|
|
|
|
if version == 0 && limit == 0 {
|
|
|
|
if version == 0 && limit == 0 {
|
|
|
|
return l.findDoc(ctx, dId)
|
|
|
|
return l.findDoc(ctx, dId)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -271,7 +294,7 @@ func (l *logModel) FindChangeLog(ctx context.Context, dId string, version uint,
|
|
|
|
return nil, err
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(res) == 0 {
|
|
|
|
if len(res) == 0 {
|
|
|
|
return &WriteLog{}, nil
|
|
|
|
return nil, mongo.ErrNoDocuments
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res[0], nil
|
|
|
|
return res[0], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|