lucy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [02/14] lucy git commit: Custom Go bindings for SortRule, SortSpec.
Date Mon, 28 Sep 2015 19:52:18 GMT
Custom Go bindings for SortRule, SortSpec.

Custom constructors.  Have SortSpec handle `rules` as a slice of
SortRules rather than a Vector.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/295662d3
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/295662d3
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/295662d3

Branch: refs/heads/master
Commit: 295662d3570b0aa47ddebe69c12322e72d7cd107
Parents: d0388c8
Author: Marvin Humphrey <marvin@rectangular.com>
Authored: Thu Sep 10 20:29:09 2015 -0700
Committer: Marvin Humphrey <marvin@rectangular.com>
Committed: Tue Sep 15 15:54:50 2015 -0700

----------------------------------------------------------------------
 go/build.go            |  5 +++++
 go/lucy/search.go      | 39 +++++++++++++++++++++++++++++++++++++++
 go/lucy/search_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/295662d3/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index f18252e..11615aa 100644
--- a/go/build.go
+++ b/go/build.go
@@ -197,6 +197,11 @@ func specClasses(parcel *cfc.Parcel) {
 	topDocsBinding.SpecMethod("Set_Match_Docs", "SetMatchDocs([]MatchDoc)")
 	topDocsBinding.SpecMethod("Get_Match_Docs", "GetMatchDocs() []MatchDoc")
 	topDocsBinding.Register()
+
+	sortSpecBinding := cfc.NewGoClass(parcel, "Lucy::Search::SortSpec")
+	sortSpecBinding.SetSuppressCtor(true)
+	sortSpecBinding.SpecMethod("Get_Rules", "GetRules() []SortRule")
+	sortSpecBinding.Register()
 }
 
 func build() {

http://git-wip-us.apache.org/repos/asf/lucy/blob/295662d3/go/lucy/search.go
----------------------------------------------------------------------
diff --git a/go/lucy/search.go b/go/lucy/search.go
index 4865072..2723265 100644
--- a/go/lucy/search.go
+++ b/go/lucy/search.go
@@ -27,6 +27,8 @@ package lucy
 #include "Lucy/Search/ANDMatcher.h"
 #include "Lucy/Search/ORMatcher.h"
 #include "Lucy/Search/SeriesMatcher.h"
+#include "Lucy/Search/SortRule.h"
+#include "Lucy/Search/SortSpec.h"
 #include "Lucy/Search/TopDocs.h"
 #include "Lucy/Document/HitDoc.h"
 #include "LucyX/Search/MockMatcher.h"
@@ -177,6 +179,43 @@ func (obj *HitsIMP) Error() error {
 	return obj.err
 }
 
+func NewFieldSortRule(field string, reverse bool) SortRule {
+	fieldC := clownfish.GoToClownfish(field, unsafe.Pointer(C.CFISH_STRING), false)
+	cfObj := C.lucy_SortRule_new(C.lucy_SortRule_FIELD, (*C.cfish_String)(fieldC), C.bool(reverse))
+	return WRAPSortRule(unsafe.Pointer(cfObj))
+}
+
+func NewDocIDSortRule(reverse bool) SortRule {
+	cfObj := C.lucy_SortRule_new(C.lucy_SortRule_DOC_ID, nil, C.bool(reverse))
+	return WRAPSortRule(unsafe.Pointer(cfObj))
+}
+
+func NewScoreSortRule(reverse bool) SortRule {
+	cfObj := C.lucy_SortRule_new(C.lucy_SortRule_SCORE, nil, C.bool(reverse))
+	return WRAPSortRule(unsafe.Pointer(cfObj))
+}
+
+func NewSortSpec(rules []SortRule) SortSpec {
+	vec := clownfish.NewVector(len(rules))
+	for _, rule := range rules {
+		vec.Push(rule)
+	}
+	cfObj := C.lucy_SortSpec_new((*C.cfish_Vector)(clownfish.Unwrap(vec, "rules")))
+	return WRAPSortSpec(unsafe.Pointer(cfObj))
+}
+
+func (spec *SortSpecIMP) GetRules() []SortRule {
+	self := (*C.lucy_SortSpec)(clownfish.Unwrap(spec, "spec"))
+	vec := C.LUCY_SortSpec_Get_Rules(self)
+	length := int(C.CFISH_Vec_Get_Size(vec))
+	slice := make([]SortRule, length)
+	for i := 0; i < length; i++ {
+		elem := C.cfish_incref(unsafe.Pointer(C.CFISH_Vec_Fetch(vec, C.size_t(i))))
+		slice[i] = WRAPSortRule(unsafe.Pointer(elem))
+	}
+	return slice
+}
+
 func NewTopDocs(matchDocs []MatchDoc, totalHits uint32) TopDocs {
 	vec := clownfish.NewVector(len(matchDocs))
 	for _, matchDoc := range matchDocs {

http://git-wip-us.apache.org/repos/asf/lucy/blob/295662d3/go/lucy/search_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/search_test.go b/go/lucy/search_test.go
index 4225799..6e40670 100644
--- a/go/lucy/search_test.go
+++ b/go/lucy/search_test.go
@@ -354,3 +354,45 @@ func TestTopDocsBasics(t *testing.T) {
 		t.Errorf("Failed round-trip serializetion of TopDocs")
 	}
 }
+
+type simpleTestDoc struct {
+	Content string
+}
+
+func TestSortSpecBasics(t *testing.T) {
+	folder := NewRAMFolder("")
+	schema := NewSchema()
+	fieldType := NewFullTextType(NewStandardTokenizer())
+	fieldType.SetSortable(true)
+	schema.SpecField("content", fieldType)
+	args := &OpenIndexerArgs{Index: folder, Schema: schema, Create: true}
+	indexer, err := OpenIndexer(args)
+	if err != nil {
+		panic(err)
+	}
+	for _, fieldVal := range []string{"a b", "a a"} {
+		indexer.AddDoc(&simpleTestDoc{fieldVal})
+	}
+	indexer.Commit()
+
+	rules := []SortRule{
+		NewFieldSortRule("content", false),
+	}
+	sortSpec := NewSortSpec(rules)
+	searcher, _ := OpenIndexSearcher(folder)
+	hits, _ := searcher.Hits("a", 0, 1, sortSpec)
+	var doc simpleTestDoc
+	hits.Next(&doc)
+	if doc.Content != "a a" {
+		t.Error("Sort by field value")
+	}
+
+	outstream := folder.OpenOut("foo")
+	sortSpec.Serialize(outstream)
+	outstream.Close()
+	inStream := folder.OpenIn("foo")
+	dupe := clownfish.GetClass(sortSpec).MakeObj().(SortSpec).Deserialize(inStream)
+	if len(dupe.GetRules()) != len(rules) {
+		t.Errorf("Failed round-trip serializetion of SortSpec")
+	}
+}


Mime
View raw message