couchdoc_conv.go 6.44 KB
Newer Older
manish's avatar
manish committed
1
2
3
4
5
6
7
8
9
10
11
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package statecouchdb

import (
	"bytes"
	"encoding/json"
	"strings"
12
	"unicode/utf8"
manish's avatar
manish committed
13
14
15
16

	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
	"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
	"github.com/hyperledger/fabric/core/ledger/util/couchdb"
17
	"github.com/pkg/errors"
manish's avatar
manish committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
)

const (
	binaryWrapper = "valueBytes"
	idField       = "_id"
	revField      = "_rev"
	versionField  = "~version"
	deletedField  = "_deleted"
)

type keyValue struct {
	key string
	*statedb.VersionedValue
}

type jsonValue map[string]interface{}

func tryCastingToJSON(b []byte) (isJSON bool, val jsonValue) {
	var jsonVal map[string]interface{}
	err := json.Unmarshal(b, &jsonVal)
	return err == nil, jsonValue(jsonVal)
}

func castToJSON(b []byte) (jsonValue, error) {
	var jsonVal map[string]interface{}
	err := json.Unmarshal(b, &jsonVal)
44
	err = errors.Wrap(err, "error unmarshalling json data")
manish's avatar
manish committed
45
46
47
48
	return jsonVal, err
}

func (v jsonValue) checkReservedFieldsNotPresent() error {
49
	for fieldName := range v {
50
		if fieldName == versionField || strings.HasPrefix(fieldName, "_") {
51
			return errors.Errorf("field [%s] is not valid for the CouchDB state database", fieldName)
manish's avatar
manish committed
52
53
54
55
56
57
58
59
60
61
		}
	}
	return nil
}

func (v jsonValue) removeRevField() {
	delete(v, revField)
}

func (v jsonValue) toBytes() ([]byte, error) {
62
63
64
	jsonBytes, err := json.Marshal(v)
	err = errors.Wrap(err, "error marshalling json data")
	return jsonBytes, err
manish's avatar
manish committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
}

func couchDocToKeyValue(doc *couchdb.CouchDoc) (*keyValue, error) {
	// initialize the return value
	var returnValue []byte
	var err error
	// create a generic map unmarshal the json
	jsonResult := make(map[string]interface{})
	decoder := json.NewDecoder(bytes.NewBuffer(doc.JSONValue))
	decoder.UseNumber()
	if err = decoder.Decode(&jsonResult); err != nil {
		return nil, err
	}
	// verify the version field exists
	if _, fieldFound := jsonResult[versionField]; !fieldFound {
80
		return nil, errors.Errorf("version field %s was not found", versionField)
manish's avatar
manish committed
81
82
83
	}
	key := jsonResult[idField].(string)
	// create the return version from the version field in the JSON
84
85
86
87
88

	returnVersion, returnMetadata, err := decodeVersionAndMetadata(jsonResult[versionField].(string))
	if err != nil {
		return nil, err
	}
manish's avatar
manish committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	// remove the _id, _rev and version fields
	delete(jsonResult, idField)
	delete(jsonResult, revField)
	delete(jsonResult, versionField)

	// handle binary or json data
	if doc.Attachments != nil { // binary attachment
		// get binary data from attachment
		for _, attachment := range doc.Attachments {
			if attachment.Name == binaryWrapper {
				returnValue = attachment.AttachmentBytes
			}
		}
	} else {
		// marshal the returned JSON data.
		if returnValue, err = json.Marshal(jsonResult); err != nil {
			return nil, err
		}
	}
108
109
110
111
112
	return &keyValue{key, &statedb.VersionedValue{
		Value:    returnValue,
		Metadata: returnMetadata,
		Version:  returnVersion},
	}, nil
manish's avatar
manish committed
113
114
115
116
117
118
119
120
121
}

