Skip to content

Commit e9cbbfe

Browse files
committed
support custom indexing policy for collections
1 parent eaec04f commit e9cbbfe

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

client.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (c *Client) Query(link string, query *Query, out interface{}) (string, erro
9494
req.QueryHeaders(n, tok)
9595
resp, err := c.do(req, out)
9696
if err != nil {
97-
if DebugQueries {
97+
if DebugQueries && resp != nil {
9898
log.Printf("docdb: %+v, cost: %v RU, error: %v", query, resp.Header.Get(HEADER_CHARGE), err)
9999
}
100100
return "", err
@@ -112,7 +112,10 @@ func (c *Client) Create(link string, body, ret interface{}, headers map[string]s
112112
return err
113113
}
114114
buf := bytes.NewBuffer(data)
115-
_, err = c.method("POST", link, ret, buf, headers)
115+
resp, err := c.method("POST", link, ret, buf, headers)
116+
if DebugQueries && resp != nil {
117+
log.Println("docdb create:", body, "cost:", resp.Header.Get(HEADER_CHARGE), "RU", "error:", err)
118+
}
116119
return err
117120
}
118121

documentdb.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,22 @@ func (db *DB) Delete() error {
9595
return db.c.DeleteDatabase(db.Self)
9696
}
9797

98-
func (db *DB) CreateCollection(id string) (*Col, error) {
99-
c, err := db.c.CreateCollection(db.Self, map[string]string{"id": id})
98+
func (db *DB) CreateCollection(id string, col *Collection) (*Col, error) {
99+
if col == nil {
100+
col = &Collection{}
101+
}
102+
col.Id = id
103+
c, err := db.c.CreateCollection(db.Self, col)
100104
if err != nil {
101105
return nil, err
102106
}
103107
return &Col{db: db, Collection: *c}, nil
104108
}
105109

106-
func (db *DB) CreateCollectionIfNotExists(id string) (*Col, error) {
110+
func (db *DB) CreateCollectionIfNotExists(id string, col *Collection) (*Col, error) {
107111
c, err := db.C(id)
108112
if err == ErrNotFound {
109-
if c, err = db.CreateCollection(id); IsExists(err) {
113+
if c, err = db.CreateCollection(id, col); IsExists(err) {
110114
c, err = db.C(id)
111115
}
112116
}

models.go

+62-19
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,95 @@ package documentdb
22

33
// Resource
44
type Resource struct {
5-
Id string `json:"id,omitempty"`
6-
Self string `json:"_self,omitempty"`
7-
Etag string `json:"_etag,omitempty"`
8-
Rid string `json:"_rid,omitempty"`
9-
Ts int `json:"_ts,omitempty"`
5+
Id string `json:"id,omitempty"`
6+
Self string `json:"_self,omitempty"`
7+
Etag string `json:"_etag,omitempty"`
8+
Rid string `json:"_rid,omitempty"`
9+
Ts int `json:"_ts,omitempty"`
1010
}
1111

12+
type IndexingMode string
13+
14+
const (
15+
Consistent = IndexingMode("Consistent")
16+
Lazy = IndexingMode("Lazy")
17+
)
18+
1219
// Indexing policy
13-
// TODO: Ex/IncludePaths
1420
type IndexingPolicy struct {
15-
IndexingMode string `json: "indexingMode,omitempty"`
16-
Automatic bool `json: "automatic,omitempty"`
21+
IndexingMode IndexingMode `json:"indexingMode,omitempty"`
22+
Automatic bool `json:"automatic"`
23+
Included []IncludedPath `json:"includedPaths,omitempty"`
24+
Excluded []ExcludedPath `json:"excludedPaths,omitempty"`
25+
}
26+
27+
type DataType string
28+
29+
const (
30+
StringType = DataType("String")
31+
NumberType = DataType("Number")
32+
PointType = DataType("Point")
33+
PolygonType = DataType("Polygon")
34+
LineStringType = DataType("LineString")
35+
)
36+
37+
type IndexKind string
38+
39+
const (
40+
Hash = IndexKind("Hash")
41+
Range = IndexKind("Range")
42+
Spatial = IndexKind("Spatial")
43+
)
44+
45+
const MaxPrecision = -1
46+
47+
type Index struct {
48+
DataType DataType `json:"dataType,omitempty"`
49+
Kind IndexKind `json:"kind,omitempty"`
50+
Precision int `json:"precision,omitempty"`
51+
}
52+
53+
type IncludedPath struct {
54+
Path string `json:"path"`
55+
Indexes []Index `json:"indexes,omitempty"`
56+
}
57+
58+
type ExcludedPath struct {
59+
Path string `json:"path"`
1760
}
1861

1962
// Database
2063
type Database struct {
2164
Resource
22-
Colls string `json:"_colls,omitempty"`
23-
Users string `json:"_users,omitempty"`
65+
Colls string `json:"_colls,omitempty"`
66+
Users string `json:"_users,omitempty"`
2467
}
2568

2669
// Collection
2770
type Collection struct {
2871
Resource
29-
IndexingPolicy IndexingPolicy `json:"indexingPolicy,omitempty"`
30-
Docs string `json:"_docs,omitempty"`
31-
Udf string `json:"_udfs,omitempty"`
32-
Sporcs string `json:"_sporcs,omitempty"`
33-
Triggers string `json:"_triggers,omitempty"`
34-
Conflicts string `json:"_conflicts,omitempty"`
72+
IndexingPolicy *IndexingPolicy `json:"indexingPolicy,omitempty"`
73+
Docs string `json:"_docs,omitempty"`
74+
Udf string `json:"_udfs,omitempty"`
75+
Sporcs string `json:"_sporcs,omitempty"`
76+
Triggers string `json:"_triggers,omitempty"`
77+
Conflicts string `json:"_conflicts,omitempty"`
3578
}
3679

3780
// Document
3881
type Document struct {
3982
Resource
40-
attachments string `json: "attachments,omitempty"`
83+
Attachments string `json:"attachments,omitempty"`
4184
}
4285

4386
// Stored Procedure
4487
type Sproc struct {
4588
Resource
46-
Body string `json:"body,omitempty"`
89+
Body string `json:"body,omitempty"`
4790
}
4891

4992
// User Defined Function
5093
type UDF struct {
5194
Resource
52-
Body string `json:"body,omitempty"`
95+
Body string `json:"body,omitempty"`
5396
}

0 commit comments

Comments
 (0)