Skip to content

Commit 0356c06

Browse files
authored
Dubbo2.7.5: Add Page and Prioritized interface (#26)
* Add Page Support * add comment * Add Comment * README.md * Fix review comment * fix review * Fix review * Add priority definition * Add comments * Fix review
1 parent 3631266 commit 0356c06

File tree

5 files changed

+207
-7
lines changed

5 files changed

+207
-7
lines changed

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
2424
## math
2525

2626
* Decimal
27-
28-
## runtime
29-
30-
* GoSafely
31-
> Using `go` in a safe way.
32-
* GoUnterminated
33-
> Run a goroutine in a safe way whose task is long live as the whole process life time.
27+
28+
## runtime
29+
30+
* GoSafely
31+
> Using `go` in a safe way.
32+
* GoUnterminated
33+
> Run a goroutine in a safe way whose task is long live as the whole process life time.
3434
3535
## sync
3636

@@ -45,3 +45,7 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
4545

4646
Timer optimization through time-wheel.
4747

48+
## page
49+
50+
Page for pagination. It contains the most common functions like offset, pagesize.
51+

page/page.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxpage
19+
20+
// Page is the default implementation of Page interface
21+
type Page struct {
22+
requestOffset int
23+
pageSize int
24+
totalSize int
25+
data []interface{}
26+
totalPages int
27+
hasNext bool
28+
}
29+
30+
// GetOffSet will return the offset
31+
func (d *Page) GetOffset() int {
32+
return d.requestOffset
33+
}
34+
35+
// GetPageSize will return the page size
36+
func (d *Page) GetPageSize() int {
37+
return d.pageSize
38+
}
39+
40+
// GetTotalPages will return the number of total pages
41+
func (d *Page) GetTotalPages() int {
42+
return d.totalPages
43+
}
44+
45+
// GetData will return the data
46+
func (d *Page) GetData() []interface{} {
47+
return d.data
48+
}
49+
50+
// GetDataSize will return the size of data.
51+
// it's len(GetData())
52+
func (d *Page) GetDataSize() int {
53+
return len(d.GetData())
54+
}
55+
56+
// HasNext will return whether has next page
57+
func (d *Page) HasNext() bool {
58+
return d.hasNext
59+
}
60+
61+
// HasData will return whether this page has data.
62+
func (d *Page) HasData() bool {
63+
return d.GetDataSize() > 0
64+
}
65+
66+
// New will create an instance
67+
func New(requestOffset int, pageSize int,
68+
data []interface{}, totalSize int) *Page {
69+
70+
remain := totalSize % pageSize
71+
totalPages := totalSize / pageSize
72+
if remain > 0 {
73+
totalPages++
74+
}
75+
76+
hasNext := totalSize-requestOffset-pageSize > 0
77+
78+
return &Page{
79+
requestOffset: requestOffset,
80+
pageSize: pageSize,
81+
data: data,
82+
totalSize: totalSize,
83+
totalPages: totalPages,
84+
hasNext: hasNext,
85+
}
86+
}

page/page_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxpage
19+
20+
import (
21+
"testing"
22+
)
23+
24+
import (
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestNewDefaultPage(t *testing.T) {
29+
data := make([]interface{}, 10)
30+
page := New(121, 10, data, 499)
31+
32+
assert.Equal(t, 10, page.GetDataSize())
33+
assert.Equal(t, 121, page.GetOffset())
34+
assert.Equal(t, 10, page.GetPageSize())
35+
assert.Equal(t, 50, page.GetTotalPages())
36+
assert.Equal(t, data, page.GetData())
37+
assert.True(t, page.HasNext())
38+
assert.True(t, page.HasData())
39+
40+
page = New(492, 10, data, 499)
41+
assert.False(t, page.HasNext())
42+
}

page/pager.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxpage
19+
20+
// Pager is the abstraction for pagination usage.
21+
type Pager interface {
22+
23+
// GetOffset will return the offset
24+
GetOffset() int
25+
26+
// GetPageSize will return the page size
27+
GetPageSize() int
28+
29+
// GetTotalPages will return the number of total pages
30+
GetTotalPages() int
31+
32+
// GetData will return the data
33+
GetData() []interface{}
34+
35+
// GetDataSize will return the size of data.
36+
// Usually it's len(GetData())
37+
GetDataSize() int
38+
39+
// HasNext will return whether has next page
40+
HasNext() bool
41+
42+
// HasData will return whether this page has data.
43+
HasData() bool
44+
}

sort/prioritized.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package gxsort
19+
20+
// Prioritizer is the abstraction of priority.
21+
type Prioritizer interface {
22+
// GetPriority will return the priority
23+
GetPriority() int
24+
}

0 commit comments

Comments
 (0)