func keyValToCouchDoc(kv *keyValue, revision string) (*couchdb.CouchDoc, error) {
	type kvType int32
	const (
		kvTypeDelete = iota
		kvTypeJSON
		kvTypeAttachment
	)
122
	key, value, metadata, version := kv.key, kv.Value, kv.Metadata, kv.Version
manish's avatar
manish committed
123
124
125
126
127
128
	jsonMap := make(jsonValue)

	var kvtype kvType
	switch {
	case value == nil:
		kvtype = kvTypeDelete
129
130
131
	// check for the case where the jsonMap is nil,  this will indicate
	// a special case for the Unmarshal that results in a valid JSON returning nil
	case json.Unmarshal(value, &jsonMap) == nil && jsonMap != nil:
manish's avatar
manish committed
132
133
134
135
136
		kvtype = kvTypeJSON
		if err := jsonMap.checkReservedFieldsNotPresent(); err != nil {
			return nil, err
		}
	default:
137
138
139
140
		// create an empty map, if the map is nil
		if jsonMap == nil {
			jsonMap = make(jsonValue)
		}
manish's avatar
manish committed
141
142
143
		kvtype = kvTypeAttachment
	}

144
145
146
147
148
149
	verAndMetadata, err := encodeVersionAndMetadata(version, metadata)
	if err != nil {
		return nil, err
	}
	// add the (version + metadata), id, revision, and delete marker (if needed)
	jsonMap[versionField] = verAndMetadata
manish's avatar
manish committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
	jsonMap[idField] = key
	if revision != "" {
		jsonMap[revField] = revision
	}
	if kvtype == kvTypeDelete {
		jsonMap[deletedField] = true
	}
	jsonBytes, err := jsonMap.toBytes()
	if err != nil {
		return nil, err
	}
	couchDoc := &couchdb.CouchDoc{JSONValue: jsonBytes}
	if kvtype == kvTypeAttachment {
		attachment := &couchdb.AttachmentInfo{}
		attachment.AttachmentBytes = value
		attachment.ContentType = "application/octet-stream"
		attachment.Name = binaryWrapper
		attachments := append([]*couchdb.AttachmentInfo{}, attachment)
		couchDoc.Attachments = attachments
	}
	return couchDoc, nil
}

// couchSavepointData data for couchdb
type couchSavepointData struct {
	BlockNum uint64 `json:"BlockNum"`
	TxNum    uint64 `json:"TxNum"`
}

func encodeSavepoint(height *version.Height) (*couchdb.CouchDoc, error) {
	var err error
	var savepointDoc couchSavepointData
	// construct savepoint document
	savepointDoc.BlockNum = height.BlockNum
	savepointDoc.TxNum = height.TxNum
	savepointDocJSON, err := json.Marshal(savepointDoc)
	if err != nil {
187
188
		err = errors.Wrap(err, "failed to marshal savepoint data")
		logger.Errorf("%+v", err)
manish's avatar
manish committed
189
190
191
192
193
194
195
196
		return nil, err
	}
	return &couchdb.CouchDoc{JSONValue: savepointDocJSON, Attachments: nil}, nil
}

func decodeSavepoint(couchDoc *couchdb.CouchDoc) (*version.Height, error) {
	savepointDoc := &couchSavepointData{}
	if err := json.Unmarshal(couchDoc.JSONValue, &savepointDoc); err != nil {
197
198
		err = errors.Wrap(err, "failed to unmarshal savepoint data")
		logger.Errorf("%+v", err)
manish's avatar
manish committed
199
200
201
202
203
204
205
206
207
208
209
210
211
		return nil, err
	}
	return &version.Height{BlockNum: savepointDoc.BlockNum, TxNum: savepointDoc.TxNum}, nil
}

func validateValue(value []byte) error {
	isJSON, jsonVal := tryCastingToJSON(value)
	if !isJSON {
		return nil
	}
	return jsonVal.checkReservedFieldsNotPresent()
}

212
213
func validateKey(key string) error {
	if !utf8.ValidString(key) {
214
		return errors.Errorf("invalid key [%x], must be a UTF-8 string", key)
215
216
	}
	if strings.HasPrefix(key, "_") {
217
		return errors.Errorf("invalid key [%s], cannot begin with \"_\"", key)
218
	}
219
220
221
	if key == "" {
		return errors.New("invalid key. Empty string is not supported as a key by couchdb")
	}
222
223
224
	return nil
}

manish's avatar
manish committed
225
226
227
228
// removeJSONRevision removes the "_rev" if this is a JSON
func removeJSONRevision(jsonValue *[]byte) error {
	jsonVal, err := castToJSON(*jsonValue)
	if err != nil {
229
		logger.Errorf("Failed to unmarshal couchdb JSON data: %+v", err)
manish's avatar
manish committed
230
231
232
233
		return err
	}
	jsonVal.removeRevField()
	if *jsonValue, err = jsonVal.toBytes(); err != nil {
234
		logger.Errorf("Failed to marshal couchdb JSON data: %+v", err)
manish's avatar
manish committed
235
236
237
	}
	return err
}